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