]>
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 #ifdef WORDS_BIGENDIAN
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 // ============================================================================
90 static size_t encode_utf16(wxUint32 input
,wxUint16
*output
)
93 if (output
) *output
++ = input
;
96 if (input
>=0x110000) {
100 *output
++ = (input
>> 10)+0xd7c0;
101 *output
++ = (input
&0x3ff)+0xdc00;
107 static size_t decode_utf16(wxUint16
*input
,wxUint32
&output
)
109 if ((*input
<0xd800) || (*input
>0xdfff)) {
113 if ((input
[1]<0xdc00) || (input
[1]>=0xdfff)) {
117 output
= ((input
[0] - 0xd7c0) << 10) + (input
[1] - 0xdc00);
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 WXDLLEXPORT_DATA(wxMBConv
) wxConvLibc
;
128 size_t wxMBConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
130 return wxMB2WC(buf
, psz
, n
);
133 size_t wxMBConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
135 return wxWC2MB(buf
, psz
, n
);
138 const wxWCharBuffer
wxMBConv::cMB2WC(const char *psz
) const
142 size_t nLen
= MB2WC((wchar_t *) NULL
, psz
, 0);
143 if (nLen
== (size_t)-1)
144 return wxWCharBuffer((wchar_t *) NULL
);
145 wxWCharBuffer
buf(nLen
);
146 MB2WC((wchar_t *)(const wchar_t *) buf
, psz
, nLen
);
150 return wxWCharBuffer((wchar_t *) NULL
);
153 const wxCharBuffer
wxMBConv::cWC2MB(const wchar_t *psz
) const
157 size_t nLen
= WC2MB((char *) NULL
, psz
, 0);
158 if (nLen
== (size_t)-1)
159 return wxCharBuffer((char *) NULL
);
160 wxCharBuffer
buf(nLen
);
161 WC2MB((char *)(const char *) buf
, psz
, nLen
);
165 return wxCharBuffer((char *) NULL
);
168 // ----------------------------------------------------------------------------
169 // standard file conversion
170 // ----------------------------------------------------------------------------
172 WXDLLEXPORT_DATA(wxMBConvFile
) wxConvFile
;
174 // just use the libc conversion for now
175 size_t wxMBConvFile::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
177 return wxMB2WC(buf
, psz
, n
);
180 size_t wxMBConvFile::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
182 return wxWC2MB(buf
, psz
, n
);
185 // ----------------------------------------------------------------------------
186 // standard gdk conversion
187 // ----------------------------------------------------------------------------
191 WXDLLEXPORT_DATA(wxMBConvGdk
) wxConvGdk
;
195 size_t wxMBConvGdk::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
198 return gdk_mbstowcs((GdkWChar
*)buf
, psz
, n
);
200 GdkWChar
*nbuf
= new GdkWChar
[n
=strlen(psz
)];
201 size_t len
= gdk_mbstowcs(nbuf
, psz
, n
);
207 size_t wxMBConvGdk::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
209 char *mbstr
= gdk_wcstombs((GdkWChar
*)psz
);
210 size_t len
= mbstr
? strlen(mbstr
) : 0;
212 if (len
> n
) len
= n
;
213 memcpy(buf
, psz
, len
);
214 if (len
< n
) buf
[len
] = 0;
221 // ----------------------------------------------------------------------------
223 // ----------------------------------------------------------------------------
225 WXDLLEXPORT_DATA(wxMBConvUTF7
) wxConvUTF7
;
228 static char utf7_setD
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
229 "abcdefghijklmnopqrstuvwxyz"
230 "0123456789'(),-./:?";
231 static char utf7_setO
[]="!\"#$%&*;<=>@[]^_`{|}";
232 static char utf7_setB
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
233 "abcdefghijklmnopqrstuvwxyz"
237 // TODO: write actual implementations of UTF-7 here
238 size_t wxMBConvUTF7::MB2WC(wchar_t * WXUNUSED(buf
),
239 const char * WXUNUSED(psz
),
240 size_t WXUNUSED(n
)) const
245 size_t wxMBConvUTF7::WC2MB(char * WXUNUSED(buf
),
246 const wchar_t * WXUNUSED(psz
),
247 size_t WXUNUSED(n
)) const
252 // ----------------------------------------------------------------------------
254 // ----------------------------------------------------------------------------
256 WXDLLEXPORT_DATA(wxMBConvUTF8
) wxConvUTF8
;
258 static wxUint32 utf8_max
[]={0x7f,0x7ff,0xffff,0x1fffff,0x3ffffff,0x7fffffff,0xffffffff};
260 size_t wxMBConvUTF8::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
264 while (*psz
&& ((!buf
) || (len
<n
))) {
265 unsigned char cc
=*psz
++, fc
=cc
;
267 for (cnt
=0; fc
&0x80; cnt
++) fc
<<=1;
275 // invalid UTF-8 sequence
279 wxUint32 res
=cc
&(0x3f>>cnt
);
282 if ((cc
&0xC0)!=0x80) {
283 // invalid UTF-8 sequence
286 res
=(res
<<6)|(cc
&0x3f);
288 if (res
<=utf8_max
[ocnt
]) {
289 // illegal UTF-8 encoding
293 size_t pa
= encode_utf16(res
, buf
);
294 if (pa
== (size_t)-1)
305 if (buf
&& (len
<n
)) *buf
= 0;
309 size_t wxMBConvUTF8::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
313 while (*psz
&& ((!buf
) || (len
<n
))) {
316 size_t pa
= decode_utf16(psz
,cc
);
317 psz
+= (pa
== (size_t)-1) ? 1 : pa
;
319 cc
=(*psz
++)&0x7fffffff;
322 for (cnt
=0; cc
>utf8_max
[cnt
]; cnt
++);
330 *buf
++=(-128>>cnt
)|((cc
>>(cnt
*6))&(0x3f>>cnt
));
332 *buf
++=0x80|((cc
>>(cnt
*6))&0x3f);
336 if (buf
&& (len
<n
)) *buf
= 0;
340 // ----------------------------------------------------------------------------
341 // specified character set
342 // ----------------------------------------------------------------------------
344 WXDLLEXPORT_DATA(wxCSConv
) wxConvLocal((const wxChar
*)NULL
);
346 #include "wx/encconv.h"
347 #include "wx/fontmap.h"
349 // TODO: add some tables here
350 // - perhaps common encodings to common codepages (for Win32)
351 // - perhaps common encodings to objects ("UTF8" -> wxConvUTF8)
352 // - move wxEncodingConverter meat in here
355 #include "wx/msw/registry.h"
356 // this should work if M$ Internet Exploiter is installed
357 static long CharsetToCodepage(const wxChar
*name
)
359 if (!name
) return GetACP();
363 wxString
path(wxT("MIME\\Database\\Charset\\"));
365 wxRegKey
key(wxRegKey::HKCR
,path
);
367 /* two cases: either there's an AliasForCharset string,
368 * or there are Codepage and InternetEncoding dwords.
369 * The InternetEncoding gives us the actual encoding,
370 * the Codepage just says which Windows character set to
371 * use when displaying the data.
373 if (key
.QueryValue(wxT("InternetEncoding"),&CP
)) break;
374 // no encoding, see if it's an alias
375 if (!key
.QueryValue(wxT("AliasForCharset"),cn
)) break;
385 wxCharacterSet(const wxChar
*name
) : cname(name
) {}
386 virtual ~wxCharacterSet() {}
387 virtual size_t MB2WC(wchar_t*buf
, const char*psz
, size_t n
) { return (size_t)-1; }
388 virtual size_t WC2MB(char*buf
, const wchar_t*psz
, size_t n
) { return (size_t)-1; }
389 virtual bool usable() { return FALSE
; }
392 class ID_CharSet
: public wxCharacterSet
396 ID_CharSet(const wxChar
*name
,wxMBConv
*cnv
) : wxCharacterSet(name
), work(cnv
) {}
397 size_t MB2WC(wchar_t*buf
, const char*psz
, size_t n
)
398 { return work
? work
->MB2WC(buf
,psz
,n
) : (size_t)-1; }
399 size_t WC2MB(char*buf
, const wchar_t*psz
, size_t n
)
400 { return work
? work
->WC2MB(buf
,psz
,n
) : (size_t)-1; }
401 bool usable() { return work
!=NULL
; }
405 class IC_CharSet
: public wxCharacterSet
409 IC_CharSet(const wxChar
*name
) : wxCharacterSet(name
), m2w((iconv_t
)-1), w2m((iconv_t
)-1) {}
411 if (m2w
!=(iconv_t
)-1) iconv_close(m2w
);
412 if (w2m
!=(iconv_t
)-1) iconv_close(w2m
);
414 void LoadM2W() { if (m2w
==(iconv_t
)-1) m2w
=iconv_open(WC_NAME
,wxConvLibc
.cWX2MB(cname
)); }
415 void LoadW2M() { if (w2m
==(iconv_t
)-1) w2m
=iconv_open(wxConvLibc
.cWX2MB(cname
),WC_NAME
); }
416 size_t MB2WC(wchar_t*buf
, const char*psz
, size_t n
) {
418 size_t inbuf
= strlen(psz
);
419 size_t outbuf
= n
*SIZEOF_WCHAR_T
;
421 fprintf(stderr
,"IC Convert to WC using %s\n",(const char*)wxConvLibc
.cWX2MB(cname
));
423 // have destination buffer, convert there
424 cres
= iconv(m2w
,&psz
,&inbuf
,(char**)&buf
,&outbuf
);
425 res
= n
-(outbuf
/SIZEOF_WCHAR_T
);
426 // convert to native endianness
429 // no destination buffer... convert using temp buffer
430 // to calculate destination buffer requirement
434 buf
= tbuf
; outbuf
= 8*SIZEOF_WCHAR_T
;
435 cres
= iconv(m2w
,&psz
,&inbuf
,(char**)&buf
,&outbuf
);
436 res
+= 8-(outbuf
/SIZEOF_WCHAR_T
);
437 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
439 if (cres
==(size_t)-1) return (size_t)-1;
442 size_t WC2MB(char*buf
, const wchar_t*psz
, size_t n
) {
444 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
445 size_t inbuf
= std::wcslen(psz
);
447 size_t inbuf
= ::wcslen(psz
);
451 fprintf(stderr
,"IC Convert from WC using %s\n",(const char*)wxConvLibc
.cWX2MB(cname
));
453 // need to copy to temp buffer to switch endianness
454 // this absolutely doesn't rock!
455 // (no, doing WC_BSWAP twice on the original buffer won't help, as it
456 // could be in read-only memory, or be accessed in some other thread)
457 wchar_t*tmpbuf
=(wchar_t*)malloc((inbuf
+1)*SIZEOF_WCHAR_T
);
458 memcpy(tmpbuf
,psz
,(inbuf
+1)*SIZEOF_WCHAR_T
);
459 WC_BSWAP(tmpbuf
, inbuf
)
463 // have destination buffer, convert there
464 cres
= iconv(w2m
,(const char**)&psz
,&inbuf
,&buf
,&outbuf
);
467 // no destination buffer... convert using temp buffer
468 // to calculate destination buffer requirement
472 buf
= tbuf
; outbuf
= 16;
473 cres
= iconv(w2m
,(const char**)&psz
,&inbuf
,&buf
,&outbuf
);
475 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
480 if (cres
==(size_t)-1) return (size_t)-1;
483 bool usable() { return TRUE
; }
488 class CP_CharSet
: public wxCharacterSet
492 CP_CharSet(const wxChar
*name
) : wxCharacterSet(name
), CodePage(CharsetToCodepage(name
)) {}
493 size_t MB2WC(wchar_t*buf
, const char*psz
, size_t n
) {
494 size_t len
= MultiByteToWideChar(CodePage
,0,psz
,-1,buf
,buf
?n
:0);
495 return len
?len
:(size_t)-1;
497 size_t WC2MB(char*buf
, const wchar_t*psz
, size_t n
) {
498 size_t len
= WideCharToMultiByte(CodePage
,0,psz
,-1,buf
,buf
?n
:0,NULL
,NULL
);
499 return len
?len
:(size_t)-1;
501 bool usable() { return CodePage
!=-1; }
505 class EC_CharSet
: public wxCharacterSet
508 // temporarily just use wxEncodingConverter stuff,
509 // so that it works while a better implementation is built
511 wxEncodingConverter m2w
, w2m
;
512 EC_CharSet(const wxChar
*name
) : wxCharacterSet(name
), enc(wxFONTENCODING_SYSTEM
)
514 if (name
) enc
= wxTheFontMapper
->CharsetToEncoding(name
, FALSE
);
515 m2w
.Init(enc
, wxFONTENCODING_UNICODE
);
516 w2m
.Init(wxFONTENCODING_UNICODE
, enc
);
518 size_t MB2WC(wchar_t*buf
, const char*psz
, size_t n
) {
519 size_t inbuf
= strlen(psz
);
520 fprintf(stderr
,"EC Convert to WC using %d\n",enc
);
521 if (buf
) m2w
.Convert(psz
,buf
);
524 size_t WC2MB(char*buf
, const wchar_t*psz
, size_t n
) {
525 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
526 size_t inbuf
= std::wcslen(psz
);
528 size_t inbuf
= ::wcslen(psz
);
530 fprintf(stderr
,"EC Convert from WC using %d\n",enc
);
531 if (buf
) w2m
.Convert(psz
,buf
);
534 bool usable() { return (enc
!=wxFONTENCODING_SYSTEM
) && (enc
!=wxFONTENCODING_DEFAULT
); }
537 static wxCharacterSet
*wxGetCharacterSet(const wxChar
*name
)
539 wxCharacterSet
*cset
= NULL
;
541 if (!wxStricmp(name
, wxT("UTF8")) || !wxStricmp(name
, wxT("UTF-8"))) {
542 cset
= new ID_CharSet(name
, &wxConvUTF8
);
545 cset
= new IC_CharSet(name
); // may not take NULL
549 if (cset
&& cset
->usable()) return cset
;
550 if (cset
) delete cset
;
552 cset
= new CP_CharSet(name
); // may take NULL
553 if (cset
->usable()) return cset
;
555 if (cset
) delete cset
;
556 cset
= new EC_CharSet(name
);
557 if (cset
->usable()) return cset
;
562 wxCSConv::wxCSConv(const wxChar
*charset
)
564 m_name
= (wxChar
*) NULL
;
565 m_cset
= (wxCharacterSet
*) NULL
;
570 wxCSConv::~wxCSConv()
572 if (m_name
) free(m_name
);
573 if (m_cset
) delete m_cset
;
576 void wxCSConv::SetName(const wxChar
*charset
)
579 m_name
= wxStrdup(charset
);
584 void wxCSConv::LoadNow()
586 // wxPrintf(wxT("Conversion request\n"));
590 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
591 // GNU libc provides current character set this way
592 char*alang
= nl_langinfo(CODESET
);
593 if (alang
) SetName(wxConvLibc
.cMB2WX(alang
));
596 // if we can't get at the character set directly,
597 // try to see if it's in the environment variables
598 // (in most cases this won't work, but I was out of ideas)
600 wxChar
*lang
= wxGetenv(wxT("LC_ALL"));
601 if (!lang
) lang
= wxGetenv(wxT("LC_CTYPE"));
602 if (!lang
) lang
= wxGetenv(wxT("LANG"));
603 wxChar
*dot
= lang
? wxStrchr(lang
, wxT('.')) : (wxChar
*)NULL
;
604 if (dot
) SetName(dot
+1);
608 m_cset
= wxGetCharacterSet(m_name
);
613 size_t wxCSConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
615 ((wxCSConv
*)this)->LoadNow(); // discard constness
617 return m_cset
->MB2WC(buf
, psz
, n
);
620 size_t len
=strlen(psz
);
622 for (size_t c
=0; c
<=len
; c
++)
623 buf
[c
] = (unsigned char)(psz
[c
]);
628 size_t wxCSConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
630 ((wxCSConv
*)this)->LoadNow(); // discard constness
632 return m_cset
->WC2MB(buf
, psz
, n
);
635 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
636 size_t len
=std::wcslen(psz
);
638 size_t len
=::wcslen(psz
);
641 for (size_t c
=0; c
<=len
; c
++)
642 buf
[c
] = (psz
[c
]>0xff) ? '?' : psz
[c
];
648 class IC_CharSetConverter
652 IC_CharSetConverter(IC_CharSet
*from
,IC_CharSet
*to
) {
653 cnv
=iconv_open(wxConvLibc
.cWX2MB(to
->cname
),wxConvLibc
.cWX2MB(from
->cname
));
655 ~IC_CharSetConverter() {
656 if (cnv
!=(iconv_t
)-1) iconv_close(cnv
);
658 size_t Convert(char*buf
, const char*psz
, size_t n
) {
659 size_t inbuf
= strlen(psz
);
661 size_t res
= iconv(cnv
,&psz
,&inbuf
,&buf
,&outbuf
);
662 if (res
==(size_t)-1) return (size_t)-1;
668 class EC_CharSetConverter
671 wxEncodingConverter cnv
;
672 EC_CharSetConverter(EC_CharSet
*from
,EC_CharSet
*to
) {
673 cnv
.Init(from
->enc
,to
->enc
);
675 size_t Convert(char*buf
, const char*psz
, size_t n
) {
676 size_t inbuf
= strlen(psz
);
677 if (buf
) cnv
.Convert(psz
,buf
);
682 #else // !wxUSE_WCHAR_T
684 // ----------------------------------------------------------------------------
685 // stand-ins in absence of wchar_t
686 // ----------------------------------------------------------------------------
688 WXDLLEXPORT_DATA(wxMBConv
) wxConvLibc
, wxConvFile
;
690 #endif // wxUSE_WCHAR_T