1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Unicode conversion classes
4 // Author: Ove Kaaven, Robert Roebling, Vadim Zeitlin, Vaclav Slavik
8 // Copyright: (c) 1999 Ove Kaaven, Robert Roebling, Vaclav Slavik
9 // (c) 2000-2003 Vadim Zeitlin
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
22 #pragma implementation "strconv.h"
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
37 #include "wx/strconv.h"
42 #include "wx/msw/private.h"
53 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
54 #define wxHAVE_WIN32_MB2WC
55 #endif // __WIN32__ but !__WXMICROWIN__
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
69 #include "wx/encconv.h"
70 #include "wx/fontmap.h"
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 #define BSWAP_UCS4(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT32_SWAP_ALWAYS(str[_c]); }
77 #define BSWAP_UTF16(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT16_SWAP_ALWAYS(str[_c]); }
79 #if SIZEOF_WCHAR_T == 4
80 #define WC_NAME "UCS4"
81 #define WC_BSWAP BSWAP_UCS4
82 #ifdef WORDS_BIGENDIAN
83 #define WC_NAME_BEST "UCS-4BE"
85 #define WC_NAME_BEST "UCS-4LE"
87 #elif SIZEOF_WCHAR_T == 2
88 #define WC_NAME "UTF16"
89 #define WC_BSWAP BSWAP_UTF16
91 #ifdef WORDS_BIGENDIAN
92 #define WC_NAME_BEST "UTF-16BE"
94 #define WC_NAME_BEST "UTF-16LE"
96 #else // sizeof(wchar_t) != 2 nor 4
97 // does this ever happen?
98 #error "Unknown sizeof(wchar_t): please report this to wx-dev@lists.wxwindows.org"
101 // ============================================================================
103 // ============================================================================
105 // ----------------------------------------------------------------------------
106 // UTF-16 en/decoding to/from UCS-4
107 // ----------------------------------------------------------------------------
110 static size_t encode_utf16(wxUint32 input
, wxUint16
*output
)
114 if (output
) *output
++ = (wxUint16
) input
;
117 else if (input
>=0x110000)
125 *output
++ = (wxUint16
) ((input
>> 10)+0xd7c0);
126 *output
++ = (wxUint16
) ((input
&0x3ff)+0xdc00);
132 static size_t decode_utf16(const wxUint16
* input
, wxUint32
& output
)
134 if ((*input
<0xd800) || (*input
>0xdfff))
139 else if ((input
[1]<0xdc00) || (input
[1]>=0xdfff))
146 output
= ((input
[0] - 0xd7c0) << 10) + (input
[1] - 0xdc00);
152 // ----------------------------------------------------------------------------
154 // ----------------------------------------------------------------------------
156 wxMBConv::~wxMBConv()
158 // nothing to do here
161 const wxWCharBuffer
wxMBConv::cMB2WC(const char *psz
) const
165 // calculate the length of the buffer needed first
166 size_t nLen
= MB2WC(NULL
, psz
, 0);
167 if ( nLen
!= (size_t)-1 )
169 // now do the actual conversion
170 wxWCharBuffer
buf(nLen
);
171 MB2WC(buf
.data(), psz
, nLen
+ 1); // with the trailing NUL
177 wxWCharBuffer
buf((wchar_t *)NULL
);
182 const wxCharBuffer
wxMBConv::cWC2MB(const wchar_t *pwz
) const
186 size_t nLen
= WC2MB(NULL
, pwz
, 0);
187 if ( nLen
!= (size_t)-1 )
189 wxCharBuffer
buf(nLen
+3); // space for a wxUint32 trailing zero
190 WC2MB(buf
.data(), pwz
, nLen
+ 4);
196 wxCharBuffer
buf((char *)NULL
);
201 // ----------------------------------------------------------------------------
203 // ----------------------------------------------------------------------------
205 size_t wxMBConvLibc::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
207 return wxMB2WC(buf
, psz
, n
);
210 size_t wxMBConvLibc::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
212 return wxWC2MB(buf
, psz
, n
);
215 // ----------------------------------------------------------------------------
217 // ----------------------------------------------------------------------------
220 static char utf7_setD
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
221 "abcdefghijklmnopqrstuvwxyz"
222 "0123456789'(),-./:?";
223 static char utf7_setO
[]="!\"#$%&*;<=>@[]^_`{|}";
224 static char utf7_setB
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
225 "abcdefghijklmnopqrstuvwxyz"
229 // TODO: write actual implementations of UTF-7 here
230 size_t wxMBConvUTF7::MB2WC(wchar_t * WXUNUSED(buf
),
231 const char * WXUNUSED(psz
),
232 size_t WXUNUSED(n
)) const
237 size_t wxMBConvUTF7::WC2MB(char * WXUNUSED(buf
),
238 const wchar_t * WXUNUSED(psz
),
239 size_t WXUNUSED(n
)) const
244 // ----------------------------------------------------------------------------
246 // ----------------------------------------------------------------------------
248 static wxUint32 utf8_max
[]=
249 { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff, 0xffffffff };
251 size_t wxMBConvUTF8::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
255 while (*psz
&& ((!buf
) || (len
< n
)))
257 unsigned char cc
= *psz
++, fc
= cc
;
259 for (cnt
= 0; fc
& 0x80; cnt
++)
273 // invalid UTF-8 sequence
278 unsigned ocnt
= cnt
- 1;
279 wxUint32 res
= cc
& (0x3f >> cnt
);
283 if ((cc
& 0xC0) != 0x80)
285 // invalid UTF-8 sequence
288 res
= (res
<< 6) | (cc
& 0x3f);
290 if (res
<= utf8_max
[ocnt
])
292 // illegal UTF-8 encoding
296 // cast is ok because wchar_t == wxUuint16 if WC_UTF16
297 size_t pa
= encode_utf16(res
, (wxUint16
*)buf
);
298 if (pa
== (size_t)-1)
307 #endif // WC_UTF16/!WC_UTF16
311 if (buf
&& (len
< n
))
316 size_t wxMBConvUTF8::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
320 while (*psz
&& ((!buf
) || (len
< n
)))
324 // cast is ok for WC_UTF16
325 size_t pa
= decode_utf16((const wxUint16
*)psz
, cc
);
326 psz
+= (pa
== (size_t)-1) ? 1 : pa
;
328 cc
=(*psz
++) & 0x7fffffff;
331 for (cnt
= 0; cc
> utf8_max
[cnt
]; cnt
++) {}
345 *buf
++ = (char) ((-128 >> cnt
) | ((cc
>> (cnt
* 6)) & (0x3f >> cnt
)));
347 *buf
++ = (char) (0x80 | ((cc
>> (cnt
* 6)) & 0x3f));
352 if (buf
&& (len
<n
)) *buf
= 0;
360 // ----------------------------------------------------------------------------
362 // ----------------------------------------------------------------------------
364 #ifdef WORDS_BIGENDIAN
365 #define wxMBConvUTF16straight wxMBConvUTF16BE
366 #define wxMBConvUTF16swap wxMBConvUTF16LE
368 #define wxMBConvUTF16swap wxMBConvUTF16BE
369 #define wxMBConvUTF16straight wxMBConvUTF16LE
375 // copy 16bit MB to 16bit String
376 size_t wxMBConvUTF16straight::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
380 while (*(wxUint16
*)psz
&& (!buf
|| len
< n
))
383 *buf
++ = *(wxUint16
*)psz
;
386 psz
+= sizeof(wxUint16
);
388 if (buf
&& len
<n
) *buf
=0;
394 // copy 16bit String to 16bit MB
395 size_t wxMBConvUTF16straight::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
399 while (*psz
&& (!buf
|| len
< n
))
403 *(wxUint16
*)buf
= *psz
;
404 buf
+= sizeof(wxUint16
);
406 len
+= sizeof(wxUint16
);
409 if (buf
&& len
<=n
-sizeof(wxUint16
)) *(wxUint16
*)buf
=0;
415 // swap 16bit MB to 16bit String
416 size_t wxMBConvUTF16swap::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
420 while (*(wxUint16
*)psz
&& (!buf
|| len
< n
))
424 ((char *)buf
)[0] = psz
[1];
425 ((char *)buf
)[1] = psz
[0];
429 psz
+= sizeof(wxUint16
);
431 if (buf
&& len
<n
) *buf
=0;
437 // swap 16bit MB to 16bit String
438 size_t wxMBConvUTF16swap::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
442 while (*psz
&& (!buf
|| len
< n
))
446 *buf
++ = ((char*)psz
)[1];
447 *buf
++ = ((char*)psz
)[0];
449 len
+= sizeof(wxUint16
);
452 if (buf
&& len
<=n
-sizeof(wxUint16
)) *(wxUint16
*)buf
=0;
461 // copy 16bit MB to 32bit String
462 size_t wxMBConvUTF16straight::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
466 while (*(wxUint16
*)psz
&& (!buf
|| len
< n
))
469 size_t pa
=decode_utf16((wxUint16
*)psz
, cc
);
470 if (pa
== (size_t)-1)
476 psz
+= pa
* sizeof(wxUint16
);
478 if (buf
&& len
<n
) *buf
=0;
484 // copy 32bit String to 16bit MB
485 size_t wxMBConvUTF16straight::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
489 while (*psz
&& (!buf
|| len
< n
))
492 size_t pa
=encode_utf16(*psz
, cc
);
494 if (pa
== (size_t)-1)
499 *(wxUint16
*)buf
= cc
[0];
500 buf
+= sizeof(wxUint16
);
503 *(wxUint16
*)buf
= cc
[1];
504 buf
+= sizeof(wxUint16
);
508 len
+= pa
*sizeof(wxUint16
);
511 if (buf
&& len
<=n
-sizeof(wxUint16
)) *(wxUint16
*)buf
=0;
517 // swap 16bit MB to 32bit String
518 size_t wxMBConvUTF16swap::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
522 while (*(wxUint16
*)psz
&& (!buf
|| len
< n
))
526 tmp
[0]=psz
[1]; tmp
[1]=psz
[0];
527 tmp
[2]=psz
[3]; tmp
[3]=psz
[2];
529 size_t pa
=decode_utf16((wxUint16
*)tmp
, cc
);
530 if (pa
== (size_t)-1)
537 psz
+= pa
* sizeof(wxUint16
);
539 if (buf
&& len
<n
) *buf
=0;
545 // swap 32bit String to 16bit MB
546 size_t wxMBConvUTF16swap::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
550 while (*psz
&& (!buf
|| len
< n
))
553 size_t pa
=encode_utf16(*psz
, cc
);
555 if (pa
== (size_t)-1)
560 *buf
++ = ((char*)cc
)[1];
561 *buf
++ = ((char*)cc
)[0];
564 *buf
++ = ((char*)cc
)[3];
565 *buf
++ = ((char*)cc
)[2];
569 len
+= pa
*sizeof(wxUint16
);
572 if (buf
&& len
<=n
-sizeof(wxUint16
)) *(wxUint16
*)buf
=0;
580 // ----------------------------------------------------------------------------
582 // ----------------------------------------------------------------------------
584 #ifdef WORDS_BIGENDIAN
585 #define wxMBConvUTF32straight wxMBConvUTF32BE
586 #define wxMBConvUTF32swap wxMBConvUTF32LE
588 #define wxMBConvUTF32swap wxMBConvUTF32BE
589 #define wxMBConvUTF32straight wxMBConvUTF32LE
593 WXDLLIMPEXP_DATA_BASE(wxMBConvUTF32LE
) wxConvUTF32LE
;
594 WXDLLIMPEXP_DATA_BASE(wxMBConvUTF32BE
) wxConvUTF32BE
;
599 // copy 32bit MB to 16bit String
600 size_t wxMBConvUTF32straight::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
604 while (*(wxUint32
*)psz
&& (!buf
|| len
< n
))
608 size_t pa
=encode_utf16(*(wxUint32
*)psz
, cc
);
609 if (pa
== (size_t)-1)
619 psz
+= sizeof(wxUint32
);
621 if (buf
&& len
<n
) *buf
=0;
627 // copy 16bit String to 32bit MB
628 size_t wxMBConvUTF32straight::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
632 while (*psz
&& (!buf
|| len
< n
))
636 // cast is ok for WC_UTF16
637 size_t pa
= decode_utf16((const wxUint16
*)psz
, cc
);
638 if (pa
== (size_t)-1)
643 *(wxUint32
*)buf
= cc
;
644 buf
+= sizeof(wxUint32
);
646 len
+= sizeof(wxUint32
);
650 if (buf
&& len
<=n
-sizeof(wxUint32
))
658 // swap 32bit MB to 16bit String
659 size_t wxMBConvUTF32swap::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
663 while (*(wxUint32
*)psz
&& (!buf
|| len
< n
))
666 tmp
[0] = psz
[3]; tmp
[1] = psz
[2];
667 tmp
[2] = psz
[1]; tmp
[3] = psz
[0];
672 size_t pa
=encode_utf16(*(wxUint32
*)tmp
, cc
);
673 if (pa
== (size_t)-1)
683 psz
+= sizeof(wxUint32
);
693 // swap 16bit String to 32bit MB
694 size_t wxMBConvUTF32swap::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
698 while (*psz
&& (!buf
|| len
< n
))
702 // cast is ok for WC_UTF16
703 size_t pa
=decode_utf16((const wxUint16
*)psz
, *(wxUint32
*)cc
);
704 if (pa
== (size_t)-1)
714 len
+= sizeof(wxUint32
);
718 if (buf
&& len
<=n
-sizeof(wxUint32
))
727 // copy 32bit MB to 32bit String
728 size_t wxMBConvUTF32straight::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
732 while (*(wxUint32
*)psz
&& (!buf
|| len
< n
))
735 *buf
++ = *(wxUint32
*)psz
;
737 psz
+= sizeof(wxUint32
);
747 // copy 32bit String to 32bit MB
748 size_t wxMBConvUTF32straight::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
752 while (*psz
&& (!buf
|| len
< n
))
756 *(wxUint32
*)buf
= *psz
;
757 buf
+= sizeof(wxUint32
);
760 len
+= sizeof(wxUint32
);
764 if (buf
&& len
<=n
-sizeof(wxUint32
))
771 // swap 32bit MB to 32bit String
772 size_t wxMBConvUTF32swap::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
776 while (*(wxUint32
*)psz
&& (!buf
|| len
< n
))
780 ((char *)buf
)[0] = psz
[3];
781 ((char *)buf
)[1] = psz
[2];
782 ((char *)buf
)[2] = psz
[1];
783 ((char *)buf
)[3] = psz
[0];
787 psz
+= sizeof(wxUint32
);
797 // swap 32bit String to 32bit MB
798 size_t wxMBConvUTF32swap::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
802 while (*psz
&& (!buf
|| len
< n
))
806 *buf
++ = ((char *)psz
)[3];
807 *buf
++ = ((char *)psz
)[2];
808 *buf
++ = ((char *)psz
)[1];
809 *buf
++ = ((char *)psz
)[0];
811 len
+= sizeof(wxUint32
);
815 if (buf
&& len
<=n
-sizeof(wxUint32
))
825 // ============================================================================
826 // The classes doing conversion using the iconv_xxx() functions
827 // ============================================================================
831 // VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with E2BIG
832 // if output buffer is _exactly_ as big as needed. Such case is (unless there's
833 // yet another bug in glibc) the only case when iconv() returns with (size_t)-1
834 // (which means error) and says there are 0 bytes left in the input buffer --
835 // when _real_ error occurs, bytes-left-in-input buffer is non-zero. Hence,
836 // this alternative test for iconv() failure.
837 // [This bug does not appear in glibc 2.2.]
838 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 1
839 #define ICONV_FAILED(cres, bufLeft) ((cres == (size_t)-1) && \
840 (errno != E2BIG || bufLeft != 0))
842 #define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1)
845 #define ICONV_CHAR_CAST(x) ((ICONV_CONST char **)(x))
847 // ----------------------------------------------------------------------------
848 // wxMBConv_iconv: encapsulates an iconv character set
849 // ----------------------------------------------------------------------------
851 class wxMBConv_iconv
: public wxMBConv
854 wxMBConv_iconv(const wxChar
*name
);
855 virtual ~wxMBConv_iconv();
857 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const;
858 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const;
861 { return (m2w
!= (iconv_t
)-1) && (w2m
!= (iconv_t
)-1); }
864 // the iconv handlers used to translate from multibyte to wide char and in
865 // the other direction
870 // the name (for iconv_open()) of a wide char charset -- if none is
871 // available on this machine, it will remain NULL
872 static const char *ms_wcCharsetName
;
874 // true if the wide char encoding we use (i.e. ms_wcCharsetName) has
875 // different endian-ness than the native one
876 static bool ms_wcNeedsSwap
;
879 const char *wxMBConv_iconv::ms_wcCharsetName
= NULL
;
880 bool wxMBConv_iconv::ms_wcNeedsSwap
= false;
882 wxMBConv_iconv::wxMBConv_iconv(const wxChar
*name
)
884 // Do it the hard way
886 for (size_t i
= 0; i
< wxStrlen(name
)+1; i
++)
887 cname
[i
] = (char) name
[i
];
889 // check for charset that represents wchar_t:
890 if (ms_wcCharsetName
== NULL
)
892 ms_wcNeedsSwap
= false;
894 // try charset with explicit bytesex info (e.g. "UCS-4LE"):
895 ms_wcCharsetName
= WC_NAME_BEST
;
896 m2w
= iconv_open(ms_wcCharsetName
, cname
);
898 if (m2w
== (iconv_t
)-1)
900 // try charset w/o bytesex info (e.g. "UCS4")
901 // and check for bytesex ourselves:
902 ms_wcCharsetName
= WC_NAME
;
903 m2w
= iconv_open(ms_wcCharsetName
, cname
);
905 // last bet, try if it knows WCHAR_T pseudo-charset
906 if (m2w
== (iconv_t
)-1)
908 ms_wcCharsetName
= "WCHAR_T";
909 m2w
= iconv_open(ms_wcCharsetName
, cname
);
912 if (m2w
!= (iconv_t
)-1)
914 char buf
[2], *bufPtr
;
915 wchar_t wbuf
[2], *wbufPtr
;
923 outsz
= SIZEOF_WCHAR_T
* 2;
927 res
= iconv(m2w
, ICONV_CHAR_CAST(&bufPtr
), &insz
,
928 (char**)&wbufPtr
, &outsz
);
930 if (ICONV_FAILED(res
, insz
))
932 ms_wcCharsetName
= NULL
;
933 wxLogLastError(wxT("iconv"));
934 wxLogError(_("Conversion to charset '%s' doesn't work."), name
);
938 ms_wcNeedsSwap
= wbuf
[0] != (wchar_t)buf
[0];
943 ms_wcCharsetName
= NULL
;
945 // VS: we must not output an error here, since wxWindows will safely
946 // fall back to using wxEncodingConverter.
947 wxLogTrace(wxT("strconv"), wxT("Impossible to convert to/from charset '%s' with iconv, falling back to wxEncodingConverter."), name
);
951 wxLogTrace(wxT("strconv"), wxT("wchar_t charset is '%s', needs swap: %i"), ms_wcCharsetName
, ms_wcNeedsSwap
);
953 else // we already have ms_wcCharsetName
955 m2w
= iconv_open(ms_wcCharsetName
, cname
);
958 // NB: don't ever pass NULL to iconv_open(), it may crash!
959 if ( ms_wcCharsetName
)
961 w2m
= iconv_open( cname
, ms_wcCharsetName
);
969 wxMBConv_iconv::~wxMBConv_iconv()
971 if ( m2w
!= (iconv_t
)-1 )
973 if ( w2m
!= (iconv_t
)-1 )
977 size_t wxMBConv_iconv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
979 size_t inbuf
= strlen(psz
);
980 size_t outbuf
= n
* SIZEOF_WCHAR_T
;
982 // VS: Use these instead of psz, buf because iconv() modifies its arguments:
983 wchar_t *bufPtr
= buf
;
984 const char *pszPtr
= psz
;
988 // have destination buffer, convert there
990 ICONV_CHAR_CAST(&pszPtr
), &inbuf
,
991 (char**)&bufPtr
, &outbuf
);
992 res
= n
- (outbuf
/ SIZEOF_WCHAR_T
);
996 // convert to native endianness
997 WC_BSWAP(buf
/* _not_ bufPtr */, res
)
1000 // NB: iconv was given only strlen(psz) characters on input, and so
1001 // it couldn't convert the trailing zero. Let's do it ourselves
1002 // if there's some room left for it in the output buffer.
1008 // no destination buffer... convert using temp buffer
1009 // to calculate destination buffer requirement
1014 outbuf
= 8*SIZEOF_WCHAR_T
;
1017 ICONV_CHAR_CAST(&pszPtr
), &inbuf
,
1018 (char**)&bufPtr
, &outbuf
);
1020 res
+= 8-(outbuf
/SIZEOF_WCHAR_T
);
1021 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
1024 if (ICONV_FAILED(cres
, inbuf
))
1026 //VS: it is ok if iconv fails, hence trace only
1027 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
1034 size_t wxMBConv_iconv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
1036 size_t inbuf
= wxWcslen(psz
) * SIZEOF_WCHAR_T
;
1040 wchar_t *tmpbuf
= 0;
1044 // need to copy to temp buffer to switch endianness
1045 // this absolutely doesn't rock!
1046 // (no, doing WC_BSWAP twice on the original buffer won't help, as it
1047 // could be in read-only memory, or be accessed in some other thread)
1048 tmpbuf
=(wchar_t*)malloc((inbuf
+1)*SIZEOF_WCHAR_T
);
1049 memcpy(tmpbuf
,psz
,(inbuf
+1)*SIZEOF_WCHAR_T
);
1050 WC_BSWAP(tmpbuf
, inbuf
)
1056 // have destination buffer, convert there
1057 cres
= iconv( w2m
, ICONV_CHAR_CAST(&psz
), &inbuf
, &buf
, &outbuf
);
1061 // NB: iconv was given only wcslen(psz) characters on input, and so
1062 // it couldn't convert the trailing zero. Let's do it ourselves
1063 // if there's some room left for it in the output buffer.
1069 // no destination buffer... convert using temp buffer
1070 // to calculate destination buffer requirement
1074 buf
= tbuf
; outbuf
= 16;
1076 cres
= iconv( w2m
, ICONV_CHAR_CAST(&psz
), &inbuf
, &buf
, &outbuf
);
1079 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
1087 if (ICONV_FAILED(cres
, inbuf
))
1089 //VS: it is ok if iconv fails, hence trace only
1090 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
1097 #endif // HAVE_ICONV
1100 // ============================================================================
1101 // Win32 conversion classes
1102 // ============================================================================
1104 #ifdef wxHAVE_WIN32_MB2WC
1107 extern WXDLLIMPEXP_BASE
long wxCharsetToCodepage(const wxChar
*charset
);
1108 extern WXDLLIMPEXP_BASE
long wxEncodingToCodepage(wxFontEncoding encoding
);
1110 class wxMBConv_win32
: public wxMBConv
1115 m_CodePage
= CP_ACP
;
1118 wxMBConv_win32(const wxChar
* name
)
1120 m_CodePage
= wxCharsetToCodepage(name
);
1123 wxMBConv_win32(wxFontEncoding encoding
)
1125 m_CodePage
= wxEncodingToCodepage(encoding
);
1128 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
1130 const size_t len
= ::MultiByteToWideChar
1132 m_CodePage
, // code page
1134 psz
, // input string
1135 -1, // its length (NUL-terminated)
1136 buf
, // output string
1137 buf
? n
: 0 // size of output buffer
1140 // note that it returns # of written chars for buf != NULL and *size*
1141 // of the needed buffer for buf == NULL
1142 return len
? (buf
? len
: len
- 1) : (size_t)-1;
1145 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
1147 const size_t len
= ::WideCharToMultiByte
1149 m_CodePage
, // code page
1151 psz
, // input string
1152 -1, // it is (wide) NUL-terminated
1153 buf
, // output buffer
1154 buf
? n
: 0, // and its size
1155 NULL
, // default "replacement" char
1156 NULL
// [out] was it used?
1159 // see the comment above!
1160 return len
? (buf
? len
: len
- 1) : (size_t)-1;
1164 { return m_CodePage
!= -1; }
1170 #endif // wxHAVE_WIN32_MB2WC
1173 // ============================================================================
1174 // wxEncodingConverter based conversion classes
1175 // ============================================================================
1179 class wxMBConv_wxwin
: public wxMBConv
1184 m_ok
= m2w
.Init(m_enc
, wxFONTENCODING_UNICODE
) &&
1185 w2m
.Init(wxFONTENCODING_UNICODE
, m_enc
);
1189 // temporarily just use wxEncodingConverter stuff,
1190 // so that it works while a better implementation is built
1191 wxMBConv_wxwin(const wxChar
* name
)
1194 m_enc
= wxFontMapper::Get()->CharsetToEncoding(name
, false);
1196 m_enc
= wxFONTENCODING_SYSTEM
;
1201 wxMBConv_wxwin(wxFontEncoding enc
)
1208 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t WXUNUSED(n
)) const
1210 size_t inbuf
= strlen(psz
);
1212 m2w
.Convert(psz
,buf
);
1216 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t WXUNUSED(n
)) const
1218 const size_t inbuf
= wxWcslen(psz
);
1220 w2m
.Convert(psz
,buf
);
1225 bool IsOk() const { return m_ok
; }
1228 wxFontEncoding m_enc
;
1229 wxEncodingConverter m2w
, w2m
;
1231 // were we initialized successfully?
1234 DECLARE_NO_COPY_CLASS(wxMBConv_wxwin
)
1237 #endif // wxUSE_FONTMAP
1239 // ============================================================================
1240 // wxCSConv implementation
1241 // ============================================================================
1243 void wxCSConv::Init()
1250 wxCSConv::wxCSConv(const wxChar
*charset
)
1259 m_encoding
= wxFONTENCODING_SYSTEM
;
1262 wxCSConv::wxCSConv(wxFontEncoding encoding
)
1264 if ( encoding
== wxFONTENCODING_MAX
|| encoding
== wxFONTENCODING_DEFAULT
)
1266 wxFAIL_MSG( _T("invalid encoding value in wxCSConv ctor") );
1268 encoding
= wxFONTENCODING_SYSTEM
;
1273 m_encoding
= encoding
;
1276 wxCSConv::~wxCSConv()
1281 wxCSConv::wxCSConv(const wxCSConv
& conv
)
1286 SetName(conv
.m_name
);
1287 m_encoding
= conv
.m_encoding
;
1290 wxCSConv
& wxCSConv::operator=(const wxCSConv
& conv
)
1294 SetName(conv
.m_name
);
1295 m_encoding
= conv
.m_encoding
;
1300 void wxCSConv::Clear()
1309 void wxCSConv::SetName(const wxChar
*charset
)
1313 m_name
= wxStrdup(charset
);
1318 static inline bool DoesntNeedConv(wxFontEncoding enc
)
1320 return enc
== wxFONTENCODING_DEFAULT
||
1321 enc
== wxFONTENCODING_SYSTEM
||
1322 enc
== wxFONTENCODING_ISO8859_1
;
1325 wxMBConv
*wxCSConv::DoCreate() const
1328 wxFontMapper
* const fontMapper
= wxFontMapper::Get();
1330 wxFontEncoding encFromName
= m_name
? fontMapper
->CharsetToEncoding(m_name
)
1331 : wxFONTENCODING_SYSTEM
;
1332 #endif // wxUSE_FONTMAP
1334 // check for the special case of ASCII charset
1335 if ( (!m_name
&& DoesntNeedConv(m_encoding
))
1337 || (m_name
&& DoesntNeedConv(encFromName
))
1338 #endif // wxUSE_FONTMAP
1341 // don't convert at all
1345 // we trust OS to do conversion better than we can so try external
1346 // conversion methods first
1348 // the full order is:
1349 // 1. OS conversion (iconv() under Unix or Win32 API)
1350 // 2. hard coded conversions for UTF
1351 // 3. wxEncodingConverter as fall back
1357 wxMBConv_iconv
*conv
= new wxMBConv_iconv(m_name
);
1363 #endif // HAVE_ICONV
1365 #ifdef wxHAVE_WIN32_MB2WC
1367 wxMBConv_win32
*conv
= m_name
? new wxMBConv_win32(m_name
)
1368 : new wxMBConv_win32(m_encoding
);
1374 #endif // wxHAVE_WIN32_MB2WC
1377 wxFontEncoding enc
= m_encoding
;
1379 if ( enc
== wxFONTENCODING_SYSTEM
)
1381 #endif // wxUSE_FONTMAP
1385 case wxFONTENCODING_UTF7
:
1386 return new wxMBConvUTF7
;
1388 case wxFONTENCODING_UTF8
:
1389 return new wxMBConvUTF8
;
1391 case wxFONTENCODING_UTF16
:
1392 return new wxMBConvUTF16
;
1394 case wxFONTENCODING_UTF16BE
:
1395 return new wxMBConvUTF16BE
;
1397 case wxFONTENCODING_UTF16LE
:
1398 return new wxMBConvUTF16LE
;
1400 case wxFONTENCODING_UTF32
:
1401 return new wxMBConvUTF32
;
1403 case wxFONTENCODING_UTF32BE
:
1404 return new wxMBConvUTF32BE
;
1406 case wxFONTENCODING_UTF32LE
:
1407 return new wxMBConvUTF32LE
;
1410 // nothing to do but put here to suppress gcc warnings
1417 wxMBConv_wxwin
*conv
= m_name
? new wxMBConv_wxwin(m_name
)
1418 : new wxMBConv_wxwin(m_encoding
);
1424 #endif // wxUSE_FONTMAP
1426 wxLogError(_("Cannot convert from the charset '%s'!"),
1430 wxFontMapper::GetEncodingDescription(m_encoding
).c_str()
1431 #else // !wxUSE_FONTMAP
1432 wxString::Format(_("encoding %s"), m_encoding
).c_str()
1433 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
1439 void wxCSConv::CreateConvIfNeeded() const
1443 wxCSConv
*self
= (wxCSConv
*)this; // const_cast
1446 // if we don't have neither the name nor the encoding, use the default
1447 // encoding for this system
1448 if ( !m_name
&& m_encoding
== wxFONTENCODING_SYSTEM
)
1450 self
->m_encoding
= wxLocale::GetSystemEncoding();
1452 #endif // wxUSE_INTL
1454 self
->m_convReal
= DoCreate();
1455 self
->m_deferred
= false;
1459 size_t wxCSConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
1461 CreateConvIfNeeded();
1464 return m_convReal
->MB2WC(buf
, psz
, n
);
1467 size_t len
= strlen(psz
);
1471 for (size_t c
= 0; c
<= len
; c
++)
1472 buf
[c
] = (unsigned char)(psz
[c
]);
1478 size_t wxCSConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
1480 CreateConvIfNeeded();
1483 return m_convReal
->WC2MB(buf
, psz
, n
);
1486 const size_t len
= wxWcslen(psz
);
1489 for (size_t c
= 0; c
<= len
; c
++)
1490 buf
[c
] = (psz
[c
] > 0xff) ? '?' : psz
[c
];
1496 // ----------------------------------------------------------------------------
1498 // ----------------------------------------------------------------------------
1501 static wxMBConv_win32 wxConvLibcObj
;
1503 static wxMBConvLibc wxConvLibcObj
;
1506 static wxCSConv
wxConvLocalObj(wxFONTENCODING_SYSTEM
);
1507 static wxCSConv
wxConvISO8859_1Obj(wxFONTENCODING_ISO8859_1
);
1508 static wxMBConvUTF7 wxConvUTF7Obj
;
1509 static wxMBConvUTF8 wxConvUTF8Obj
;
1512 WXDLLIMPEXP_DATA_BASE(wxMBConv
&) wxConvLibc
= wxConvLibcObj
;
1513 WXDLLIMPEXP_DATA_BASE(wxCSConv
&) wxConvLocal
= wxConvLocalObj
;
1514 WXDLLIMPEXP_DATA_BASE(wxCSConv
&) wxConvISO8859_1
= wxConvISO8859_1Obj
;
1515 WXDLLIMPEXP_DATA_BASE(wxMBConvUTF7
&) wxConvUTF7
= wxConvUTF7Obj
;
1516 WXDLLIMPEXP_DATA_BASE(wxMBConvUTF8
&) wxConvUTF8
= wxConvUTF8Obj
;
1517 WXDLLIMPEXP_DATA_BASE(wxMBConv
*) wxConvCurrent
= &wxConvLibcObj
;
1519 #else // !wxUSE_WCHAR_T
1521 // stand-ins in absence of wchar_t
1522 WXDLLIMPEXP_DATA_BASE(wxMBConv
) wxConvLibc
,
1527 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T