]>
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"
53 #include "wx/strconv.h"
57 #if defined(WORDS_BIGENDIAN) || defined(__STDC_ISO_10646__)
58 #define BSWAP_UCS4(str, len)
59 #define BSWAP_UCS2(str, len)
61 #define BSWAP_UCS4(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT32_SWAP_ALWAYS(str[_c]); }
62 #define BSWAP_UCS2(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT16_SWAP_ALWAYS(str[_c]); }
65 #define BSWAP_UTF32(str, len) BSWAP_UCS4(str, len)
66 #define BSWAP_UTF16(str, len) BSWAP_UCS2(str, len)
68 #if SIZEOF_WCHAR_T == 4
69 #define WC_NAME "UCS4"
70 #define WC_BSWAP BSWAP_UCS4
71 #elif SIZEOF_WCHAR_T == 2
72 #define WC_NAME "UTF16"
73 #define WC_BSWAP BSWAP_UTF16
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 WXDLLEXPORT_DATA(wxMBConv
*) wxConvCurrent
= &wxConvLibc
;
83 // ============================================================================
85 // ============================================================================
91 static size_t encode_utf16(wxUint32 input
,wxUint16
*output
)
95 if (output
) *output
++ = input
;
98 else if (input
>=0x110000)
106 *output
++ = (input
>> 10)+0xd7c0;
107 *output
++ = (input
&0x3ff)+0xdc00;
113 static size_t decode_utf16(wxUint16
*input
,wxUint32
&output
)
115 if ((*input
<0xd800) || (*input
>0xdfff))
120 else if ((input
[1]<0xdc00) || (input
[1]>=0xdfff))
127 output
= ((input
[0] - 0xd7c0) << 10) + (input
[1] - 0xdc00);
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 WXDLLEXPORT_DATA(wxMBConv
) wxConvLibc
;
140 size_t wxMBConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
142 return wxMB2WC(buf
, psz
, n
);
145 size_t wxMBConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
147 return wxWC2MB(buf
, psz
, n
);
150 const wxWCharBuffer
wxMBConv::cMB2WC(const char *psz
) const
154 size_t nLen
= MB2WC((wchar_t *) NULL
, psz
, 0);
155 if (nLen
== (size_t)-1)
156 return wxWCharBuffer((wchar_t *) NULL
);
157 wxWCharBuffer
buf(nLen
);
158 MB2WC((wchar_t *)(const wchar_t *) buf
, psz
, nLen
);
162 return wxWCharBuffer((wchar_t *) NULL
);
165 const wxCharBuffer
wxMBConv::cWC2MB(const wchar_t *psz
) const
169 size_t nLen
= WC2MB((char *) NULL
, psz
, 0);
170 if (nLen
== (size_t)-1)
171 return wxCharBuffer((char *) NULL
);
172 wxCharBuffer
buf(nLen
);
173 WC2MB((char *)(const char *) buf
, psz
, nLen
);
177 return wxCharBuffer((char *) NULL
);
180 // ----------------------------------------------------------------------------
181 // standard file conversion
182 // ----------------------------------------------------------------------------
184 WXDLLEXPORT_DATA(wxMBConvFile
) wxConvFile
;
186 // just use the libc conversion for now
187 size_t wxMBConvFile::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
189 return wxMB2WC(buf
, psz
, n
);
192 size_t wxMBConvFile::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
194 return wxWC2MB(buf
, psz
, n
);
197 // ----------------------------------------------------------------------------
198 // standard gdk conversion
199 // ----------------------------------------------------------------------------
203 WXDLLEXPORT_DATA(wxMBConvGdk
) wxConvGdk
;
207 size_t wxMBConvGdk::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
211 return gdk_mbstowcs((GdkWChar
*)buf
, psz
, n
);
215 GdkWChar
*nbuf
= new GdkWChar
[n
=strlen(psz
)];
216 size_t len
= gdk_mbstowcs(nbuf
, psz
, n
);
222 size_t wxMBConvGdk::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
224 char *mbstr
= gdk_wcstombs((GdkWChar
*)psz
);
225 size_t len
= mbstr
? strlen(mbstr
) : 0;
230 memcpy(buf
, psz
, len
);
239 // ----------------------------------------------------------------------------
241 // ----------------------------------------------------------------------------
243 WXDLLEXPORT_DATA(wxMBConvUTF7
) wxConvUTF7
;
246 static char utf7_setD
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
247 "abcdefghijklmnopqrstuvwxyz"
248 "0123456789'(),-./:?";
249 static char utf7_setO
[]="!\"#$%&*;<=>@[]^_`{|}";
250 static char utf7_setB
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
251 "abcdefghijklmnopqrstuvwxyz"
255 // TODO: write actual implementations of UTF-7 here
256 size_t wxMBConvUTF7::MB2WC(wchar_t * WXUNUSED(buf
),
257 const char * WXUNUSED(psz
),
258 size_t WXUNUSED(n
)) const
263 size_t wxMBConvUTF7::WC2MB(char * WXUNUSED(buf
),
264 const wchar_t * WXUNUSED(psz
),
265 size_t WXUNUSED(n
)) const
270 // ----------------------------------------------------------------------------
272 // ----------------------------------------------------------------------------
274 WXDLLEXPORT_DATA(wxMBConvUTF8
) wxConvUTF8
;
276 static wxUint32 utf8_max
[]=
277 { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff, 0xffffffff };
279 size_t wxMBConvUTF8::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
283 while (*psz
&& ((!buf
) || (len
< n
)))
285 unsigned char cc
= *psz
++, fc
= cc
;
287 for (cnt
= 0; fc
& 0x80; cnt
++)
301 // invalid UTF-8 sequence
306 unsigned ocnt
= cnt
- 1;
307 wxUint32 res
= cc
& (0x3f >> cnt
);
311 if ((cc
& 0xC0) != 0x80)
313 // invalid UTF-8 sequence
316 res
= (res
<< 6) | (cc
& 0x3f);
318 if (res
<= utf8_max
[ocnt
])
320 // illegal UTF-8 encoding
324 size_t pa
= encode_utf16(res
, buf
);
325 if (pa
== (size_t)-1)
338 if (buf
&& (len
< n
))
343 size_t wxMBConvUTF8::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
347 while (*psz
&& ((!buf
) || (len
< n
)))
351 size_t pa
= decode_utf16(psz
,cc
);
352 psz
+= (pa
== (size_t)-1) ? 1 : pa
;
354 cc
=(*psz
++) & 0x7fffffff;
357 for (cnt
= 0; cc
> utf8_max
[cnt
]; cnt
++) {}
371 *buf
++ = (-128 >> cnt
) | ((cc
>> (cnt
* 6)) & (0x3f >> cnt
));
373 *buf
++ = 0x80 | ((cc
>> (cnt
* 6)) & 0x3f);
378 if (buf
&& (len
<n
)) *buf
= 0;
382 // ----------------------------------------------------------------------------
383 // specified character set
384 // ----------------------------------------------------------------------------
386 WXDLLEXPORT_DATA(wxCSConv
) wxConvLocal((const wxChar
*)NULL
);
388 #include "wx/encconv.h"
389 #include "wx/fontmap.h"
391 // TODO: add some tables here
392 // - perhaps common encodings to common codepages (for Win32)
393 // - perhaps common encodings to objects ("UTF8" -> wxConvUTF8)
394 // - move wxEncodingConverter meat in here
397 #include "wx/msw/registry.h"
398 // this should work if M$ Internet Exploiter is installed
399 static long CharsetToCodepage(const wxChar
*name
)
408 wxString
path(wxT("MIME\\Database\\Charset\\"));
410 wxRegKey
key(wxRegKey::HKCR
, path
);
412 if (!key
.Exists()) continue;
414 // two cases: either there's an AliasForCharset string,
415 // or there are Codepage and InternetEncoding dwords.
416 // The InternetEncoding gives us the actual encoding,
417 // the Codepage just says which Windows character set to
418 // use when displaying the data.
419 if (key
.HasValue(wxT("InternetEncoding")) &&
420 key
.QueryValue(wxT("InternetEncoding"), &CP
)) break;
422 // no encoding, see if it's an alias
423 if (!key
.HasValue(wxT("AliasForCharset")) ||
424 !key
.QueryValue(wxT("AliasForCharset"), cn
)) break;
434 wxCharacterSet(const wxChar
*name
)
436 virtual ~wxCharacterSet()
438 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
439 { return (size_t)-1; }
440 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
441 { return (size_t)-1; }
442 virtual bool usable()
448 class ID_CharSet
: public wxCharacterSet
451 ID_CharSet(const wxChar
*name
,wxMBConv
*cnv
)
452 : wxCharacterSet(name
), work(cnv
) {}
454 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
455 { return work
? work
->MB2WC(buf
,psz
,n
) : (size_t)-1; }
457 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
458 { return work
? work
->WC2MB(buf
,psz
,n
) : (size_t)-1; }
461 { return work
!=NULL
; }
469 // VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with E2BIG
470 // if output buffer is _exactly_ as big as needed. Such case is (unless there's
471 // yet another bug in glibc) the only case when iconv() returns with (size_t)-1
472 // (which means error) and says there are 0 bytes left in the input buffer --
473 // when _real_ error occurs, bytes-left-in-input buffer is non-zero. Hence,
474 // this alternative test for iconv() failure.
475 // [This bug does not appear in glibc 2.2.]
476 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 1
477 #define ICONV_FAILED(cres, bufLeft) ((cres == (size_t)-1) && \
478 (errno != E2BIG || bufLeft != 0))
480 #define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1)
483 class IC_CharSet
: public wxCharacterSet
486 IC_CharSet(const wxChar
*name
)
487 : wxCharacterSet(name
)
489 m2w
= iconv_open(WC_NAME
, wxConvLibc
.cWX2MB(cname
));
490 w2m
= iconv_open(wxConvLibc
.cWX2MB(cname
), WC_NAME
);
495 if ( m2w
!= (iconv_t
)-1 )
497 if ( w2m
!= (iconv_t
)-1 )
501 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
503 size_t inbuf
= strlen(psz
);
504 size_t outbuf
= n
* SIZEOF_WCHAR_T
;
506 // VS: Use these instead of psz, buf because iconv() modifies its arguments:
507 wchar_t *bufPtr
= buf
;
508 const char *pszPtr
= psz
;
512 // have destination buffer, convert there
513 #ifdef WX_ICONV_TAKES_CHAR
514 cres
= iconv(m2w
, (char**)&pszPtr
, &inbuf
, (char**)&bufPtr
, &outbuf
);
516 cres
= iconv(m2w
, &pszPtr
, &inbuf
, (char**)&bufPtr
, &outbuf
);
518 res
= n
- (outbuf
/ SIZEOF_WCHAR_T
);
519 // convert to native endianness
521 WC_BSWAP(buf
/* _not_ bufPtr */, res
)
526 // no destination buffer... convert using temp buffer
527 // to calculate destination buffer requirement
531 bufPtr
= tbuf
; outbuf
= 8*SIZEOF_WCHAR_T
;
532 #ifdef WX_ICONV_TAKES_CHAR
533 cres
= iconv( m2w
, (char**)&pszPtr
, &inbuf
, (char**)&bufPtr
, &outbuf
);
535 cres
= iconv( m2w
, &pszPtr
, &inbuf
, (char**)&bufPtr
, &outbuf
);
537 res
+= 8-(outbuf
/SIZEOF_WCHAR_T
);
538 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
541 if (ICONV_FAILED(cres
, inbuf
))
547 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
549 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
550 size_t inbuf
= std::wcslen(psz
) * SIZEOF_WCHAR_T
;
552 size_t inbuf
= ::wcslen(psz
) * SIZEOF_WCHAR_T
;
558 // need to copy to temp buffer to switch endianness
559 // this absolutely doesn't rock!
560 // (no, doing WC_BSWAP twice on the original buffer won't help, as it
561 // could be in read-only memory, or be accessed in some other thread)
562 wchar_t *tmpbuf
=(wchar_t*)malloc((inbuf
+1)*SIZEOF_WCHAR_T
);
563 memcpy(tmpbuf
,psz
,(inbuf
+1)*SIZEOF_WCHAR_T
);
564 WC_BSWAP(tmpbuf
, inbuf
)
569 // have destination buffer, convert there
570 #ifdef WX_ICONV_TAKES_CHAR
571 cres
= iconv( w2m
, (char**)&psz
, &inbuf
, &buf
, &outbuf
);
573 cres
= iconv( w2m
, (const char**)&psz
, &inbuf
, &buf
, &outbuf
);
579 // no destination buffer... convert using temp buffer
580 // to calculate destination buffer requirement
584 buf
= tbuf
; outbuf
= 16;
585 #ifdef WX_ICONV_TAKES_CHAR
586 cres
= iconv( w2m
, (char**)&psz
, &inbuf
, &buf
, &outbuf
);
588 cres
= iconv( w2m
, (const char**)&psz
, &inbuf
, &buf
, &outbuf
);
591 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
596 if (ICONV_FAILED(cres
, inbuf
))
603 { return (m2w
!= (iconv_t
)-1) && (w2m
!= (iconv_t
)-1); }
611 class CP_CharSet
: public wxCharacterSet
614 CP_CharSet(const wxChar
*name
)
615 : wxCharacterSet(name
), CodePage(CharsetToCodepage(name
)) {}
617 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
620 MultiByteToWideChar(CodePage
, 0, psz
, -1, buf
, buf
? n
: 0);
621 return len
? len
: (size_t)-1;
624 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
626 size_t len
= WideCharToMultiByte(CodePage
, 0, psz
, -1, buf
,
627 buf
? n
: 0, NULL
, NULL
);
628 return len
? len
: (size_t)-1;
632 { return CodePage
!= -1; }
639 class EC_CharSet
: public wxCharacterSet
642 // temporarily just use wxEncodingConverter stuff,
643 // so that it works while a better implementation is built
644 EC_CharSet(const wxChar
*name
) : wxCharacterSet(name
),
645 enc(wxFONTENCODING_SYSTEM
)
648 enc
= wxTheFontMapper
->CharsetToEncoding(name
, FALSE
);
649 m2w
.Init(enc
, wxFONTENCODING_UNICODE
);
650 w2m
.Init(wxFONTENCODING_UNICODE
, enc
);
653 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
655 size_t inbuf
= strlen(psz
);
657 m2w
.Convert(psz
,buf
);
661 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
663 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
664 size_t inbuf
= std::wcslen(psz
);
666 size_t inbuf
= ::wcslen(psz
);
669 w2m
.Convert(psz
,buf
);
675 { return (enc
!=wxFONTENCODING_SYSTEM
) && (enc
!=wxFONTENCODING_DEFAULT
); }
679 wxEncodingConverter m2w
, w2m
;
682 static wxCharacterSet
*wxGetCharacterSet(const wxChar
*name
)
684 wxCharacterSet
*cset
= NULL
;
687 if (wxStricmp(name
, wxT("UTF8")) == 0 || wxStricmp(name
, wxT("UTF-8")) == 0)
689 cset
= new ID_CharSet(name
, &wxConvUTF8
);
694 cset
= new IC_CharSet(name
); // may not take NULL
699 if (cset
&& cset
->usable()) return cset
;
707 cset
= new CP_CharSet(name
); // may take NULL
714 cset
= new EC_CharSet(name
);
719 wxLogError(_("Unknown encoding '%s'!"), name
);
723 wxCSConv::wxCSConv(const wxChar
*charset
)
725 m_name
= (wxChar
*)NULL
;
726 m_cset
= (wxCharacterSet
*) NULL
;
732 wxCSConv::~wxCSConv()
738 void wxCSConv::SetName(const wxChar
*charset
)
742 m_name
= wxStrdup(charset
);
747 void wxCSConv::LoadNow()
753 wxString name
= wxLocale::GetSystemEncodingName();
758 m_cset
= wxGetCharacterSet(m_name
);
763 size_t wxCSConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
765 ((wxCSConv
*)this)->LoadNow(); // discard constness
768 return m_cset
->MB2WC(buf
, psz
, n
);
771 size_t len
= strlen(psz
);
775 for (size_t c
= 0; c
<= len
; c
++)
776 buf
[c
] = (unsigned char)(psz
[c
]);
782 size_t wxCSConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
784 ((wxCSConv
*)this)->LoadNow(); // discard constness
787 return m_cset
->WC2MB(buf
, psz
, n
);
790 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
791 size_t len
=std::wcslen(psz
);
793 size_t len
=::wcslen(psz
);
797 for (size_t c
= 0; c
<= len
; c
++)
798 buf
[c
] = (psz
[c
] > 0xff) ? '?' : psz
[c
];
806 class IC_CharSetConverter
809 IC_CharSetConverter(IC_CharSet
*from
, IC_CharSet
*to
)
811 cnv
= iconv_open(wxConvLibc
.cWX2MB(to
->cname
),
812 wxConvLibc
.cWX2MB(from
->cname
));
815 ~IC_CharSetConverter()
817 if (cnv
!= (iconv_t
)-1)
821 size_t Convert(char *buf
, const char *psz
, size_t n
)
823 size_t inbuf
= strlen(psz
);
825 #ifdef WX_ICONV_TAKES_CHAR
826 size_t res
= iconv( cnv
, (char**)&psz
, &inbuf
, &buf
, &outbuf
);
828 size_t res
= iconv( cnv
, &psz
, &inbuf
, &buf
, &outbuf
);
830 if (res
== (size_t)-1)
839 #endif // HAVE_ICONV_H
841 class EC_CharSetConverter
844 EC_CharSetConverter(EC_CharSet
*from
,EC_CharSet
*to
)
845 { cnv
.Init(from
->enc
,to
->enc
); }
847 size_t Convert(char*buf
, const char*psz
, size_t n
)
849 size_t inbuf
= strlen(psz
);
850 if (buf
) cnv
.Convert(psz
,buf
);
855 wxEncodingConverter cnv
;
858 #else // !wxUSE_WCHAR_T
860 // ----------------------------------------------------------------------------
861 // stand-ins in absence of wchar_t
862 // ----------------------------------------------------------------------------
864 WXDLLEXPORT_DATA(wxMBConv
) wxConvLibc
, wxConvFile
;
866 #endif // wxUSE_WCHAR_T