forked from HIT-SCIR/ltp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.cpp
280 lines (248 loc) · 5.28 KB
/
encode.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "encode.hpp"
#include "decode_gbk.h"
#include <iostream>
#include <cstdlib>
using namespace std;
ENC_NS_BEG
#include "conversion_utf.h"
enum _ENCODE_CONST_
{
MAX_CODE_NUM = 65536
};
wchar_t _gbk2uni[MAX_CODE_NUM];
wchar_t _uni2gbk[MAX_CODE_NUM];
bool _bGbkUnicodeInit;
void InitGBKDecoder()
{
if( _bGbkUnicodeInit ) return;
memset(_gbk2uni, ' ', sizeof(_gbk2uni));
memset(_uni2gbk, ' ', sizeof(_uni2gbk));
int i;
for(i=0; i<=256; ++i)
{
_gbk2uni[i] = i;
_uni2gbk[i] = i;
}
const int size=sizeof(gbk_utf)/sizeof(gbk_utf[0]);
for(i=0; i<size; ++i)
{
_gbk2uni[gbk_utf[i][0]] = gbk_utf[i][1];
_uni2gbk[gbk_utf[i][1]] = gbk_utf[i][0];
}
_bGbkUnicodeInit=true;
}
void init()
{
InitGBKDecoder();
}
size_t _multi_wide(const std::string &sMulti, std::wstring &wsWide)
{
unsigned char c;
wsWide.reserve( sMulti.size() );
wsWide.resize(0);
for(size_t i=0; i<sMulti.size(); ++i)
{
if( ((c = sMulti[i]) & 0x80) == 0x80 )
{
i++;
if( i<sMulti.size() )
wsWide.append(1, ((wchar_t)(c<<8) & 0xFF00) | ((unsigned char)sMulti[i] & 0xFF));
else
{
wsWide.append(1, (wchar_t)(c & 0xFF));
i--; // backward
}
}
else
wsWide.append(1, (wchar_t)( c & 0xFF ) );
}
return wsWide.size();
}
size_t _wide_multi(const std::wstring &wsWide, std::string &sMulti)
{
wstring::const_iterator i;
sMulti.reserve(wsWide.size());
sMulti.resize(0);
wchar_t c=0;
for(i=wsWide.begin(); i!=wsWide.end(); ++i)
{
if( ((c=*i) & 0xff00) != 0 )
{
sMulti.append(1, (char)( (c >> 8 ) & 0xff) );
}
sMulti.append(1, (char)( c & 0xff ) );
}
return (unsigned)sMulti.length();
}
wstring& decode_gbk(const std::string& s, std::wstring &ws)
{
ws.resize(0);
_multi_wide(s, ws);
wstring::iterator i;
for(i=ws.begin(); i!=ws.end(); ++i)
{
*i = _gbk2uni[ *i & 0xFFFF ];
}
#ifdef NEW_VERSION
setlocale(LC_ALL, "chs");
const char* _Source = s.c_str();
size_t _Dsize = s.size() + 1;
wchar_t *_Dest = new wchar_t[_Dsize];
wmemset(_Dest, 0, _Dsize);
mbstowcs(_Dest,_Source,_Dsize);
ws.assign(_Dest);
delete []_Dest;
setlocale(LC_ALL, "C");
#endif
return ws;
}
std::wstring decode_gbk(const string& s)
{
wstring ws;
decode_gbk(s, ws);
return ws;
}
std::wstring decode_gbk(const char* s)
{
wstring ws;
decode_gbk(string(s), ws);
return ws;
}
// convert unicode to GBK
string& encode_gbk(const std::wstring &src, std::string &dst)
{
wstring ws;
ws.resize(src.size());
wstring::const_iterator i;
size_t j=0;
for(i=src.begin(); i!=src.end(); ++i, ++j)
{
ws[j] = _uni2gbk[*i & 0xFFFF];
}
_wide_multi(ws, dst);
#ifdef NEW_VERSION
string curLocale(setlocale(LC_ALL, NULL)); // curLocale = "C";
setlocale(LC_ALL, "chs");
const wchar_t* _Source = src.c_str();
size_t _Dsize = 2 * src.size() + 1;
char *_Dest = new char[_Dsize];
memset(_Dest,0,_Dsize);
wcstombs(_Dest,_Source,_Dsize);
dst.assign(_Dest);
delete []_Dest;
setlocale(LC_ALL, curLocale.c_str());
#endif
return dst;
}
std::string encode_gbk(const std::wstring &ws)
{
string dst;
encode_gbk(ws, dst);
return dst;
}
std::string encode_gbk(const wchar_t* ws)
{
string dst;
encode_gbk(wstring(ws), dst);
return dst;
}
// converting UTF8 to UTF16
wstring& decode_utf8(const string& u8, wstring &u16)
{
wchar_t w;
const unsigned char *pu8 = (const unsigned char*)u8.c_str();
size_t len=0;
u16.resize(0);
for(size_t i=0; i<u8.length(); i+=len)
{
len = g_f_u8towc(w, pu8+i);
if( len == (size_t)-1 )
break;
u16.append(1, w);
}
return u16;
}
std::wstring decode_utf8(const std::string& s)
{
wstring ws;
decode_utf8(s, ws);
return ws;
}
std::wstring decode_utf8(const char* s)
{
wstring ws;
decode_utf8(string(s), ws);
return ws;
}
string& encode_utf8(const wstring &u16, string &u8)
{
const int buf_size = 10;
char buf[buf_size];
size_t len;
u8.reserve(u16.length() * 2);
u8.resize(0);
wstring::const_iterator i;
for(i=u16.begin(); i!=u16.end(); ++i)
{
len = g_f_wctou8(buf, *i);
if( len == (size_t)-1 )
break;
u8.append(buf, len);
}
return u8;
}
std::string encode_utf8(const std::wstring &ws)
{
string s;
encode_utf8(ws, s);
return s;
}
std::string encode_utf8(const wchar_t* ws)
{
string s;
encode_utf8(wstring(ws), s);
return s;
}
//////////////////////////////////////////////////////////////////////////
/// BIG5
//////////////////////////////////////////////////////////////////////////
// convert BIG5 code to Unicode
void decode_big5(const std::string& s, std::wstring &ws)
{
setlocale(LC_ALL, "cht");
const char* _Source = s.c_str();
size_t _Dsize = s.size() + 1;
wchar_t *_Dest = new wchar_t[_Dsize];
wmemset(_Dest, 0, _Dsize);
mbstowcs(_Dest,_Source,_Dsize);
ws.assign(_Dest);
delete []_Dest;
setlocale(LC_ALL, "C");
}
// convert unicode to GBK
void encode_big5(const std::wstring &src, std::string &dst)
{
string curLocale(setlocale(LC_ALL, NULL)); // curLocale = "C";
setlocale(LC_ALL, "cht");
const wchar_t* _Source = src.c_str();
size_t _Dsize = 2 * src.size() + 1;
char *_Dest = new char[_Dsize];
memset(_Dest,0,_Dsize);
wcstombs(_Dest,_Source,_Dsize);
dst.assign(_Dest);
delete []_Dest;
setlocale(LC_ALL, curLocale.c_str());
}
ENC_NS_END
/*ostream& operator << (ostream &out, const wstring &str)
{
out << enc::encode_gbk(str);
return out;
}
ostream& operator << (ostream &out, const wchar_t *str)
{
out << enc::encode_gbk(str);
return out;
}
<<<<<<< .mine
*/