-
Notifications
You must be signed in to change notification settings - Fork 2
/
coding2utf16.cpp
88 lines (77 loc) · 2.75 KB
/
coding2utf16.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* -*- C++ -*-
*
* coding2utf16.cpp
*
* Copyright (C) 2014 jh10001 <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "coding2utf16.h"
char Coding2UTF16::space[4];
char Coding2UTF16::minus[4];
char Coding2UTF16::bracket[8];
char Coding2UTF16::num_str[24];
char Coding2UTF16::DEFAULT_START_KINSOKU[40];
char Coding2UTF16::DEFAULT_END_KINSOKU[12];
char Coding2UTF16::DEFAULT_SAVE_MENU_NAME[12];
char Coding2UTF16::DEFAULT_LOAD_MENU_NAME[12];
char Coding2UTF16::DEFAULT_SAVE_ITEM_NAME[8];
char Coding2UTF16::MESSAGE_SAVE_EXIST[24];
char Coding2UTF16::MESSAGE_SAVE_EMPTY[32];
char Coding2UTF16::MESSAGE_SAVE_CONFIRM[40];
char Coding2UTF16::MESSAGE_LOAD_CONFIRM[40];
char Coding2UTF16::MESSAGE_RESET_CONFIRM[36];
char Coding2UTF16::MESSAGE_END_CONFIRM[32];
char Coding2UTF16::MESSAGE_YES[8];
char Coding2UTF16::MESSAGE_NO[8];
char Coding2UTF16::MESSAGE_OK[8];
char Coding2UTF16::MESSAGE_CANCEL[12];
int Coding2UTF16::convUTF16ToUTF8(unsigned char dst[4], uint16_t src) const {
if (src & 0xff80) {
if (src & 0xf800) {
// UCS-2 = U+0800 - U+FFFF -> UTF-8 (3 bytes)
dst[0] = 0xe0 | (src >> 12);
dst[1] = 0x80 | ((src >> 6) & 0x3f);
dst[2] = 0x80 | (src & 0x3f);
dst[3] = 0;
return 3;
}
// UCS-2 = U+0080 - U+07FF -> UTF-8 (2 bytes)
dst[0] = 0xc0 | (src >> 6);
dst[1] = 0x80 | (src & 0x3f);
dst[2] = 0;
return 2;
}
// UCS-2 = U+0000 - U+007F -> UTF-8 (1 byte)
dst[0] = src;
dst[1] = 0;
return 1;
}
unsigned short Coding2UTF16::convUTF8ToUTF16(const char **src) {
unsigned short utf16=0;
if (**src & 0x80) {
if (**src & 0x20) {
utf16 |= ((unsigned short)((*(*src)++)&0x0f)) << 12;
utf16 |= ((unsigned short)((*(*src)++)&0x3f)) << 6;
utf16 |= ((unsigned short)((*(*src)++)&0x3f));
} else {
utf16 |= ((unsigned short)((*(*src)++)&0x1f)) << 6;
utf16 |= (unsigned short)((*(*src)++)&0x3f);
}
} else {
utf16 |= (unsigned short)(*(*src)++);
}
return utf16;
}