]>
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"
37 #include "wx/msw/private.h"
45 #include "wx/module.h"
46 #include "wx/strconv.h"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
53 WXDLLEXPORT_DATA(wxMBConv
) wxConvLibc
;
54 WXDLLEXPORT_DATA(wxCSConv
) wxConvLocal((const wxChar
*)NULL
);
56 // stand-ins in absence of wchar_t
57 WXDLLEXPORT_DATA(wxMBConv
) wxConvLibc
, wxConvFile
, wxConvLocal
;
58 #endif // wxUSE_WCHAR_T
60 WXDLLEXPORT_DATA(wxMBConv
*) wxConvCurrent
= &wxConvLibc
;
62 class wxStrConvModule
: public wxModule
65 wxStrConvModule() : wxModule() { }
66 virtual bool OnInit() { return TRUE
; }
74 DECLARE_DYNAMIC_CLASS(wxStrConvModule
)
77 IMPLEMENT_DYNAMIC_CLASS(wxStrConvModule
, wxModule
)
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
94 #include "wx/encconv.h"
95 #include "wx/fontmap.h"
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 #define BSWAP_UCS4(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT32_SWAP_ALWAYS(str[_c]); }
102 #define BSWAP_UTF16(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT16_SWAP_ALWAYS(str[_c]); }
104 // under Unix SIZEOF_WCHAR_T is defined by configure, but under other platforms
105 // it might be not defined - assume the most common value
106 #ifndef SIZEOF_WCHAR_T
107 #define SIZEOF_WCHAR_T 2
108 #endif // !defined(SIZEOF_WCHAR_T)
110 #if SIZEOF_WCHAR_T == 4
111 #define WC_NAME "UCS4"
112 #define WC_BSWAP BSWAP_UCS4
113 #ifdef WORDS_BIGENDIAN
114 #define WC_NAME_BEST "UCS-4BE"
116 #define WC_NAME_BEST "UCS-4LE"
118 #elif SIZEOF_WCHAR_T == 2
119 #define WC_NAME "UTF16"
120 #define WC_BSWAP BSWAP_UTF16
122 #ifdef WORDS_BIGENDIAN
123 #define WC_NAME_BEST "UTF-16BE"
125 #define WC_NAME_BEST "UTF-16LE"
127 #else // sizeof(wchar_t) != 2 nor 4
128 // I don't know what to do about this
129 #error "Weird sizeof(wchar_t): please report your platform details to wx-users mailing list"
132 // ============================================================================
134 // ============================================================================
136 // ----------------------------------------------------------------------------
137 // UTF-16 en/decoding
138 // ----------------------------------------------------------------------------
142 static size_t encode_utf16(wxUint32 input
, wchar_t *output
)
146 if (output
) *output
++ = (wchar_t) input
;
149 else if (input
>=0x110000)
157 *output
++ = (wchar_t) ((input
>> 10)+0xd7c0);
158 *output
++ = (wchar_t) ((input
&0x3ff)+0xdc00);
164 static size_t decode_utf16(const wchar_t* input
, wxUint32
& output
)
166 if ((*input
<0xd800) || (*input
>0xdfff))
171 else if ((input
[1]<0xdc00) || (input
[1]>=0xdfff))
178 output
= ((input
[0] - 0xd7c0) << 10) + (input
[1] - 0xdc00);
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
189 #define IGNORE_LIBC 0
191 size_t wxMBConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
196 for (size_t i
= 0; i
< strlen( psz
)+1; i
++)
197 buf
[i
] = (wchar_t) psz
[i
];
198 return strlen( psz
);
202 return strlen( psz
);
205 return wxMB2WC(buf
, psz
, n
);
209 size_t wxMBConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
214 for (size_t i
= 0; i
< wxStrlen( psz
)+1; i
++)
215 buf
[i
] = (char) psz
[i
];
216 return wxStrlen( psz
);
220 return wxStrlen( psz
);
223 return wxWC2MB(buf
, psz
, n
);
227 const wxWCharBuffer
wxMBConv::cMB2WC(const char *psz
) const
231 size_t nLen
= MB2WC((wchar_t *) NULL
, psz
, 0); // return value excludes /0
232 if (nLen
== (size_t)-1)
233 return wxWCharBuffer((wchar_t *) NULL
);
234 wxWCharBuffer
buf(nLen
); // this allocates nLen1+
235 MB2WC((wchar_t *)(const wchar_t *) buf
, psz
, nLen
+1);
239 return wxWCharBuffer((wchar_t *) NULL
);
242 const wxCharBuffer
wxMBConv::cWC2MB(const wchar_t *pwz
) const
244 // return value excludes NUL
245 size_t nLen
= pwz
? WC2MB((char *) NULL
, pwz
, 0) : (size_t)-1;
246 if (nLen
== (size_t)-1)
247 return wxCharBuffer((const char *)NULL
);
249 wxCharBuffer
buf(nLen
); // this allocates nLen+1
250 WC2MB((char *)(const char *) buf
, pwz
, nLen
+1);
254 // ----------------------------------------------------------------------------
255 // standard gdk conversion
256 // ----------------------------------------------------------------------------
260 WXDLLEXPORT_DATA(wxMBConvGdk
) wxConvGdk
;
264 size_t wxMBConvGdk::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
268 return gdk_mbstowcs((GdkWChar
*)buf
, psz
, n
);
272 GdkWChar
*nbuf
= new GdkWChar
[n
=strlen(psz
)];
273 size_t len
= gdk_mbstowcs(nbuf
, psz
, n
);
279 size_t wxMBConvGdk::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
281 char *mbstr
= gdk_wcstombs((GdkWChar
*)psz
);
282 size_t len
= mbstr
? strlen(mbstr
) : 0;
287 memcpy(buf
, psz
, len
);
296 // ----------------------------------------------------------------------------
298 // ----------------------------------------------------------------------------
300 WXDLLEXPORT_DATA(wxMBConvUTF7
) wxConvUTF7
;
303 static char utf7_setD
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
304 "abcdefghijklmnopqrstuvwxyz"
305 "0123456789'(),-./:?";
306 static char utf7_setO
[]="!\"#$%&*;<=>@[]^_`{|}";
307 static char utf7_setB
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
308 "abcdefghijklmnopqrstuvwxyz"
312 // TODO: write actual implementations of UTF-7 here
313 size_t wxMBConvUTF7::MB2WC(wchar_t * WXUNUSED(buf
),
314 const char * WXUNUSED(psz
),
315 size_t WXUNUSED(n
)) const
320 size_t wxMBConvUTF7::WC2MB(char * WXUNUSED(buf
),
321 const wchar_t * WXUNUSED(psz
),
322 size_t WXUNUSED(n
)) const
327 // ----------------------------------------------------------------------------
329 // ----------------------------------------------------------------------------
331 WXDLLEXPORT_DATA(wxMBConvUTF8
) wxConvUTF8
;
333 static wxUint32 utf8_max
[]=
334 { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff, 0xffffffff };
336 size_t wxMBConvUTF8::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
340 while (*psz
&& ((!buf
) || (len
< n
)))
342 unsigned char cc
= *psz
++, fc
= cc
;
344 for (cnt
= 0; fc
& 0x80; cnt
++)
358 // invalid UTF-8 sequence
363 unsigned ocnt
= cnt
- 1;
364 wxUint32 res
= cc
& (0x3f >> cnt
);
368 if ((cc
& 0xC0) != 0x80)
370 // invalid UTF-8 sequence
373 res
= (res
<< 6) | (cc
& 0x3f);
375 if (res
<= utf8_max
[ocnt
])
377 // illegal UTF-8 encoding
381 size_t pa
= encode_utf16(res
, buf
);
382 if (pa
== (size_t)-1)
391 #endif // WC_UTF16/!WC_UTF16
395 if (buf
&& (len
< n
))
400 size_t wxMBConvUTF8::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
404 while (*psz
&& ((!buf
) || (len
< n
)))
408 size_t pa
= decode_utf16(psz
, cc
);
409 psz
+= (pa
== (size_t)-1) ? 1 : pa
;
411 cc
=(*psz
++) & 0x7fffffff;
414 for (cnt
= 0; cc
> utf8_max
[cnt
]; cnt
++) {}
428 *buf
++ = (char) ((-128 >> cnt
) | ((cc
>> (cnt
* 6)) & (0x3f >> cnt
)));
430 *buf
++ = (char) (0x80 | ((cc
>> (cnt
* 6)) & 0x3f));
435 if (buf
&& (len
<n
)) *buf
= 0;
440 // ============================================================================
441 // wxCharacterSet and derived classes
442 // ============================================================================
444 // ----------------------------------------------------------------------------
445 // wxCharacterSet is the ABC for the classes below
446 // ----------------------------------------------------------------------------
451 wxCharacterSet(const wxChar
*name
) : cname(name
) {}
452 virtual ~wxCharacterSet() {}
453 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) = 0;
454 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) = 0;
455 virtual bool usable() const = 0;
460 // ----------------------------------------------------------------------------
461 // ID_CharSet: implementation of wxCharacterSet using an existing wxMBConv
462 // ----------------------------------------------------------------------------
464 class ID_CharSet
: public wxCharacterSet
467 ID_CharSet(const wxChar
*name
, wxMBConv
*cnv
)
468 : wxCharacterSet(name
), work(cnv
) {}
470 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
471 { return work
? work
->MB2WC(buf
,psz
,n
) : (size_t)-1; }
473 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
474 { return work
? work
->WC2MB(buf
,psz
,n
) : (size_t)-1; }
477 { return work
!=NULL
; }
483 // ============================================================================
484 // The classes doing conversion using the iconv_xxx() functions
485 // ============================================================================
489 // VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with E2BIG
490 // if output buffer is _exactly_ as big as needed. Such case is (unless there's
491 // yet another bug in glibc) the only case when iconv() returns with (size_t)-1
492 // (which means error) and says there are 0 bytes left in the input buffer --
493 // when _real_ error occurs, bytes-left-in-input buffer is non-zero. Hence,
494 // this alternative test for iconv() failure.
495 // [This bug does not appear in glibc 2.2.]
496 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 1
497 #define ICONV_FAILED(cres, bufLeft) ((cres == (size_t)-1) && \
498 (errno != E2BIG || bufLeft != 0))
500 #define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1)
503 #define ICONV_CHAR_CAST(x) ((ICONV_CONST char **)(x))
505 // ----------------------------------------------------------------------------
506 // IC_CharSet: encapsulates an iconv character set
507 // ----------------------------------------------------------------------------
509 class IC_CharSet
: public wxCharacterSet
512 IC_CharSet(const wxChar
*name
);
513 virtual ~IC_CharSet();
515 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
);
516 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
);
519 { return (m2w
!= (iconv_t
)-1) && (w2m
!= (iconv_t
)-1); }
522 // the iconv handlers used to translate from multibyte to wide char and in
523 // the other direction
528 // the name (for iconv_open()) of a wide char charset - if none is
529 // available on this machine, it will remain NULL
530 static const char *ms_wcCharsetName
;
532 // true if the wide char encoding we use (i.e. ms_wcCharsetName) has
533 // different endian-ness than the native one
534 static bool ms_wcNeedsSwap
;
537 const char *IC_CharSet::ms_wcCharsetName
= NULL
;
538 bool IC_CharSet::ms_wcNeedsSwap
= FALSE
;
540 IC_CharSet::IC_CharSet(const wxChar
*name
)
541 : wxCharacterSet(name
)
543 // Do it the hard way
545 for (size_t i
= 0; i
< wxStrlen(name
)+1; i
++)
546 cname
[i
] = (char) name
[i
];
548 // check for charset that represents wchar_t:
549 if (ms_wcCharsetName
== NULL
)
551 ms_wcNeedsSwap
= FALSE
;
553 // try charset with explicit bytesex info (e.g. "UCS-4LE"):
554 ms_wcCharsetName
= WC_NAME_BEST
;
555 m2w
= iconv_open(ms_wcCharsetName
, cname
);
557 if (m2w
== (iconv_t
)-1)
559 // try charset w/o bytesex info (e.g. "UCS4")
560 // and check for bytesex ourselves:
561 ms_wcCharsetName
= WC_NAME
;
562 m2w
= iconv_open(ms_wcCharsetName
, cname
);
564 // last bet, try if it knows WCHAR_T pseudo-charset
565 if (m2w
== (iconv_t
)-1)
567 ms_wcCharsetName
= "WCHAR_T";
568 m2w
= iconv_open(ms_wcCharsetName
, cname
);
571 if (m2w
!= (iconv_t
)-1)
573 char buf
[2], *bufPtr
;
574 wchar_t wbuf
[2], *wbufPtr
;
582 outsz
= SIZEOF_WCHAR_T
* 2;
586 res
= iconv(m2w
, ICONV_CHAR_CAST(&bufPtr
), &insz
,
587 (char**)&wbufPtr
, &outsz
);
589 if (ICONV_FAILED(res
, insz
))
591 ms_wcCharsetName
= NULL
;
592 wxLogLastError(wxT("iconv"));
593 wxLogError(_("Convertion to charset '%s' doesn't work."), name
);
597 ms_wcNeedsSwap
= wbuf
[0] != (wchar_t)buf
[0];
602 ms_wcCharsetName
= NULL
;
604 // VS: we must not output an error here, since wxWindows will safely
605 // fall back to using wxEncodingConverter.
606 wxLogTrace(wxT("strconv"), wxT("Impossible to convert to/from charset '%s' with iconv, falling back to wxEncodingConverter."), name
);
610 wxLogTrace(wxT("strconv"), wxT("wchar_t charset is '%s', needs swap: %i"), ms_wcCharsetName
, ms_wcNeedsSwap
);
612 else // we already have ms_wcCharsetName
614 m2w
= iconv_open(ms_wcCharsetName
, cname
);
617 // NB: don't ever pass NULL to iconv_open(), it may crash!
618 if ( ms_wcCharsetName
)
620 w2m
= iconv_open( cname
, ms_wcCharsetName
);
628 IC_CharSet::~IC_CharSet()
630 if ( m2w
!= (iconv_t
)-1 )
632 if ( w2m
!= (iconv_t
)-1 )
636 size_t IC_CharSet::MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
638 size_t inbuf
= strlen(psz
);
639 size_t outbuf
= n
* SIZEOF_WCHAR_T
;
641 // VS: Use these instead of psz, buf because iconv() modifies its arguments:
642 wchar_t *bufPtr
= buf
;
643 const char *pszPtr
= psz
;
647 // have destination buffer, convert there
649 ICONV_CHAR_CAST(&pszPtr
), &inbuf
,
650 (char**)&bufPtr
, &outbuf
);
651 res
= n
- (outbuf
/ SIZEOF_WCHAR_T
);
655 // convert to native endianness
656 WC_BSWAP(buf
/* _not_ bufPtr */, res
)
659 // NB: iconv was given only strlen(psz) characters on input, and so
660 // it couldn't convert the trailing zero. Let's do it ourselves
661 // if there's some room left for it in the output buffer.
667 // no destination buffer... convert using temp buffer
668 // to calculate destination buffer requirement
673 outbuf
= 8*SIZEOF_WCHAR_T
;
676 ICONV_CHAR_CAST(&pszPtr
), &inbuf
,
677 (char**)&bufPtr
, &outbuf
);
679 res
+= 8-(outbuf
/SIZEOF_WCHAR_T
);
680 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
683 if (ICONV_FAILED(cres
, inbuf
))
685 //VS: it is ok if iconv fails, hence trace only
686 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
693 size_t IC_CharSet::WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
695 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
696 size_t inbuf
= std::wcslen(psz
) * SIZEOF_WCHAR_T
;
698 size_t inbuf
= ::wcslen(psz
) * SIZEOF_WCHAR_T
;
707 // need to copy to temp buffer to switch endianness
708 // this absolutely doesn't rock!
709 // (no, doing WC_BSWAP twice on the original buffer won't help, as it
710 // could be in read-only memory, or be accessed in some other thread)
711 tmpbuf
=(wchar_t*)malloc((inbuf
+1)*SIZEOF_WCHAR_T
);
712 memcpy(tmpbuf
,psz
,(inbuf
+1)*SIZEOF_WCHAR_T
);
713 WC_BSWAP(tmpbuf
, inbuf
)
719 // have destination buffer, convert there
720 cres
= iconv( w2m
, ICONV_CHAR_CAST(&psz
), &inbuf
, &buf
, &outbuf
);
724 // NB: iconv was given only wcslen(psz) characters on input, and so
725 // it couldn't convert the trailing zero. Let's do it ourselves
726 // if there's some room left for it in the output buffer.
732 // no destination buffer... convert using temp buffer
733 // to calculate destination buffer requirement
737 buf
= tbuf
; outbuf
= 16;
739 cres
= iconv( w2m
, ICONV_CHAR_CAST(&psz
), &inbuf
, &buf
, &outbuf
);
742 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
750 if (ICONV_FAILED(cres
, inbuf
))
752 //VS: it is ok if iconv fails, hence trace only
753 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
762 // ============================================================================
763 // Win32 conversion classes
764 // ============================================================================
766 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
768 extern long wxCharsetToCodepage(const wxChar
*charset
); // from utils.cpp
770 class CP_CharSet
: public wxCharacterSet
773 CP_CharSet(const wxChar
* name
)
774 : wxCharacterSet(name
)
776 m_CodePage
= wxCharsetToCodepage(name
);
779 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
783 MultiByteToWideChar(m_CodePage
, 0, psz
, -1, (WCHAR
*) buf
, buf
? n
: 0);
785 MultiByteToWideChar(m_CodePage
, 0, psz
, -1, buf
, buf
? n
: 0);
787 //VS: returns # of written chars for buf!=NULL and *size*
788 // needed buffer for buf==NULL
789 return len
? (buf
? len
: len
-1) : (size_t)-1;
792 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
795 size_t len
= WideCharToMultiByte(m_CodePage
, 0, (const WCHAR
*) psz
, -1, buf
,
796 buf
? n
: 0, NULL
, NULL
);
798 size_t len
= WideCharToMultiByte(m_CodePage
, 0, psz
, -1, buf
,
799 buf
? n
: 0, NULL
, NULL
);
801 //VS: returns # of written chars for buf!=NULL and *size*
802 // needed buffer for buf==NULL
803 return len
? (buf
? len
: len
-1) : (size_t)-1;
807 { return m_CodePage
!= -1; }
814 // ============================================================================
815 // wxEncodingConverter based conversion classes
816 // ============================================================================
820 class EC_CharSet
: public wxCharacterSet
823 // temporarily just use wxEncodingConverter stuff,
824 // so that it works while a better implementation is built
825 EC_CharSet(const wxChar
* name
) : wxCharacterSet(name
),
826 enc(wxFONTENCODING_SYSTEM
)
829 enc
= wxFontMapper::Get()->CharsetToEncoding(name
, FALSE
);
831 m_ok
= m2w
.Init(enc
, wxFONTENCODING_UNICODE
) &&
832 w2m
.Init(wxFONTENCODING_UNICODE
, enc
);
835 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t WXUNUSED(n
))
837 size_t inbuf
= strlen(psz
);
839 m2w
.Convert(psz
,buf
);
843 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t WXUNUSED(n
))
845 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
846 || ( defined(__MWERKS__) && defined(__WXMSW__) )
847 size_t inbuf
= std::wcslen(psz
);
849 size_t inbuf
= ::wcslen(psz
);
852 w2m
.Convert(psz
,buf
);
857 bool usable() const { return m_ok
; }
861 wxEncodingConverter m2w
, w2m
;
863 // were we initialized successfully?
867 #endif // wxUSE_FONTMAP
869 // ----------------------------------------------------------------------------
870 // the function creating the wxCharacterSet for the specified charset on the
871 // current system, trying all possibilities
872 // ----------------------------------------------------------------------------
874 static wxCharacterSet
*wxGetCharacterSet(const wxChar
*name
)
876 // check for the special case of ASCII charset
878 if ( wxFontMapper::Get()->CharsetToEncoding(name
) == wxFONTENCODING_DEFAULT
)
879 #else // wxUSE_FONTMAP
881 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
883 // don't convert at all
887 // the test above must have taken care of this case
888 wxCHECK_MSG( name
, NULL
, _T("NULL name must be wxFONTENCODING_DEFAULT") );
890 wxCharacterSet
*cset
;
892 if ( wxStricmp(name
, wxT("UTF8")) == 0 || wxStricmp(name
, wxT("UTF-8")) == 0)
894 cset
= new ID_CharSet(name
, &wxConvUTF8
);
899 cset
= new IC_CharSet(name
);
902 #endif // HAVE_ICONV/!HAVE_ICONV
905 // it can only be NULL in this case
908 #endif // !HAVE_ICONV
910 if ( cset
->usable() )
917 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
918 cset
= new CP_CharSet(name
);
919 if ( cset
->usable() )
927 cset
= new EC_CharSet(name
);
928 if ( cset
->usable() )
933 #endif // wxUSE_FONTMAP
935 wxLogError(_("Cannot convert from encoding '%s'!"), name
);
940 // ============================================================================
941 // wxCSConv implementation
942 // ============================================================================
944 wxCSConv::wxCSConv(const wxChar
*charset
)
946 m_name
= (wxChar
*)NULL
;
947 m_cset
= (wxCharacterSet
*) NULL
;
953 wxCSConv::~wxCSConv()
958 wxCSConv::wxCSConv(const wxCSConv
& conv
)
962 SetName(conv
.m_name
);
965 wxCSConv
& wxCSConv::operator=(const wxCSConv
& conv
)
968 SetName(conv
.m_name
);
972 void wxCSConv::Clear()
982 void wxCSConv::SetName(const wxChar
*charset
)
986 m_name
= wxStrdup(charset
);
991 void wxCSConv::LoadNow()
997 wxString name
= wxLocale::GetSystemEncodingName();
1004 // wxGetCharacterSet() complains about NULL name
1005 m_cset
= m_name
? wxGetCharacterSet(m_name
) : NULL
;
1010 size_t wxCSConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
1012 ((wxCSConv
*)this)->LoadNow(); // discard constness
1015 return m_cset
->MB2WC(buf
, psz
, n
);
1018 size_t len
= strlen(psz
);
1022 for (size_t c
= 0; c
<= len
; c
++)
1023 buf
[c
] = (unsigned char)(psz
[c
]);
1029 size_t wxCSConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
1031 ((wxCSConv
*)this)->LoadNow(); // discard constness
1034 return m_cset
->WC2MB(buf
, psz
, n
);
1037 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
1038 || ( defined(__MWERKS__) && defined(__WXMSW__) )
1039 size_t len
=std::wcslen(psz
);
1041 size_t len
=::wcslen(psz
);
1045 for (size_t c
= 0; c
<= len
; c
++)
1046 buf
[c
] = (psz
[c
] > 0xff) ? '?' : psz
[c
];
1052 #endif // wxUSE_WCHAR_T