]>
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"
58 #if defined(WORDS_BIGENDIAN) || defined(__STDC_ISO_10646__)
59 #define BSWAP_UCS4(str, len)
60 #define BSWAP_UCS2(str, len)
62 #define BSWAP_UCS4(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT32_SWAP_ALWAYS(str[_c]); }
63 #define BSWAP_UCS2(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT16_SWAP_ALWAYS(str[_c]); }
66 #define BSWAP_UTF32(str, len) BSWAP_UCS4(str, len)
67 #define BSWAP_UTF16(str, len) BSWAP_UCS2(str, len)
69 #if SIZEOF_WCHAR_T == 4
70 #define WC_NAME "UCS4"
71 #define WC_BSWAP BSWAP_UCS4
72 #elif SIZEOF_WCHAR_T == 2
73 #define WC_NAME "UTF16"
74 #define WC_BSWAP BSWAP_UTF16
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 WXDLLEXPORT_DATA(wxMBConv
*) wxConvCurrent
= &wxConvLibc
;
84 // ============================================================================
86 // ============================================================================
92 static size_t encode_utf16(wxUint32 input
,wxUint16
*output
)
95 if (output
) *output
++ = input
;
98 if (input
>=0x110000) {
102 *output
++ = (input
>> 10)+0xd7c0;
103 *output
++ = (input
&0x3ff)+0xdc00;
109 static size_t decode_utf16(wxUint16
*input
,wxUint32
&output
)
111 if ((*input
<0xd800) || (*input
>0xdfff)) {
115 if ((input
[1]<0xdc00) || (input
[1]>=0xdfff)) {
119 output
= ((input
[0] - 0xd7c0) << 10) + (input
[1] - 0xdc00);
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 WXDLLEXPORT_DATA(wxMBConv
) wxConvLibc
;
132 size_t wxMBConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
134 return wxMB2WC(buf
, psz
, n
);
137 size_t wxMBConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
139 return wxWC2MB(buf
, psz
, n
);
142 const wxWCharBuffer
wxMBConv::cMB2WC(const char *psz
) const
146 size_t nLen
= MB2WC((wchar_t *) NULL
, psz
, 0);
147 if (nLen
== (size_t)-1)
148 return wxWCharBuffer((wchar_t *) NULL
);
149 wxWCharBuffer
buf(nLen
);
150 MB2WC((wchar_t *)(const wchar_t *) buf
, psz
, nLen
);
154 return wxWCharBuffer((wchar_t *) NULL
);
157 const wxCharBuffer
wxMBConv::cWC2MB(const wchar_t *psz
) const
161 size_t nLen
= WC2MB((char *) NULL
, psz
, 0);
162 if (nLen
== (size_t)-1)
163 return wxCharBuffer((char *) NULL
);
164 wxCharBuffer
buf(nLen
);
165 WC2MB((char *)(const char *) buf
, psz
, nLen
);
169 return wxCharBuffer((char *) NULL
);
172 // ----------------------------------------------------------------------------
173 // standard file conversion
174 // ----------------------------------------------------------------------------
176 WXDLLEXPORT_DATA(wxMBConvFile
) wxConvFile
;
178 // just use the libc conversion for now
179 size_t wxMBConvFile::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
181 return wxMB2WC(buf
, psz
, n
);
184 size_t wxMBConvFile::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
186 return wxWC2MB(buf
, psz
, n
);
189 // ----------------------------------------------------------------------------
190 // standard gdk conversion
191 // ----------------------------------------------------------------------------
195 WXDLLEXPORT_DATA(wxMBConvGdk
) wxConvGdk
;
199 size_t wxMBConvGdk::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
202 return gdk_mbstowcs((GdkWChar
*)buf
, psz
, n
);
204 GdkWChar
*nbuf
= new GdkWChar
[n
=strlen(psz
)];
205 size_t len
= gdk_mbstowcs(nbuf
, psz
, n
);
211 size_t wxMBConvGdk::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
213 char *mbstr
= gdk_wcstombs((GdkWChar
*)psz
);
214 size_t len
= mbstr
? strlen(mbstr
) : 0;
216 if (len
> n
) len
= n
;
217 memcpy(buf
, psz
, len
);
218 if (len
< n
) buf
[len
] = 0;
225 // ----------------------------------------------------------------------------
227 // ----------------------------------------------------------------------------
229 WXDLLEXPORT_DATA(wxMBConvUTF7
) wxConvUTF7
;
232 static char utf7_setD
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
233 "abcdefghijklmnopqrstuvwxyz"
234 "0123456789'(),-./:?";
235 static char utf7_setO
[]="!\"#$%&*;<=>@[]^_`{|}";
236 static char utf7_setB
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
237 "abcdefghijklmnopqrstuvwxyz"
241 // TODO: write actual implementations of UTF-7 here
242 size_t wxMBConvUTF7::MB2WC(wchar_t * WXUNUSED(buf
),
243 const char * WXUNUSED(psz
),
244 size_t WXUNUSED(n
)) const
249 size_t wxMBConvUTF7::WC2MB(char * WXUNUSED(buf
),
250 const wchar_t * WXUNUSED(psz
),
251 size_t WXUNUSED(n
)) const
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
260 WXDLLEXPORT_DATA(wxMBConvUTF8
) wxConvUTF8
;
262 static wxUint32 utf8_max
[]={0x7f,0x7ff,0xffff,0x1fffff,0x3ffffff,0x7fffffff,0xffffffff};
264 size_t wxMBConvUTF8::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
268 while (*psz
&& ((!buf
) || (len
<n
))) {
269 unsigned char cc
=*psz
++, fc
=cc
;
271 for (cnt
=0; fc
&0x80; cnt
++) fc
<<=1;
279 // invalid UTF-8 sequence
283 wxUint32 res
=cc
&(0x3f>>cnt
);
286 if ((cc
&0xC0)!=0x80) {
287 // invalid UTF-8 sequence
290 res
=(res
<<6)|(cc
&0x3f);
292 if (res
<=utf8_max
[ocnt
]) {
293 // illegal UTF-8 encoding
297 size_t pa
= encode_utf16(res
, buf
);
298 if (pa
== (size_t)-1)
309 if (buf
&& (len
<n
)) *buf
= 0;
313 size_t wxMBConvUTF8::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
317 while (*psz
&& ((!buf
) || (len
<n
))) {
320 size_t pa
= decode_utf16(psz
,cc
);
321 psz
+= (pa
== (size_t)-1) ? 1 : pa
;
323 cc
=(*psz
++)&0x7fffffff;
326 for (cnt
=0; cc
>utf8_max
[cnt
]; cnt
++);
334 *buf
++=(-128>>cnt
)|((cc
>>(cnt
*6))&(0x3f>>cnt
));
336 *buf
++=0x80|((cc
>>(cnt
*6))&0x3f);
340 if (buf
&& (len
<n
)) *buf
= 0;
344 // ----------------------------------------------------------------------------
345 // specified character set
346 // ----------------------------------------------------------------------------
348 WXDLLEXPORT_DATA(wxCSConv
) wxConvLocal((const wxChar
*)NULL
);
350 #include "wx/encconv.h"
351 #include "wx/fontmap.h"
353 // TODO: add some tables here
354 // - perhaps common encodings to common codepages (for Win32)
355 // - perhaps common encodings to objects ("UTF8" -> wxConvUTF8)
356 // - move wxEncodingConverter meat in here
359 #include "wx/msw/registry.h"
360 // this should work if M$ Internet Exploiter is installed
361 static long CharsetToCodepage(const wxChar
*name
)
363 if (!name
) return GetACP();
367 wxString
path(wxT("MIME\\Database\\Charset\\"));
369 wxRegKey
key(wxRegKey::HKCR
,path
);
371 /* two cases: either there's an AliasForCharset string,
372 * or there are Codepage and InternetEncoding dwords.
373 * The InternetEncoding gives us the actual encoding,
374 * the Codepage just says which Windows character set to
375 * use when displaying the data.
377 if (key
.QueryValue(wxT("InternetEncoding"),&CP
)) break;
378 // no encoding, see if it's an alias
379 if (!key
.QueryValue(wxT("AliasForCharset"),cn
)) break;
389 wxCharacterSet(const wxChar
*name
) : cname(name
) {}
390 virtual ~wxCharacterSet() {}
391 virtual size_t MB2WC(wchar_t*buf
, const char*psz
, size_t n
) { return (size_t)-1; }
392 virtual size_t WC2MB(char*buf
, const wchar_t*psz
, size_t n
) { return (size_t)-1; }
393 virtual bool usable() { return FALSE
; }
396 class ID_CharSet
: public wxCharacterSet
400 ID_CharSet(const wxChar
*name
,wxMBConv
*cnv
) : wxCharacterSet(name
), work(cnv
) {}
401 size_t MB2WC(wchar_t*buf
, const char*psz
, size_t n
)
402 { return work
? work
->MB2WC(buf
,psz
,n
) : (size_t)-1; }
403 size_t WC2MB(char*buf
, const wchar_t*psz
, size_t n
)
404 { return work
? work
->WC2MB(buf
,psz
,n
) : (size_t)-1; }
405 bool usable() { return work
!=NULL
; }
409 class IC_CharSet
: public wxCharacterSet
413 IC_CharSet(const wxChar
*name
) : wxCharacterSet(name
), m2w((iconv_t
)-1), w2m((iconv_t
)-1) {}
415 if (m2w
!=(iconv_t
)-1) iconv_close(m2w
);
416 if (w2m
!=(iconv_t
)-1) iconv_close(w2m
);
418 void LoadM2W() { if (m2w
==(iconv_t
)-1) m2w
=iconv_open(WC_NAME
,wxConvLibc
.cWX2MB(cname
)); }
419 void LoadW2M() { if (w2m
==(iconv_t
)-1) w2m
=iconv_open(wxConvLibc
.cWX2MB(cname
),WC_NAME
); }
420 size_t MB2WC(wchar_t*buf
, const char*psz
, size_t n
) {
422 size_t inbuf
= strlen(psz
);
423 size_t outbuf
= n
*SIZEOF_WCHAR_T
;
425 fprintf(stderr
,"IC Convert to WC using %s\n",(const char*)wxConvLibc
.cWX2MB(cname
));
427 // have destination buffer, convert there
428 cres
= iconv(m2w
,&psz
,&inbuf
,(char**)&buf
,&outbuf
);
429 res
= n
-(outbuf
/SIZEOF_WCHAR_T
);
430 // convert to native endianness
433 // no destination buffer... convert using temp buffer
434 // to calculate destination buffer requirement
438 buf
= tbuf
; outbuf
= 8*SIZEOF_WCHAR_T
;
439 cres
= iconv(m2w
,&psz
,&inbuf
,(char**)&buf
,&outbuf
);
440 res
+= 8-(outbuf
/SIZEOF_WCHAR_T
);
441 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
443 if (cres
==(size_t)-1) return (size_t)-1;
446 size_t WC2MB(char*buf
, const wchar_t*psz
, size_t n
) {
448 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
449 size_t inbuf
= std::wcslen(psz
);
451 size_t inbuf
= ::wcslen(psz
);
455 fprintf(stderr
,"IC Convert from WC using %s\n",(const char*)wxConvLibc
.cWX2MB(cname
));
457 // need to copy to temp buffer to switch endianness
458 // this absolutely doesn't rock!
459 // (no, doing WC_BSWAP twice on the original buffer won't help, as it
460 // could be in read-only memory, or be accessed in some other thread)
461 wchar_t*tmpbuf
=(wchar_t*)malloc((inbuf
+1)*SIZEOF_WCHAR_T
);
462 memcpy(tmpbuf
,psz
,(inbuf
+1)*SIZEOF_WCHAR_T
);
463 WC_BSWAP(tmpbuf
, inbuf
)
467 // have destination buffer, convert there
468 cres
= iconv(w2m
,(const char**)&psz
,&inbuf
,&buf
,&outbuf
);
471 // no destination buffer... convert using temp buffer
472 // to calculate destination buffer requirement
476 buf
= tbuf
; outbuf
= 16;
477 cres
= iconv(w2m
,(const char**)&psz
,&inbuf
,&buf
,&outbuf
);
479 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
484 if (cres
==(size_t)-1) return (size_t)-1;
487 bool usable() { return TRUE
; }
492 class CP_CharSet
: public wxCharacterSet
496 CP_CharSet(const wxChar
*name
) : wxCharacterSet(name
), CodePage(CharsetToCodepage(name
)) {}
497 size_t MB2WC(wchar_t*buf
, const char*psz
, size_t n
) {
498 size_t len
= MultiByteToWideChar(CodePage
,0,psz
,-1,buf
,buf
?n
:0);
499 return len
?len
:(size_t)-1;
501 size_t WC2MB(char*buf
, const wchar_t*psz
, size_t n
) {
502 size_t len
= WideCharToMultiByte(CodePage
,0,psz
,-1,buf
,buf
?n
:0,NULL
,NULL
);
503 return len
?len
:(size_t)-1;
505 bool usable() { return CodePage
!=-1; }
509 class EC_CharSet
: public wxCharacterSet
512 // temporarily just use wxEncodingConverter stuff,
513 // so that it works while a better implementation is built
515 wxEncodingConverter m2w
, w2m
;
516 EC_CharSet(const wxChar
*name
) : wxCharacterSet(name
), enc(wxFONTENCODING_SYSTEM
)
518 if (name
) enc
= wxTheFontMapper
->CharsetToEncoding(name
, FALSE
);
519 m2w
.Init(enc
, wxFONTENCODING_UNICODE
);
520 w2m
.Init(wxFONTENCODING_UNICODE
, enc
);
522 size_t MB2WC(wchar_t*buf
, const char*psz
, size_t n
) {
523 size_t inbuf
= strlen(psz
);
524 fprintf(stderr
,"EC Convert to WC using %d\n",enc
);
525 if (buf
) m2w
.Convert(psz
,buf
);
528 size_t WC2MB(char*buf
, const wchar_t*psz
, size_t n
) {
529 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
530 size_t inbuf
= std::wcslen(psz
);
532 size_t inbuf
= ::wcslen(psz
);
534 fprintf(stderr
,"EC Convert from WC using %d\n",enc
);
535 if (buf
) w2m
.Convert(psz
,buf
);
538 bool usable() { return (enc
!=wxFONTENCODING_SYSTEM
) && (enc
!=wxFONTENCODING_DEFAULT
); }
541 static wxCharacterSet
*wxGetCharacterSet(const wxChar
*name
)
543 wxCharacterSet
*cset
= NULL
;
545 if (!wxStricmp(name
, wxT("UTF8")) || !wxStricmp(name
, wxT("UTF-8"))) {
546 cset
= new ID_CharSet(name
, &wxConvUTF8
);
549 cset
= new IC_CharSet(name
); // may not take NULL
553 if (cset
&& cset
->usable()) return cset
;
554 if (cset
) delete cset
;
556 cset
= new CP_CharSet(name
); // may take NULL
557 if (cset
->usable()) return cset
;
559 if (cset
) delete cset
;
560 cset
= new EC_CharSet(name
);
561 if (cset
->usable()) return cset
;
566 wxCSConv::wxCSConv(const wxChar
*charset
)
568 m_name
= (wxChar
*) NULL
;
569 m_cset
= (wxCharacterSet
*) NULL
;
574 wxCSConv::~wxCSConv()
576 if (m_name
) free(m_name
);
577 if (m_cset
) delete m_cset
;
580 void wxCSConv::SetName(const wxChar
*charset
)
583 m_name
= wxStrdup(charset
);
588 void wxCSConv::LoadNow()
590 // wxPrintf(wxT("Conversion request\n"));
594 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
595 // GNU libc provides current character set this way
596 char*alang
= nl_langinfo(CODESET
);
597 if (alang
) SetName(wxConvLibc
.cMB2WX(alang
));
600 // if we can't get at the character set directly,
601 // try to see if it's in the environment variables
602 // (in most cases this won't work, but I was out of ideas)
604 wxChar
*lang
= wxGetenv(wxT("LC_ALL"));
605 if (!lang
) lang
= wxGetenv(wxT("LC_CTYPE"));
606 if (!lang
) lang
= wxGetenv(wxT("LANG"));
607 wxChar
*dot
= lang
? wxStrchr(lang
, wxT('.')) : (wxChar
*)NULL
;
608 if (dot
) SetName(dot
+1);
612 m_cset
= wxGetCharacterSet(m_name
);
617 size_t wxCSConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
619 ((wxCSConv
*)this)->LoadNow(); // discard constness
621 return m_cset
->MB2WC(buf
, psz
, n
);
624 size_t len
=strlen(psz
);
626 for (size_t c
=0; c
<=len
; c
++)
627 buf
[c
] = (unsigned char)(psz
[c
]);
632 size_t wxCSConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
634 ((wxCSConv
*)this)->LoadNow(); // discard constness
636 return m_cset
->WC2MB(buf
, psz
, n
);
639 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
640 size_t len
=std::wcslen(psz
);
642 size_t len
=::wcslen(psz
);
645 for (size_t c
=0; c
<=len
; c
++)
646 buf
[c
] = (psz
[c
]>0xff) ? '?' : psz
[c
];
652 class IC_CharSetConverter
656 IC_CharSetConverter(IC_CharSet
*from
,IC_CharSet
*to
) {
657 cnv
=iconv_open(wxConvLibc
.cWX2MB(to
->cname
),wxConvLibc
.cWX2MB(from
->cname
));
659 ~IC_CharSetConverter() {
660 if (cnv
!=(iconv_t
)-1) iconv_close(cnv
);
662 size_t Convert(char*buf
, const char*psz
, size_t n
) {
663 size_t inbuf
= strlen(psz
);
665 size_t res
= iconv(cnv
,&psz
,&inbuf
,&buf
,&outbuf
);
666 if (res
==(size_t)-1) return (size_t)-1;
672 class EC_CharSetConverter
675 wxEncodingConverter cnv
;
676 EC_CharSetConverter(EC_CharSet
*from
,EC_CharSet
*to
) {
677 cnv
.Init(from
->enc
,to
->enc
);
679 size_t Convert(char*buf
, const char*psz
, size_t n
) {
680 size_t inbuf
= strlen(psz
);
681 if (buf
) cnv
.Convert(psz
,buf
);
686 #else // !wxUSE_WCHAR_T
688 // ----------------------------------------------------------------------------
689 // stand-ins in absence of wchar_t
690 // ----------------------------------------------------------------------------
692 WXDLLEXPORT_DATA(wxMBConv
) wxConvLibc
, wxConvFile
;
694 #endif // wxUSE_WCHAR_T