]>
git.saurik.com Git - wxWidgets.git/blob - src/common/strconv.cpp
9c505d0cc82c9b815b42d2da03aabbff299ea687
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"
43 #ifdef HAVE_LANGINFO_H
48 #include "wx/strconv.h"
50 #ifdef WORDS_BIGENDIAN
51 #define BSWAP_UCS4(str, len)
52 #define BSWAP_UCS2(str, len)
54 #define BSWAP_UCS4(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT32_SWAP_ALWAYS(str[_c]); }
55 #define BSWAP_UCS2(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT16_SWAP_ALWAYS(str[_c]); }
58 #define BSWAP_UTF32(str, len) BSWAP_UCS4(str, len)
59 #define BSWAP_UTF16(str, len) BSWAP_UCS2(str, len)
61 #if SIZEOF_WCHAR_T == 4
62 #define WC_NAME "UCS4"
63 #define WC_BSWAP BSWAP_UCS4
64 #elif SIZEOF_WCHAR_T == 2
65 #define WC_NAME "UTF16"
66 #define WC_BSWAP BSWAP_UTF16
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 WXDLLEXPORT_DATA(wxMBConv
*) wxConvCurrent
= &wxConvLibc
;
76 // ============================================================================
78 // ============================================================================
82 static size_t encode_utf16(wxUint32 input
,wxUint16
*output
)
85 if (output
) *output
++ = input
;
88 if (input
>=0x110000) {
92 *output
++ = (input
>> 10)+0xd7c0;
93 *output
++ = (input
&0x3ff)+0xdc00;
99 static size_t decode_utf16(wxUint16
*input
,wxUint32
&output
)
101 if ((*input
<0xd800) || (*input
>0xdfff)) {
105 if ((input
[1]<0xdc00) || (input
[1]>=0xdfff)) {
109 output
= ((input
[0] - 0xd7c0) << 10) + (input
[1] - 0xdc00);
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 WXDLLEXPORT_DATA(wxMBConv
) wxConvLibc
;
120 size_t wxMBConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
122 return wxMB2WC(buf
, psz
, n
);
125 size_t wxMBConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
127 return wxWC2MB(buf
, psz
, n
);
130 const wxWCharBuffer
wxMBConv::cMB2WC(const char *psz
) const
134 size_t nLen
= MB2WC((wchar_t *) NULL
, psz
, 0);
135 if (nLen
== (size_t)-1)
136 return wxWCharBuffer((wchar_t *) NULL
);
137 wxWCharBuffer
buf(nLen
);
138 MB2WC((wchar_t *)(const wchar_t *) buf
, psz
, nLen
);
142 return wxWCharBuffer((wchar_t *) NULL
);
145 const wxCharBuffer
wxMBConv::cWC2MB(const wchar_t *psz
) const
149 size_t nLen
= WC2MB((char *) NULL
, psz
, 0);
150 if (nLen
== (size_t)-1)
151 return wxCharBuffer((char *) NULL
);
152 wxCharBuffer
buf(nLen
);
153 WC2MB((char *)(const char *) buf
, psz
, nLen
);
157 return wxCharBuffer((char *) NULL
);
160 // ----------------------------------------------------------------------------
161 // standard file conversion
162 // ----------------------------------------------------------------------------
164 WXDLLEXPORT_DATA(wxMBConvFile
) wxConvFile
;
166 // just use the libc conversion for now
167 size_t wxMBConvFile::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
169 return wxMB2WC(buf
, psz
, n
);
172 size_t wxMBConvFile::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
174 return wxWC2MB(buf
, psz
, n
);
177 // ----------------------------------------------------------------------------
178 // standard gdk conversion
179 // ----------------------------------------------------------------------------
183 WXDLLEXPORT_DATA(wxMBConvGdk
) wxConvGdk
;
187 size_t wxMBConvGdk::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
190 return gdk_mbstowcs((GdkWChar
*)buf
, psz
, n
);
192 GdkWChar
*nbuf
= new GdkWChar
[n
=strlen(psz
)];
193 size_t len
= gdk_mbstowcs(nbuf
, psz
, n
);
199 size_t wxMBConvGdk::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
201 char *mbstr
= gdk_wcstombs((GdkWChar
*)psz
);
202 size_t len
= mbstr
? strlen(mbstr
) : 0;
204 if (len
> n
) len
= n
;
205 memcpy(buf
, psz
, len
);
206 if (len
< n
) buf
[len
] = 0;
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
217 WXDLLEXPORT_DATA(wxMBConvUTF7
) wxConvUTF7
;
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 WXDLLEXPORT_DATA(wxMBConvUTF8
) wxConvUTF8
;
250 static wxUint32 utf8_max
[]={0x7f,0x7ff,0xffff,0x1fffff,0x3ffffff,0x7fffffff,0xffffffff};
252 size_t wxMBConvUTF8::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
256 while (*psz
&& ((!buf
) || (len
<n
))) {
257 unsigned char cc
=*psz
++, fc
=cc
;
259 for (cnt
=0; fc
&0x80; cnt
++) fc
<<=1;
267 // invalid UTF-8 sequence
271 wxUint32 res
=cc
&(0x3f>>cnt
);
274 if ((cc
&0xC0)!=0x80) {
275 // invalid UTF-8 sequence
278 res
=(res
<<6)|(cc
&0x3f);
280 if (res
<=utf8_max
[ocnt
]) {
281 // illegal UTF-8 encoding
285 size_t pa
= encode_utf16(res
, buf
);
286 if (pa
== (size_t)-1)
297 if (buf
&& (len
<n
)) *buf
= 0;
301 size_t wxMBConvUTF8::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
305 while (*psz
&& ((!buf
) || (len
<n
))) {
308 size_t pa
= decode_utf16(psz
,cc
);
309 psz
+= (pa
== (size_t)-1) ? 1 : pa
;
311 cc
=(*psz
++)&0x7fffffff;
314 for (cnt
=0; cc
>utf8_max
[cnt
]; cnt
++);
322 *buf
++=(-128>>cnt
)|((cc
>>(cnt
*6))&(0x3f>>cnt
));
324 *buf
++=0x80|((cc
>>(cnt
*6))&0x3f);
328 if (buf
&& (len
<n
)) *buf
= 0;
332 // ----------------------------------------------------------------------------
333 // specified character set
334 // ----------------------------------------------------------------------------
336 WXDLLEXPORT_DATA(wxCSConv
) wxConvLocal((const wxChar
*)NULL
);
338 #include "wx/encconv.h"
339 #include "wx/fontmap.h"
341 // TODO: add some tables here
342 // - perhaps common encodings to common codepages (for Win32)
343 // - perhaps common encodings to objects ("UTF8" -> wxConvUTF8)
344 // - move wxEncodingConverter meat in here
347 #include "wx/msw/registry.h"
348 // this should work if M$ Internet Exploiter is installed
349 static long CharsetToCodepage(const wxChar
*name
)
351 if (!name
) return GetACP();
355 wxString
path(wxT("MIME\\Database\\Charset\\"));
357 wxRegKey
key(wxRegKey::HKCR
,path
);
359 /* two cases: either there's an AliasForCharset string,
360 * or there are Codepage and InternetEncoding dwords.
361 * The InternetEncoding gives us the actual encoding,
362 * the Codepage just says which Windows character set to
363 * use when displaying the data.
365 if (key
.QueryValue(wxT("InternetEncoding"),&CP
)) break;
366 // no encoding, see if it's an alias
367 if (!key
.QueryValue(wxT("AliasForCharset"),cn
)) break;
377 wxCharacterSet(const wxChar
*name
) : cname(name
) {}
378 virtual ~wxCharacterSet() {}
379 virtual size_t MB2WC(wchar_t*buf
, const char*psz
, size_t n
) { return (size_t)-1; }
380 virtual size_t WC2MB(char*buf
, const wchar_t*psz
, size_t n
) { return (size_t)-1; }
381 virtual bool usable() { return FALSE
; }
384 class ID_CharSet
: public wxCharacterSet
388 ID_CharSet(const wxChar
*name
,wxMBConv
*cnv
) : wxCharacterSet(name
), work(cnv
) {}
389 size_t MB2WC(wchar_t*buf
, const char*psz
, size_t n
)
390 { return work
? work
->MB2WC(buf
,psz
,n
) : (size_t)-1; }
391 size_t WC2MB(char*buf
, const wchar_t*psz
, size_t n
)
392 { return work
? work
->WC2MB(buf
,psz
,n
) : (size_t)-1; }
393 bool usable() { return work
!=NULL
; }
397 class IC_CharSet
: public wxCharacterSet
401 IC_CharSet(const wxChar
*name
) : wxCharacterSet(name
), m2w((iconv_t
)-1), w2m((iconv_t
)-1) {}
403 if (m2w
!=(iconv_t
)-1) iconv_close(m2w
);
404 if (w2m
!=(iconv_t
)-1) iconv_close(w2m
);
406 void LoadM2W() { if (m2w
==(iconv_t
)-1) m2w
=iconv_open(WC_NAME
,wxConvLibc
.cWX2MB(cname
)); }
407 void LoadW2M() { if (w2m
==(iconv_t
)-1) w2m
=iconv_open(wxConvLibc
.cWX2MB(cname
),WC_NAME
); }
408 size_t MB2WC(wchar_t*buf
, const char*psz
, size_t n
) {
410 size_t inbuf
= strlen(psz
);
411 size_t outbuf
= n
*SIZEOF_WCHAR_T
;
413 fprintf(stderr
,"IC Convert to WC using %s\n",(const char*)wxConvLibc
.cWX2MB(cname
));
415 // have destination buffer, convert there
416 cres
= iconv(m2w
,&psz
,&inbuf
,(char**)&buf
,&outbuf
);
417 res
= n
-(outbuf
/SIZEOF_WCHAR_T
);
418 // convert to native endianness
421 // no destination buffer... convert using temp buffer
422 // to calculate destination buffer requirement
426 buf
= tbuf
; outbuf
= 8*SIZEOF_WCHAR_T
;
427 cres
= iconv(m2w
,&psz
,&inbuf
,(char**)&buf
,&outbuf
);
428 res
+= 8-(outbuf
/SIZEOF_WCHAR_T
);
429 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
431 if (cres
==(size_t)-1) return (size_t)-1;
434 size_t WC2MB(char*buf
, const wchar_t*psz
, size_t n
) {
436 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
437 size_t inbuf
= std::wcslen(psz
);
439 size_t inbuf
= ::wcslen(psz
);
443 fprintf(stderr
,"IC Convert from WC using %s\n",(const char*)wxConvLibc
.cWX2MB(cname
));
445 // need to copy to temp buffer to switch endianness
446 // this absolutely doesn't rock!
447 // (no, doing WC_BSWAP twice on the original buffer won't help, as it
448 // could be in read-only memory, or be accessed in some other thread)
449 wchar_t*tmpbuf
=(wchar_t*)malloc((inbuf
+1)*SIZEOF_WCHAR_T
);
450 memcpy(tmpbuf
,psz
,(inbuf
+1)*SIZEOF_WCHAR_T
);
451 WC_BSWAP(tmpbuf
, inbuf
)
455 // have destination buffer, convert there
456 cres
= iconv(w2m
,(const char**)&psz
,&inbuf
,&buf
,&outbuf
);
459 // no destination buffer... convert using temp buffer
460 // to calculate destination buffer requirement
464 buf
= tbuf
; outbuf
= 16;
465 cres
= iconv(w2m
,(const char**)&psz
,&inbuf
,&buf
,&outbuf
);
467 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
472 if (cres
==(size_t)-1) return (size_t)-1;
475 bool usable() { return TRUE
; }
480 class CP_CharSet
: public wxCharacterSet
484 CP_CharSet(const wxChar
*name
) : wxCharacterSet(name
), CodePage(CharsetToCodepage(name
)) {}
485 size_t MB2WC(wchar_t*buf
, const char*psz
, size_t n
) {
486 size_t len
= MultiByteToWideChar(CodePage
,0,psz
,-1,buf
,buf
?n
:0);
487 return len
?len
:(size_t)-1;
489 size_t WC2MB(char*buf
, const wchar_t*psz
, size_t n
) {
490 size_t len
= WideCharToMultiByte(CodePage
,0,psz
,-1,buf
,buf
?n
:0,NULL
,NULL
);
491 return len
?len
:(size_t)-1;
493 bool usable() { return CodePage
!=-1; }
497 class EC_CharSet
: public wxCharacterSet
500 // temporarily just use wxEncodingConverter stuff,
501 // so that it works while a better implementation is built
503 wxEncodingConverter m2w
, w2m
;
504 EC_CharSet(const wxChar
*name
) : wxCharacterSet(name
), enc(wxFONTENCODING_SYSTEM
)
506 if (name
) enc
= wxTheFontMapper
->CharsetToEncoding(name
, FALSE
);
507 m2w
.Init(enc
, wxFONTENCODING_UNICODE
);
508 w2m
.Init(wxFONTENCODING_UNICODE
, enc
);
510 size_t MB2WC(wchar_t*buf
, const char*psz
, size_t n
) {
511 size_t inbuf
= strlen(psz
);
512 fprintf(stderr
,"EC Convert to WC using %d\n",enc
);
513 if (buf
) m2w
.Convert(psz
,buf
);
516 size_t WC2MB(char*buf
, const wchar_t*psz
, size_t n
) {
517 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
518 size_t inbuf
= std::wcslen(psz
);
520 size_t inbuf
= ::wcslen(psz
);
522 fprintf(stderr
,"EC Convert from WC using %d\n",enc
);
523 if (buf
) w2m
.Convert(psz
,buf
);
526 bool usable() { return (enc
!=wxFONTENCODING_SYSTEM
) && (enc
!=wxFONTENCODING_DEFAULT
); }
529 static wxCharacterSet
*wxGetCharacterSet(const wxChar
*name
)
531 wxCharacterSet
*cset
= NULL
;
533 if (!wxStricmp(name
, wxT("UTF8")) || !wxStricmp(name
, wxT("UTF-8"))) {
534 cset
= new ID_CharSet(name
, &wxConvUTF8
);
537 cset
= new IC_CharSet(name
); // may not take NULL
541 if (cset
&& cset
->usable()) return cset
;
542 if (cset
) delete cset
;
544 cset
= new CP_CharSet(name
); // may take NULL
545 if (cset
->usable()) return cset
;
547 if (cset
) delete cset
;
548 cset
= new EC_CharSet(name
);
549 if (cset
->usable()) return cset
;
554 wxCSConv::wxCSConv(const wxChar
*charset
)
556 m_name
= (wxChar
*) NULL
;
557 m_cset
= (wxCharacterSet
*) NULL
;
562 wxCSConv::~wxCSConv()
564 if (m_name
) free(m_name
);
565 if (m_cset
) delete m_cset
;
568 void wxCSConv::SetName(const wxChar
*charset
)
571 m_name
= wxStrdup(charset
);
576 void wxCSConv::LoadNow()
578 // wxPrintf(wxT("Conversion request\n"));
582 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
583 // GNU libc provides current character set this way
584 char*alang
= nl_langinfo(CODESET
);
585 if (alang
) SetName(wxConvLibc
.cMB2WX(alang
));
588 // if we can't get at the character set directly,
589 // try to see if it's in the environment variables
590 // (in most cases this won't work, but I was out of ideas)
592 wxChar
*lang
= wxGetenv(wxT("LC_ALL"));
593 if (!lang
) lang
= wxGetenv(wxT("LC_CTYPE"));
594 if (!lang
) lang
= wxGetenv(wxT("LANG"));
595 wxChar
*dot
= lang
? wxStrchr(lang
, wxT('.')) : (wxChar
*)NULL
;
596 if (dot
) SetName(dot
+1);
600 m_cset
= wxGetCharacterSet(m_name
);
605 size_t wxCSConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
607 ((wxCSConv
*)this)->LoadNow(); // discard constness
609 return m_cset
->MB2WC(buf
, psz
, n
);
612 size_t len
=strlen(psz
);
614 for (size_t c
=0; c
<=len
; c
++)
615 buf
[c
] = (unsigned char)(psz
[c
]);
620 size_t wxCSConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
622 ((wxCSConv
*)this)->LoadNow(); // discard constness
624 return m_cset
->WC2MB(buf
, psz
, n
);
627 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
628 size_t len
=std::wcslen(psz
);
630 size_t len
=::wcslen(psz
);
633 for (size_t c
=0; c
<=len
; c
++)
634 buf
[c
] = (psz
[c
]>0xff) ? '?' : psz
[c
];
640 class IC_CharSetConverter
644 IC_CharSetConverter(IC_CharSet
*from
,IC_CharSet
*to
) {
645 cnv
=iconv_open(wxConvLibc
.cWX2MB(to
->cname
),wxConvLibc
.cWX2MB(from
->cname
));
647 ~IC_CharSetConverter() {
648 if (cnv
!=(iconv_t
)-1) iconv_close(cnv
);
650 size_t Convert(char*buf
, const char*psz
, size_t n
) {
651 size_t inbuf
= strlen(psz
);
653 size_t res
= iconv(cnv
,&psz
,&inbuf
,&buf
,&outbuf
);
654 if (res
==(size_t)-1) return (size_t)-1;
660 class EC_CharSetConverter
663 wxEncodingConverter cnv
;
664 EC_CharSetConverter(EC_CharSet
*from
,EC_CharSet
*to
) {
665 cnv
.Init(from
->enc
,to
->enc
);
667 size_t Convert(char*buf
, const char*psz
, size_t n
) {
668 size_t inbuf
= strlen(psz
);
669 if (buf
) cnv
.Convert(psz
,buf
);
674 #else // !wxUSE_WCHAR_T
676 // ----------------------------------------------------------------------------
677 // stand-ins in absence of wchar_t
678 // ----------------------------------------------------------------------------
680 WXDLLEXPORT_DATA(wxMBConv
) wxConvLibc
, wxConvFile
;
682 #endif // wxUSE_WCHAR_T