]> git.saurik.com Git - wxWidgets.git/blob - src/common/strconv.cpp
ffce9eb4187cefa75222f4bebad621bf99eac817
[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 licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 #ifndef __WXWINCE__
41 #include <errno.h>
42 #endif
43
44 #include <ctype.h>
45 #include <string.h>
46 #include <stdlib.h>
47
48 #include "wx/module.h"
49 #include "wx/strconv.h"
50
51 // ----------------------------------------------------------------------------
52 // globals
53 // ----------------------------------------------------------------------------
54
55 #if wxUSE_WCHAR_T
56 WXDLLIMPEXP_DATA_BASE(wxMBConv) wxConvLibc;
57 WXDLLIMPEXP_DATA_BASE(wxCSConv) wxConvLocal((const wxChar *)NULL);
58 WXDLLIMPEXP_DATA_BASE(wxCSConv) wxConvISO8859_1(_T("iso-8859-1"));
59 #else
60 // stand-ins in absence of wchar_t
61 WXDLLIMPEXP_DATA_BASE(wxMBConv) wxConvLibc,
62 wxConvFile,
63 wxConvISO8859_1,
64 wxConvLocal,
65 wxConvUTF8;
66 #endif // wxUSE_WCHAR_T
67
68 WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvCurrent = &wxConvLibc;
69
70 class wxStrConvModule: public wxModule
71 {
72 public:
73 wxStrConvModule() : wxModule() { }
74 virtual bool OnInit() { return TRUE; }
75 virtual void OnExit()
76 {
77 #if wxUSE_WCHAR_T
78 wxConvLocal.Clear();
79 wxConvISO8859_1.Clear();
80 #endif
81 }
82
83 DECLARE_DYNAMIC_CLASS(wxStrConvModule)
84 };
85
86 IMPLEMENT_DYNAMIC_CLASS(wxStrConvModule, wxModule)
87
88
89 // ----------------------------------------------------------------------------
90 // headers
91 // ----------------------------------------------------------------------------
92
93 #if wxUSE_WCHAR_T
94
95 #ifdef __SALFORDC__
96 #include <clib.h>
97 #endif
98
99 #ifdef HAVE_ICONV
100 #include <iconv.h>
101 #endif
102
103 #include "wx/encconv.h"
104 #include "wx/fontmap.h"
105
106 // ----------------------------------------------------------------------------
107 // macros
108 // ----------------------------------------------------------------------------
109
110 #define BSWAP_UCS4(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT32_SWAP_ALWAYS(str[_c]); }
111 #define BSWAP_UTF16(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT16_SWAP_ALWAYS(str[_c]); }
112
113 // under Unix SIZEOF_WCHAR_T is defined by configure, but under other platforms
114 // it might be not defined - assume the most common value
115 #ifndef SIZEOF_WCHAR_T
116 #define SIZEOF_WCHAR_T 2
117 #endif // !defined(SIZEOF_WCHAR_T)
118
119 #if SIZEOF_WCHAR_T == 4
120 #define WC_NAME "UCS4"
121 #define WC_BSWAP BSWAP_UCS4
122 #ifdef WORDS_BIGENDIAN
123 #define WC_NAME_BEST "UCS-4BE"
124 #else
125 #define WC_NAME_BEST "UCS-4LE"
126 #endif
127 #elif SIZEOF_WCHAR_T == 2
128 #define WC_NAME "UTF16"
129 #define WC_BSWAP BSWAP_UTF16
130 #define WC_UTF16
131 #ifdef WORDS_BIGENDIAN
132 #define WC_NAME_BEST "UTF-16BE"
133 #else
134 #define WC_NAME_BEST "UTF-16LE"
135 #endif
136 #else // sizeof(wchar_t) != 2 nor 4
137 // I don't know what to do about this
138 #error "Weird sizeof(wchar_t): please report your platform details to wx-users mailing list"
139 #endif
140
141 // ============================================================================
142 // implementation
143 // ============================================================================
144
145 // ----------------------------------------------------------------------------
146 // UTF-16 en/decoding to/from UCS-4
147 // ----------------------------------------------------------------------------
148
149
150 static size_t encode_utf16(wxUint32 input, wxUint16 *output)
151 {
152 if (input<=0xffff)
153 {
154 if (output) *output++ = (wxUint16) input;
155 return 1;
156 }
157 else if (input>=0x110000)
158 {
159 return (size_t)-1;
160 }
161 else
162 {
163 if (output)
164 {
165 *output++ = (wxUint16) ((input >> 10)+0xd7c0);
166 *output++ = (wxUint16) ((input&0x3ff)+0xdc00);
167 }
168 return 2;
169 }
170 }
171
172 static size_t decode_utf16(const wxUint16* input, wxUint32& output)
173 {
174 if ((*input<0xd800) || (*input>0xdfff))
175 {
176 output = *input;
177 return 1;
178 }
179 else if ((input[1]<0xdc00) || (input[1]>=0xdfff))
180 {
181 output = *input;
182 return (size_t)-1;
183 }
184 else
185 {
186 output = ((input[0] - 0xd7c0) << 10) + (input[1] - 0xdc00);
187 return 2;
188 }
189 }
190
191
192 // ----------------------------------------------------------------------------
193 // wxMBConv
194 // ----------------------------------------------------------------------------
195
196 #define IGNORE_LIBC 0
197
198 wxMBConv::~wxMBConv()
199 {
200 // nothing to do here
201 }
202
203 size_t wxMBConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const
204 {
205 #if IGNORE_LIBC
206 if (buf)
207 {
208 for (size_t i = 0; i < strlen( psz )+1; i++)
209 buf[i] = (wchar_t) psz[i];
210 return strlen( psz );
211 }
212 else
213 {
214 return strlen( psz );
215 }
216 #else
217 return wxMB2WC(buf, psz, n);
218 #endif
219 }
220
221 size_t wxMBConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
222 {
223 #if IGNORE_LIBC
224 if (buf)
225 {
226 for (size_t i = 0; i < wxStrlen( psz )+1; i++)
227 buf[i] = (char) psz[i];
228 return wxStrlen( psz );
229 }
230 else
231 {
232 return wxStrlen( psz );
233 }
234 #else
235 return wxWC2MB(buf, psz, n);
236 #endif
237 }
238
239 const wxWCharBuffer wxMBConv::cMB2WC(const char *psz) const
240 {
241 if ( psz )
242 {
243 // calculate the length of the buffer needed first
244 size_t nLen = MB2WC(NULL, psz, 0);
245 if ( nLen != (size_t)-1 )
246 {
247 // now do the actual conversion
248 wxWCharBuffer buf(nLen);
249 MB2WC(buf.data(), psz, nLen + 1); // with the trailing NUL
250
251 return buf;
252 }
253 }
254
255 wxWCharBuffer buf((wchar_t *)NULL);
256
257 return buf;
258 }
259
260 const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *pwz) const
261 {
262 if ( pwz )
263 {
264 size_t nLen = WC2MB(NULL, pwz, 0);
265 if ( nLen != (size_t)-1 )
266 {
267 wxCharBuffer buf(nLen+3); // space for a wxUint32 trailing zero
268 WC2MB(buf.data(), pwz, nLen + 4);
269
270 return buf;
271 }
272 }
273
274 wxCharBuffer buf((char *)NULL);
275
276 return buf;
277 }
278
279 // ----------------------------------------------------------------------------
280 // UTF-7
281 // ----------------------------------------------------------------------------
282
283 WXDLLIMPEXP_DATA_BASE(wxMBConvUTF7) wxConvUTF7;
284
285 #if 0
286 static char utf7_setD[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
287 "abcdefghijklmnopqrstuvwxyz"
288 "0123456789'(),-./:?";
289 static char utf7_setO[]="!\"#$%&*;<=>@[]^_`{|}";
290 static char utf7_setB[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
291 "abcdefghijklmnopqrstuvwxyz"
292 "0123456789+/";
293 #endif
294
295 // TODO: write actual implementations of UTF-7 here
296 size_t wxMBConvUTF7::MB2WC(wchar_t * WXUNUSED(buf),
297 const char * WXUNUSED(psz),
298 size_t WXUNUSED(n)) const
299 {
300 return 0;
301 }
302
303 size_t wxMBConvUTF7::WC2MB(char * WXUNUSED(buf),
304 const wchar_t * WXUNUSED(psz),
305 size_t WXUNUSED(n)) const
306 {
307 return 0;
308 }
309
310 // ----------------------------------------------------------------------------
311 // UTF-8
312 // ----------------------------------------------------------------------------
313
314 WXDLLIMPEXP_DATA_BASE(wxMBConvUTF8) wxConvUTF8;
315
316 static wxUint32 utf8_max[]=
317 { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff, 0xffffffff };
318
319 size_t wxMBConvUTF8::MB2WC(wchar_t *buf, const char *psz, size_t n) const
320 {
321 size_t len = 0;
322
323 while (*psz && ((!buf) || (len < n)))
324 {
325 unsigned char cc = *psz++, fc = cc;
326 unsigned cnt;
327 for (cnt = 0; fc & 0x80; cnt++)
328 fc <<= 1;
329 if (!cnt)
330 {
331 // plain ASCII char
332 if (buf)
333 *buf++ = cc;
334 len++;
335 }
336 else
337 {
338 cnt--;
339 if (!cnt)
340 {
341 // invalid UTF-8 sequence
342 return (size_t)-1;
343 }
344 else
345 {
346 unsigned ocnt = cnt - 1;
347 wxUint32 res = cc & (0x3f >> cnt);
348 while (cnt--)
349 {
350 cc = *psz++;
351 if ((cc & 0xC0) != 0x80)
352 {
353 // invalid UTF-8 sequence
354 return (size_t)-1;
355 }
356 res = (res << 6) | (cc & 0x3f);
357 }
358 if (res <= utf8_max[ocnt])
359 {
360 // illegal UTF-8 encoding
361 return (size_t)-1;
362 }
363 #ifdef WC_UTF16
364 size_t pa = encode_utf16(res, buf);
365 if (pa == (size_t)-1)
366 return (size_t)-1;
367 if (buf)
368 buf += pa;
369 len += pa;
370 #else // !WC_UTF16
371 if (buf)
372 *buf++ = res;
373 len++;
374 #endif // WC_UTF16/!WC_UTF16
375 }
376 }
377 }
378 if (buf && (len < n))
379 *buf = 0;
380 return len;
381 }
382
383 size_t wxMBConvUTF8::WC2MB(char *buf, const wchar_t *psz, size_t n) const
384 {
385 size_t len = 0;
386
387 while (*psz && ((!buf) || (len < n)))
388 {
389 wxUint32 cc;
390 #ifdef WC_UTF16
391 size_t pa = decode_utf16(psz, cc);
392 psz += (pa == (size_t)-1) ? 1 : pa;
393 #else
394 cc=(*psz++) & 0x7fffffff;
395 #endif
396 unsigned cnt;
397 for (cnt = 0; cc > utf8_max[cnt]; cnt++) {}
398 if (!cnt)
399 {
400 // plain ASCII char
401 if (buf)
402 *buf++ = (char) cc;
403 len++;
404 }
405
406 else
407 {
408 len += cnt + 1;
409 if (buf)
410 {
411 *buf++ = (char) ((-128 >> cnt) | ((cc >> (cnt * 6)) & (0x3f >> cnt)));
412 while (cnt--)
413 *buf++ = (char) (0x80 | ((cc >> (cnt * 6)) & 0x3f));
414 }
415 }
416 }
417
418 if (buf && (len<n)) *buf = 0;
419
420 return len;
421 }
422
423
424
425
426 // ----------------------------------------------------------------------------
427 // UTF-16
428 // ----------------------------------------------------------------------------
429
430 #ifdef WORDS_BIGENDIAN
431 #define wxMBConvUTF16straight wxMBConvUTF16BE
432 #define wxMBConvUTF16swap wxMBConvUTF16LE
433 #else
434 #define wxMBConvUTF16swap wxMBConvUTF16BE
435 #define wxMBConvUTF16straight wxMBConvUTF16LE
436 #endif
437
438
439 WXDLLIMPEXP_DATA_BASE(wxMBConvUTF16LE) wxConvUTF16LE;
440 WXDLLIMPEXP_DATA_BASE(wxMBConvUTF16BE) wxConvUTF16BE;
441
442
443
444
445
446 #ifdef WC_UTF16
447
448
449 // copy 16bit MB to 16bit String
450 size_t wxMBConvUTF16straight::MB2WC(wchar_t *buf, const char *psz, size_t n) const
451 {
452 size_t len=0;
453
454 while (*(wxUint16*)psz && (!buf || len < n))
455 {
456 if (buf)
457 *buf++ = *(wxUint16*)psz;
458 len++;
459
460 psz += sizeof(wxUint16);
461 }
462 if (buf && len<n) *buf=0;
463
464 return len;
465 }
466
467
468 // copy 16bit String to 16bit MB
469 size_t wxMBConvUTF16straight::WC2MB(char *buf, const wchar_t *psz, size_t n) const
470 {
471 size_t len=0;
472
473 while (*psz && (!buf || len < n))
474 {
475 if (buf)
476 {
477 *(wxUint16*)buf = *psz;
478 buf += sizeof(wxUint16);
479 }
480 len += sizeof(wxUint16);
481 psz++;
482 }
483 if (buf && len<=n-sizeof(wxUint16)) *(wxUint16*)buf=0;
484
485 return len;
486 }
487
488
489 // swap 16bit MB to 16bit String
490 size_t wxMBConvUTF16swap::MB2WC(wchar_t *buf, const char *psz, size_t n) const
491 {
492 size_t len=0;
493
494 while (*(wxUint16*)psz && (!buf || len < n))
495 {
496 if (buf)
497 {
498 ((char *)buf)[0] = psz[1];
499 ((char *)buf)[1] = psz[0];
500 buf++;
501 }
502 len++;
503 psz += sizeof(wxUint16);
504 }
505 if (buf && len<n) *buf=0;
506
507 return len;
508 }
509
510
511 // swap 16bit MB to 16bit String
512 size_t wxMBConvUTF16swap::WC2MB(char *buf, const wchar_t *psz, size_t n) const
513 {
514 size_t len=0;
515
516 while (*psz && (!buf || len < n))
517 {
518 if (buf)
519 {
520 *buf++ = ((char*)psz)[1];
521 *buf++ = ((char*)psz)[0];
522 }
523 len += sizeof(wxUint16);
524 psz++;
525 }
526 if (buf && len<=n-sizeof(wxUint16)) *(wxUint16*)buf=0;
527
528 return len;
529 }
530
531
532 #else // WC_UTF16
533
534
535 // copy 16bit MB to 32bit String
536 size_t wxMBConvUTF16straight::MB2WC(wchar_t *buf, const char *psz, size_t n) const
537 {
538 size_t len=0;
539
540 while (*(wxUint16*)psz && (!buf || len < n))
541 {
542 wxUint32 cc;
543 size_t pa=decode_utf16((wxUint16*)psz, cc);
544 if (pa == (size_t)-1)
545 return pa;
546
547 if (buf)
548 *buf++ = cc;
549 len++;
550 psz += pa * sizeof(wxUint16);
551 }
552 if (buf && len<n) *buf=0;
553
554 return len;
555 }
556
557
558 // copy 32bit String to 16bit MB
559 size_t wxMBConvUTF16straight::WC2MB(char *buf, const wchar_t *psz, size_t n) const
560 {
561 size_t len=0;
562
563 while (*psz && (!buf || len < n))
564 {
565 wxUint16 cc[2];
566 size_t pa=encode_utf16(*psz, cc);
567
568 if (pa == (size_t)-1)
569 return pa;
570
571 if (buf)
572 {
573 *(wxUint16*)buf = cc[0];
574 buf += sizeof(wxUint16);
575 if (pa > 1)
576 {
577 *(wxUint16*)buf = cc[1];
578 buf += sizeof(wxUint16);
579 }
580 }
581
582 len += pa*sizeof(wxUint16);
583 psz++;
584 }
585 if (buf && len<=n-sizeof(wxUint16)) *(wxUint16*)buf=0;
586
587 return len;
588 }
589
590
591 // swap 16bit MB to 32bit String
592 size_t wxMBConvUTF16swap::MB2WC(wchar_t *buf, const char *psz, size_t n) const
593 {
594 size_t len=0;
595
596 while (*(wxUint16*)psz && (!buf || len < n))
597 {
598 wxUint32 cc;
599 char tmp[4];
600 tmp[0]=psz[1]; tmp[1]=psz[0];
601 tmp[2]=psz[3]; tmp[3]=psz[2];
602
603 size_t pa=decode_utf16((wxUint16*)tmp, cc);
604 if (pa == (size_t)-1)
605 return pa;
606
607 if (buf)
608 *buf++ = cc;
609
610 len++;
611 psz += pa * sizeof(wxUint16);
612 }
613 if (buf && len<n) *buf=0;
614
615 return len;
616 }
617
618
619 // swap 32bit String to 16bit MB
620 size_t wxMBConvUTF16swap::WC2MB(char *buf, const wchar_t *psz, size_t n) const
621 {
622 size_t len=0;
623
624 while (*psz && (!buf || len < n))
625 {
626 wxUint16 cc[2];
627 size_t pa=encode_utf16(*psz, cc);
628
629 if (pa == (size_t)-1)
630 return pa;
631
632 if (buf)
633 {
634 *buf++ = ((char*)cc)[1];
635 *buf++ = ((char*)cc)[0];
636 if (pa > 1)
637 {
638 *buf++ = ((char*)cc)[3];
639 *buf++ = ((char*)cc)[2];
640 }
641 }
642
643 len += pa*sizeof(wxUint16);
644 psz++;
645 }
646 if (buf && len<=n-sizeof(wxUint16)) *(wxUint16*)buf=0;
647
648 return len;
649 }
650
651 #endif // WC_UTF16
652
653
654 // ----------------------------------------------------------------------------
655 // UTF-32
656 // ----------------------------------------------------------------------------
657
658 #ifdef WORDS_BIGENDIAN
659 #define wxMBConvUTF32straight wxMBConvUTF32BE
660 #define wxMBConvUTF32swap wxMBConvUTF32LE
661 #else
662 #define wxMBConvUTF32swap wxMBConvUTF32BE
663 #define wxMBConvUTF32straight wxMBConvUTF32LE
664 #endif
665
666
667 WXDLLIMPEXP_DATA_BASE(wxMBConvUTF32LE) wxConvUTF32LE;
668 WXDLLIMPEXP_DATA_BASE(wxMBConvUTF32BE) wxConvUTF32BE;
669
670
671 #ifdef WC_UTF16
672
673 // copy 32bit MB to 16bit String
674 size_t wxMBConvUTF32straight::MB2WC(wchar_t *buf, const char *psz, size_t n) const
675 {
676 size_t len=0;
677
678 while (*(wxUint32*)psz && (!buf || len < n))
679 {
680 wxUint16 cc[2];
681
682 size_t pa=encode_utf16(*(wxUint32*)psz, cc);
683 if (pa == (size_t)-1)
684 return pa;
685
686 if (buf)
687 {
688 *buf++ = cc[0];
689 if (pa > 1)
690 *buf++ = cc[1];
691 }
692 len += pa;
693 psz += sizeof(wxUint32);
694 }
695 if (buf && len<n) *buf=0;
696
697 return len;
698 }
699
700
701 // copy 16bit String to 32bit MB
702 size_t wxMBConvUTF32straight::WC2MB(char *buf, const wchar_t *psz, size_t n) const
703 {
704 size_t len=0;
705
706 while (*psz && (!buf || len < n))
707 {
708 wxUint32 cc;
709
710 size_t pa=decode_utf16(psz, cc);
711 if (pa == (size_t)-1)
712 return pa;
713
714 if (buf)
715 {
716 *(wxUint32*)buf = cc;
717 buf += sizeof(wxUint32);
718 }
719 len += sizeof(wxUint32);
720 psz += pa;
721 }
722 if (buf && len<=n-sizeof(wxUint32)) *(wxUint32*)buf=0;
723
724 return len;
725 }
726
727
728
729 // swap 32bit MB to 16bit String
730 size_t wxMBConvUTF32swap::MB2WC(wchar_t *buf, const char *psz, size_t n) const
731 {
732 size_t len=0;
733
734 while (*(wxUint32*)psz && (!buf || len < n))
735 {
736 char tmp[4];
737 tmp[0] = psz[3]; tmp[1] = psz[2];
738 tmp[2] = psz[1]; tmp[3] = psz[0];
739
740
741 wxUint16 cc[2];
742
743 size_t pa=encode_utf16(*(wxUint32*)tmp, cc);
744 if (pa == (size_t)-1)
745 return pa;
746
747 if (buf)
748 {
749 *buf++ = cc[0];
750 if (pa > 1)
751 *buf++ = cc[1];
752 }
753 len += pa;
754 psz += sizeof(wxUint32);
755 }
756 if (buf && len<n) *buf=0;
757
758 return len;
759 }
760
761
762 // swap 16bit String to 32bit MB
763 size_t wxMBConvUTF32swap::WC2MB(char *buf, const wchar_t *psz, size_t n) const
764 {
765 size_t len=0;
766
767 while (*psz && (!buf || len < n))
768 {
769 char cc[4];
770
771 size_t pa=decode_utf16(psz, *(wxUint32*)cc);
772 if (pa == (size_t)-1)
773 return pa;
774
775 if (buf)
776 {
777 *buf++ = cc[3];
778 *buf++ = cc[2];
779 *buf++ = cc[1];
780 *buf++ = cc[0];
781 }
782 len += sizeof(wxUint32);
783 psz += pa;
784 }
785 if (buf && len<=n-sizeof(wxUint32)) *(wxUint32*)buf=0;
786
787 return len;
788 }
789
790 #else // WC_UTF16
791
792
793 // copy 32bit MB to 32bit String
794 size_t wxMBConvUTF32straight::MB2WC(wchar_t *buf, const char *psz, size_t n) const
795 {
796 size_t len=0;
797
798 while (*(wxUint32*)psz && (!buf || len < n))
799 {
800 if (buf)
801 *buf++ = *(wxUint32*)psz;
802 len++;
803 psz += sizeof(wxUint32);
804 }
805 if (buf && len<n) *buf=0;
806
807 return len;
808 }
809
810
811 // copy 32bit String to 32bit MB
812 size_t wxMBConvUTF32straight::WC2MB(char *buf, const wchar_t *psz, size_t n) const
813 {
814 size_t len=0;
815
816 while (*psz && (!buf || len < n))
817 {
818 if (buf)
819 {
820 *(wxUint32*)buf = *psz;
821 buf += sizeof(wxUint32);
822 }
823
824 len += sizeof(wxUint32);
825 psz++;
826 }
827
828 if (buf && len<=n-sizeof(wxUint32)) *(wxUint32*)buf=0;
829
830 return len;
831 }
832
833
834 // swap 32bit MB to 32bit String
835 size_t wxMBConvUTF32swap::MB2WC(wchar_t *buf, const char *psz, size_t n) const
836 {
837 size_t len=0;
838
839 while (*(wxUint32*)psz && (!buf || len < n))
840 {
841 if (buf)
842 {
843 ((char *)buf)[0] = psz[3];
844 ((char *)buf)[1] = psz[2];
845 ((char *)buf)[2] = psz[1];
846 ((char *)buf)[3] = psz[0];
847 buf++;
848 }
849 len++;
850 psz += sizeof(wxUint32);
851 }
852 if (buf && len<n) *buf=0;
853
854 return len;
855 }
856
857
858 // swap 32bit String to 32bit MB
859 size_t wxMBConvUTF32swap::WC2MB(char *buf, const wchar_t *psz, size_t n) const
860 {
861 size_t len=0;
862
863 while (*psz && (!buf || len < n))
864 {
865 if (buf)
866 {
867 *buf++ = ((char *)psz)[3];
868 *buf++ = ((char *)psz)[2];
869 *buf++ = ((char *)psz)[1];
870 *buf++ = ((char *)psz)[0];
871 }
872 len += sizeof(wxUint32);
873 psz++;
874 }
875 if (buf && len<=n-sizeof(wxUint32)) *(wxUint32*)buf=0;
876
877 return len;
878 }
879
880
881 #endif // WC_UTF16
882
883
884 // ============================================================================
885 // wxCharacterSet and derived classes
886 // ============================================================================
887
888 // ----------------------------------------------------------------------------
889 // wxCharacterSet is the ABC for the classes below
890 // ----------------------------------------------------------------------------
891
892 class wxCharacterSet
893 {
894 public:
895 wxCharacterSet() { }
896 virtual ~wxCharacterSet() {}
897
898 virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) = 0;
899 virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) = 0;
900 virtual bool usable() const = 0;
901 };
902
903 // ----------------------------------------------------------------------------
904 // ID_CharSet: implementation of wxCharacterSet using an existing wxMBConv
905 // ----------------------------------------------------------------------------
906
907 class ID_CharSet : public wxCharacterSet
908 {
909 public:
910 ID_CharSet(wxMBConv *cnv) : work(cnv) {}
911
912 size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
913 { return work ? work->MB2WC(buf,psz,n) : (size_t)-1; }
914
915 size_t WC2MB(char *buf, const wchar_t *psz, size_t n)
916 { return work ? work->WC2MB(buf,psz,n) : (size_t)-1; }
917
918 bool usable() const
919 { return work!=NULL; }
920 public:
921 wxMBConv*work;
922 };
923
924
925 // ============================================================================
926 // The classes doing conversion using the iconv_xxx() functions
927 // ============================================================================
928
929 #ifdef HAVE_ICONV
930
931 // VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with E2BIG
932 // if output buffer is _exactly_ as big as needed. Such case is (unless there's
933 // yet another bug in glibc) the only case when iconv() returns with (size_t)-1
934 // (which means error) and says there are 0 bytes left in the input buffer --
935 // when _real_ error occurs, bytes-left-in-input buffer is non-zero. Hence,
936 // this alternative test for iconv() failure.
937 // [This bug does not appear in glibc 2.2.]
938 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 1
939 #define ICONV_FAILED(cres, bufLeft) ((cres == (size_t)-1) && \
940 (errno != E2BIG || bufLeft != 0))
941 #else
942 #define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1)
943 #endif
944
945 #define ICONV_CHAR_CAST(x) ((ICONV_CONST char **)(x))
946
947 // ----------------------------------------------------------------------------
948 // IC_CharSet: encapsulates an iconv character set
949 // ----------------------------------------------------------------------------
950
951 class IC_CharSet : public wxCharacterSet
952 {
953 public:
954 IC_CharSet(const wxChar *name);
955 virtual ~IC_CharSet();
956
957 virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n);
958 virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n);
959
960 bool usable() const
961 { return (m2w != (iconv_t)-1) && (w2m != (iconv_t)-1); }
962
963 protected:
964 // the iconv handlers used to translate from multibyte to wide char and in
965 // the other direction
966 iconv_t m2w,
967 w2m;
968
969 private:
970 // the name (for iconv_open()) of a wide char charset - if none is
971 // available on this machine, it will remain NULL
972 static const char *ms_wcCharsetName;
973
974 // true if the wide char encoding we use (i.e. ms_wcCharsetName) has
975 // different endian-ness than the native one
976 static bool ms_wcNeedsSwap;
977 };
978
979 const char *IC_CharSet::ms_wcCharsetName = NULL;
980 bool IC_CharSet::ms_wcNeedsSwap = FALSE;
981
982 IC_CharSet::IC_CharSet(const wxChar *name)
983 {
984 // Do it the hard way
985 char cname[100];
986 for (size_t i = 0; i < wxStrlen(name)+1; i++)
987 cname[i] = (char) name[i];
988
989 // check for charset that represents wchar_t:
990 if (ms_wcCharsetName == NULL)
991 {
992 ms_wcNeedsSwap = FALSE;
993
994 // try charset with explicit bytesex info (e.g. "UCS-4LE"):
995 ms_wcCharsetName = WC_NAME_BEST;
996 m2w = iconv_open(ms_wcCharsetName, cname);
997
998 if (m2w == (iconv_t)-1)
999 {
1000 // try charset w/o bytesex info (e.g. "UCS4")
1001 // and check for bytesex ourselves:
1002 ms_wcCharsetName = WC_NAME;
1003 m2w = iconv_open(ms_wcCharsetName, cname);
1004
1005 // last bet, try if it knows WCHAR_T pseudo-charset
1006 if (m2w == (iconv_t)-1)
1007 {
1008 ms_wcCharsetName = "WCHAR_T";
1009 m2w = iconv_open(ms_wcCharsetName, cname);
1010 }
1011
1012 if (m2w != (iconv_t)-1)
1013 {
1014 char buf[2], *bufPtr;
1015 wchar_t wbuf[2], *wbufPtr;
1016 size_t insz, outsz;
1017 size_t res;
1018
1019 buf[0] = 'A';
1020 buf[1] = 0;
1021 wbuf[0] = 0;
1022 insz = 2;
1023 outsz = SIZEOF_WCHAR_T * 2;
1024 wbufPtr = wbuf;
1025 bufPtr = buf;
1026
1027 res = iconv(m2w, ICONV_CHAR_CAST(&bufPtr), &insz,
1028 (char**)&wbufPtr, &outsz);
1029
1030 if (ICONV_FAILED(res, insz))
1031 {
1032 ms_wcCharsetName = NULL;
1033 wxLogLastError(wxT("iconv"));
1034 wxLogError(_("Conversion to charset '%s' doesn't work."), name);
1035 }
1036 else
1037 {
1038 ms_wcNeedsSwap = wbuf[0] != (wchar_t)buf[0];
1039 }
1040 }
1041 else
1042 {
1043 ms_wcCharsetName = NULL;
1044
1045 // VS: we must not output an error here, since wxWindows will safely
1046 // fall back to using wxEncodingConverter.
1047 wxLogTrace(wxT("strconv"), wxT("Impossible to convert to/from charset '%s' with iconv, falling back to wxEncodingConverter."), name);
1048 //wxLogError(
1049 }
1050 }
1051 wxLogTrace(wxT("strconv"), wxT("wchar_t charset is '%s', needs swap: %i"), ms_wcCharsetName, ms_wcNeedsSwap);
1052 }
1053 else // we already have ms_wcCharsetName
1054 {
1055 m2w = iconv_open(ms_wcCharsetName, cname);
1056 }
1057
1058 // NB: don't ever pass NULL to iconv_open(), it may crash!
1059 if ( ms_wcCharsetName )
1060 {
1061 w2m = iconv_open( cname, ms_wcCharsetName);
1062 }
1063 else
1064 {
1065 w2m = (iconv_t)-1;
1066 }
1067 }
1068
1069 IC_CharSet::~IC_CharSet()
1070 {
1071 if ( m2w != (iconv_t)-1 )
1072 iconv_close(m2w);
1073 if ( w2m != (iconv_t)-1 )
1074 iconv_close(w2m);
1075 }
1076
1077 size_t IC_CharSet::MB2WC(wchar_t *buf, const char *psz, size_t n)
1078 {
1079 size_t inbuf = strlen(psz);
1080 size_t outbuf = n * SIZEOF_WCHAR_T;
1081 size_t res, cres;
1082 // VS: Use these instead of psz, buf because iconv() modifies its arguments:
1083 wchar_t *bufPtr = buf;
1084 const char *pszPtr = psz;
1085
1086 if (buf)
1087 {
1088 // have destination buffer, convert there
1089 cres = iconv(m2w,
1090 ICONV_CHAR_CAST(&pszPtr), &inbuf,
1091 (char**)&bufPtr, &outbuf);
1092 res = n - (outbuf / SIZEOF_WCHAR_T);
1093
1094 if (ms_wcNeedsSwap)
1095 {
1096 // convert to native endianness
1097 WC_BSWAP(buf /* _not_ bufPtr */, res)
1098 }
1099
1100 // NB: iconv was given only strlen(psz) characters on input, and so
1101 // it couldn't convert the trailing zero. Let's do it ourselves
1102 // if there's some room left for it in the output buffer.
1103 if (res < n)
1104 buf[res] = 0;
1105 }
1106 else
1107 {
1108 // no destination buffer... convert using temp buffer
1109 // to calculate destination buffer requirement
1110 wchar_t tbuf[8];
1111 res = 0;
1112 do {
1113 bufPtr = tbuf;
1114 outbuf = 8*SIZEOF_WCHAR_T;
1115
1116 cres = iconv(m2w,
1117 ICONV_CHAR_CAST(&pszPtr), &inbuf,
1118 (char**)&bufPtr, &outbuf );
1119
1120 res += 8-(outbuf/SIZEOF_WCHAR_T);
1121 } while ((cres==(size_t)-1) && (errno==E2BIG));
1122 }
1123
1124 if (ICONV_FAILED(cres, inbuf))
1125 {
1126 //VS: it is ok if iconv fails, hence trace only
1127 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
1128 return (size_t)-1;
1129 }
1130
1131 return res;
1132 }
1133
1134 size_t IC_CharSet::WC2MB(char *buf, const wchar_t *psz, size_t n)
1135 {
1136 size_t inbuf = wxWcslen(psz) * SIZEOF_WCHAR_T;
1137 size_t outbuf = n;
1138 size_t res, cres;
1139
1140 wchar_t *tmpbuf = 0;
1141
1142 if (ms_wcNeedsSwap)
1143 {
1144 // need to copy to temp buffer to switch endianness
1145 // this absolutely doesn't rock!
1146 // (no, doing WC_BSWAP twice on the original buffer won't help, as it
1147 // could be in read-only memory, or be accessed in some other thread)
1148 tmpbuf=(wchar_t*)malloc((inbuf+1)*SIZEOF_WCHAR_T);
1149 memcpy(tmpbuf,psz,(inbuf+1)*SIZEOF_WCHAR_T);
1150 WC_BSWAP(tmpbuf, inbuf)
1151 psz=tmpbuf;
1152 }
1153
1154 if (buf)
1155 {
1156 // have destination buffer, convert there
1157 cres = iconv( w2m, ICONV_CHAR_CAST(&psz), &inbuf, &buf, &outbuf );
1158
1159 res = n-outbuf;
1160
1161 // NB: iconv was given only wcslen(psz) characters on input, and so
1162 // it couldn't convert the trailing zero. Let's do it ourselves
1163 // if there's some room left for it in the output buffer.
1164 if (res < n)
1165 buf[0] = 0;
1166 }
1167 else
1168 {
1169 // no destination buffer... convert using temp buffer
1170 // to calculate destination buffer requirement
1171 char tbuf[16];
1172 res = 0;
1173 do {
1174 buf = tbuf; outbuf = 16;
1175
1176 cres = iconv( w2m, ICONV_CHAR_CAST(&psz), &inbuf, &buf, &outbuf );
1177
1178 res += 16 - outbuf;
1179 } while ((cres==(size_t)-1) && (errno==E2BIG));
1180 }
1181
1182 if (ms_wcNeedsSwap)
1183 {
1184 free(tmpbuf);
1185 }
1186
1187 if (ICONV_FAILED(cres, inbuf))
1188 {
1189 //VS: it is ok if iconv fails, hence trace only
1190 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
1191 return (size_t)-1;
1192 }
1193
1194 return res;
1195 }
1196
1197 #endif // HAVE_ICONV
1198
1199 // ============================================================================
1200 // Win32 conversion classes
1201 // ============================================================================
1202
1203 #if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
1204
1205 // from utils.cpp
1206 extern WXDLLIMPEXP_BASE long wxCharsetToCodepage(const wxChar *charset);
1207 extern WXDLLIMPEXP_BASE long wxEncodingToCodepage(wxFontEncoding encoding);
1208
1209 class CP_CharSet : public wxCharacterSet
1210 {
1211 public:
1212 CP_CharSet(const wxChar* name)
1213 {
1214 m_CodePage = wxCharsetToCodepage(name);
1215 }
1216
1217 CP_CharSet(wxFontEncoding encoding)
1218 {
1219 m_CodePage = wxEncodingToCodepage(encoding);
1220 }
1221
1222 size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
1223 {
1224 const size_t len = ::MultiByteToWideChar
1225 (
1226 m_CodePage, // code page
1227 0, // flags (none)
1228 psz, // input string
1229 -1, // its length (NUL-terminated)
1230 buf, // output string
1231 buf ? n : 0 // size of output buffer
1232 );
1233
1234 // note that it returns # of written chars for buf != NULL and *size*
1235 // of the needed buffer for buf == NULL
1236 return len ? (buf ? len : len - 1) : (size_t)-1;
1237 }
1238
1239 size_t WC2MB(char *buf, const wchar_t *psz, size_t n)
1240 {
1241 const size_t len = ::WideCharToMultiByte
1242 (
1243 m_CodePage, // code page
1244 0, // flags (none)
1245 psz, // input string
1246 -1, // it is (wide) NUL-terminated
1247 buf, // output buffer
1248 buf ? n : 0, // and its size
1249 NULL, // default "replacement" char
1250 NULL // [out] was it used?
1251 );
1252
1253 // see the comment above!
1254 return len ? (buf ? len : len - 1) : (size_t)-1;
1255 }
1256
1257 bool usable() const
1258 { return m_CodePage != -1; }
1259
1260 public:
1261 long m_CodePage;
1262 };
1263 #endif // defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
1264
1265 // ============================================================================
1266 // wxEncodingConverter based conversion classes
1267 // ============================================================================
1268
1269 #if wxUSE_FONTMAP
1270
1271 class EC_CharSet : public wxCharacterSet
1272 {
1273 private:
1274 void Init()
1275 {
1276 m_ok = m2w.Init(m_enc, wxFONTENCODING_UNICODE) &&
1277 w2m.Init(wxFONTENCODING_UNICODE, m_enc);
1278 }
1279
1280 public:
1281 // temporarily just use wxEncodingConverter stuff,
1282 // so that it works while a better implementation is built
1283 EC_CharSet(const wxChar* name)
1284 {
1285 if (name)
1286 m_enc = wxFontMapper::Get()->CharsetToEncoding(name, FALSE);
1287 else
1288 m_enc = wxFONTENCODING_SYSTEM;
1289
1290 Init();
1291 }
1292
1293 EC_CharSet(wxFontEncoding enc)
1294 {
1295 m_enc = enc;
1296
1297 Init();
1298 }
1299
1300 size_t MB2WC(wchar_t *buf, const char *psz, size_t WXUNUSED(n))
1301 {
1302 size_t inbuf = strlen(psz);
1303 if (buf)
1304 m2w.Convert(psz,buf);
1305 return inbuf;
1306 }
1307
1308 size_t WC2MB(char *buf, const wchar_t *psz, size_t WXUNUSED(n))
1309 {
1310 const size_t inbuf = wxWcslen(psz);
1311 if (buf)
1312 w2m.Convert(psz,buf);
1313
1314 return inbuf;
1315 }
1316
1317 bool usable() const { return m_ok; }
1318
1319 public:
1320 wxFontEncoding m_enc;
1321 wxEncodingConverter m2w, w2m;
1322
1323 // were we initialized successfully?
1324 bool m_ok;
1325
1326 DECLARE_NO_COPY_CLASS(EC_CharSet)
1327 };
1328
1329 #endif // wxUSE_FONTMAP
1330
1331 // ----------------------------------------------------------------------------
1332 // the function creating the wxCharacterSet for the specified charset on the
1333 // current system, trying all possibilities
1334 //
1335 // it uses the name if it is given or encoding if name == NULL
1336 // ----------------------------------------------------------------------------
1337
1338 static wxCharacterSet *
1339 wxGetCharacterSet(const wxChar *name, wxFontEncoding encoding)
1340 {
1341 // check for the special case of ASCII charset
1342 if ( (!name && encoding == wxFONTENCODING_DEFAULT)
1343 #if wxUSE_FONTMAP
1344 || (name && wxFontMapper::Get()->
1345 CharsetToEncoding(name) == wxFONTENCODING_DEFAULT)
1346 #endif // wxUSE_FONTMAP
1347 )
1348 {
1349 // don't convert at all
1350 return NULL;
1351 }
1352
1353 wxCharacterSet *cset = NULL;
1354
1355 if (name)
1356 {
1357 if((wxStricmp(name, wxT("UTF8")) == 0) ||
1358 (wxStricmp(name, wxT("UTF-8")) == 0) ||
1359 encoding == wxFONTENCODING_UTF8 )
1360 {
1361 cset = new ID_CharSet(&wxConvUTF8);
1362 }
1363 else if((wxStricmp(name, wxT("UTF16")) == 0) ||
1364 (wxStricmp(name, wxT("UTF-16")) == 0) ||
1365 encoding == wxFONTENCODING_UTF16 )
1366 {
1367 #ifdef WORDS_BIGENDIAN
1368 cset = new ID_CharSet(&wxConvUTF16BE);
1369 #else
1370 cset = new ID_CharSet(&wxConvUTF16LE);
1371 #endif
1372 }
1373 else if((wxStricmp(name, wxT("UTF16BE")) == 0) ||
1374 (wxStricmp(name, wxT("UTF-16BE")) == 0) ||
1375 encoding == wxFONTENCODING_UTF16BE )
1376 {
1377 cset = new ID_CharSet(&wxConvUTF16BE);
1378 }
1379 else if((wxStricmp(name, wxT("UTF16LE")) == 0) ||
1380 (wxStricmp(name, wxT("UTF-16LE")) == 0) ||
1381 encoding == wxFONTENCODING_UTF16LE )
1382 {
1383 cset = new ID_CharSet(&wxConvUTF16LE);
1384 }
1385 else if((wxStricmp(name, wxT("UTF32")) == 0) ||
1386 (wxStricmp(name, wxT("UTF-32")) == 0) ||
1387 (wxStricmp(name, wxT("UCS4")) == 0) ||
1388 (wxStricmp(name, wxT("UCS-4")) == 0) ||
1389 encoding == wxFONTENCODING_UTF32 )
1390 {
1391 #ifdef WORDS_BIGENDIAN
1392 cset = new ID_CharSet(&wxConvUTF32BE);
1393 #else
1394 cset = new ID_CharSet(&wxConvUTF32LE);
1395 #endif
1396 }
1397 else if((wxStricmp(name, wxT("UTF32BE")) == 0) ||
1398 (wxStricmp(name, wxT("UTF-32BE")) == 0) ||
1399 (wxStricmp(name, wxT("UCS4BE")) == 0) ||
1400 (wxStricmp(name, wxT("UCS-4BE")) == 0) ||
1401 encoding == wxFONTENCODING_UTF32BE )
1402 {
1403 cset = new ID_CharSet(&wxConvUTF32BE);
1404 }
1405 else if((wxStricmp(name, wxT("UTF32LE")) == 0) ||
1406 (wxStricmp(name, wxT("UTF-32LE")) == 0) ||
1407 (wxStricmp(name, wxT("UCS4LE")) == 0) ||
1408 (wxStricmp(name, wxT("UCS-4LE")) == 0) ||
1409 encoding == wxFONTENCODING_UTF32 )
1410 {
1411 cset = new ID_CharSet(&wxConvUTF32LE);
1412 }
1413 #ifdef HAVE_ICONV
1414 else
1415 {
1416 cset = new IC_CharSet(name);
1417 }
1418 #endif // HAVE_ICONV
1419 }
1420
1421 // it can only be NULL in this case
1422 #ifndef HAVE_ICONV
1423 if ( cset )
1424 #endif // !HAVE_ICONV
1425 {
1426 if ( cset->usable() )
1427 return cset;
1428
1429 delete cset;
1430 cset = NULL;
1431 }
1432
1433 #if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
1434 cset = name ? new CP_CharSet(name) : new CP_CharSet(encoding);
1435 if ( cset->usable() )
1436 return cset;
1437
1438 delete cset;
1439 cset = NULL;
1440 #endif // defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
1441
1442 #if wxUSE_FONTMAP
1443 cset = name ? new EC_CharSet(name) : new EC_CharSet(encoding);
1444 if ( cset->usable() )
1445 return cset;
1446
1447 delete cset;
1448 cset = NULL;
1449 #endif // wxUSE_FONTMAP
1450
1451 wxLogError(_("Cannot convert from encoding '%s'!"),
1452 name ? name
1453 :
1454 #if wxUSE_FONTMAP
1455 wxFontMapper::GetEncodingDescription(encoding).c_str()
1456 #else // !wxUSE_FONTMAP
1457 wxString::Format(_T("%s"), encoding).c_str()
1458 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
1459 );
1460
1461 return NULL;
1462 }
1463
1464 // ============================================================================
1465 // wxCSConv implementation
1466 // ============================================================================
1467
1468 void wxCSConv::Init()
1469 {
1470 m_name = (wxChar *)NULL;
1471 m_cset = (wxCharacterSet *) NULL;
1472 m_deferred = TRUE;
1473 }
1474
1475 wxCSConv::wxCSConv(const wxChar *charset)
1476 {
1477 Init();
1478 m_encoding = wxFONTENCODING_DEFAULT;
1479
1480 SetName(charset);
1481 }
1482
1483 wxCSConv::wxCSConv(wxFontEncoding encoding)
1484 {
1485 Init();
1486
1487 m_encoding = encoding;
1488 }
1489
1490 wxCSConv::~wxCSConv()
1491 {
1492 Clear();
1493 }
1494
1495 wxCSConv::wxCSConv(const wxCSConv& conv)
1496 : wxMBConv()
1497 {
1498 Init();
1499
1500 SetName(conv.m_name);
1501 m_encoding = conv.m_encoding;
1502 }
1503
1504 wxCSConv& wxCSConv::operator=(const wxCSConv& conv)
1505 {
1506 Clear();
1507
1508 SetName(conv.m_name);
1509 m_encoding = conv.m_encoding;
1510
1511 return *this;
1512 }
1513
1514 void wxCSConv::Clear()
1515 {
1516 free(m_name);
1517 delete m_cset;
1518
1519 m_name = NULL;
1520 m_cset = NULL;
1521 }
1522
1523 void wxCSConv::SetName(const wxChar *charset)
1524 {
1525 if (charset)
1526 {
1527 m_name = wxStrdup(charset);
1528 m_deferred = TRUE;
1529 }
1530 }
1531
1532 void wxCSConv::LoadNow()
1533 {
1534 if ( m_deferred )
1535 {
1536 // it would probably be better to make GetSystemEncodingName() always
1537 // available (i.e. even when wxUSE_INTL == 0)?
1538 #if wxUSE_INTL
1539 if ( !m_name && m_encoding == wxFONTENCODING_DEFAULT )
1540 {
1541 wxString name = wxLocale::GetSystemEncodingName();
1542 if ( !name.empty() )
1543 {
1544 SetName(name);
1545 }
1546 }
1547 #endif // wxUSE_INTL
1548
1549 // wxGetCharacterSet() complains about NULL name
1550 m_cset = wxGetCharacterSet(m_name, m_encoding);
1551 m_deferred = FALSE;
1552 }
1553 }
1554
1555 size_t wxCSConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const
1556 {
1557 ((wxCSConv *)this)->LoadNow(); // discard constness
1558
1559 if (m_cset)
1560 return m_cset->MB2WC(buf, psz, n);
1561
1562 // latin-1 (direct)
1563 size_t len = strlen(psz);
1564
1565 if (buf)
1566 {
1567 for (size_t c = 0; c <= len; c++)
1568 buf[c] = (unsigned char)(psz[c]);
1569 }
1570
1571 return len;
1572 }
1573
1574 size_t wxCSConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
1575 {
1576 ((wxCSConv *)this)->LoadNow(); // discard constness
1577
1578 if (m_cset)
1579 return m_cset->WC2MB(buf, psz, n);
1580
1581 // latin-1 (direct)
1582 const size_t len = wxWcslen(psz);
1583 if (buf)
1584 {
1585 for (size_t c = 0; c <= len; c++)
1586 buf[c] = (psz[c] > 0xff) ? '?' : psz[c];
1587 }
1588
1589 return len;
1590 }
1591
1592 #endif // wxUSE_WCHAR_T
1593
1594