]>
Commit | Line | Data |
---|---|---|
6001e347 RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: strconv.cpp | |
3 | // Purpose: Unicode conversion classes | |
4 | // Author: Ove Kaaven, Robert Roebling, Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 29/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1999 Ove Kaaven, Robert Roebling, Vadim Zeitlin | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
f6bcfd97 BP |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
6001e347 RR |
20 | #ifdef __GNUG__ |
21 | #pragma implementation "strconv.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
0a1c1e62 GRG |
31 | #ifdef __WXMSW__ |
32 | #include "wx/msw/private.h" | |
33 | #endif | |
34 | ||
1cd52418 | 35 | #include <errno.h> |
6001e347 RR |
36 | #include <ctype.h> |
37 | #include <string.h> | |
38 | #include <stdlib.h> | |
39 | ||
40 | #ifdef __SALFORDC__ | |
41 | #include <clib.h> | |
42 | #endif | |
43 | ||
1cd52418 OK |
44 | #ifdef HAVE_ICONV_H |
45 | #include <iconv.h> | |
46 | #endif | |
1cd52418 | 47 | |
3e61dfb0 OK |
48 | #ifdef __WXMSW__ |
49 | #include <windows.h> | |
50 | #endif | |
51 | ||
6001e347 RR |
52 | #include "wx/debug.h" |
53 | #include "wx/strconv.h" | |
3caec1bb VS |
54 | #include "wx/intl.h" |
55 | #include "wx/log.h" | |
6001e347 | 56 | |
d43088ee | 57 | #if defined(WORDS_BIGENDIAN) || defined(__STDC_ISO_10646__) |
1cd52418 OK |
58 | #define BSWAP_UCS4(str, len) |
59 | #define BSWAP_UCS2(str, len) | |
60 | #else | |
61 | #define BSWAP_UCS4(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT32_SWAP_ALWAYS(str[_c]); } | |
62 | #define BSWAP_UCS2(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT16_SWAP_ALWAYS(str[_c]); } | |
63 | #define WC_NEED_BSWAP | |
64 | #endif | |
65 | #define BSWAP_UTF32(str, len) BSWAP_UCS4(str, len) | |
66 | #define BSWAP_UTF16(str, len) BSWAP_UCS2(str, len) | |
67 | ||
68 | #if SIZEOF_WCHAR_T == 4 | |
69 | #define WC_NAME "UCS4" | |
70 | #define WC_BSWAP BSWAP_UCS4 | |
71 | #elif SIZEOF_WCHAR_T == 2 | |
72 | #define WC_NAME "UTF16" | |
73 | #define WC_BSWAP BSWAP_UTF16 | |
74 | #define WC_UTF16 | |
75 | #endif | |
76 | ||
f6bcfd97 BP |
77 | // ---------------------------------------------------------------------------- |
78 | // globals | |
79 | // ---------------------------------------------------------------------------- | |
6001e347 RR |
80 | |
81 | WXDLLEXPORT_DATA(wxMBConv *) wxConvCurrent = &wxConvLibc; | |
82 | ||
f6bcfd97 BP |
83 | // ============================================================================ |
84 | // implementation | |
85 | // ============================================================================ | |
6001e347 | 86 | |
f6bcfd97 | 87 | #if wxUSE_WCHAR_T |
6001e347 | 88 | |
b0a6bb75 VZ |
89 | #ifdef WC_UTF16 |
90 | ||
1cd52418 OK |
91 | static size_t encode_utf16(wxUint32 input,wxUint16*output) |
92 | { | |
dccce9ea | 93 | if (input<=0xffff) |
4def3b35 VS |
94 | { |
95 | if (output) *output++ = input; | |
96 | return 1; | |
dccce9ea VZ |
97 | } |
98 | else if (input>=0x110000) | |
4def3b35 VS |
99 | { |
100 | return (size_t)-1; | |
dccce9ea VZ |
101 | } |
102 | else | |
4def3b35 | 103 | { |
dccce9ea | 104 | if (output) |
4def3b35 VS |
105 | { |
106 | *output++ = (input >> 10)+0xd7c0; | |
107 | *output++ = (input&0x3ff)+0xdc00; | |
108 | } | |
109 | return 2; | |
1cd52418 | 110 | } |
1cd52418 OK |
111 | } |
112 | ||
113 | static size_t decode_utf16(wxUint16*input,wxUint32&output) | |
114 | { | |
dccce9ea | 115 | if ((*input<0xd800) || (*input>0xdfff)) |
4def3b35 VS |
116 | { |
117 | output = *input; | |
118 | return 1; | |
dccce9ea VZ |
119 | } |
120 | else if ((input[1]<0xdc00) || (input[1]>=0xdfff)) | |
4def3b35 VS |
121 | { |
122 | output = *input; | |
123 | return (size_t)-1; | |
dccce9ea VZ |
124 | } |
125 | else | |
4def3b35 VS |
126 | { |
127 | output = ((input[0] - 0xd7c0) << 10) + (input[1] - 0xdc00); | |
128 | return 2; | |
129 | } | |
1cd52418 OK |
130 | } |
131 | ||
b0a6bb75 VZ |
132 | #endif // WC_UTF16 |
133 | ||
f6bcfd97 | 134 | // ---------------------------------------------------------------------------- |
6001e347 | 135 | // wxMBConv |
f6bcfd97 | 136 | // ---------------------------------------------------------------------------- |
6001e347 RR |
137 | |
138 | WXDLLEXPORT_DATA(wxMBConv) wxConvLibc; | |
139 | ||
140 | size_t wxMBConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
141 | { | |
142 | return wxMB2WC(buf, psz, n); | |
143 | } | |
144 | ||
145 | size_t wxMBConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
146 | { | |
147 | return wxWC2MB(buf, psz, n); | |
148 | } | |
149 | ||
150 | const wxWCharBuffer wxMBConv::cMB2WC(const char *psz) const | |
151 | { | |
f6bcfd97 | 152 | if (psz) |
6001e347 RR |
153 | { |
154 | size_t nLen = MB2WC((wchar_t *) NULL, psz, 0); | |
f6bcfd97 BP |
155 | if (nLen == (size_t)-1) |
156 | return wxWCharBuffer((wchar_t *) NULL); | |
6001e347 RR |
157 | wxWCharBuffer buf(nLen); |
158 | MB2WC((wchar_t *)(const wchar_t *) buf, psz, nLen); | |
159 | return buf; | |
f6bcfd97 BP |
160 | } |
161 | else | |
6001e347 RR |
162 | return wxWCharBuffer((wchar_t *) NULL); |
163 | } | |
164 | ||
165 | const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *psz) const | |
166 | { | |
f6bcfd97 | 167 | if (psz) |
6001e347 RR |
168 | { |
169 | size_t nLen = WC2MB((char *) NULL, psz, 0); | |
f6bcfd97 BP |
170 | if (nLen == (size_t)-1) |
171 | return wxCharBuffer((char *) NULL); | |
6001e347 RR |
172 | wxCharBuffer buf(nLen); |
173 | WC2MB((char *)(const char *) buf, psz, nLen); | |
174 | return buf; | |
f6bcfd97 BP |
175 | } |
176 | else | |
6001e347 RR |
177 | return wxCharBuffer((char *) NULL); |
178 | } | |
179 | ||
f6bcfd97 | 180 | // ---------------------------------------------------------------------------- |
6001e347 | 181 | // standard file conversion |
f6bcfd97 | 182 | // ---------------------------------------------------------------------------- |
6001e347 RR |
183 | |
184 | WXDLLEXPORT_DATA(wxMBConvFile) wxConvFile; | |
185 | ||
186 | // just use the libc conversion for now | |
187 | size_t wxMBConvFile::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
188 | { | |
189 | return wxMB2WC(buf, psz, n); | |
190 | } | |
191 | ||
192 | size_t wxMBConvFile::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
193 | { | |
194 | return wxWC2MB(buf, psz, n); | |
195 | } | |
196 | ||
f6bcfd97 | 197 | // ---------------------------------------------------------------------------- |
6001e347 | 198 | // standard gdk conversion |
f6bcfd97 BP |
199 | // ---------------------------------------------------------------------------- |
200 | ||
201 | #ifdef __WXGTK12__ | |
6001e347 RR |
202 | |
203 | WXDLLEXPORT_DATA(wxMBConvGdk) wxConvGdk; | |
204 | ||
205 | #include <gdk/gdk.h> | |
206 | ||
207 | size_t wxMBConvGdk::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
208 | { | |
dccce9ea | 209 | if (buf) |
4def3b35 VS |
210 | { |
211 | return gdk_mbstowcs((GdkWChar *)buf, psz, n); | |
dccce9ea VZ |
212 | } |
213 | else | |
4def3b35 VS |
214 | { |
215 | GdkWChar *nbuf = new GdkWChar[n=strlen(psz)]; | |
216 | size_t len = gdk_mbstowcs(nbuf, psz, n); | |
217 | delete[] nbuf; | |
218 | return len; | |
219 | } | |
6001e347 RR |
220 | } |
221 | ||
222 | size_t wxMBConvGdk::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
223 | { | |
4def3b35 VS |
224 | char *mbstr = gdk_wcstombs((GdkWChar *)psz); |
225 | size_t len = mbstr ? strlen(mbstr) : 0; | |
dccce9ea | 226 | if (buf) |
4def3b35 | 227 | { |
dccce9ea | 228 | if (len > n) |
4def3b35 VS |
229 | len = n; |
230 | memcpy(buf, psz, len); | |
dccce9ea | 231 | if (len < n) |
4def3b35 VS |
232 | buf[len] = 0; |
233 | } | |
234 | return len; | |
6001e347 | 235 | } |
f6bcfd97 | 236 | |
6001e347 RR |
237 | #endif // GTK > 1.0 |
238 | ||
239 | // ---------------------------------------------------------------------------- | |
240 | // UTF-7 | |
241 | // ---------------------------------------------------------------------------- | |
242 | ||
243 | WXDLLEXPORT_DATA(wxMBConvUTF7) wxConvUTF7; | |
244 | ||
245 | #if 0 | |
246 | static char utf7_setD[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
247 | "abcdefghijklmnopqrstuvwxyz" | |
248 | "0123456789'(),-./:?"; | |
249 | static char utf7_setO[]="!\"#$%&*;<=>@[]^_`{|}"; | |
250 | static char utf7_setB[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
251 | "abcdefghijklmnopqrstuvwxyz" | |
252 | "0123456789+/"; | |
253 | #endif | |
254 | ||
255 | // TODO: write actual implementations of UTF-7 here | |
256 | size_t wxMBConvUTF7::MB2WC(wchar_t * WXUNUSED(buf), | |
257 | const char * WXUNUSED(psz), | |
258 | size_t WXUNUSED(n)) const | |
259 | { | |
260 | return 0; | |
261 | } | |
262 | ||
263 | size_t wxMBConvUTF7::WC2MB(char * WXUNUSED(buf), | |
264 | const wchar_t * WXUNUSED(psz), | |
265 | size_t WXUNUSED(n)) const | |
266 | { | |
267 | return 0; | |
268 | } | |
269 | ||
f6bcfd97 | 270 | // ---------------------------------------------------------------------------- |
6001e347 | 271 | // UTF-8 |
f6bcfd97 | 272 | // ---------------------------------------------------------------------------- |
6001e347 RR |
273 | |
274 | WXDLLEXPORT_DATA(wxMBConvUTF8) wxConvUTF8; | |
275 | ||
dccce9ea | 276 | static wxUint32 utf8_max[]= |
4def3b35 | 277 | { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff, 0xffffffff }; |
6001e347 RR |
278 | |
279 | size_t wxMBConvUTF8::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
280 | { | |
4def3b35 VS |
281 | size_t len = 0; |
282 | ||
dccce9ea | 283 | while (*psz && ((!buf) || (len < n))) |
4def3b35 VS |
284 | { |
285 | unsigned char cc = *psz++, fc = cc; | |
286 | unsigned cnt; | |
dccce9ea | 287 | for (cnt = 0; fc & 0x80; cnt++) |
4def3b35 | 288 | fc <<= 1; |
dccce9ea | 289 | if (!cnt) |
4def3b35 VS |
290 | { |
291 | // plain ASCII char | |
dccce9ea | 292 | if (buf) |
4def3b35 VS |
293 | *buf++ = cc; |
294 | len++; | |
dccce9ea VZ |
295 | } |
296 | else | |
4def3b35 VS |
297 | { |
298 | cnt--; | |
dccce9ea | 299 | if (!cnt) |
4def3b35 VS |
300 | { |
301 | // invalid UTF-8 sequence | |
302 | return (size_t)-1; | |
dccce9ea VZ |
303 | } |
304 | else | |
4def3b35 VS |
305 | { |
306 | unsigned ocnt = cnt - 1; | |
307 | wxUint32 res = cc & (0x3f >> cnt); | |
dccce9ea | 308 | while (cnt--) |
4def3b35 VS |
309 | { |
310 | cc = *psz++; | |
dccce9ea | 311 | if ((cc & 0xC0) != 0x80) |
4def3b35 VS |
312 | { |
313 | // invalid UTF-8 sequence | |
314 | return (size_t)-1; | |
315 | } | |
316 | res = (res << 6) | (cc & 0x3f); | |
317 | } | |
dccce9ea | 318 | if (res <= utf8_max[ocnt]) |
4def3b35 VS |
319 | { |
320 | // illegal UTF-8 encoding | |
321 | return (size_t)-1; | |
322 | } | |
1cd52418 | 323 | #ifdef WC_UTF16 |
4def3b35 VS |
324 | size_t pa = encode_utf16(res, buf); |
325 | if (pa == (size_t)-1) | |
326 | return (size_t)-1; | |
dccce9ea | 327 | if (buf) |
4def3b35 VS |
328 | buf += pa; |
329 | len += pa; | |
1cd52418 | 330 | #else |
dccce9ea | 331 | if (buf) |
4def3b35 VS |
332 | *buf++ = res; |
333 | len++; | |
1cd52418 | 334 | #endif |
4def3b35 VS |
335 | } |
336 | } | |
6001e347 | 337 | } |
dccce9ea | 338 | if (buf && (len < n)) |
4def3b35 VS |
339 | *buf = 0; |
340 | return len; | |
6001e347 RR |
341 | } |
342 | ||
343 | size_t wxMBConvUTF8::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
344 | { | |
4def3b35 | 345 | size_t len = 0; |
6001e347 | 346 | |
dccce9ea | 347 | while (*psz && ((!buf) || (len < n))) |
4def3b35 VS |
348 | { |
349 | wxUint32 cc; | |
1cd52418 | 350 | #ifdef WC_UTF16 |
4def3b35 VS |
351 | size_t pa = decode_utf16(psz,cc); |
352 | psz += (pa == (size_t)-1) ? 1 : pa; | |
1cd52418 | 353 | #else |
4def3b35 VS |
354 | cc=(*psz++) & 0x7fffffff; |
355 | #endif | |
356 | unsigned cnt; | |
357 | for (cnt = 0; cc > utf8_max[cnt]; cnt++) {} | |
dccce9ea | 358 | if (!cnt) |
4def3b35 VS |
359 | { |
360 | // plain ASCII char | |
dccce9ea | 361 | if (buf) |
4def3b35 VS |
362 | *buf++ = cc; |
363 | len++; | |
dccce9ea VZ |
364 | } |
365 | ||
366 | else | |
4def3b35 VS |
367 | { |
368 | len += cnt + 1; | |
dccce9ea | 369 | if (buf) |
4def3b35 VS |
370 | { |
371 | *buf++ = (-128 >> cnt) | ((cc >> (cnt * 6)) & (0x3f >> cnt)); | |
372 | while (cnt--) | |
373 | *buf++ = 0x80 | ((cc >> (cnt * 6)) & 0x3f); | |
374 | } | |
375 | } | |
6001e347 | 376 | } |
4def3b35 VS |
377 | |
378 | if (buf && (len<n)) *buf = 0; | |
379 | return len; | |
6001e347 RR |
380 | } |
381 | ||
382 | // ---------------------------------------------------------------------------- | |
383 | // specified character set | |
384 | // ---------------------------------------------------------------------------- | |
385 | ||
f6bcfd97 BP |
386 | WXDLLEXPORT_DATA(wxCSConv) wxConvLocal((const wxChar *)NULL); |
387 | ||
388 | #include "wx/encconv.h" | |
389 | #include "wx/fontmap.h" | |
6001e347 | 390 | |
1cd52418 OK |
391 | // TODO: add some tables here |
392 | // - perhaps common encodings to common codepages (for Win32) | |
393 | // - perhaps common encodings to objects ("UTF8" -> wxConvUTF8) | |
394 | // - move wxEncodingConverter meat in here | |
395 | ||
396 | #ifdef __WIN32__ | |
397 | #include "wx/msw/registry.h" | |
398 | // this should work if M$ Internet Exploiter is installed | |
399 | static long CharsetToCodepage(const wxChar *name) | |
400 | { | |
dccce9ea | 401 | if (!name) |
f1339c56 | 402 | return GetACP(); |
dccce9ea | 403 | |
f1339c56 | 404 | long CP=-1; |
dccce9ea | 405 | |
f1339c56 RR |
406 | wxString cn(name); |
407 | do { | |
5ce0e4ac | 408 | wxString path(wxT("MIME\\Database\\Charset\\")); |
f1339c56 | 409 | path += cn; |
5ce0e4ac VS |
410 | wxRegKey key(wxRegKey::HKCR, path); |
411 | ||
412 | if (!key.Exists()) continue; | |
dccce9ea | 413 | |
5ce0e4ac VS |
414 | // two cases: either there's an AliasForCharset string, |
415 | // or there are Codepage and InternetEncoding dwords. | |
416 | // The InternetEncoding gives us the actual encoding, | |
417 | // the Codepage just says which Windows character set to | |
418 | // use when displaying the data. | |
419 | if (key.HasValue(wxT("InternetEncoding")) && | |
420 | key.QueryValue(wxT("InternetEncoding"), &CP)) break; | |
dccce9ea | 421 | |
f1339c56 | 422 | // no encoding, see if it's an alias |
5ce0e4ac VS |
423 | if (!key.HasValue(wxT("AliasForCharset")) || |
424 | !key.QueryValue(wxT("AliasForCharset"), cn)) break; | |
f1339c56 | 425 | } while (1); |
dccce9ea | 426 | |
f1339c56 | 427 | return CP; |
1cd52418 OK |
428 | } |
429 | #endif | |
430 | ||
6001e347 RR |
431 | class wxCharacterSet |
432 | { | |
1cd52418 | 433 | public: |
f1339c56 RR |
434 | wxCharacterSet(const wxChar*name) |
435 | : cname(name) {} | |
436 | virtual ~wxCharacterSet() | |
437 | {} | |
dccce9ea | 438 | virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) |
f1339c56 | 439 | { return (size_t)-1; } |
4def3b35 | 440 | virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) |
f1339c56 RR |
441 | { return (size_t)-1; } |
442 | virtual bool usable() | |
443 | { return FALSE; } | |
444 | public: | |
445 | const wxChar*cname; | |
1cd52418 OK |
446 | }; |
447 | ||
448 | class ID_CharSet : public wxCharacterSet | |
449 | { | |
450 | public: | |
4def3b35 | 451 | ID_CharSet(const wxChar *name,wxMBConv *cnv) |
f1339c56 | 452 | : wxCharacterSet(name), work(cnv) {} |
dccce9ea | 453 | |
4def3b35 | 454 | size_t MB2WC(wchar_t *buf, const char *psz, size_t n) |
f1339c56 | 455 | { return work ? work->MB2WC(buf,psz,n) : (size_t)-1; } |
dccce9ea | 456 | |
4def3b35 | 457 | size_t WC2MB(char *buf, const wchar_t *psz, size_t n) |
f1339c56 | 458 | { return work ? work->WC2MB(buf,psz,n) : (size_t)-1; } |
dccce9ea | 459 | |
f1339c56 RR |
460 | bool usable() |
461 | { return work!=NULL; } | |
462 | public: | |
463 | wxMBConv*work; | |
1cd52418 OK |
464 | }; |
465 | ||
3caec1bb | 466 | |
1cd52418 | 467 | #ifdef HAVE_ICONV_H |
3caec1bb VS |
468 | |
469 | // VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with E2BIG | |
470 | // if output buffer is _exactly_ as big as needed. Such case is (unless there's | |
471 | // yet another bug in glibc) the only case when iconv() returns with (size_t)-1 | |
472 | // (which means error) and says there are 0 bytes left in the input buffer -- | |
473 | // when _real_ error occurs, bytes-left-in-input buffer is non-zero. Hence, | |
474 | // this alternative test for iconv() failure. | |
475 | // [This bug does not appear in glibc 2.2.] | |
476 | #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 1 | |
477 | #define ICONV_FAILED(cres, bufLeft) ((cres == (size_t)-1) && \ | |
478 | (errno != E2BIG || bufLeft != 0)) | |
479 | #else | |
480 | #define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1) | |
481 | #endif | |
482 | ||
1cd52418 OK |
483 | class IC_CharSet : public wxCharacterSet |
484 | { | |
485 | public: | |
dccce9ea | 486 | IC_CharSet(const wxChar *name) |
3caec1bb | 487 | : wxCharacterSet(name) |
f1339c56 | 488 | { |
3caec1bb VS |
489 | m2w = iconv_open(WC_NAME, wxConvLibc.cWX2MB(cname)); |
490 | w2m = iconv_open(wxConvLibc.cWX2MB(cname), WC_NAME); | |
f1339c56 | 491 | } |
dccce9ea | 492 | |
3caec1bb VS |
493 | ~IC_CharSet() |
494 | { | |
dccce9ea | 495 | if ( m2w != (iconv_t)-1 ) |
3caec1bb | 496 | iconv_close(m2w); |
dccce9ea | 497 | if ( w2m != (iconv_t)-1 ) |
3caec1bb | 498 | iconv_close(w2m); |
f1339c56 | 499 | } |
dccce9ea | 500 | |
3caec1bb | 501 | size_t MB2WC(wchar_t *buf, const char *psz, size_t n) |
f1339c56 | 502 | { |
f1339c56 | 503 | size_t inbuf = strlen(psz); |
3caec1bb | 504 | size_t outbuf = n * SIZEOF_WCHAR_T; |
f1339c56 | 505 | size_t res, cres; |
3caec1bb VS |
506 | // VS: Use these instead of psz, buf because iconv() modifies its arguments: |
507 | wchar_t *bufPtr = buf; | |
508 | const char *pszPtr = psz; | |
509 | ||
f1339c56 RR |
510 | if (buf) |
511 | { | |
512 | // have destination buffer, convert there | |
95c8801c | 513 | #ifdef WX_ICONV_TAKES_CHAR |
3caec1bb | 514 | cres = iconv(m2w, (char**)&pszPtr, &inbuf, (char**)&bufPtr, &outbuf); |
95c8801c | 515 | #else |
3caec1bb | 516 | cres = iconv(m2w, &pszPtr, &inbuf, (char**)&bufPtr, &outbuf); |
95c8801c | 517 | #endif |
3caec1bb | 518 | res = n - (outbuf / SIZEOF_WCHAR_T); |
f1339c56 | 519 | // convert to native endianness |
3caec1bb VS |
520 | #ifdef WC_NEED_BSWAP |
521 | WC_BSWAP(buf /* _not_ bufPtr */, res) | |
522 | #endif | |
f1339c56 RR |
523 | } |
524 | else | |
525 | { | |
526 | // no destination buffer... convert using temp buffer | |
527 | // to calculate destination buffer requirement | |
528 | wchar_t tbuf[8]; | |
529 | res = 0; | |
530 | do { | |
3caec1bb | 531 | bufPtr = tbuf; outbuf = 8*SIZEOF_WCHAR_T; |
95c8801c | 532 | #ifdef WX_ICONV_TAKES_CHAR |
3caec1bb | 533 | cres = iconv( m2w, (char**)&pszPtr, &inbuf, (char**)&bufPtr, &outbuf ); |
95c8801c | 534 | #else |
3caec1bb | 535 | cres = iconv( m2w, &pszPtr, &inbuf, (char**)&bufPtr, &outbuf ); |
95c8801c | 536 | #endif |
f1339c56 RR |
537 | res += 8-(outbuf/SIZEOF_WCHAR_T); |
538 | } while ((cres==(size_t)-1) && (errno==E2BIG)); | |
539 | } | |
dccce9ea | 540 | |
3caec1bb | 541 | if (ICONV_FAILED(cres, inbuf)) |
f1339c56 | 542 | return (size_t)-1; |
3caec1bb | 543 | |
f1339c56 RR |
544 | return res; |
545 | } | |
dccce9ea | 546 | |
4def3b35 | 547 | size_t WC2MB(char *buf, const wchar_t *psz, size_t n) |
f1339c56 | 548 | { |
1cd52418 | 549 | #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530) |
3caec1bb | 550 | size_t inbuf = std::wcslen(psz) * SIZEOF_WCHAR_T; |
1cd52418 | 551 | #else |
3caec1bb | 552 | size_t inbuf = ::wcslen(psz) * SIZEOF_WCHAR_T; |
1cd52418 | 553 | #endif |
f1339c56 RR |
554 | size_t outbuf = n; |
555 | size_t res, cres; | |
3caec1bb | 556 | |
1cd52418 | 557 | #ifdef WC_NEED_BSWAP |
f1339c56 RR |
558 | // need to copy to temp buffer to switch endianness |
559 | // this absolutely doesn't rock! | |
560 | // (no, doing WC_BSWAP twice on the original buffer won't help, as it | |
561 | // could be in read-only memory, or be accessed in some other thread) | |
4def3b35 | 562 | wchar_t *tmpbuf=(wchar_t*)malloc((inbuf+1)*SIZEOF_WCHAR_T); |
f1339c56 RR |
563 | memcpy(tmpbuf,psz,(inbuf+1)*SIZEOF_WCHAR_T); |
564 | WC_BSWAP(tmpbuf, inbuf) | |
565 | psz=tmpbuf; | |
1cd52418 | 566 | #endif |
f1339c56 RR |
567 | if (buf) |
568 | { | |
569 | // have destination buffer, convert there | |
95c8801c | 570 | #ifdef WX_ICONV_TAKES_CHAR |
f1339c56 | 571 | cres = iconv( w2m, (char**)&psz, &inbuf, &buf, &outbuf ); |
95c8801c VS |
572 | #else |
573 | cres = iconv( w2m, (const char**)&psz, &inbuf, &buf, &outbuf ); | |
574 | #endif | |
f1339c56 RR |
575 | res = n-outbuf; |
576 | } | |
577 | else | |
578 | { | |
579 | // no destination buffer... convert using temp buffer | |
580 | // to calculate destination buffer requirement | |
581 | char tbuf[16]; | |
582 | res = 0; | |
583 | do { | |
584 | buf = tbuf; outbuf = 16; | |
95c8801c | 585 | #ifdef WX_ICONV_TAKES_CHAR |
f1339c56 | 586 | cres = iconv( w2m, (char**)&psz, &inbuf, &buf, &outbuf ); |
95c8801c VS |
587 | #else |
588 | cres = iconv( w2m, (const char**)&psz, &inbuf, &buf, &outbuf ); | |
589 | #endif | |
f1339c56 RR |
590 | res += 16 - outbuf; |
591 | } while ((cres==(size_t)-1) && (errno==E2BIG)); | |
592 | } | |
1cd52418 | 593 | #ifdef WC_NEED_BSWAP |
f1339c56 | 594 | free(tmpbuf); |
1cd52418 | 595 | #endif |
3caec1bb | 596 | if (ICONV_FAILED(cres, inbuf)) |
f1339c56 | 597 | return (size_t)-1; |
dccce9ea | 598 | |
f1339c56 RR |
599 | return res; |
600 | } | |
dccce9ea | 601 | |
f1339c56 | 602 | bool usable() |
3caec1bb | 603 | { return (m2w != (iconv_t)-1) && (w2m != (iconv_t)-1); } |
dccce9ea | 604 | |
f1339c56 RR |
605 | public: |
606 | iconv_t m2w, w2m; | |
1cd52418 OK |
607 | }; |
608 | #endif | |
609 | ||
610 | #ifdef __WIN32__ | |
611 | class CP_CharSet : public wxCharacterSet | |
612 | { | |
613 | public: | |
dccce9ea | 614 | CP_CharSet(const wxChar*name) |
f1339c56 | 615 | : wxCharacterSet(name), CodePage(CharsetToCodepage(name)) {} |
dccce9ea | 616 | |
4def3b35 | 617 | size_t MB2WC(wchar_t *buf, const char *psz, size_t n) |
f1339c56 | 618 | { |
dccce9ea | 619 | size_t len = |
4def3b35 | 620 | MultiByteToWideChar(CodePage, 0, psz, -1, buf, buf ? n : 0); |
f1339c56 RR |
621 | return len ? len : (size_t)-1; |
622 | } | |
dccce9ea | 623 | |
4def3b35 | 624 | size_t WC2MB(char *buf, const wchar_t *psz, size_t n) |
f1339c56 | 625 | { |
4def3b35 VS |
626 | size_t len = WideCharToMultiByte(CodePage, 0, psz, -1, buf, |
627 | buf ? n : 0, NULL, NULL); | |
f1339c56 RR |
628 | return len ? len : (size_t)-1; |
629 | } | |
dccce9ea | 630 | |
f1339c56 | 631 | bool usable() |
4def3b35 | 632 | { return CodePage != -1; } |
f1339c56 RR |
633 | |
634 | public: | |
635 | long CodePage; | |
1cd52418 OK |
636 | }; |
637 | #endif | |
638 | ||
639 | class EC_CharSet : public wxCharacterSet | |
640 | { | |
6001e347 | 641 | public: |
f1339c56 RR |
642 | // temporarily just use wxEncodingConverter stuff, |
643 | // so that it works while a better implementation is built | |
dccce9ea | 644 | EC_CharSet(const wxChar*name) : wxCharacterSet(name), |
4def3b35 | 645 | enc(wxFONTENCODING_SYSTEM) |
f1339c56 RR |
646 | { |
647 | if (name) | |
648 | enc = wxTheFontMapper->CharsetToEncoding(name, FALSE); | |
649 | m2w.Init(enc, wxFONTENCODING_UNICODE); | |
650 | w2m.Init(wxFONTENCODING_UNICODE, enc); | |
651 | } | |
dccce9ea | 652 | |
4def3b35 | 653 | size_t MB2WC(wchar_t *buf, const char *psz, size_t n) |
f1339c56 RR |
654 | { |
655 | size_t inbuf = strlen(psz); | |
dccce9ea | 656 | if (buf) |
4def3b35 | 657 | m2w.Convert(psz,buf); |
f1339c56 RR |
658 | return inbuf; |
659 | } | |
dccce9ea | 660 | |
4def3b35 | 661 | size_t WC2MB(char *buf, const wchar_t *psz, size_t n) |
f1339c56 | 662 | { |
1cd52418 | 663 | #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530) |
f1339c56 | 664 | size_t inbuf = std::wcslen(psz); |
1cd52418 | 665 | #else |
f1339c56 | 666 | size_t inbuf = ::wcslen(psz); |
1cd52418 | 667 | #endif |
f1339c56 RR |
668 | if (buf) |
669 | w2m.Convert(psz,buf); | |
dccce9ea | 670 | |
f1339c56 RR |
671 | return inbuf; |
672 | } | |
dccce9ea | 673 | |
f1339c56 RR |
674 | bool usable() |
675 | { return (enc!=wxFONTENCODING_SYSTEM) && (enc!=wxFONTENCODING_DEFAULT); } | |
676 | ||
677 | public: | |
678 | wxFontEncoding enc; | |
679 | wxEncodingConverter m2w, w2m; | |
f6bcfd97 | 680 | }; |
6001e347 | 681 | |
f6bcfd97 | 682 | static wxCharacterSet *wxGetCharacterSet(const wxChar *name) |
6001e347 | 683 | { |
f1339c56 RR |
684 | wxCharacterSet *cset = NULL; |
685 | if (name) | |
686 | { | |
4def3b35 | 687 | if (wxStricmp(name, wxT("UTF8")) == 0 || wxStricmp(name, wxT("UTF-8")) == 0) |
f1339c56 RR |
688 | { |
689 | cset = new ID_CharSet(name, &wxConvUTF8); | |
690 | } | |
691 | else | |
692 | { | |
1cd52418 | 693 | #ifdef HAVE_ICONV_H |
f1339c56 | 694 | cset = new IC_CharSet(name); // may not take NULL |
1cd52418 | 695 | #endif |
f1339c56 | 696 | } |
1cd52418 | 697 | } |
dccce9ea | 698 | |
f1339c56 | 699 | if (cset && cset->usable()) return cset; |
dccce9ea VZ |
700 | if (cset) |
701 | { | |
702 | delete cset; | |
703 | cset = NULL; | |
704 | } | |
705 | ||
1cd52418 | 706 | #ifdef __WIN32__ |
f1339c56 | 707 | cset = new CP_CharSet(name); // may take NULL |
dccce9ea VZ |
708 | if (cset->usable()) |
709 | return cset; | |
710 | ||
711 | delete cset; | |
712 | #endif // __WIN32__ | |
713 | ||
f1339c56 | 714 | cset = new EC_CharSet(name); |
dccce9ea VZ |
715 | if (cset->usable()) |
716 | return cset; | |
717 | ||
f1339c56 | 718 | delete cset; |
3caec1bb | 719 | wxLogError(_("Unknown encoding '%s'!"), name); |
f1339c56 | 720 | return NULL; |
6001e347 RR |
721 | } |
722 | ||
6001e347 RR |
723 | wxCSConv::wxCSConv(const wxChar *charset) |
724 | { | |
dccce9ea | 725 | m_name = (wxChar *)NULL; |
f1339c56 | 726 | m_cset = (wxCharacterSet *) NULL; |
82713003 VZ |
727 | m_deferred = TRUE; |
728 | ||
f1339c56 | 729 | SetName(charset); |
6001e347 RR |
730 | } |
731 | ||
732 | wxCSConv::~wxCSConv() | |
733 | { | |
dccce9ea VZ |
734 | free(m_name); |
735 | delete m_cset; | |
6001e347 RR |
736 | } |
737 | ||
738 | void wxCSConv::SetName(const wxChar *charset) | |
739 | { | |
f1339c56 RR |
740 | if (charset) |
741 | { | |
742 | m_name = wxStrdup(charset); | |
743 | m_deferred = TRUE; | |
744 | } | |
6001e347 RR |
745 | } |
746 | ||
747 | void wxCSConv::LoadNow() | |
748 | { | |
f1339c56 RR |
749 | if (m_deferred) |
750 | { | |
dccce9ea | 751 | if ( !m_name ) |
f1339c56 | 752 | { |
dccce9ea VZ |
753 | wxString name = wxLocale::GetSystemEncodingName(); |
754 | if ( !name.empty() ) | |
755 | SetName(name); | |
f1339c56 | 756 | } |
dccce9ea | 757 | |
f1339c56 RR |
758 | m_cset = wxGetCharacterSet(m_name); |
759 | m_deferred = FALSE; | |
6001e347 | 760 | } |
6001e347 RR |
761 | } |
762 | ||
763 | size_t wxCSConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
764 | { | |
f1339c56 | 765 | ((wxCSConv *)this)->LoadNow(); // discard constness |
dccce9ea | 766 | |
f1339c56 RR |
767 | if (m_cset) |
768 | return m_cset->MB2WC(buf, psz, n); | |
769 | ||
770 | // latin-1 (direct) | |
4def3b35 | 771 | size_t len = strlen(psz); |
dccce9ea | 772 | |
f1339c56 RR |
773 | if (buf) |
774 | { | |
4def3b35 | 775 | for (size_t c = 0; c <= len; c++) |
f1339c56 RR |
776 | buf[c] = (unsigned char)(psz[c]); |
777 | } | |
dccce9ea | 778 | |
f1339c56 | 779 | return len; |
6001e347 RR |
780 | } |
781 | ||
782 | size_t wxCSConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
783 | { | |
f1339c56 | 784 | ((wxCSConv *)this)->LoadNow(); // discard constness |
dccce9ea | 785 | |
f1339c56 RR |
786 | if (m_cset) |
787 | return m_cset->WC2MB(buf, psz, n); | |
1cd52418 | 788 | |
f1339c56 | 789 | // latin-1 (direct) |
d834f22c | 790 | #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530) |
f1339c56 | 791 | size_t len=std::wcslen(psz); |
d834f22c | 792 | #else |
f1339c56 | 793 | size_t len=::wcslen(psz); |
d834f22c | 794 | #endif |
f1339c56 RR |
795 | if (buf) |
796 | { | |
4def3b35 VS |
797 | for (size_t c = 0; c <= len; c++) |
798 | buf[c] = (psz[c] > 0xff) ? '?' : psz[c]; | |
f1339c56 | 799 | } |
dccce9ea | 800 | |
f1339c56 | 801 | return len; |
6001e347 RR |
802 | } |
803 | ||
1cd52418 | 804 | #ifdef HAVE_ICONV_H |
dccce9ea | 805 | |
1cd52418 OK |
806 | class IC_CharSetConverter |
807 | { | |
808 | public: | |
4def3b35 VS |
809 | IC_CharSetConverter(IC_CharSet *from, IC_CharSet *to) |
810 | { | |
dccce9ea VZ |
811 | cnv = iconv_open(wxConvLibc.cWX2MB(to->cname), |
812 | wxConvLibc.cWX2MB(from->cname)); | |
4def3b35 | 813 | } |
dccce9ea | 814 | |
f1339c56 | 815 | ~IC_CharSetConverter() |
dccce9ea VZ |
816 | { |
817 | if (cnv != (iconv_t)-1) | |
818 | iconv_close(cnv); | |
4def3b35 | 819 | } |
dccce9ea | 820 | |
4def3b35 | 821 | size_t Convert(char *buf, const char *psz, size_t n) |
f1339c56 RR |
822 | { |
823 | size_t inbuf = strlen(psz); | |
824 | size_t outbuf = n; | |
95c8801c | 825 | #ifdef WX_ICONV_TAKES_CHAR |
f1339c56 | 826 | size_t res = iconv( cnv, (char**)&psz, &inbuf, &buf, &outbuf ); |
95c8801c VS |
827 | #else |
828 | size_t res = iconv( cnv, &psz, &inbuf, &buf, &outbuf ); | |
829 | #endif | |
dccce9ea | 830 | if (res == (size_t)-1) |
4def3b35 VS |
831 | return (size_t)-1; |
832 | return (n - outbuf); | |
f1339c56 RR |
833 | } |
834 | ||
835 | public: | |
836 | iconv_t cnv; | |
1cd52418 | 837 | }; |
dccce9ea VZ |
838 | |
839 | #endif // HAVE_ICONV_H | |
1cd52418 OK |
840 | |
841 | class EC_CharSetConverter | |
842 | { | |
843 | public: | |
f1339c56 RR |
844 | EC_CharSetConverter(EC_CharSet*from,EC_CharSet*to) |
845 | { cnv.Init(from->enc,to->enc); } | |
dccce9ea | 846 | |
f1339c56 RR |
847 | size_t Convert(char*buf, const char*psz, size_t n) |
848 | { | |
849 | size_t inbuf = strlen(psz); | |
850 | if (buf) cnv.Convert(psz,buf); | |
851 | return inbuf; | |
852 | } | |
dccce9ea | 853 | |
f1339c56 RR |
854 | public: |
855 | wxEncodingConverter cnv; | |
1cd52418 OK |
856 | }; |
857 | ||
f6bcfd97 BP |
858 | #else // !wxUSE_WCHAR_T |
859 | ||
860 | // ---------------------------------------------------------------------------- | |
861 | // stand-ins in absence of wchar_t | |
862 | // ---------------------------------------------------------------------------- | |
863 | ||
864 | WXDLLEXPORT_DATA(wxMBConv) wxConvLibc, wxConvFile; | |
865 | ||
866 | #endif // wxUSE_WCHAR_T | |
6001e347 RR |
867 | |
868 |