]>
Commit | Line | Data |
---|---|---|
6001e347 RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: strconv.cpp | |
3 | // Purpose: Unicode conversion classes | |
3a0d76bc | 4 | // Author: Ove Kaaven, Robert Roebling, Vadim Zeitlin, Vaclav Slavik |
6001e347 RR |
5 | // Modified by: |
6 | // Created: 29/01/98 | |
7 | // RCS-ID: $Id$ | |
3a0d76bc | 8 | // Copyright: (c) 1999 Ove Kaaven, Robert Roebling, Vadim Zeitlin, Vaclav Slavik |
6001e347 RR |
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 | ||
373658eb VZ |
31 | #ifndef WX_PRECOMP |
32 | #include "wx/intl.h" | |
33 | #include "wx/log.h" | |
34 | #endif // WX_PRECOMP | |
35 | ||
0a1c1e62 | 36 | #ifdef __WXMSW__ |
373658eb | 37 | #include "wx/msw/private.h" |
0a1c1e62 GRG |
38 | #endif |
39 | ||
1cd52418 | 40 | #include <errno.h> |
6001e347 RR |
41 | #include <ctype.h> |
42 | #include <string.h> | |
43 | #include <stdlib.h> | |
44 | ||
65e50848 | 45 | #include "wx/module.h" |
7af284fd | 46 | #include "wx/strconv.h" |
7af284fd VS |
47 | |
48 | // ---------------------------------------------------------------------------- | |
49 | // globals | |
50 | // ---------------------------------------------------------------------------- | |
51 | ||
373658eb VZ |
52 | #if wxUSE_WCHAR_T |
53 | WXDLLEXPORT_DATA(wxMBConv) wxConvLibc; | |
54 | WXDLLEXPORT_DATA(wxCSConv) wxConvLocal((const wxChar *)NULL); | |
55 | #else | |
56 | // stand-ins in absence of wchar_t | |
57 | WXDLLEXPORT_DATA(wxMBConv) wxConvLibc, wxConvFile; | |
58 | #endif // wxUSE_WCHAR_T | |
7af284fd | 59 | |
373658eb | 60 | WXDLLEXPORT_DATA(wxMBConv *) wxConvCurrent = &wxConvLibc; |
7af284fd | 61 | |
65e50848 JS |
62 | class wxStrConvModule: public wxModule |
63 | { | |
64 | public: | |
65 | wxStrConvModule() : wxModule() { } | |
66 | virtual bool OnInit() { return TRUE; } | |
67 | virtual void OnExit() | |
68 | { | |
69 | wxConvLocal.Clear(); | |
70 | } | |
71 | ||
72 | DECLARE_DYNAMIC_CLASS(wxStrConvModule) | |
73 | }; | |
74 | ||
75 | IMPLEMENT_DYNAMIC_CLASS(wxStrConvModule, wxModule) | |
76 | ||
77 | ||
373658eb VZ |
78 | // ---------------------------------------------------------------------------- |
79 | // headers | |
80 | // ---------------------------------------------------------------------------- | |
7af284fd VS |
81 | |
82 | #if wxUSE_WCHAR_T | |
83 | ||
6001e347 | 84 | #ifdef __SALFORDC__ |
373658eb | 85 | #include <clib.h> |
6001e347 RR |
86 | #endif |
87 | ||
b040e242 | 88 | #ifdef HAVE_ICONV |
373658eb | 89 | #include <iconv.h> |
1cd52418 | 90 | #endif |
1cd52418 | 91 | |
373658eb VZ |
92 | #include "wx/encconv.h" |
93 | #include "wx/fontmap.h" | |
94 | ||
95 | // ---------------------------------------------------------------------------- | |
96 | // macros | |
97 | // ---------------------------------------------------------------------------- | |
3e61dfb0 | 98 | |
1cd52418 | 99 | #define BSWAP_UCS4(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT32_SWAP_ALWAYS(str[_c]); } |
3a0d76bc | 100 | #define BSWAP_UTF16(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT16_SWAP_ALWAYS(str[_c]); } |
1cd52418 | 101 | |
a3f2769e VZ |
102 | // under Unix SIZEOF_WCHAR_T is defined by configure, but under other platforms |
103 | // it might be not defined - assume the most common value | |
104 | #ifndef SIZEOF_WCHAR_T | |
105 | #define SIZEOF_WCHAR_T 2 | |
106 | #endif // !defined(SIZEOF_WCHAR_T) | |
107 | ||
1cd52418 | 108 | #if SIZEOF_WCHAR_T == 4 |
3a0d76bc VS |
109 | #define WC_NAME "UCS4" |
110 | #define WC_BSWAP BSWAP_UCS4 | |
111 | #ifdef WORDS_BIGENDIAN | |
112 | #define WC_NAME_BEST "UCS-4BE" | |
113 | #else | |
114 | #define WC_NAME_BEST "UCS-4LE" | |
115 | #endif | |
1cd52418 | 116 | #elif SIZEOF_WCHAR_T == 2 |
3a0d76bc VS |
117 | #define WC_NAME "UTF16" |
118 | #define WC_BSWAP BSWAP_UTF16 | |
a3f2769e | 119 | #define WC_UTF16 |
3a0d76bc VS |
120 | #ifdef WORDS_BIGENDIAN |
121 | #define WC_NAME_BEST "UTF-16BE" | |
122 | #else | |
123 | #define WC_NAME_BEST "UTF-16LE" | |
124 | #endif | |
bab1e722 | 125 | #else // sizeof(wchar_t) != 2 nor 4 |
a3f2769e VZ |
126 | // I don't know what to do about this |
127 | #error "Weird sizeof(wchar_t): please report your platform details to wx-users mailing list" | |
1cd52418 OK |
128 | #endif |
129 | ||
373658eb VZ |
130 | // ============================================================================ |
131 | // implementation | |
132 | // ============================================================================ | |
133 | ||
134 | // ---------------------------------------------------------------------------- | |
135 | // UTF-16 en/decoding | |
136 | // ---------------------------------------------------------------------------- | |
6001e347 | 137 | |
b0a6bb75 VZ |
138 | #ifdef WC_UTF16 |
139 | ||
eccf1b2c | 140 | static size_t encode_utf16(wxUint32 input, wchar_t *output) |
1cd52418 | 141 | { |
dccce9ea | 142 | if (input<=0xffff) |
4def3b35 | 143 | { |
574c939e | 144 | if (output) *output++ = (wchar_t) input; |
4def3b35 | 145 | return 1; |
dccce9ea VZ |
146 | } |
147 | else if (input>=0x110000) | |
4def3b35 VS |
148 | { |
149 | return (size_t)-1; | |
dccce9ea VZ |
150 | } |
151 | else | |
4def3b35 | 152 | { |
dccce9ea | 153 | if (output) |
4def3b35 | 154 | { |
574c939e KB |
155 | *output++ = (wchar_t) ((input >> 10)+0xd7c0); |
156 | *output++ = (wchar_t) ((input&0x3ff)+0xdc00); | |
4def3b35 VS |
157 | } |
158 | return 2; | |
1cd52418 | 159 | } |
1cd52418 OK |
160 | } |
161 | ||
eccf1b2c | 162 | static size_t decode_utf16(const wchar_t* input, wxUint32& output) |
1cd52418 | 163 | { |
dccce9ea | 164 | if ((*input<0xd800) || (*input>0xdfff)) |
4def3b35 VS |
165 | { |
166 | output = *input; | |
167 | return 1; | |
dccce9ea VZ |
168 | } |
169 | else if ((input[1]<0xdc00) || (input[1]>=0xdfff)) | |
4def3b35 VS |
170 | { |
171 | output = *input; | |
172 | return (size_t)-1; | |
dccce9ea VZ |
173 | } |
174 | else | |
4def3b35 VS |
175 | { |
176 | output = ((input[0] - 0xd7c0) << 10) + (input[1] - 0xdc00); | |
177 | return 2; | |
178 | } | |
1cd52418 OK |
179 | } |
180 | ||
b0a6bb75 VZ |
181 | #endif // WC_UTF16 |
182 | ||
f6bcfd97 | 183 | // ---------------------------------------------------------------------------- |
6001e347 | 184 | // wxMBConv |
f6bcfd97 | 185 | // ---------------------------------------------------------------------------- |
6001e347 | 186 | |
6001e347 RR |
187 | size_t wxMBConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const |
188 | { | |
189 | return wxMB2WC(buf, psz, n); | |
190 | } | |
191 | ||
192 | size_t wxMBConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
193 | { | |
194 | return wxWC2MB(buf, psz, n); | |
195 | } | |
196 | ||
197 | const wxWCharBuffer wxMBConv::cMB2WC(const char *psz) const | |
198 | { | |
f6bcfd97 | 199 | if (psz) |
6001e347 RR |
200 | { |
201 | size_t nLen = MB2WC((wchar_t *) NULL, psz, 0); | |
f6bcfd97 BP |
202 | if (nLen == (size_t)-1) |
203 | return wxWCharBuffer((wchar_t *) NULL); | |
6001e347 RR |
204 | wxWCharBuffer buf(nLen); |
205 | MB2WC((wchar_t *)(const wchar_t *) buf, psz, nLen); | |
206 | return buf; | |
f6bcfd97 BP |
207 | } |
208 | else | |
6001e347 RR |
209 | return wxWCharBuffer((wchar_t *) NULL); |
210 | } | |
211 | ||
212 | const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *psz) const | |
213 | { | |
f6bcfd97 | 214 | if (psz) |
6001e347 RR |
215 | { |
216 | size_t nLen = WC2MB((char *) NULL, psz, 0); | |
f6bcfd97 BP |
217 | if (nLen == (size_t)-1) |
218 | return wxCharBuffer((char *) NULL); | |
6001e347 RR |
219 | wxCharBuffer buf(nLen); |
220 | WC2MB((char *)(const char *) buf, psz, nLen); | |
221 | return buf; | |
f6bcfd97 BP |
222 | } |
223 | else | |
6001e347 RR |
224 | return wxCharBuffer((char *) NULL); |
225 | } | |
226 | ||
f6bcfd97 | 227 | // ---------------------------------------------------------------------------- |
6001e347 | 228 | // standard file conversion |
f6bcfd97 | 229 | // ---------------------------------------------------------------------------- |
6001e347 RR |
230 | |
231 | WXDLLEXPORT_DATA(wxMBConvFile) wxConvFile; | |
232 | ||
233 | // just use the libc conversion for now | |
234 | size_t wxMBConvFile::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
235 | { | |
236 | return wxMB2WC(buf, psz, n); | |
237 | } | |
238 | ||
239 | size_t wxMBConvFile::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
240 | { | |
241 | return wxWC2MB(buf, psz, n); | |
242 | } | |
243 | ||
f6bcfd97 | 244 | // ---------------------------------------------------------------------------- |
6001e347 | 245 | // standard gdk conversion |
f6bcfd97 BP |
246 | // ---------------------------------------------------------------------------- |
247 | ||
248 | #ifdef __WXGTK12__ | |
6001e347 RR |
249 | |
250 | WXDLLEXPORT_DATA(wxMBConvGdk) wxConvGdk; | |
251 | ||
252 | #include <gdk/gdk.h> | |
253 | ||
254 | size_t wxMBConvGdk::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
255 | { | |
dccce9ea | 256 | if (buf) |
4def3b35 VS |
257 | { |
258 | return gdk_mbstowcs((GdkWChar *)buf, psz, n); | |
dccce9ea VZ |
259 | } |
260 | else | |
4def3b35 VS |
261 | { |
262 | GdkWChar *nbuf = new GdkWChar[n=strlen(psz)]; | |
263 | size_t len = gdk_mbstowcs(nbuf, psz, n); | |
264 | delete[] nbuf; | |
265 | return len; | |
266 | } | |
6001e347 RR |
267 | } |
268 | ||
269 | size_t wxMBConvGdk::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
270 | { | |
4def3b35 VS |
271 | char *mbstr = gdk_wcstombs((GdkWChar *)psz); |
272 | size_t len = mbstr ? strlen(mbstr) : 0; | |
dccce9ea | 273 | if (buf) |
4def3b35 | 274 | { |
dccce9ea | 275 | if (len > n) |
4def3b35 VS |
276 | len = n; |
277 | memcpy(buf, psz, len); | |
dccce9ea | 278 | if (len < n) |
4def3b35 VS |
279 | buf[len] = 0; |
280 | } | |
281 | return len; | |
6001e347 | 282 | } |
f6bcfd97 | 283 | |
6001e347 RR |
284 | #endif // GTK > 1.0 |
285 | ||
286 | // ---------------------------------------------------------------------------- | |
287 | // UTF-7 | |
288 | // ---------------------------------------------------------------------------- | |
289 | ||
290 | WXDLLEXPORT_DATA(wxMBConvUTF7) wxConvUTF7; | |
291 | ||
292 | #if 0 | |
293 | static char utf7_setD[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
294 | "abcdefghijklmnopqrstuvwxyz" | |
295 | "0123456789'(),-./:?"; | |
296 | static char utf7_setO[]="!\"#$%&*;<=>@[]^_`{|}"; | |
297 | static char utf7_setB[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
298 | "abcdefghijklmnopqrstuvwxyz" | |
299 | "0123456789+/"; | |
300 | #endif | |
301 | ||
302 | // TODO: write actual implementations of UTF-7 here | |
303 | size_t wxMBConvUTF7::MB2WC(wchar_t * WXUNUSED(buf), | |
304 | const char * WXUNUSED(psz), | |
305 | size_t WXUNUSED(n)) const | |
306 | { | |
307 | return 0; | |
308 | } | |
309 | ||
310 | size_t wxMBConvUTF7::WC2MB(char * WXUNUSED(buf), | |
311 | const wchar_t * WXUNUSED(psz), | |
312 | size_t WXUNUSED(n)) const | |
313 | { | |
314 | return 0; | |
315 | } | |
316 | ||
f6bcfd97 | 317 | // ---------------------------------------------------------------------------- |
6001e347 | 318 | // UTF-8 |
f6bcfd97 | 319 | // ---------------------------------------------------------------------------- |
6001e347 RR |
320 | |
321 | WXDLLEXPORT_DATA(wxMBConvUTF8) wxConvUTF8; | |
322 | ||
dccce9ea | 323 | static wxUint32 utf8_max[]= |
4def3b35 | 324 | { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff, 0xffffffff }; |
6001e347 RR |
325 | |
326 | size_t wxMBConvUTF8::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
327 | { | |
4def3b35 VS |
328 | size_t len = 0; |
329 | ||
dccce9ea | 330 | while (*psz && ((!buf) || (len < n))) |
4def3b35 VS |
331 | { |
332 | unsigned char cc = *psz++, fc = cc; | |
333 | unsigned cnt; | |
dccce9ea | 334 | for (cnt = 0; fc & 0x80; cnt++) |
4def3b35 | 335 | fc <<= 1; |
dccce9ea | 336 | if (!cnt) |
4def3b35 VS |
337 | { |
338 | // plain ASCII char | |
dccce9ea | 339 | if (buf) |
4def3b35 VS |
340 | *buf++ = cc; |
341 | len++; | |
dccce9ea VZ |
342 | } |
343 | else | |
4def3b35 VS |
344 | { |
345 | cnt--; | |
dccce9ea | 346 | if (!cnt) |
4def3b35 VS |
347 | { |
348 | // invalid UTF-8 sequence | |
349 | return (size_t)-1; | |
dccce9ea VZ |
350 | } |
351 | else | |
4def3b35 VS |
352 | { |
353 | unsigned ocnt = cnt - 1; | |
354 | wxUint32 res = cc & (0x3f >> cnt); | |
dccce9ea | 355 | while (cnt--) |
4def3b35 VS |
356 | { |
357 | cc = *psz++; | |
dccce9ea | 358 | if ((cc & 0xC0) != 0x80) |
4def3b35 VS |
359 | { |
360 | // invalid UTF-8 sequence | |
361 | return (size_t)-1; | |
362 | } | |
363 | res = (res << 6) | (cc & 0x3f); | |
364 | } | |
dccce9ea | 365 | if (res <= utf8_max[ocnt]) |
4def3b35 VS |
366 | { |
367 | // illegal UTF-8 encoding | |
368 | return (size_t)-1; | |
369 | } | |
1cd52418 | 370 | #ifdef WC_UTF16 |
4def3b35 VS |
371 | size_t pa = encode_utf16(res, buf); |
372 | if (pa == (size_t)-1) | |
373 | return (size_t)-1; | |
dccce9ea | 374 | if (buf) |
4def3b35 VS |
375 | buf += pa; |
376 | len += pa; | |
373658eb | 377 | #else // !WC_UTF16 |
dccce9ea | 378 | if (buf) |
4def3b35 VS |
379 | *buf++ = res; |
380 | len++; | |
373658eb | 381 | #endif // WC_UTF16/!WC_UTF16 |
4def3b35 VS |
382 | } |
383 | } | |
6001e347 | 384 | } |
dccce9ea | 385 | if (buf && (len < n)) |
4def3b35 VS |
386 | *buf = 0; |
387 | return len; | |
6001e347 RR |
388 | } |
389 | ||
390 | size_t wxMBConvUTF8::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
391 | { | |
4def3b35 | 392 | size_t len = 0; |
6001e347 | 393 | |
dccce9ea | 394 | while (*psz && ((!buf) || (len < n))) |
4def3b35 VS |
395 | { |
396 | wxUint32 cc; | |
1cd52418 | 397 | #ifdef WC_UTF16 |
eccf1b2c | 398 | size_t pa = decode_utf16(psz, cc); |
4def3b35 | 399 | psz += (pa == (size_t)-1) ? 1 : pa; |
1cd52418 | 400 | #else |
4def3b35 VS |
401 | cc=(*psz++) & 0x7fffffff; |
402 | #endif | |
403 | unsigned cnt; | |
404 | for (cnt = 0; cc > utf8_max[cnt]; cnt++) {} | |
dccce9ea | 405 | if (!cnt) |
4def3b35 VS |
406 | { |
407 | // plain ASCII char | |
dccce9ea | 408 | if (buf) |
574c939e | 409 | *buf++ = (char) cc; |
4def3b35 | 410 | len++; |
dccce9ea VZ |
411 | } |
412 | ||
413 | else | |
4def3b35 VS |
414 | { |
415 | len += cnt + 1; | |
dccce9ea | 416 | if (buf) |
4def3b35 | 417 | { |
574c939e | 418 | *buf++ = (char) ((-128 >> cnt) | ((cc >> (cnt * 6)) & (0x3f >> cnt))); |
4def3b35 | 419 | while (cnt--) |
574c939e | 420 | *buf++ = (char) (0x80 | ((cc >> (cnt * 6)) & 0x3f)); |
4def3b35 VS |
421 | } |
422 | } | |
6001e347 | 423 | } |
4def3b35 VS |
424 | |
425 | if (buf && (len<n)) *buf = 0; | |
426 | return len; | |
6001e347 RR |
427 | } |
428 | ||
36acb880 VZ |
429 | // ============================================================================ |
430 | // wxCharacterSet and derived classes | |
431 | // ============================================================================ | |
432 | ||
433 | // ---------------------------------------------------------------------------- | |
434 | // wxCharacterSet is the ABC for the classes below | |
435 | // ---------------------------------------------------------------------------- | |
436 | ||
6001e347 RR |
437 | class wxCharacterSet |
438 | { | |
1cd52418 | 439 | public: |
4f61e22c VS |
440 | wxCharacterSet(const wxChar*name) : cname(name) {} |
441 | virtual ~wxCharacterSet() {} | |
442 | virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) = 0; | |
443 | virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) = 0; | |
444 | virtual bool usable() const = 0; | |
f1339c56 RR |
445 | public: |
446 | const wxChar*cname; | |
1cd52418 OK |
447 | }; |
448 | ||
36acb880 VZ |
449 | // ---------------------------------------------------------------------------- |
450 | // ID_CharSet: implementation of wxCharacterSet using an existing wxMBConv | |
451 | // ---------------------------------------------------------------------------- | |
452 | ||
1cd52418 OK |
453 | class ID_CharSet : public wxCharacterSet |
454 | { | |
455 | public: | |
36acb880 | 456 | ID_CharSet(const wxChar *name, wxMBConv *cnv) |
f1339c56 | 457 | : wxCharacterSet(name), work(cnv) {} |
dccce9ea | 458 | |
4def3b35 | 459 | size_t MB2WC(wchar_t *buf, const char *psz, size_t n) |
f1339c56 | 460 | { return work ? work->MB2WC(buf,psz,n) : (size_t)-1; } |
dccce9ea | 461 | |
4def3b35 | 462 | size_t WC2MB(char *buf, const wchar_t *psz, size_t n) |
f1339c56 | 463 | { return work ? work->WC2MB(buf,psz,n) : (size_t)-1; } |
dccce9ea | 464 | |
4f61e22c | 465 | bool usable() const |
f1339c56 RR |
466 | { return work!=NULL; } |
467 | public: | |
468 | wxMBConv*work; | |
1cd52418 OK |
469 | }; |
470 | ||
3caec1bb | 471 | |
36acb880 VZ |
472 | // ============================================================================ |
473 | // The classes doing conversion using the iconv_xxx() functions | |
474 | // ============================================================================ | |
3caec1bb | 475 | |
b040e242 | 476 | #ifdef HAVE_ICONV |
3a0d76bc | 477 | |
3caec1bb VS |
478 | // VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with E2BIG |
479 | // if output buffer is _exactly_ as big as needed. Such case is (unless there's | |
480 | // yet another bug in glibc) the only case when iconv() returns with (size_t)-1 | |
481 | // (which means error) and says there are 0 bytes left in the input buffer -- | |
482 | // when _real_ error occurs, bytes-left-in-input buffer is non-zero. Hence, | |
483 | // this alternative test for iconv() failure. | |
484 | // [This bug does not appear in glibc 2.2.] | |
485 | #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 1 | |
486 | #define ICONV_FAILED(cres, bufLeft) ((cres == (size_t)-1) && \ | |
487 | (errno != E2BIG || bufLeft != 0)) | |
488 | #else | |
489 | #define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1) | |
490 | #endif | |
491 | ||
b040e242 | 492 | #define ICONV_CHAR_CAST(x) ((ICONV_CONST char **)(x)) |
36acb880 VZ |
493 | |
494 | // ---------------------------------------------------------------------------- | |
495 | // IC_CharSet: encapsulates an iconv character set | |
496 | // ---------------------------------------------------------------------------- | |
497 | ||
1cd52418 OK |
498 | class IC_CharSet : public wxCharacterSet |
499 | { | |
500 | public: | |
36acb880 VZ |
501 | IC_CharSet(const wxChar *name); |
502 | virtual ~IC_CharSet(); | |
503 | ||
504 | virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n); | |
505 | virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n); | |
506 | ||
507 | bool usable() const | |
508 | { return (m2w != (iconv_t)-1) && (w2m != (iconv_t)-1); } | |
509 | ||
510 | protected: | |
511 | // the iconv handlers used to translate from multibyte to wide char and in | |
512 | // the other direction | |
513 | iconv_t m2w, | |
514 | w2m; | |
515 | ||
516 | private: | |
517 | // the name (for iconv_open()) of a wide char charset - if none is | |
518 | // available on this machine, it will remain NULL | |
519 | static const char *ms_wcCharsetName; | |
520 | ||
521 | // true if the wide char encoding we use (i.e. ms_wcCharsetName) has | |
522 | // different endian-ness than the native one | |
405d8f46 | 523 | static bool ms_wcNeedsSwap; |
36acb880 VZ |
524 | }; |
525 | ||
526 | const char *IC_CharSet::ms_wcCharsetName = NULL; | |
405d8f46 | 527 | bool IC_CharSet::ms_wcNeedsSwap = FALSE; |
36acb880 VZ |
528 | |
529 | IC_CharSet::IC_CharSet(const wxChar *name) | |
530 | : wxCharacterSet(name) | |
531 | { | |
532 | // check for charset that represents wchar_t: | |
533 | if (ms_wcCharsetName == NULL) | |
f1339c56 | 534 | { |
36acb880 | 535 | ms_wcNeedsSwap = FALSE; |
dccce9ea | 536 | |
36acb880 VZ |
537 | // try charset with explicit bytesex info (e.g. "UCS-4LE"): |
538 | ms_wcCharsetName = WC_NAME_BEST; | |
539 | m2w = iconv_open(ms_wcCharsetName, wxConvLibc.cWX2MB(name)); | |
3a0d76bc | 540 | |
36acb880 VZ |
541 | if (m2w == (iconv_t)-1) |
542 | { | |
543 | // try charset w/o bytesex info (e.g. "UCS4") | |
544 | // and check for bytesex ourselves: | |
545 | ms_wcCharsetName = WC_NAME; | |
546 | m2w = iconv_open(ms_wcCharsetName, wxConvLibc.cWX2MB(name)); | |
547 | ||
548 | // last bet, try if it knows WCHAR_T pseudo-charset | |
3a0d76bc VS |
549 | if (m2w == (iconv_t)-1) |
550 | { | |
36acb880 VZ |
551 | ms_wcCharsetName = "WCHAR_T"; |
552 | m2w = iconv_open(ms_wcCharsetName, wxConvLibc.cWX2MB(name)); | |
553 | } | |
3a0d76bc | 554 | |
36acb880 VZ |
555 | if (m2w != (iconv_t)-1) |
556 | { | |
557 | char buf[2], *bufPtr; | |
558 | wchar_t wbuf[2], *wbufPtr; | |
559 | size_t insz, outsz; | |
560 | size_t res; | |
561 | ||
562 | buf[0] = 'A'; | |
563 | buf[1] = 0; | |
564 | wbuf[0] = 0; | |
565 | insz = 2; | |
566 | outsz = SIZEOF_WCHAR_T * 2; | |
567 | wbufPtr = wbuf; | |
568 | bufPtr = buf; | |
569 | ||
570 | res = iconv(m2w, ICONV_CHAR_CAST(&bufPtr), &insz, | |
571 | (char**)&wbufPtr, &outsz); | |
572 | ||
573 | if (ICONV_FAILED(res, insz)) | |
3a0d76bc | 574 | { |
36acb880 VZ |
575 | ms_wcCharsetName = NULL; |
576 | wxLogLastError(wxT("iconv")); | |
577 | wxLogError(_("Convertion to charset '%s' doesn't work."), name); | |
3a0d76bc VS |
578 | } |
579 | else | |
580 | { | |
36acb880 | 581 | ms_wcNeedsSwap = wbuf[0] != (wchar_t)buf[0]; |
3a0d76bc VS |
582 | } |
583 | } | |
36acb880 VZ |
584 | else |
585 | { | |
586 | ms_wcCharsetName = NULL; | |
373658eb | 587 | |
957686c8 VS |
588 | // VS: we must not output an error here, since wxWindows will safely |
589 | // fall back to using wxEncodingConverter. | |
590 | wxLogTrace(wxT("strconv"), wxT("Impossible to convert to/from charset '%s' with iconv, falling back to wxEncodingConverter."), name); | |
591 | //wxLogError( | |
36acb880 | 592 | } |
3a0d76bc | 593 | } |
36acb880 | 594 | wxLogTrace(wxT("strconv"), wxT("wchar_t charset is '%s', needs swap: %i"), ms_wcCharsetName, ms_wcNeedsSwap); |
3a0d76bc | 595 | } |
36acb880 | 596 | else // we already have ms_wcCharsetName |
3caec1bb | 597 | { |
36acb880 | 598 | m2w = iconv_open(ms_wcCharsetName, wxConvLibc.cWX2MB(name)); |
f1339c56 | 599 | } |
dccce9ea | 600 | |
36acb880 VZ |
601 | // NB: don't ever pass NULL to iconv_open(), it may crash! |
602 | if ( ms_wcCharsetName ) | |
f1339c56 | 603 | { |
36acb880 VZ |
604 | w2m = iconv_open(wxConvLibc.cWX2MB(name), ms_wcCharsetName); |
605 | } | |
405d8f46 VZ |
606 | else |
607 | { | |
608 | w2m = (iconv_t)-1; | |
609 | } | |
36acb880 | 610 | } |
3caec1bb | 611 | |
36acb880 VZ |
612 | IC_CharSet::~IC_CharSet() |
613 | { | |
614 | if ( m2w != (iconv_t)-1 ) | |
615 | iconv_close(m2w); | |
616 | if ( w2m != (iconv_t)-1 ) | |
617 | iconv_close(w2m); | |
618 | } | |
3a0d76bc | 619 | |
36acb880 VZ |
620 | size_t IC_CharSet::MB2WC(wchar_t *buf, const char *psz, size_t n) |
621 | { | |
622 | size_t inbuf = strlen(psz); | |
623 | size_t outbuf = n * SIZEOF_WCHAR_T; | |
624 | size_t res, cres; | |
625 | // VS: Use these instead of psz, buf because iconv() modifies its arguments: | |
626 | wchar_t *bufPtr = buf; | |
627 | const char *pszPtr = psz; | |
628 | ||
629 | if (buf) | |
630 | { | |
631 | // have destination buffer, convert there | |
632 | cres = iconv(m2w, | |
633 | ICONV_CHAR_CAST(&pszPtr), &inbuf, | |
634 | (char**)&bufPtr, &outbuf); | |
635 | res = n - (outbuf / SIZEOF_WCHAR_T); | |
dccce9ea | 636 | |
36acb880 | 637 | if (ms_wcNeedsSwap) |
3a0d76bc | 638 | { |
36acb880 VZ |
639 | // convert to native endianness |
640 | WC_BSWAP(buf /* _not_ bufPtr */, res) | |
3a0d76bc | 641 | } |
36acb880 VZ |
642 | } |
643 | else | |
644 | { | |
645 | // no destination buffer... convert using temp buffer | |
646 | // to calculate destination buffer requirement | |
647 | wchar_t tbuf[8]; | |
648 | res = 0; | |
649 | do { | |
650 | bufPtr = tbuf; | |
651 | outbuf = 8*SIZEOF_WCHAR_T; | |
652 | ||
653 | cres = iconv(m2w, | |
654 | ICONV_CHAR_CAST(&pszPtr), &inbuf, | |
655 | (char**)&bufPtr, &outbuf ); | |
656 | ||
657 | res += 8-(outbuf/SIZEOF_WCHAR_T); | |
658 | } while ((cres==(size_t)-1) && (errno==E2BIG)); | |
f1339c56 | 659 | } |
dccce9ea | 660 | |
36acb880 | 661 | if (ICONV_FAILED(cres, inbuf)) |
f1339c56 | 662 | { |
36acb880 VZ |
663 | //VS: it is ok if iconv fails, hence trace only |
664 | wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode())); | |
665 | return (size_t)-1; | |
666 | } | |
667 | ||
668 | return res; | |
669 | } | |
670 | ||
671 | size_t IC_CharSet::WC2MB(char *buf, const wchar_t *psz, size_t n) | |
672 | { | |
1cd52418 | 673 | #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530) |
36acb880 | 674 | size_t inbuf = std::wcslen(psz) * SIZEOF_WCHAR_T; |
1cd52418 | 675 | #else |
36acb880 | 676 | size_t inbuf = ::wcslen(psz) * SIZEOF_WCHAR_T; |
1cd52418 | 677 | #endif |
36acb880 VZ |
678 | size_t outbuf = n; |
679 | size_t res, cres; | |
3a0d76bc | 680 | |
36acb880 | 681 | wchar_t *tmpbuf = 0; |
3caec1bb | 682 | |
36acb880 VZ |
683 | if (ms_wcNeedsSwap) |
684 | { | |
685 | // need to copy to temp buffer to switch endianness | |
686 | // this absolutely doesn't rock! | |
687 | // (no, doing WC_BSWAP twice on the original buffer won't help, as it | |
688 | // could be in read-only memory, or be accessed in some other thread) | |
689 | tmpbuf=(wchar_t*)malloc((inbuf+1)*SIZEOF_WCHAR_T); | |
690 | memcpy(tmpbuf,psz,(inbuf+1)*SIZEOF_WCHAR_T); | |
691 | WC_BSWAP(tmpbuf, inbuf) | |
692 | psz=tmpbuf; | |
693 | } | |
3a0d76bc | 694 | |
36acb880 VZ |
695 | if (buf) |
696 | { | |
697 | // have destination buffer, convert there | |
698 | cres = iconv( w2m, ICONV_CHAR_CAST(&psz), &inbuf, &buf, &outbuf ); | |
3a0d76bc | 699 | |
36acb880 VZ |
700 | res = n-outbuf; |
701 | } | |
702 | else | |
703 | { | |
704 | // no destination buffer... convert using temp buffer | |
705 | // to calculate destination buffer requirement | |
706 | char tbuf[16]; | |
707 | res = 0; | |
708 | do { | |
709 | buf = tbuf; outbuf = 16; | |
710 | ||
711 | cres = iconv( w2m, ICONV_CHAR_CAST(&psz), &inbuf, &buf, &outbuf ); | |
dccce9ea | 712 | |
36acb880 VZ |
713 | res += 16 - outbuf; |
714 | } while ((cres==(size_t)-1) && (errno==E2BIG)); | |
f1339c56 | 715 | } |
dccce9ea | 716 | |
36acb880 VZ |
717 | if (ms_wcNeedsSwap) |
718 | { | |
719 | free(tmpbuf); | |
720 | } | |
dccce9ea | 721 | |
36acb880 VZ |
722 | if (ICONV_FAILED(cres, inbuf)) |
723 | { | |
724 | //VS: it is ok if iconv fails, hence trace only | |
725 | wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode())); | |
726 | return (size_t)-1; | |
727 | } | |
728 | ||
729 | return res; | |
730 | } | |
731 | ||
b040e242 | 732 | #endif // HAVE_ICONV |
36acb880 VZ |
733 | |
734 | // ============================================================================ | |
735 | // Win32 conversion classes | |
736 | // ============================================================================ | |
1cd52418 | 737 | |
04ef50df | 738 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) |
373658eb VZ |
739 | |
740 | extern long wxCharsetToCodepage(const wxChar *charset); // from utils.cpp | |
741 | ||
1cd52418 OK |
742 | class CP_CharSet : public wxCharacterSet |
743 | { | |
744 | public: | |
b1d66b54 VZ |
745 | CP_CharSet(const wxChar* name) |
746 | : wxCharacterSet(name) | |
747 | { | |
748 | m_CodePage = wxCharsetToCodepage(name); | |
749 | } | |
dccce9ea | 750 | |
4def3b35 | 751 | size_t MB2WC(wchar_t *buf, const char *psz, size_t n) |
f1339c56 | 752 | { |
dccce9ea | 753 | size_t len = |
b1d66b54 | 754 | MultiByteToWideChar(m_CodePage, 0, psz, -1, buf, buf ? n : 0); |
1e6feb95 | 755 | //VS: returns # of written chars for buf!=NULL and *size* |
35d764b0 VS |
756 | // needed buffer for buf==NULL |
757 | return len ? (buf ? len : len-1) : (size_t)-1; | |
f1339c56 | 758 | } |
dccce9ea | 759 | |
4def3b35 | 760 | size_t WC2MB(char *buf, const wchar_t *psz, size_t n) |
f1339c56 | 761 | { |
b1d66b54 | 762 | size_t len = WideCharToMultiByte(m_CodePage, 0, psz, -1, buf, |
4def3b35 | 763 | buf ? n : 0, NULL, NULL); |
1e6feb95 | 764 | //VS: returns # of written chars for buf!=NULL and *size* |
35d764b0 VS |
765 | // needed buffer for buf==NULL |
766 | return len ? (buf ? len : len-1) : (size_t)-1; | |
f1339c56 | 767 | } |
dccce9ea | 768 | |
4f61e22c | 769 | bool usable() const |
b1d66b54 | 770 | { return m_CodePage != -1; } |
f1339c56 RR |
771 | |
772 | public: | |
b1d66b54 | 773 | long m_CodePage; |
1cd52418 | 774 | }; |
1e6feb95 VZ |
775 | #endif // __WIN32__ |
776 | ||
36acb880 VZ |
777 | // ============================================================================ |
778 | // wxEncodingConverter based conversion classes | |
779 | // ============================================================================ | |
780 | ||
1e6feb95 | 781 | #if wxUSE_FONTMAP |
1cd52418 OK |
782 | |
783 | class EC_CharSet : public wxCharacterSet | |
784 | { | |
6001e347 | 785 | public: |
f1339c56 RR |
786 | // temporarily just use wxEncodingConverter stuff, |
787 | // so that it works while a better implementation is built | |
b1d66b54 VZ |
788 | EC_CharSet(const wxChar* name) : wxCharacterSet(name), |
789 | enc(wxFONTENCODING_SYSTEM) | |
f1339c56 RR |
790 | { |
791 | if (name) | |
792 | enc = wxTheFontMapper->CharsetToEncoding(name, FALSE); | |
cafbf6fb VZ |
793 | |
794 | m_ok = m2w.Init(enc, wxFONTENCODING_UNICODE) && | |
795 | w2m.Init(wxFONTENCODING_UNICODE, enc); | |
f1339c56 | 796 | } |
dccce9ea | 797 | |
574c939e | 798 | size_t MB2WC(wchar_t *buf, const char *psz, size_t WXUNUSED(n)) |
f1339c56 RR |
799 | { |
800 | size_t inbuf = strlen(psz); | |
dccce9ea | 801 | if (buf) |
4def3b35 | 802 | m2w.Convert(psz,buf); |
f1339c56 RR |
803 | return inbuf; |
804 | } | |
dccce9ea | 805 | |
574c939e | 806 | size_t WC2MB(char *buf, const wchar_t *psz, size_t WXUNUSED(n)) |
f1339c56 | 807 | { |
de85a884 VZ |
808 | #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \ |
809 | || ( defined(__MWERKS__) && defined(__WXMSW__) ) | |
f1339c56 | 810 | size_t inbuf = std::wcslen(psz); |
1cd52418 | 811 | #else |
f1339c56 | 812 | size_t inbuf = ::wcslen(psz); |
1cd52418 | 813 | #endif |
f1339c56 RR |
814 | if (buf) |
815 | w2m.Convert(psz,buf); | |
dccce9ea | 816 | |
f1339c56 RR |
817 | return inbuf; |
818 | } | |
dccce9ea | 819 | |
cafbf6fb | 820 | bool usable() const { return m_ok; } |
f1339c56 RR |
821 | |
822 | public: | |
823 | wxFontEncoding enc; | |
824 | wxEncodingConverter m2w, w2m; | |
cafbf6fb VZ |
825 | |
826 | // were we initialized successfully? | |
827 | bool m_ok; | |
f6bcfd97 | 828 | }; |
6001e347 | 829 | |
1e6feb95 VZ |
830 | #endif // wxUSE_FONTMAP |
831 | ||
36acb880 VZ |
832 | // ---------------------------------------------------------------------------- |
833 | // the function creating the wxCharacterSet for the specified charset on the | |
834 | // current system, trying all possibilities | |
835 | // ---------------------------------------------------------------------------- | |
836 | ||
f6bcfd97 | 837 | static wxCharacterSet *wxGetCharacterSet(const wxChar *name) |
6001e347 | 838 | { |
cafbf6fb VZ |
839 | // check for the special case of ASCII charset |
840 | #if wxUSE_FONTMAP | |
841 | if ( wxTheFontMapper->CharsetToEncoding(name) == wxFONTENCODING_DEFAULT ) | |
842 | #else // wxUSE_FONTMAP | |
843 | if ( !name ) | |
844 | #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP | |
f1339c56 | 845 | { |
cafbf6fb VZ |
846 | // don't convert at all |
847 | return NULL; | |
1cd52418 | 848 | } |
dccce9ea | 849 | |
cafbf6fb VZ |
850 | // the test above must have taken care of this case |
851 | wxCHECK_MSG( name, NULL, _T("NULL name must be wxFONTENCODING_DEFAULT") ); | |
1e6feb95 | 852 | |
cafbf6fb VZ |
853 | wxCharacterSet *cset; |
854 | ||
855 | if ( wxStricmp(name, wxT("UTF8")) == 0 || wxStricmp(name, wxT("UTF-8")) == 0) | |
856 | { | |
857 | cset = new ID_CharSet(name, &wxConvUTF8); | |
858 | } | |
859 | else | |
dccce9ea | 860 | { |
cafbf6fb VZ |
861 | #ifdef HAVE_ICONV |
862 | cset = new IC_CharSet(name); | |
863 | #else // !HAVE_ICONV | |
dccce9ea | 864 | cset = NULL; |
cafbf6fb | 865 | #endif // HAVE_ICONV/!HAVE_ICONV |
dccce9ea VZ |
866 | } |
867 | ||
734eda8a VZ |
868 | // it can only be NULL in this case |
869 | #ifndef HAVE_ICONV | |
870 | if ( cset ) | |
871 | #endif // !HAVE_ICONV | |
872 | { | |
873 | if ( cset->usable() ) | |
874 | return cset; | |
cafbf6fb | 875 | |
734eda8a VZ |
876 | delete cset; |
877 | cset = NULL; | |
878 | } | |
cafbf6fb | 879 | |
04ef50df | 880 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) |
cafbf6fb VZ |
881 | cset = new CP_CharSet(name); |
882 | if ( cset->usable() ) | |
dccce9ea VZ |
883 | return cset; |
884 | ||
885 | delete cset; | |
cafbf6fb | 886 | cset = NULL; |
dccce9ea VZ |
887 | #endif // __WIN32__ |
888 | ||
1e6feb95 | 889 | #if wxUSE_FONTMAP |
f1339c56 | 890 | cset = new EC_CharSet(name); |
cafbf6fb | 891 | if ( cset->usable() ) |
dccce9ea VZ |
892 | return cset; |
893 | ||
f1339c56 | 894 | delete cset; |
cafbf6fb VZ |
895 | cset = NULL; |
896 | #endif // wxUSE_FONTMAP | |
897 | ||
898 | wxLogError(_("Cannot convert from encoding '%s'!"), name); | |
899 | ||
f1339c56 | 900 | return NULL; |
6001e347 RR |
901 | } |
902 | ||
36acb880 VZ |
903 | // ============================================================================ |
904 | // wxCSConv implementation | |
905 | // ============================================================================ | |
906 | ||
6001e347 RR |
907 | wxCSConv::wxCSConv(const wxChar *charset) |
908 | { | |
dccce9ea | 909 | m_name = (wxChar *)NULL; |
f1339c56 | 910 | m_cset = (wxCharacterSet *) NULL; |
82713003 VZ |
911 | m_deferred = TRUE; |
912 | ||
f1339c56 | 913 | SetName(charset); |
6001e347 RR |
914 | } |
915 | ||
916 | wxCSConv::~wxCSConv() | |
917 | { | |
65e50848 JS |
918 | Clear(); |
919 | } | |
920 | ||
921 | void wxCSConv::Clear() | |
922 | { | |
923 | if (m_name) | |
924 | free(m_name); | |
925 | if (m_cset) | |
926 | delete m_cset; | |
927 | m_name = NULL; | |
928 | m_cset = NULL; | |
6001e347 RR |
929 | } |
930 | ||
931 | void wxCSConv::SetName(const wxChar *charset) | |
932 | { | |
f1339c56 RR |
933 | if (charset) |
934 | { | |
935 | m_name = wxStrdup(charset); | |
936 | m_deferred = TRUE; | |
937 | } | |
6001e347 RR |
938 | } |
939 | ||
940 | void wxCSConv::LoadNow() | |
941 | { | |
f1339c56 RR |
942 | if (m_deferred) |
943 | { | |
dccce9ea | 944 | if ( !m_name ) |
f1339c56 | 945 | { |
dccce9ea VZ |
946 | wxString name = wxLocale::GetSystemEncodingName(); |
947 | if ( !name.empty() ) | |
948 | SetName(name); | |
f1339c56 | 949 | } |
dccce9ea | 950 | |
a45a98fb VZ |
951 | // wxGetCharacterSet() complains about NULL name |
952 | m_cset = m_name ? wxGetCharacterSet(m_name) : NULL; | |
f1339c56 | 953 | m_deferred = FALSE; |
6001e347 | 954 | } |
6001e347 RR |
955 | } |
956 | ||
957 | size_t wxCSConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
958 | { | |
f1339c56 | 959 | ((wxCSConv *)this)->LoadNow(); // discard constness |
dccce9ea | 960 | |
f1339c56 RR |
961 | if (m_cset) |
962 | return m_cset->MB2WC(buf, psz, n); | |
963 | ||
964 | // latin-1 (direct) | |
4def3b35 | 965 | size_t len = strlen(psz); |
dccce9ea | 966 | |
f1339c56 RR |
967 | if (buf) |
968 | { | |
4def3b35 | 969 | for (size_t c = 0; c <= len; c++) |
f1339c56 RR |
970 | buf[c] = (unsigned char)(psz[c]); |
971 | } | |
dccce9ea | 972 | |
f1339c56 | 973 | return len; |
6001e347 RR |
974 | } |
975 | ||
976 | size_t wxCSConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
977 | { | |
f1339c56 | 978 | ((wxCSConv *)this)->LoadNow(); // discard constness |
dccce9ea | 979 | |
f1339c56 RR |
980 | if (m_cset) |
981 | return m_cset->WC2MB(buf, psz, n); | |
1cd52418 | 982 | |
f1339c56 | 983 | // latin-1 (direct) |
de85a884 VZ |
984 | #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \ |
985 | || ( defined(__MWERKS__) && defined(__WXMSW__) ) | |
f1339c56 | 986 | size_t len=std::wcslen(psz); |
d834f22c | 987 | #else |
f1339c56 | 988 | size_t len=::wcslen(psz); |
d834f22c | 989 | #endif |
f1339c56 RR |
990 | if (buf) |
991 | { | |
4def3b35 VS |
992 | for (size_t c = 0; c <= len; c++) |
993 | buf[c] = (psz[c] > 0xff) ? '?' : psz[c]; | |
f1339c56 | 994 | } |
dccce9ea | 995 | |
f1339c56 | 996 | return len; |
6001e347 RR |
997 | } |
998 | ||
f6bcfd97 | 999 | #endif // wxUSE_WCHAR_T |
6001e347 RR |
1000 | |
1001 |