1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Unicode conversion classes
4 // Author: Ove Kaaven, Robert Roebling, Vadim Zeitlin, Vaclav Slavik
8 // Copyright: (c) 1999 Ove Kaaven, Robert Roebling, Vadim Zeitlin, Vaclav Slavik
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"
42 #include "wx/strconv.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 WXDLLEXPORT_DATA(wxMBConv
*) wxConvCurrent
= &wxConvLibc
;
53 // ============================================================================
55 // ============================================================================
71 #define BSWAP_UCS4(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT32_SWAP_ALWAYS(str[_c]); }
72 #define BSWAP_UTF16(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT16_SWAP_ALWAYS(str[_c]); }
74 // under Unix SIZEOF_WCHAR_T is defined by configure, but under other platforms
75 // it might be not defined - assume the most common value
76 #ifndef SIZEOF_WCHAR_T
77 #define SIZEOF_WCHAR_T 2
78 #endif // !defined(SIZEOF_WCHAR_T)
80 #if SIZEOF_WCHAR_T == 4
81 #define WC_NAME "UCS4"
82 #define WC_BSWAP BSWAP_UCS4
83 #ifdef WORDS_BIGENDIAN
84 #define WC_NAME_BEST "UCS-4BE"
86 #define WC_NAME_BEST "UCS-4LE"
88 #elif SIZEOF_WCHAR_T == 2
89 #define WC_NAME "UTF16"
90 #define WC_BSWAP BSWAP_UTF16
92 #ifdef WORDS_BIGENDIAN
93 #define WC_NAME_BEST "UTF-16BE"
95 #define WC_NAME_BEST "UTF-16LE"
97 #else // sizeof(wchar_t) != 2 nor 4
98 // I don't know what to do about this
99 #error "Weird sizeof(wchar_t): please report your platform details to wx-users mailing list"
105 static size_t encode_utf16(wxUint32 input
, wchar_t *output
)
109 if (output
) *output
++ = input
;
112 else if (input
>=0x110000)
120 *output
++ = (input
>> 10)+0xd7c0;
121 *output
++ = (input
&0x3ff)+0xdc00;
127 static size_t decode_utf16(const wchar_t* input
, wxUint32
& output
)
129 if ((*input
<0xd800) || (*input
>0xdfff))
134 else if ((input
[1]<0xdc00) || (input
[1]>=0xdfff))
141 output
= ((input
[0] - 0xd7c0) << 10) + (input
[1] - 0xdc00);
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
152 WXDLLEXPORT_DATA(wxMBConv
) wxConvLibc
;
154 size_t wxMBConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
156 return wxMB2WC(buf
, psz
, n
);
159 size_t wxMBConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
161 return wxWC2MB(buf
, psz
, n
);
164 const wxWCharBuffer
wxMBConv::cMB2WC(const char *psz
) const
168 size_t nLen
= MB2WC((wchar_t *) NULL
, psz
, 0);
169 if (nLen
== (size_t)-1)
170 return wxWCharBuffer((wchar_t *) NULL
);
171 wxWCharBuffer
buf(nLen
);
172 MB2WC((wchar_t *)(const wchar_t *) buf
, psz
, nLen
);
176 return wxWCharBuffer((wchar_t *) NULL
);
179 const wxCharBuffer
wxMBConv::cWC2MB(const wchar_t *psz
) const
183 size_t nLen
= WC2MB((char *) NULL
, psz
, 0);
184 if (nLen
== (size_t)-1)
185 return wxCharBuffer((char *) NULL
);
186 wxCharBuffer
buf(nLen
);
187 WC2MB((char *)(const char *) buf
, psz
, nLen
);
191 return wxCharBuffer((char *) NULL
);
194 // ----------------------------------------------------------------------------
195 // standard file conversion
196 // ----------------------------------------------------------------------------
198 WXDLLEXPORT_DATA(wxMBConvFile
) wxConvFile
;
200 // just use the libc conversion for now
201 size_t wxMBConvFile::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
203 return wxMB2WC(buf
, psz
, n
);
206 size_t wxMBConvFile::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
208 return wxWC2MB(buf
, psz
, n
);
211 // ----------------------------------------------------------------------------
212 // standard gdk conversion
213 // ----------------------------------------------------------------------------
217 WXDLLEXPORT_DATA(wxMBConvGdk
) wxConvGdk
;
221 size_t wxMBConvGdk::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
225 return gdk_mbstowcs((GdkWChar
*)buf
, psz
, n
);
229 GdkWChar
*nbuf
= new GdkWChar
[n
=strlen(psz
)];
230 size_t len
= gdk_mbstowcs(nbuf
, psz
, n
);
236 size_t wxMBConvGdk::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
238 char *mbstr
= gdk_wcstombs((GdkWChar
*)psz
);
239 size_t len
= mbstr
? strlen(mbstr
) : 0;
244 memcpy(buf
, psz
, len
);
253 // ----------------------------------------------------------------------------
255 // ----------------------------------------------------------------------------
257 WXDLLEXPORT_DATA(wxMBConvUTF7
) wxConvUTF7
;
260 static char utf7_setD
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
261 "abcdefghijklmnopqrstuvwxyz"
262 "0123456789'(),-./:?";
263 static char utf7_setO
[]="!\"#$%&*;<=>@[]^_`{|}";
264 static char utf7_setB
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
265 "abcdefghijklmnopqrstuvwxyz"
269 // TODO: write actual implementations of UTF-7 here
270 size_t wxMBConvUTF7::MB2WC(wchar_t * WXUNUSED(buf
),
271 const char * WXUNUSED(psz
),
272 size_t WXUNUSED(n
)) const
277 size_t wxMBConvUTF7::WC2MB(char * WXUNUSED(buf
),
278 const wchar_t * WXUNUSED(psz
),
279 size_t WXUNUSED(n
)) const
284 // ----------------------------------------------------------------------------
286 // ----------------------------------------------------------------------------
288 WXDLLEXPORT_DATA(wxMBConvUTF8
) wxConvUTF8
;
290 static wxUint32 utf8_max
[]=
291 { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff, 0xffffffff };
293 size_t wxMBConvUTF8::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
297 while (*psz
&& ((!buf
) || (len
< n
)))
299 unsigned char cc
= *psz
++, fc
= cc
;
301 for (cnt
= 0; fc
& 0x80; cnt
++)
315 // invalid UTF-8 sequence
320 unsigned ocnt
= cnt
- 1;
321 wxUint32 res
= cc
& (0x3f >> cnt
);
325 if ((cc
& 0xC0) != 0x80)
327 // invalid UTF-8 sequence
330 res
= (res
<< 6) | (cc
& 0x3f);
332 if (res
<= utf8_max
[ocnt
])
334 // illegal UTF-8 encoding
338 size_t pa
= encode_utf16(res
, buf
);
339 if (pa
== (size_t)-1)
352 if (buf
&& (len
< n
))
357 size_t wxMBConvUTF8::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
361 while (*psz
&& ((!buf
) || (len
< n
)))
365 size_t pa
= decode_utf16(psz
, cc
);
366 psz
+= (pa
== (size_t)-1) ? 1 : pa
;
368 cc
=(*psz
++) & 0x7fffffff;
371 for (cnt
= 0; cc
> utf8_max
[cnt
]; cnt
++) {}
385 *buf
++ = (-128 >> cnt
) | ((cc
>> (cnt
* 6)) & (0x3f >> cnt
));
387 *buf
++ = 0x80 | ((cc
>> (cnt
* 6)) & 0x3f);
392 if (buf
&& (len
<n
)) *buf
= 0;
396 // ----------------------------------------------------------------------------
397 // specified character set
398 // ----------------------------------------------------------------------------
400 WXDLLEXPORT_DATA(wxCSConv
) wxConvLocal((const wxChar
*)NULL
);
402 #include "wx/encconv.h"
403 #include "wx/fontmap.h"
405 // TODO: add some tables here
406 // - perhaps common encodings to common codepages (for Win32)
407 // - perhaps common encodings to objects ("UTF8" -> wxConvUTF8)
408 // - move wxEncodingConverter meat in here
410 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
414 // VZ: the new version of wxCharsetToCodepage() is more politically correct
415 // and should work on other Windows versions as well but the old version is
416 // still needed for !wxUSE_FONTMAP || !wxUSE_GUI case
418 extern long wxEncodingToCodepage(wxFontEncoding encoding
)
420 // translate encoding into the Windows CHARSET
421 wxNativeEncodingInfo natveEncInfo
;
422 if ( !wxGetNativeFontEncoding(encoding
, &natveEncInfo
) )
425 // translate CHARSET to code page
426 CHARSETINFO csetInfo
;
427 if ( !::TranslateCharsetInfo((DWORD
*)(DWORD
)natveEncInfo
.charset
,
431 wxLogLastError(_T("TranslateCharsetInfo(TCI_SRCCHARSET)"));
436 return csetInfo
.ciACP
;
441 extern long wxCharsetToCodepage(const wxChar
*name
)
443 // first get the font encoding for this charset
447 wxFontEncoding enc
= wxTheFontMapper
->CharsetToEncoding(name
, FALSE
);
448 if ( enc
== wxFONTENCODING_SYSTEM
)
451 // the use the helper function
452 return wxEncodingToCodepage(enc
);
455 #endif // wxUSE_FONTMAP
459 // include old wxCharsetToCodepage() by OK if needed
460 #if !wxUSE_GUI || !wxUSE_FONTMAP
462 #include "wx/msw/registry.h"
464 // this should work if Internet Exploiter is installed
465 extern long wxCharsetToCodepage(const wxChar
*name
)
474 wxString
path(wxT("MIME\\Database\\Charset\\"));
476 wxRegKey
key(wxRegKey::HKCR
, path
);
478 if (!key
.Exists()) break;
480 // two cases: either there's an AliasForCharset string,
481 // or there are Codepage and InternetEncoding dwords.
482 // The InternetEncoding gives us the actual encoding,
483 // the Codepage just says which Windows character set to
484 // use when displaying the data.
485 if (key
.HasValue(wxT("InternetEncoding")) &&
486 key
.QueryValue(wxT("InternetEncoding"), &CP
)) break;
488 // no encoding, see if it's an alias
489 if (!key
.HasValue(wxT("AliasForCharset")) ||
490 !key
.QueryValue(wxT("AliasForCharset"), cn
)) break;
496 #endif // !wxUSE_GUI || !wxUSE_FONTMAP
500 // ============================================================================
501 // wxCharacterSet and derived classes
502 // ============================================================================
504 // ----------------------------------------------------------------------------
505 // wxCharacterSet is the ABC for the classes below
506 // ----------------------------------------------------------------------------
511 wxCharacterSet(const wxChar
*name
)
513 virtual ~wxCharacterSet()
515 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
516 { return (size_t)-1; }
517 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
518 { return (size_t)-1; }
519 virtual bool usable()
525 // ----------------------------------------------------------------------------
526 // ID_CharSet: implementation of wxCharacterSet using an existing wxMBConv
527 // ----------------------------------------------------------------------------
529 class ID_CharSet
: public wxCharacterSet
532 ID_CharSet(const wxChar
*name
, wxMBConv
*cnv
)
533 : wxCharacterSet(name
), work(cnv
) {}
535 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
536 { return work
? work
->MB2WC(buf
,psz
,n
) : (size_t)-1; }
538 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
539 { return work
? work
->WC2MB(buf
,psz
,n
) : (size_t)-1; }
542 { return work
!=NULL
; }
548 // ============================================================================
549 // The classes doing conversion using the iconv_xxx() functions
550 // ============================================================================
554 // VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with E2BIG
555 // if output buffer is _exactly_ as big as needed. Such case is (unless there's
556 // yet another bug in glibc) the only case when iconv() returns with (size_t)-1
557 // (which means error) and says there are 0 bytes left in the input buffer --
558 // when _real_ error occurs, bytes-left-in-input buffer is non-zero. Hence,
559 // this alternative test for iconv() failure.
560 // [This bug does not appear in glibc 2.2.]
561 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 1
562 #define ICONV_FAILED(cres, bufLeft) ((cres == (size_t)-1) && \
563 (errno != E2BIG || bufLeft != 0))
565 #define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1)
568 #ifdef WX_ICONV_TAKES_CHAR
569 #define ICONV_CHAR_CAST(x) (char **)x
571 #define ICONV_CHAR_CAST(x) (const char **)x
574 // ----------------------------------------------------------------------------
575 // IC_CharSet: encapsulates an iconv character set
576 // ----------------------------------------------------------------------------
578 class IC_CharSet
: public wxCharacterSet
581 IC_CharSet(const wxChar
*name
);
582 virtual ~IC_CharSet();
584 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
);
585 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
);
588 { return (m2w
!= (iconv_t
)-1) && (w2m
!= (iconv_t
)-1); }
591 // the iconv handlers used to translate from multibyte to wide char and in
592 // the other direction
597 // the name (for iconv_open()) of a wide char charset - if none is
598 // available on this machine, it will remain NULL
599 static const char *ms_wcCharsetName
;
601 // true if the wide char encoding we use (i.e. ms_wcCharsetName) has
602 // different endian-ness than the native one
603 static bool ms_wcNeedsSwap
= FALSE
;
606 const char *IC_CharSet::ms_wcCharsetName
= NULL
;
608 IC_CharSet::IC_CharSet(const wxChar
*name
)
609 : wxCharacterSet(name
)
611 // check for charset that represents wchar_t:
612 if (ms_wcCharsetName
== NULL
)
614 ms_wcNeedsSwap
= FALSE
;
616 // try charset with explicit bytesex info (e.g. "UCS-4LE"):
617 ms_wcCharsetName
= WC_NAME_BEST
;
618 m2w
= iconv_open(ms_wcCharsetName
, wxConvLibc
.cWX2MB(name
));
620 if (m2w
== (iconv_t
)-1)
622 // try charset w/o bytesex info (e.g. "UCS4")
623 // and check for bytesex ourselves:
624 ms_wcCharsetName
= WC_NAME
;
625 m2w
= iconv_open(ms_wcCharsetName
, wxConvLibc
.cWX2MB(name
));
627 // last bet, try if it knows WCHAR_T pseudo-charset
628 if (m2w
== (iconv_t
)-1)
630 ms_wcCharsetName
= "WCHAR_T";
631 m2w
= iconv_open(ms_wcCharsetName
, wxConvLibc
.cWX2MB(name
));
634 if (m2w
!= (iconv_t
)-1)
636 char buf
[2], *bufPtr
;
637 wchar_t wbuf
[2], *wbufPtr
;
645 outsz
= SIZEOF_WCHAR_T
* 2;
649 res
= iconv(m2w
, ICONV_CHAR_CAST(&bufPtr
), &insz
,
650 (char**)&wbufPtr
, &outsz
);
652 if (ICONV_FAILED(res
, insz
))
654 ms_wcCharsetName
= NULL
;
655 wxLogLastError(wxT("iconv"));
656 wxLogError(_("Convertion to charset '%s' doesn't work."), name
);
660 ms_wcNeedsSwap
= wbuf
[0] != (wchar_t)buf
[0];
665 ms_wcCharsetName
= NULL
;
666 wxLogError(_("Impossible to convert to/from charset '%s'."), name
);
669 wxLogTrace(wxT("strconv"), wxT("wchar_t charset is '%s', needs swap: %i"), ms_wcCharsetName
, ms_wcNeedsSwap
);
671 else // we already have ms_wcCharsetName
673 m2w
= iconv_open(ms_wcCharsetName
, wxConvLibc
.cWX2MB(name
));
676 // NB: don't ever pass NULL to iconv_open(), it may crash!
677 if ( ms_wcCharsetName
)
679 w2m
= iconv_open(wxConvLibc
.cWX2MB(name
), ms_wcCharsetName
);
683 IC_CharSet::~IC_CharSet()
685 if ( m2w
!= (iconv_t
)-1 )
687 if ( w2m
!= (iconv_t
)-1 )
691 size_t IC_CharSet::MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
693 size_t inbuf
= strlen(psz
);
694 size_t outbuf
= n
* SIZEOF_WCHAR_T
;
696 // VS: Use these instead of psz, buf because iconv() modifies its arguments:
697 wchar_t *bufPtr
= buf
;
698 const char *pszPtr
= psz
;
702 // have destination buffer, convert there
704 ICONV_CHAR_CAST(&pszPtr
), &inbuf
,
705 (char**)&bufPtr
, &outbuf
);
706 res
= n
- (outbuf
/ SIZEOF_WCHAR_T
);
710 // convert to native endianness
711 WC_BSWAP(buf
/* _not_ bufPtr */, res
)
716 // no destination buffer... convert using temp buffer
717 // to calculate destination buffer requirement
722 outbuf
= 8*SIZEOF_WCHAR_T
;
725 ICONV_CHAR_CAST(&pszPtr
), &inbuf
,
726 (char**)&bufPtr
, &outbuf
);
728 res
+= 8-(outbuf
/SIZEOF_WCHAR_T
);
729 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
732 if (ICONV_FAILED(cres
, inbuf
))
734 //VS: it is ok if iconv fails, hence trace only
735 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
742 size_t IC_CharSet::WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
744 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
745 size_t inbuf
= std::wcslen(psz
) * SIZEOF_WCHAR_T
;
747 size_t inbuf
= ::wcslen(psz
) * SIZEOF_WCHAR_T
;
756 // need to copy to temp buffer to switch endianness
757 // this absolutely doesn't rock!
758 // (no, doing WC_BSWAP twice on the original buffer won't help, as it
759 // could be in read-only memory, or be accessed in some other thread)
760 tmpbuf
=(wchar_t*)malloc((inbuf
+1)*SIZEOF_WCHAR_T
);
761 memcpy(tmpbuf
,psz
,(inbuf
+1)*SIZEOF_WCHAR_T
);
762 WC_BSWAP(tmpbuf
, inbuf
)
768 // have destination buffer, convert there
769 cres
= iconv( w2m
, ICONV_CHAR_CAST(&psz
), &inbuf
, &buf
, &outbuf
);
775 // no destination buffer... convert using temp buffer
776 // to calculate destination buffer requirement
780 buf
= tbuf
; outbuf
= 16;
782 cres
= iconv( w2m
, ICONV_CHAR_CAST(&psz
), &inbuf
, &buf
, &outbuf
);
785 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
793 if (ICONV_FAILED(cres
, inbuf
))
795 //VS: it is ok if iconv fails, hence trace only
796 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
803 #endif // HAVE_ICONV_H
805 // ============================================================================
806 // Win32 conversion classes
807 // ============================================================================
809 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
810 class CP_CharSet
: public wxCharacterSet
813 CP_CharSet(const wxChar
* name
)
814 : wxCharacterSet(name
)
816 m_CodePage
= wxCharsetToCodepage(name
);
819 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
822 MultiByteToWideChar(m_CodePage
, 0, psz
, -1, buf
, buf
? n
: 0);
823 //VS: returns # of written chars for buf!=NULL and *size*
824 // needed buffer for buf==NULL
825 return len
? (buf
? len
: len
-1) : (size_t)-1;
828 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
830 size_t len
= WideCharToMultiByte(m_CodePage
, 0, psz
, -1, buf
,
831 buf
? n
: 0, NULL
, NULL
);
832 //VS: returns # of written chars for buf!=NULL and *size*
833 // needed buffer for buf==NULL
834 return len
? (buf
? len
: len
-1) : (size_t)-1;
838 { return m_CodePage
!= -1; }
845 // ============================================================================
846 // wxEncodingConverter based conversion classes
847 // ============================================================================
851 class EC_CharSet
: public wxCharacterSet
854 // temporarily just use wxEncodingConverter stuff,
855 // so that it works while a better implementation is built
856 EC_CharSet(const wxChar
* name
) : wxCharacterSet(name
),
857 enc(wxFONTENCODING_SYSTEM
)
860 enc
= wxTheFontMapper
->CharsetToEncoding(name
, FALSE
);
861 m2w
.Init(enc
, wxFONTENCODING_UNICODE
);
862 w2m
.Init(wxFONTENCODING_UNICODE
, enc
);
865 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
867 size_t inbuf
= strlen(psz
);
869 m2w
.Convert(psz
,buf
);
873 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
875 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
876 || ( defined(__MWERKS__) && defined(__WXMSW__) )
877 size_t inbuf
= std::wcslen(psz
);
879 size_t inbuf
= ::wcslen(psz
);
882 w2m
.Convert(psz
,buf
);
888 { return (enc
!=wxFONTENCODING_SYSTEM
) && (enc
!=wxFONTENCODING_DEFAULT
); }
892 wxEncodingConverter m2w
, w2m
;
895 #endif // wxUSE_FONTMAP
897 // ----------------------------------------------------------------------------
898 // the function creating the wxCharacterSet for the specified charset on the
899 // current system, trying all possibilities
900 // ----------------------------------------------------------------------------
902 static wxCharacterSet
*wxGetCharacterSet(const wxChar
*name
)
904 wxCharacterSet
*cset
= NULL
;
907 if (wxStricmp(name
, wxT("UTF8")) == 0 || wxStricmp(name
, wxT("UTF-8")) == 0)
909 cset
= new ID_CharSet(name
, &wxConvUTF8
);
914 cset
= new IC_CharSet(name
); // may not take NULL
919 if (cset
&& cset
->usable())
928 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
929 cset
= new CP_CharSet(name
); // may take NULL
937 cset
= new EC_CharSet(name
);
940 #endif // wxUSE_FONTMAP
943 wxLogError(_("Unknown encoding '%s'!"), name
);
947 // ============================================================================
948 // wxCSConv implementation
949 // ============================================================================
951 wxCSConv::wxCSConv(const wxChar
*charset
)
953 m_name
= (wxChar
*)NULL
;
954 m_cset
= (wxCharacterSet
*) NULL
;
960 wxCSConv::~wxCSConv()
966 void wxCSConv::SetName(const wxChar
*charset
)
970 m_name
= wxStrdup(charset
);
975 void wxCSConv::LoadNow()
981 wxString name
= wxLocale::GetSystemEncodingName();
986 // wxGetCharacterSet() complains about NULL name
987 m_cset
= m_name
? wxGetCharacterSet(m_name
) : NULL
;
992 size_t wxCSConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
994 ((wxCSConv
*)this)->LoadNow(); // discard constness
997 return m_cset
->MB2WC(buf
, psz
, n
);
1000 size_t len
= strlen(psz
);
1004 for (size_t c
= 0; c
<= len
; c
++)
1005 buf
[c
] = (unsigned char)(psz
[c
]);
1011 size_t wxCSConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
1013 ((wxCSConv
*)this)->LoadNow(); // discard constness
1016 return m_cset
->WC2MB(buf
, psz
, n
);
1019 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
1020 || ( defined(__MWERKS__) && defined(__WXMSW__) )
1021 size_t len
=std::wcslen(psz
);
1023 size_t len
=::wcslen(psz
);
1027 for (size_t c
= 0; c
<= len
; c
++)
1028 buf
[c
] = (psz
[c
] > 0xff) ? '?' : psz
[c
];
1034 // VZ: are the classes below used at all??
1039 class IC_CharSetConverter
1042 IC_CharSetConverter(IC_CharSet
*from
, IC_CharSet
*to
)
1044 cnv
= iconv_open(wxConvLibc
.cWX2MB(to
->cname
),
1045 wxConvLibc
.cWX2MB(from
->cname
));
1048 ~IC_CharSetConverter()
1050 if (cnv
!= (iconv_t
)-1)
1054 size_t Convert(char *buf
, const char *psz
, size_t n
)
1056 size_t inbuf
= strlen(psz
);
1058 size_t res
= iconv( cnv
, ICONV_CHAR_CAST(&psz
), &inbuf
, &buf
, &outbuf
);
1059 if (res
== (size_t)-1)
1061 return (n
- outbuf
);
1068 #endif // HAVE_ICONV_H
1070 class EC_CharSetConverter
1073 EC_CharSetConverter(EC_CharSet
* from
,EC_CharSet
* to
)
1074 { cnv
.Init(from
->enc
,to
->enc
); }
1076 size_t Convert(char* buf
, const char* psz
, size_t n
)
1078 size_t inbuf
= strlen(psz
);
1079 if (buf
) cnv
.Convert(psz
,buf
);
1084 wxEncodingConverter cnv
;
1089 #else // !wxUSE_WCHAR_T
1091 // ----------------------------------------------------------------------------
1092 // stand-ins in absence of wchar_t
1093 // ----------------------------------------------------------------------------
1095 WXDLLEXPORT_DATA(wxMBConv
) wxConvLibc
, wxConvFile
;
1097 #endif // wxUSE_WCHAR_T