]>
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, 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__) 
 411 #include "wx/msw/registry.h" 
 412 // this should work if M$ Internet Exploiter is installed 
 413 static long CharsetToCodepage(const wxChar 
*name
) 
 422         wxString 
path(wxT("MIME\\Database\\Charset\\")); 
 424         wxRegKey 
key(wxRegKey::HKCR
, path
); 
 426         if (!key
.Exists()) break; 
 428         // two cases: either there's an AliasForCharset string, 
 429         // or there are Codepage and InternetEncoding dwords. 
 430         // The InternetEncoding gives us the actual encoding, 
 431         // the Codepage just says which Windows character set to 
 432         // use when displaying the data. 
 433         if (key
.HasValue(wxT("InternetEncoding")) && 
 434             key
.QueryValue(wxT("InternetEncoding"), &CP
)) break; 
 436         // no encoding, see if it's an alias 
 437         if (!key
.HasValue(wxT("AliasForCharset")) || 
 438             !key
.QueryValue(wxT("AliasForCharset"), cn
)) break; 
 448     wxCharacterSet(const wxChar
*name
) 
 450     virtual ~wxCharacterSet() 
 452     virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) 
 453         { return (size_t)-1; } 
 454     virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) 
 455         { return (size_t)-1; } 
 456     virtual bool usable() 
 462 class ID_CharSet 
: public wxCharacterSet
 
 465     ID_CharSet(const wxChar 
*name
,wxMBConv 
*cnv
) 
 466         : wxCharacterSet(name
), work(cnv
) {} 
 468     size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) 
 469         { return work 
? work
->MB2WC(buf
,psz
,n
) : (size_t)-1; } 
 471     size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) 
 472         { return work 
? work
->WC2MB(buf
,psz
,n
) : (size_t)-1; } 
 475         { return work
!=NULL
; } 
 483 bool g_wcNeedsSwap 
= FALSE
; 
 484 static const char *g_wcCharset 
= NULL
; 
 486 // VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with E2BIG 
 487 //     if output buffer is _exactly_ as big as needed. Such case is (unless there's 
 488 //     yet another bug in glibc) the only case when iconv() returns with (size_t)-1 
 489 //     (which means error) and says there are 0 bytes left in the input buffer -- 
 490 //     when _real_ error occurs, bytes-left-in-input buffer is non-zero. Hence, 
 491 //     this alternative test for iconv() failure. 
 492 //     [This bug does not appear in glibc 2.2.] 
 493 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 1 
 494 #define ICONV_FAILED(cres, bufLeft) ((cres == (size_t)-1) && \ 
 495                                      (errno != E2BIG || bufLeft != 0)) 
 497 #define ICONV_FAILED(cres, bufLeft)  (cres == (size_t)-1) 
 500 class IC_CharSet 
: public wxCharacterSet
 
 503     IC_CharSet(const wxChar 
*name
) 
 504         : wxCharacterSet(name
) 
 506         // check for charset that represents wchar_t: 
 507         if (g_wcCharset 
== NULL
) 
 509             g_wcNeedsSwap 
= FALSE
; 
 511             // try charset with explicit bytesex info (e.g. "UCS-4LE"): 
 512             g_wcCharset 
= WC_NAME_BEST
; 
 513             m2w 
= iconv_open(g_wcCharset
, wxConvLibc
.cWX2MB(name
)); 
 515             if (m2w 
== (iconv_t
)-1) 
 517                 // try charset w/o bytesex info (e.g. "UCS4") 
 518                 // and check for bytesex ourselves:  
 519                 g_wcCharset 
= WC_NAME
; 
 520                 m2w 
= iconv_open(g_wcCharset
, wxConvLibc
.cWX2MB(name
)); 
 522                 // last bet, try if it knows WCHAR_T pseudo-charset 
 523                 if (m2w 
== (iconv_t
)-1) 
 525                     g_wcCharset 
= "WCHAR_T"; 
 526                     m2w 
= iconv_open(g_wcCharset
, wxConvLibc
.cWX2MB(name
)); 
 529                 if (m2w 
!= (iconv_t
)-1) 
 531                     char    buf
[2], *bufPtr
; 
 532                     wchar_t wbuf
[2], *wbufPtr
; 
 540                     outsz 
= SIZEOF_WCHAR_T 
* 2; 
 544                     #ifdef WX_ICONV_TAKES_CHAR 
 545                     res 
= iconv(m2w
, (char**)&bufPtr
, &insz
, (char**)&wbufPtr
, &outsz
); 
 547                     res 
= iconv(m2w
, (const char**)&bufPtr
, &insz
, (char**)&wbufPtr
, &outsz
); 
 549                     if (ICONV_FAILED(res
, insz
)) 
 552                         wxLogLastError(wxT("iconv")); 
 553                         wxLogError(_("Convertion to charset '%s' doesn't work."), name
); 
 557                         g_wcNeedsSwap 
= (wbuf
[0] != (wchar_t)buf
[0]); 
 563                     wxLogError(_("Don't know how to convert to/from charset '%s'."), name
); 
 566             wxLogTrace(wxT("strconv"), wxT("wchar_t charset is '%s', needs swap: %i"), g_wcCharset
, g_wcNeedsSwap
); 
 569             m2w 
= iconv_open(g_wcCharset
, wxConvLibc
.cWX2MB(name
)); 
 571         w2m 
= iconv_open(wxConvLibc
.cWX2MB(name
), g_wcCharset
); 
 576         if ( m2w 
!= (iconv_t
)-1 ) 
 578         if ( w2m 
!= (iconv_t
)-1 ) 
 582     size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) 
 584         size_t inbuf 
= strlen(psz
); 
 585         size_t outbuf 
= n 
* SIZEOF_WCHAR_T
; 
 587         // VS: Use these instead of psz, buf because iconv() modifies its arguments: 
 588         wchar_t *bufPtr 
= buf
; 
 589         const char *pszPtr 
= psz
; 
 593             // have destination buffer, convert there 
 594 #ifdef WX_ICONV_TAKES_CHAR 
 595             cres 
= iconv(m2w
, (char**)&pszPtr
, &inbuf
, (char**)&bufPtr
, &outbuf
); 
 597             cres 
= iconv(m2w
, &pszPtr
, &inbuf
, (char**)&bufPtr
, &outbuf
); 
 599             res 
= n 
- (outbuf 
/ SIZEOF_WCHAR_T
); 
 603                 // convert to native endianness 
 604                 WC_BSWAP(buf 
/* _not_ bufPtr */, res
) 
 609             // no destination buffer... convert using temp buffer 
 610             // to calculate destination buffer requirement 
 614                 bufPtr 
= tbuf
; outbuf 
= 8*SIZEOF_WCHAR_T
; 
 615 #ifdef WX_ICONV_TAKES_CHAR 
 616                 cres 
= iconv( m2w
, (char**)&pszPtr
, &inbuf
, (char**)&bufPtr
, &outbuf 
); 
 618                 cres 
= iconv( m2w
, &pszPtr
, &inbuf
, (char**)&bufPtr
, &outbuf 
); 
 620                 res 
+= 8-(outbuf
/SIZEOF_WCHAR_T
); 
 621             } while ((cres
==(size_t)-1) && (errno
==E2BIG
)); 
 624         if (ICONV_FAILED(cres
, inbuf
)) 
 626             //VS: it is ok if iconv fails, hence trace only 
 627             wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode())); 
 634     size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) 
 636 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530) 
 637         size_t inbuf 
= std::wcslen(psz
) * SIZEOF_WCHAR_T
; 
 639         size_t inbuf 
= ::wcslen(psz
) * SIZEOF_WCHAR_T
; 
 648             // need to copy to temp buffer to switch endianness 
 649             // this absolutely doesn't rock! 
 650             // (no, doing WC_BSWAP twice on the original buffer won't help, as it 
 651             //  could be in read-only memory, or be accessed in some other thread) 
 652             tmpbuf
=(wchar_t*)malloc((inbuf
+1)*SIZEOF_WCHAR_T
); 
 653             memcpy(tmpbuf
,psz
,(inbuf
+1)*SIZEOF_WCHAR_T
); 
 654             WC_BSWAP(tmpbuf
, inbuf
) 
 660             // have destination buffer, convert there 
 661 #ifdef WX_ICONV_TAKES_CHAR 
 662             cres 
= iconv( w2m
, (char**)&psz
, &inbuf
, &buf
, &outbuf 
); 
 664             cres 
= iconv( w2m
, (const char**)&psz
, &inbuf
, &buf
, &outbuf 
); 
 670             // no destination buffer... convert using temp buffer 
 671             // to calculate destination buffer requirement 
 675                 buf 
= tbuf
; outbuf 
= 16; 
 676 #ifdef WX_ICONV_TAKES_CHAR 
 677                 cres 
= iconv( w2m
, (char**)&psz
, &inbuf
, &buf
, &outbuf 
); 
 679                 cres 
= iconv( w2m
, (const char**)&psz
, &inbuf
, &buf
, &outbuf 
); 
 682             } while ((cres
==(size_t)-1) && (errno
==E2BIG
)); 
 690         if (ICONV_FAILED(cres
, inbuf
)) 
 692             //VS: it is ok if iconv fails, hence trace only 
 693             wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode())); 
 701         { return (m2w 
!= (iconv_t
)-1) && (w2m 
!= (iconv_t
)-1); } 
 708 #if defined(__WIN32__) && !defined(__WXMICROWIN__) 
 709 class CP_CharSet 
: public wxCharacterSet
 
 712     CP_CharSet(const wxChar
*name
) 
 713         : wxCharacterSet(name
), CodePage(CharsetToCodepage(name
)) {} 
 715     size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) 
 718             MultiByteToWideChar(CodePage
, 0, psz
, -1, buf
, buf 
? n 
: 0); 
 719         //VS: returns # of written chars for buf!=NULL and *size* 
 720         //    needed buffer for buf==NULL 
 721         return len 
? (buf 
? len 
: len
-1) : (size_t)-1; 
 724     size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) 
 726         size_t len 
= WideCharToMultiByte(CodePage
, 0, psz
, -1, buf
, 
 727                                          buf 
? n 
: 0, NULL
, NULL
); 
 728         //VS: returns # of written chars for buf!=NULL and *size* 
 729         //    needed buffer for buf==NULL 
 730         return len 
? (buf 
? len 
: len
-1) : (size_t)-1; 
 734         { return CodePage 
!= -1; } 
 743 class EC_CharSet 
: public wxCharacterSet
 
 746     // temporarily just use wxEncodingConverter stuff, 
 747     // so that it works while a better implementation is built 
 748     EC_CharSet(const wxChar
*name
) : wxCharacterSet(name
), 
 749                                     enc(wxFONTENCODING_SYSTEM
) 
 752             enc 
= wxTheFontMapper
->CharsetToEncoding(name
, FALSE
); 
 753         m2w
.Init(enc
, wxFONTENCODING_UNICODE
); 
 754         w2m
.Init(wxFONTENCODING_UNICODE
, enc
); 
 757     size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) 
 759         size_t inbuf 
= strlen(psz
); 
 761             m2w
.Convert(psz
,buf
); 
 765     size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) 
 767 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530) 
 768         size_t inbuf 
= std::wcslen(psz
); 
 770         size_t inbuf 
= ::wcslen(psz
); 
 773             w2m
.Convert(psz
,buf
); 
 779         { return (enc
!=wxFONTENCODING_SYSTEM
) && (enc
!=wxFONTENCODING_DEFAULT
); } 
 783     wxEncodingConverter m2w
, w2m
; 
 786 #endif // wxUSE_FONTMAP 
 788 static wxCharacterSet 
*wxGetCharacterSet(const wxChar 
*name
) 
 790     wxCharacterSet 
*cset 
= NULL
; 
 793         if (wxStricmp(name
, wxT("UTF8")) == 0 || wxStricmp(name
, wxT("UTF-8")) == 0) 
 795             cset 
= new ID_CharSet(name
, &wxConvUTF8
); 
 800             cset 
= new IC_CharSet(name
); // may not take NULL 
 805     if (cset 
&& cset
->usable()) 
 814 #if defined(__WIN32__) && !defined(__WXMICROWIN__) 
 815     cset 
= new CP_CharSet(name
); // may take NULL 
 823     cset 
= new EC_CharSet(name
); 
 826 #endif // wxUSE_FONTMAP 
 829     wxLogError(_("Unknown encoding '%s'!"), name
); 
 833 wxCSConv::wxCSConv(const wxChar 
*charset
) 
 835     m_name 
= (wxChar 
*)NULL
; 
 836     m_cset 
= (wxCharacterSet 
*) NULL
; 
 842 wxCSConv::~wxCSConv() 
 848 void wxCSConv::SetName(const wxChar 
*charset
) 
 852         m_name 
= wxStrdup(charset
); 
 857 void wxCSConv::LoadNow() 
 863             wxString name 
= wxLocale::GetSystemEncodingName(); 
 868         // wxGetCharacterSet() complains about NULL name 
 869         m_cset 
= m_name 
? wxGetCharacterSet(m_name
) : NULL
; 
 874 size_t wxCSConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const 
 876     ((wxCSConv 
*)this)->LoadNow(); // discard constness 
 879         return m_cset
->MB2WC(buf
, psz
, n
); 
 882     size_t len 
= strlen(psz
); 
 886         for (size_t c 
= 0; c 
<= len
; c
++) 
 887             buf
[c
] = (unsigned char)(psz
[c
]); 
 893 size_t wxCSConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const 
 895     ((wxCSConv 
*)this)->LoadNow(); // discard constness 
 898         return m_cset
->WC2MB(buf
, psz
, n
); 
 901 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530) 
 902     size_t len
=std::wcslen(psz
); 
 904     size_t len
=::wcslen(psz
); 
 908         for (size_t c 
= 0; c 
<= len
; c
++) 
 909             buf
[c
] = (psz
[c
] > 0xff) ? '?' : psz
[c
]; 
 917 class IC_CharSetConverter
 
 920     IC_CharSetConverter(IC_CharSet 
*from
, IC_CharSet 
*to
) 
 922         cnv 
= iconv_open(wxConvLibc
.cWX2MB(to
->cname
), 
 923                          wxConvLibc
.cWX2MB(from
->cname
)); 
 926     ~IC_CharSetConverter() 
 928         if (cnv 
!= (iconv_t
)-1) 
 932     size_t Convert(char *buf
, const char *psz
, size_t n
) 
 934         size_t inbuf 
= strlen(psz
); 
 936 #ifdef WX_ICONV_TAKES_CHAR 
 937         size_t res 
= iconv( cnv
, (char**)&psz
, &inbuf
, &buf
, &outbuf 
); 
 939         size_t res 
= iconv( cnv
, &psz
, &inbuf
, &buf
, &outbuf 
); 
 941         if (res 
== (size_t)-1) 
 950 #endif // HAVE_ICONV_H 
 952 class EC_CharSetConverter
 
 955     EC_CharSetConverter(EC_CharSet
* from
,EC_CharSet
* to
) 
 956         { cnv
.Init(from
->enc
,to
->enc
); } 
 958     size_t Convert(char* buf
, const char* psz
, size_t n
) 
 960         size_t inbuf 
= strlen(psz
); 
 961         if (buf
) cnv
.Convert(psz
,buf
); 
 966     wxEncodingConverter cnv
; 
 969 #else // !wxUSE_WCHAR_T 
 971 // ---------------------------------------------------------------------------- 
 972 // stand-ins in absence of wchar_t 
 973 // ---------------------------------------------------------------------------- 
 975 WXDLLEXPORT_DATA(wxMBConv
) wxConvLibc
, wxConvFile
; 
 977 #endif // wxUSE_WCHAR_T