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