]>
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
)
97 if (output
) *output
++ = input
;
100 if (input
>=0x110000) {
104 *output
++ = (input
>> 10)+0xd7c0;
105 *output
++ = (input
&0x3ff)+0xdc00;
111 static size_t decode_utf16(wxUint16
*input
,wxUint32
&output
)
113 if ((*input
<0xd800) || (*input
>0xdfff)) {
117 if ((input
[1]<0xdc00) || (input
[1]>=0xdfff)) {
121 output
= ((input
[0] - 0xd7c0) << 10) + (input
[1] - 0xdc00);
128 // ----------------------------------------------------------------------------
130 // ----------------------------------------------------------------------------
132 WXDLLEXPORT_DATA(wxMBConv
) wxConvLibc
;
134 size_t wxMBConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
136 return wxMB2WC(buf
, psz
, n
);
139 size_t wxMBConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
141 return wxWC2MB(buf
, psz
, n
);
144 const wxWCharBuffer
wxMBConv::cMB2WC(const char *psz
) const
148 size_t nLen
= MB2WC((wchar_t *) NULL
, psz
, 0);
149 if (nLen
== (size_t)-1)
150 return wxWCharBuffer((wchar_t *) NULL
);
151 wxWCharBuffer
buf(nLen
);
152 MB2WC((wchar_t *)(const wchar_t *) buf
, psz
, nLen
);
156 return wxWCharBuffer((wchar_t *) NULL
);
159 const wxCharBuffer
wxMBConv::cWC2MB(const wchar_t *psz
) const
163 size_t nLen
= WC2MB((char *) NULL
, psz
, 0);
164 if (nLen
== (size_t)-1)
165 return wxCharBuffer((char *) NULL
);
166 wxCharBuffer
buf(nLen
);
167 WC2MB((char *)(const char *) buf
, psz
, nLen
);
171 return wxCharBuffer((char *) NULL
);
174 // ----------------------------------------------------------------------------
175 // standard file conversion
176 // ----------------------------------------------------------------------------
178 WXDLLEXPORT_DATA(wxMBConvFile
) wxConvFile
;
180 // just use the libc conversion for now
181 size_t wxMBConvFile::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
183 return wxMB2WC(buf
, psz
, n
);
186 size_t wxMBConvFile::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
188 return wxWC2MB(buf
, psz
, n
);
191 // ----------------------------------------------------------------------------
192 // standard gdk conversion
193 // ----------------------------------------------------------------------------
197 WXDLLEXPORT_DATA(wxMBConvGdk
) wxConvGdk
;
201 size_t wxMBConvGdk::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
204 return gdk_mbstowcs((GdkWChar
*)buf
, psz
, n
);
206 GdkWChar
*nbuf
= new GdkWChar
[n
=strlen(psz
)];
207 size_t len
= gdk_mbstowcs(nbuf
, psz
, n
);
213 size_t wxMBConvGdk::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
215 char *mbstr
= gdk_wcstombs((GdkWChar
*)psz
);
216 size_t len
= mbstr
? strlen(mbstr
) : 0;
218 if (len
> n
) len
= n
;
219 memcpy(buf
, psz
, len
);
220 if (len
< n
) buf
[len
] = 0;
227 // ----------------------------------------------------------------------------
229 // ----------------------------------------------------------------------------
231 WXDLLEXPORT_DATA(wxMBConvUTF7
) wxConvUTF7
;
234 static char utf7_setD
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
235 "abcdefghijklmnopqrstuvwxyz"
236 "0123456789'(),-./:?";
237 static char utf7_setO
[]="!\"#$%&*;<=>@[]^_`{|}";
238 static char utf7_setB
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
239 "abcdefghijklmnopqrstuvwxyz"
243 // TODO: write actual implementations of UTF-7 here
244 size_t wxMBConvUTF7::MB2WC(wchar_t * WXUNUSED(buf
),
245 const char * WXUNUSED(psz
),
246 size_t WXUNUSED(n
)) const
251 size_t wxMBConvUTF7::WC2MB(char * WXUNUSED(buf
),
252 const wchar_t * WXUNUSED(psz
),
253 size_t WXUNUSED(n
)) const
258 // ----------------------------------------------------------------------------
260 // ----------------------------------------------------------------------------
262 WXDLLEXPORT_DATA(wxMBConvUTF8
) wxConvUTF8
;
264 static wxUint32 utf8_max
[]={0x7f,0x7ff,0xffff,0x1fffff,0x3ffffff,0x7fffffff,0xffffffff};
266 size_t wxMBConvUTF8::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
270 while (*psz
&& ((!buf
) || (len
<n
))) {
271 unsigned char cc
=*psz
++, fc
=cc
;
273 for (cnt
=0; fc
&0x80; cnt
++) fc
<<=1;
281 // invalid UTF-8 sequence
285 wxUint32 res
=cc
&(0x3f>>cnt
);
288 if ((cc
&0xC0)!=0x80) {
289 // invalid UTF-8 sequence
292 res
=(res
<<6)|(cc
&0x3f);
294 if (res
<=utf8_max
[ocnt
]) {
295 // illegal UTF-8 encoding
299 size_t pa
= encode_utf16(res
, buf
);
300 if (pa
== (size_t)-1)
311 if (buf
&& (len
<n
)) *buf
= 0;
315 size_t wxMBConvUTF8::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
319 while (*psz
&& ((!buf
) || (len
<n
))) {
322 size_t pa
= decode_utf16(psz
,cc
);
323 psz
+= (pa
== (size_t)-1) ? 1 : pa
;
325 cc
=(*psz
++)&0x7fffffff;
328 for (cnt
=0; cc
>utf8_max
[cnt
]; cnt
++);
336 *buf
++=(-128>>cnt
)|((cc
>>(cnt
*6))&(0x3f>>cnt
));
338 *buf
++=0x80|((cc
>>(cnt
*6))&0x3f);
342 if (buf
&& (len
<n
)) *buf
= 0;
346 // ----------------------------------------------------------------------------
347 // specified character set
348 // ----------------------------------------------------------------------------
350 WXDLLEXPORT_DATA(wxCSConv
) wxConvLocal((const wxChar
*)NULL
);
352 #include "wx/encconv.h"
353 #include "wx/fontmap.h"
355 // TODO: add some tables here
356 // - perhaps common encodings to common codepages (for Win32)
357 // - perhaps common encodings to objects ("UTF8" -> wxConvUTF8)
358 // - move wxEncodingConverter meat in here
361 #include "wx/msw/registry.h"
362 // this should work if M$ Internet Exploiter is installed
363 static long CharsetToCodepage(const wxChar
*name
)
372 wxString
path( wxT("MIME\\Database\\Charset\\") );
374 wxRegKey
key( wxRegKey::HKCR
, path
);
376 /* two cases: either there's an AliasForCharset string,
377 * or there are Codepage and InternetEncoding dwords.
378 * The InternetEncoding gives us the actual encoding,
379 * the Codepage just says which Windows character set to
380 * use when displaying the data.
382 if (key
.QueryValue( wxT("InternetEncoding"), &CP
)) break;
384 // no encoding, see if it's an alias
385 if (!key
.QueryValue( wxT("AliasForCharset"), cn
)) break;
395 wxCharacterSet(const wxChar
*name
)
397 virtual ~wxCharacterSet()
399 virtual size_t MB2WC(wchar_t*buf
, const char*psz
, size_t n
)
400 { return (size_t)-1; }
401 virtual size_t WC2MB(char*buf
, const wchar_t*psz
, size_t n
)
402 { return (size_t)-1; }
403 virtual bool usable()
409 class ID_CharSet
: public wxCharacterSet
412 ID_CharSet(const wxChar
*name
,wxMBConv
*cnv
)
413 : wxCharacterSet(name
), work(cnv
) {}
415 size_t MB2WC(wchar_t*buf
, const char*psz
, size_t n
)
416 { return work
? work
->MB2WC(buf
,psz
,n
) : (size_t)-1; }
418 size_t WC2MB(char*buf
, const wchar_t*psz
, size_t n
)
419 { return work
? work
->WC2MB(buf
,psz
,n
) : (size_t)-1; }
422 { return work
!=NULL
; }
430 // VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with E2BIG
431 // if output buffer is _exactly_ as big as needed. Such case is (unless there's
432 // yet another bug in glibc) the only case when iconv() returns with (size_t)-1
433 // (which means error) and says there are 0 bytes left in the input buffer --
434 // when _real_ error occurs, bytes-left-in-input buffer is non-zero. Hence,
435 // this alternative test for iconv() failure.
436 // [This bug does not appear in glibc 2.2.]
437 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 1
438 #define ICONV_FAILED(cres, bufLeft) ((cres == (size_t)-1) && \
439 (errno != E2BIG || bufLeft != 0))
441 #define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1)
444 class IC_CharSet
: public wxCharacterSet
447 IC_CharSet(const wxChar
*name
)
448 : wxCharacterSet(name
)
450 m2w
= iconv_open(WC_NAME
, wxConvLibc
.cWX2MB(cname
));
451 w2m
= iconv_open(wxConvLibc
.cWX2MB(cname
), WC_NAME
);
456 if ( m2w
!= (iconv_t
)-1 )
458 if ( w2m
!= (iconv_t
)-1 )
462 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
464 size_t inbuf
= strlen(psz
);
465 size_t outbuf
= n
* SIZEOF_WCHAR_T
;
467 // VS: Use these instead of psz, buf because iconv() modifies its arguments:
468 wchar_t *bufPtr
= buf
;
469 const char *pszPtr
= psz
;
473 // have destination buffer, convert there
474 #ifdef WX_ICONV_TAKES_CHAR
475 cres
= iconv(m2w
, (char**)&pszPtr
, &inbuf
, (char**)&bufPtr
, &outbuf
);
477 cres
= iconv(m2w
, &pszPtr
, &inbuf
, (char**)&bufPtr
, &outbuf
);
479 res
= n
- (outbuf
/ SIZEOF_WCHAR_T
);
480 // convert to native endianness
482 WC_BSWAP(buf
/* _not_ bufPtr */, res
)
487 // no destination buffer... convert using temp buffer
488 // to calculate destination buffer requirement
492 bufPtr
= tbuf
; outbuf
= 8*SIZEOF_WCHAR_T
;
493 #ifdef WX_ICONV_TAKES_CHAR
494 cres
= iconv( m2w
, (char**)&pszPtr
, &inbuf
, (char**)&bufPtr
, &outbuf
);
496 cres
= iconv( m2w
, &pszPtr
, &inbuf
, (char**)&bufPtr
, &outbuf
);
498 res
+= 8-(outbuf
/SIZEOF_WCHAR_T
);
499 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
502 if (ICONV_FAILED(cres
, inbuf
))
508 size_t WC2MB(char*buf
, const wchar_t*psz
, size_t n
)
510 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
511 size_t inbuf
= std::wcslen(psz
) * SIZEOF_WCHAR_T
;
513 size_t inbuf
= ::wcslen(psz
) * SIZEOF_WCHAR_T
;
519 // need to copy to temp buffer to switch endianness
520 // this absolutely doesn't rock!
521 // (no, doing WC_BSWAP twice on the original buffer won't help, as it
522 // could be in read-only memory, or be accessed in some other thread)
523 wchar_t*tmpbuf
=(wchar_t*)malloc((inbuf
+1)*SIZEOF_WCHAR_T
);
524 memcpy(tmpbuf
,psz
,(inbuf
+1)*SIZEOF_WCHAR_T
);
525 WC_BSWAP(tmpbuf
, inbuf
)
530 // have destination buffer, convert there
531 #ifdef WX_ICONV_TAKES_CHAR
532 cres
= iconv( w2m
, (char**)&psz
, &inbuf
, &buf
, &outbuf
);
534 cres
= iconv( w2m
, (const char**)&psz
, &inbuf
, &buf
, &outbuf
);
540 // no destination buffer... convert using temp buffer
541 // to calculate destination buffer requirement
545 buf
= tbuf
; outbuf
= 16;
546 #ifdef WX_ICONV_TAKES_CHAR
547 cres
= iconv( w2m
, (char**)&psz
, &inbuf
, &buf
, &outbuf
);
549 cres
= iconv( w2m
, (const char**)&psz
, &inbuf
, &buf
, &outbuf
);
552 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
557 if (ICONV_FAILED(cres
, inbuf
))
564 { return (m2w
!= (iconv_t
)-1) && (w2m
!= (iconv_t
)-1); }
572 class CP_CharSet
: public wxCharacterSet
575 CP_CharSet(const wxChar
*name
)
576 : wxCharacterSet(name
), CodePage(CharsetToCodepage(name
)) {}
578 size_t MB2WC(wchar_t*buf
, const char*psz
, size_t n
)
580 size_t len
= MultiByteToWideChar(CodePage
,0,psz
,-1,buf
,buf
?n
:0);
581 return len
? len
: (size_t)-1;
584 size_t WC2MB(char*buf
, const wchar_t*psz
, size_t n
)
586 size_t len
= WideCharToMultiByte(CodePage
,0,psz
,-1,buf
,buf
?n
:0,NULL
,NULL
);
587 return len
? len
: (size_t)-1;
591 { return CodePage
!=-1; }
598 class EC_CharSet
: public wxCharacterSet
601 // temporarily just use wxEncodingConverter stuff,
602 // so that it works while a better implementation is built
603 EC_CharSet(const wxChar
*name
) : wxCharacterSet(name
), enc(wxFONTENCODING_SYSTEM
)
606 enc
= wxTheFontMapper
->CharsetToEncoding(name
, FALSE
);
607 m2w
.Init(enc
, wxFONTENCODING_UNICODE
);
608 w2m
.Init(wxFONTENCODING_UNICODE
, enc
);
611 size_t MB2WC(wchar_t*buf
, const char*psz
, size_t n
)
613 size_t inbuf
= strlen(psz
);
614 if (buf
) m2w
.Convert(psz
,buf
);
618 size_t WC2MB(char*buf
, const wchar_t*psz
, size_t n
)
620 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
621 size_t inbuf
= std::wcslen(psz
);
623 size_t inbuf
= ::wcslen(psz
);
626 w2m
.Convert(psz
,buf
);
632 { return (enc
!=wxFONTENCODING_SYSTEM
) && (enc
!=wxFONTENCODING_DEFAULT
); }
636 wxEncodingConverter m2w
, w2m
;
639 static wxCharacterSet
*wxGetCharacterSet(const wxChar
*name
)
641 wxCharacterSet
*cset
= NULL
;
644 if (!wxStricmp(name
, wxT("UTF8")) || !wxStricmp(name
, wxT("UTF-8")))
646 cset
= new ID_CharSet(name
, &wxConvUTF8
);
651 cset
= new IC_CharSet(name
); // may not take NULL
656 if (cset
&& cset
->usable()) return cset
;
657 if (cset
) delete cset
;
660 cset
= new CP_CharSet(name
); // may take NULL
661 if (cset
->usable()) return cset
;
663 if (cset
) delete cset
;
664 cset
= new EC_CharSet(name
);
665 if (cset
->usable()) return cset
;
667 wxLogError(_("Unknown encoding '%s'!"), name
);
671 wxCSConv::wxCSConv(const wxChar
*charset
)
673 m_name
= (wxChar
*) NULL
;
674 m_cset
= (wxCharacterSet
*) NULL
;
679 wxCSConv::~wxCSConv()
681 if (m_name
) free(m_name
);
682 if (m_cset
) delete m_cset
;
685 void wxCSConv::SetName(const wxChar
*charset
)
689 m_name
= wxStrdup(charset
);
694 void wxCSConv::LoadNow()
701 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
702 // GNU libc provides current character set this way
703 char*alang
= nl_langinfo(CODESET
);
706 SetName(wxConvLibc
.cMB2WX(alang
));
711 // if we can't get at the character set directly,
712 // try to see if it's in the environment variables
713 // (in most cases this won't work, but I was out of ideas)
714 wxChar
*lang
= wxGetenv(wxT("LC_ALL"));
715 if (!lang
) lang
= wxGetenv(wxT("LC_CTYPE"));
716 if (!lang
) lang
= wxGetenv(wxT("LANG"));
717 wxChar
*dot
= lang
? wxStrchr(lang
, wxT('.')) : (wxChar
*)NULL
;
718 if (dot
) SetName(dot
+1);
722 m_cset
= wxGetCharacterSet(m_name
);
727 size_t wxCSConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
729 ((wxCSConv
*)this)->LoadNow(); // discard constness
732 return m_cset
->MB2WC(buf
, psz
, n
);
735 size_t len
=strlen(psz
);
739 for (size_t c
=0; c
<=len
; c
++)
740 buf
[c
] = (unsigned char)(psz
[c
]);
746 size_t wxCSConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
748 ((wxCSConv
*)this)->LoadNow(); // discard constness
751 return m_cset
->WC2MB(buf
, psz
, n
);
754 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
755 size_t len
=std::wcslen(psz
);
757 size_t len
=::wcslen(psz
);
761 for (size_t c
=0; c
<=len
; c
++)
762 buf
[c
] = (psz
[c
]>0xff) ? '?' : psz
[c
];
769 class IC_CharSetConverter
772 IC_CharSetConverter(IC_CharSet
*from
,IC_CharSet
*to
)
773 { cnv
= iconv_open(wxConvLibc
.cWX2MB(to
->cname
),wxConvLibc
.cWX2MB(from
->cname
)); }
775 ~IC_CharSetConverter()
776 { if (cnv
!=(iconv_t
)-1) iconv_close(cnv
); }
778 size_t Convert(char*buf
, const char*psz
, size_t n
)
780 size_t inbuf
= strlen(psz
);
782 #ifdef WX_ICONV_TAKES_CHAR
783 size_t res
= iconv( cnv
, (char**)&psz
, &inbuf
, &buf
, &outbuf
);
785 size_t res
= iconv( cnv
, &psz
, &inbuf
, &buf
, &outbuf
);
787 if (res
==(size_t)-1) return (size_t)-1;
796 class EC_CharSetConverter
799 EC_CharSetConverter(EC_CharSet
*from
,EC_CharSet
*to
)
800 { cnv
.Init(from
->enc
,to
->enc
); }
802 size_t Convert(char*buf
, const char*psz
, size_t n
)
804 size_t inbuf
= strlen(psz
);
805 if (buf
) cnv
.Convert(psz
,buf
);
810 wxEncodingConverter cnv
;
813 #else // !wxUSE_WCHAR_T
815 // ----------------------------------------------------------------------------
816 // stand-ins in absence of wchar_t
817 // ----------------------------------------------------------------------------
819 WXDLLEXPORT_DATA(wxMBConv
) wxConvLibc
, wxConvFile
;
821 #endif // wxUSE_WCHAR_T