]>
git.saurik.com Git - wxWidgets.git/blob - src/common/strconv.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Unicode conversion classes
4 // Author: Ove Kaaven, Robert Roebling, Vadim Zeitlin
8 // Copyright: (c) 1999 Ove Kaaven, Robert Roebling, Vadim Zeitlin
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "strconv.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/msw/private.h"
47 #ifdef HAVE_LANGINFO_H
56 #include "wx/strconv.h"
60 #if defined(WORDS_BIGENDIAN) || defined(__STDC_ISO_10646__)
61 #define BSWAP_UCS4(str, len)
62 #define BSWAP_UCS2(str, len)
64 #define BSWAP_UCS4(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT32_SWAP_ALWAYS(str[_c]); }
65 #define BSWAP_UCS2(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT16_SWAP_ALWAYS(str[_c]); }
68 #define BSWAP_UTF32(str, len) BSWAP_UCS4(str, len)
69 #define BSWAP_UTF16(str, len) BSWAP_UCS2(str, len)
71 #if SIZEOF_WCHAR_T == 4
72 #define WC_NAME "UCS4"
73 #define WC_BSWAP BSWAP_UCS4
74 #elif SIZEOF_WCHAR_T == 2
75 #define WC_NAME "UTF16"
76 #define WC_BSWAP BSWAP_UTF16
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 WXDLLEXPORT_DATA(wxMBConv
*) wxConvCurrent
= &wxConvLibc
;
86 // ============================================================================
88 // ============================================================================
94 static size_t encode_utf16(wxUint32 input
,wxUint16
*output
)
98 if (output
) *output
++ = input
;
101 else if (input
>=0x110000)
109 *output
++ = (input
>> 10)+0xd7c0;
110 *output
++ = (input
&0x3ff)+0xdc00;
116 static size_t decode_utf16(wxUint16
*input
,wxUint32
&output
)
118 if ((*input
<0xd800) || (*input
>0xdfff))
123 else if ((input
[1]<0xdc00) || (input
[1]>=0xdfff))
130 output
= ((input
[0] - 0xd7c0) << 10) + (input
[1] - 0xdc00);
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 WXDLLEXPORT_DATA(wxMBConv
) wxConvLibc
;
143 size_t wxMBConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
145 return wxMB2WC(buf
, psz
, n
);
148 size_t wxMBConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
150 return wxWC2MB(buf
, psz
, n
);
153 const wxWCharBuffer
wxMBConv::cMB2WC(const char *psz
) const
157 size_t nLen
= MB2WC((wchar_t *) NULL
, psz
, 0);
158 if (nLen
== (size_t)-1)
159 return wxWCharBuffer((wchar_t *) NULL
);
160 wxWCharBuffer
buf(nLen
);
161 MB2WC((wchar_t *)(const wchar_t *) buf
, psz
, nLen
);
165 return wxWCharBuffer((wchar_t *) NULL
);
168 const wxCharBuffer
wxMBConv::cWC2MB(const wchar_t *psz
) const
172 size_t nLen
= WC2MB((char *) NULL
, psz
, 0);
173 if (nLen
== (size_t)-1)
174 return wxCharBuffer((char *) NULL
);
175 wxCharBuffer
buf(nLen
);
176 WC2MB((char *)(const char *) buf
, psz
, nLen
);
180 return wxCharBuffer((char *) NULL
);
183 // ----------------------------------------------------------------------------
184 // standard file conversion
185 // ----------------------------------------------------------------------------
187 WXDLLEXPORT_DATA(wxMBConvFile
) wxConvFile
;
189 // just use the libc conversion for now
190 size_t wxMBConvFile::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
192 return wxMB2WC(buf
, psz
, n
);
195 size_t wxMBConvFile::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
197 return wxWC2MB(buf
, psz
, n
);
200 // ----------------------------------------------------------------------------
201 // standard gdk conversion
202 // ----------------------------------------------------------------------------
206 WXDLLEXPORT_DATA(wxMBConvGdk
) wxConvGdk
;
210 size_t wxMBConvGdk::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
214 return gdk_mbstowcs((GdkWChar
*)buf
, psz
, n
);
218 GdkWChar
*nbuf
= new GdkWChar
[n
=strlen(psz
)];
219 size_t len
= gdk_mbstowcs(nbuf
, psz
, n
);
225 size_t wxMBConvGdk::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
227 char *mbstr
= gdk_wcstombs((GdkWChar
*)psz
);
228 size_t len
= mbstr
? strlen(mbstr
) : 0;
233 memcpy(buf
, psz
, len
);
242 // ----------------------------------------------------------------------------
244 // ----------------------------------------------------------------------------
246 WXDLLEXPORT_DATA(wxMBConvUTF7
) wxConvUTF7
;
249 static char utf7_setD
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
250 "abcdefghijklmnopqrstuvwxyz"
251 "0123456789'(),-./:?";
252 static char utf7_setO
[]="!\"#$%&*;<=>@[]^_`{|}";
253 static char utf7_setB
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
254 "abcdefghijklmnopqrstuvwxyz"
258 // TODO: write actual implementations of UTF-7 here
259 size_t wxMBConvUTF7::MB2WC(wchar_t * WXUNUSED(buf
),
260 const char * WXUNUSED(psz
),
261 size_t WXUNUSED(n
)) const
266 size_t wxMBConvUTF7::WC2MB(char * WXUNUSED(buf
),
267 const wchar_t * WXUNUSED(psz
),
268 size_t WXUNUSED(n
)) const
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
277 WXDLLEXPORT_DATA(wxMBConvUTF8
) wxConvUTF8
;
279 static wxUint32 utf8_max
[]=
280 { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff, 0xffffffff };
282 size_t wxMBConvUTF8::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
286 while (*psz
&& ((!buf
) || (len
< n
)))
288 unsigned char cc
= *psz
++, fc
= cc
;
290 for (cnt
= 0; fc
& 0x80; cnt
++)
304 // invalid UTF-8 sequence
309 unsigned ocnt
= cnt
- 1;
310 wxUint32 res
= cc
& (0x3f >> cnt
);
314 if ((cc
& 0xC0) != 0x80)
316 // invalid UTF-8 sequence
319 res
= (res
<< 6) | (cc
& 0x3f);
321 if (res
<= utf8_max
[ocnt
])
323 // illegal UTF-8 encoding
327 size_t pa
= encode_utf16(res
, buf
);
328 if (pa
== (size_t)-1)
341 if (buf
&& (len
< n
))
346 size_t wxMBConvUTF8::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
350 while (*psz
&& ((!buf
) || (len
< n
)))
354 size_t pa
= decode_utf16(psz
,cc
);
355 psz
+= (pa
== (size_t)-1) ? 1 : pa
;
357 cc
=(*psz
++) & 0x7fffffff;
360 for (cnt
= 0; cc
> utf8_max
[cnt
]; cnt
++) {}
374 *buf
++ = (-128 >> cnt
) | ((cc
>> (cnt
* 6)) & (0x3f >> cnt
));
376 *buf
++ = 0x80 | ((cc
>> (cnt
* 6)) & 0x3f);
381 if (buf
&& (len
<n
)) *buf
= 0;
385 // ----------------------------------------------------------------------------
386 // specified character set
387 // ----------------------------------------------------------------------------
389 WXDLLEXPORT_DATA(wxCSConv
) wxConvLocal((const wxChar
*)NULL
);
391 #include "wx/encconv.h"
392 #include "wx/fontmap.h"
394 // TODO: add some tables here
395 // - perhaps common encodings to common codepages (for Win32)
396 // - perhaps common encodings to objects ("UTF8" -> wxConvUTF8)
397 // - move wxEncodingConverter meat in here
400 #include "wx/msw/registry.h"
401 // this should work if M$ Internet Exploiter is installed
402 static long CharsetToCodepage(const wxChar
*name
)
411 wxString
path(wxT("MIME\\Database\\Charset\\"));
413 wxRegKey
key(wxRegKey::HKCR
, path
);
415 if (!key
.Exists()) continue;
417 // two cases: either there's an AliasForCharset string,
418 // or there are Codepage and InternetEncoding dwords.
419 // The InternetEncoding gives us the actual encoding,
420 // the Codepage just says which Windows character set to
421 // use when displaying the data.
422 if (key
.HasValue(wxT("InternetEncoding")) &&
423 key
.QueryValue(wxT("InternetEncoding"), &CP
)) break;
425 // no encoding, see if it's an alias
426 if (!key
.HasValue(wxT("AliasForCharset")) ||
427 !key
.QueryValue(wxT("AliasForCharset"), cn
)) break;
437 wxCharacterSet(const wxChar
*name
)
439 virtual ~wxCharacterSet()
441 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
442 { return (size_t)-1; }
443 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
444 { return (size_t)-1; }
445 virtual bool usable()
451 class ID_CharSet
: public wxCharacterSet
454 ID_CharSet(const wxChar
*name
,wxMBConv
*cnv
)
455 : wxCharacterSet(name
), work(cnv
) {}
457 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
458 { return work
? work
->MB2WC(buf
,psz
,n
) : (size_t)-1; }
460 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
461 { return work
? work
->WC2MB(buf
,psz
,n
) : (size_t)-1; }
464 { return work
!=NULL
; }
472 // VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with E2BIG
473 // if output buffer is _exactly_ as big as needed. Such case is (unless there's
474 // yet another bug in glibc) the only case when iconv() returns with (size_t)-1
475 // (which means error) and says there are 0 bytes left in the input buffer --
476 // when _real_ error occurs, bytes-left-in-input buffer is non-zero. Hence,
477 // this alternative test for iconv() failure.
478 // [This bug does not appear in glibc 2.2.]
479 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 1
480 #define ICONV_FAILED(cres, bufLeft) ((cres == (size_t)-1) && \
481 (errno != E2BIG || bufLeft != 0))
483 #define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1)
486 class IC_CharSet
: public wxCharacterSet
489 IC_CharSet(const wxChar
*name
)
490 : wxCharacterSet(name
)
492 m2w
= iconv_open(WC_NAME
, wxConvLibc
.cWX2MB(cname
));
493 w2m
= iconv_open(wxConvLibc
.cWX2MB(cname
), WC_NAME
);
498 if ( m2w
!= (iconv_t
)-1 )
500 if ( w2m
!= (iconv_t
)-1 )
504 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
506 size_t inbuf
= strlen(psz
);
507 size_t outbuf
= n
* SIZEOF_WCHAR_T
;
509 // VS: Use these instead of psz, buf because iconv() modifies its arguments:
510 wchar_t *bufPtr
= buf
;
511 const char *pszPtr
= psz
;
515 // have destination buffer, convert there
516 #ifdef WX_ICONV_TAKES_CHAR
517 cres
= iconv(m2w
, (char**)&pszPtr
, &inbuf
, (char**)&bufPtr
, &outbuf
);
519 cres
= iconv(m2w
, &pszPtr
, &inbuf
, (char**)&bufPtr
, &outbuf
);
521 res
= n
- (outbuf
/ SIZEOF_WCHAR_T
);
522 // convert to native endianness
524 WC_BSWAP(buf
/* _not_ bufPtr */, res
)
529 // no destination buffer... convert using temp buffer
530 // to calculate destination buffer requirement
534 bufPtr
= tbuf
; outbuf
= 8*SIZEOF_WCHAR_T
;
535 #ifdef WX_ICONV_TAKES_CHAR
536 cres
= iconv( m2w
, (char**)&pszPtr
, &inbuf
, (char**)&bufPtr
, &outbuf
);
538 cres
= iconv( m2w
, &pszPtr
, &inbuf
, (char**)&bufPtr
, &outbuf
);
540 res
+= 8-(outbuf
/SIZEOF_WCHAR_T
);
541 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
544 if (ICONV_FAILED(cres
, inbuf
))
550 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
552 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
553 size_t inbuf
= std::wcslen(psz
) * SIZEOF_WCHAR_T
;
555 size_t inbuf
= ::wcslen(psz
) * SIZEOF_WCHAR_T
;
561 // need to copy to temp buffer to switch endianness
562 // this absolutely doesn't rock!
563 // (no, doing WC_BSWAP twice on the original buffer won't help, as it
564 // could be in read-only memory, or be accessed in some other thread)
565 wchar_t *tmpbuf
=(wchar_t*)malloc((inbuf
+1)*SIZEOF_WCHAR_T
);
566 memcpy(tmpbuf
,psz
,(inbuf
+1)*SIZEOF_WCHAR_T
);
567 WC_BSWAP(tmpbuf
, inbuf
)
572 // have destination buffer, convert there
573 #ifdef WX_ICONV_TAKES_CHAR
574 cres
= iconv( w2m
, (char**)&psz
, &inbuf
, &buf
, &outbuf
);
576 cres
= iconv( w2m
, (const char**)&psz
, &inbuf
, &buf
, &outbuf
);
582 // no destination buffer... convert using temp buffer
583 // to calculate destination buffer requirement
587 buf
= tbuf
; outbuf
= 16;
588 #ifdef WX_ICONV_TAKES_CHAR
589 cres
= iconv( w2m
, (char**)&psz
, &inbuf
, &buf
, &outbuf
);
591 cres
= iconv( w2m
, (const char**)&psz
, &inbuf
, &buf
, &outbuf
);
594 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
599 if (ICONV_FAILED(cres
, inbuf
))
606 { return (m2w
!= (iconv_t
)-1) && (w2m
!= (iconv_t
)-1); }
614 class CP_CharSet
: public wxCharacterSet
617 CP_CharSet(const wxChar
*name
)
618 : wxCharacterSet(name
), CodePage(CharsetToCodepage(name
)) {}
620 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
623 MultiByteToWideChar(CodePage
, 0, psz
, -1, buf
, buf
? n
: 0);
624 return len
? len
: (size_t)-1;
627 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
629 size_t len
= WideCharToMultiByte(CodePage
, 0, psz
, -1, buf
,
630 buf
? n
: 0, NULL
, NULL
);
631 return len
? len
: (size_t)-1;
635 { return CodePage
!= -1; }
642 class EC_CharSet
: public wxCharacterSet
645 // temporarily just use wxEncodingConverter stuff,
646 // so that it works while a better implementation is built
647 EC_CharSet(const wxChar
*name
) : wxCharacterSet(name
),
648 enc(wxFONTENCODING_SYSTEM
)
651 enc
= wxTheFontMapper
->CharsetToEncoding(name
, FALSE
);
652 m2w
.Init(enc
, wxFONTENCODING_UNICODE
);
653 w2m
.Init(wxFONTENCODING_UNICODE
, enc
);
656 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
658 size_t inbuf
= strlen(psz
);
660 m2w
.Convert(psz
,buf
);
664 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
666 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
667 size_t inbuf
= std::wcslen(psz
);
669 size_t inbuf
= ::wcslen(psz
);
672 w2m
.Convert(psz
,buf
);
678 { return (enc
!=wxFONTENCODING_SYSTEM
) && (enc
!=wxFONTENCODING_DEFAULT
); }
682 wxEncodingConverter m2w
, w2m
;
685 static wxCharacterSet
*wxGetCharacterSet(const wxChar
*name
)
687 wxCharacterSet
*cset
= NULL
;
690 if (wxStricmp(name
, wxT("UTF8")) == 0 || wxStricmp(name
, wxT("UTF-8")) == 0)
692 cset
= new ID_CharSet(name
, &wxConvUTF8
);
697 cset
= new IC_CharSet(name
); // may not take NULL
702 if (cset
&& cset
->usable()) return cset
;
710 cset
= new CP_CharSet(name
); // may take NULL
717 cset
= new EC_CharSet(name
);
722 wxLogError(_("Unknown encoding '%s'!"), name
);
726 wxCSConv::wxCSConv(const wxChar
*charset
)
728 m_name
= (wxChar
*)NULL
;
729 m_cset
= (wxCharacterSet
*) NULL
;
733 wxCSConv::~wxCSConv()
739 void wxCSConv::SetName(const wxChar
*charset
)
743 m_name
= wxStrdup(charset
);
748 void wxCSConv::LoadNow()
754 wxString name
= wxLocale::GetSystemEncodingName();
759 m_cset
= wxGetCharacterSet(m_name
);
764 size_t wxCSConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
766 ((wxCSConv
*)this)->LoadNow(); // discard constness
769 return m_cset
->MB2WC(buf
, psz
, n
);
772 size_t len
= strlen(psz
);
776 for (size_t c
= 0; c
<= len
; c
++)
777 buf
[c
] = (unsigned char)(psz
[c
]);
783 size_t wxCSConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
785 ((wxCSConv
*)this)->LoadNow(); // discard constness
788 return m_cset
->WC2MB(buf
, psz
, n
);
791 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
792 size_t len
=std::wcslen(psz
);
794 size_t len
=::wcslen(psz
);
798 for (size_t c
= 0; c
<= len
; c
++)
799 buf
[c
] = (psz
[c
] > 0xff) ? '?' : psz
[c
];
807 class IC_CharSetConverter
810 IC_CharSetConverter(IC_CharSet
*from
, IC_CharSet
*to
)
812 cnv
= iconv_open(wxConvLibc
.cWX2MB(to
->cname
),
813 wxConvLibc
.cWX2MB(from
->cname
));
816 ~IC_CharSetConverter()
818 if (cnv
!= (iconv_t
)-1)
822 size_t Convert(char *buf
, const char *psz
, size_t n
)
824 size_t inbuf
= strlen(psz
);
826 #ifdef WX_ICONV_TAKES_CHAR
827 size_t res
= iconv( cnv
, (char**)&psz
, &inbuf
, &buf
, &outbuf
);
829 size_t res
= iconv( cnv
, &psz
, &inbuf
, &buf
, &outbuf
);
831 if (res
== (size_t)-1)
840 #endif // HAVE_ICONV_H
842 class EC_CharSetConverter
845 EC_CharSetConverter(EC_CharSet
*from
,EC_CharSet
*to
)
846 { cnv
.Init(from
->enc
,to
->enc
); }
848 size_t Convert(char*buf
, const char*psz
, size_t n
)
850 size_t inbuf
= strlen(psz
);
851 if (buf
) cnv
.Convert(psz
,buf
);
856 wxEncodingConverter cnv
;
859 #else // !wxUSE_WCHAR_T
861 // ----------------------------------------------------------------------------
862 // stand-ins in absence of wchar_t
863 // ----------------------------------------------------------------------------
865 WXDLLEXPORT_DATA(wxMBConv
) wxConvLibc
, wxConvFile
;
867 #endif // wxUSE_WCHAR_T