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