]>
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
;
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 // printf( "libc %s\n", buf );
199 return strlen( psz
);
203 return strlen( psz
);
206 return wxMB2WC(buf
, psz
, n
);
210 size_t wxMBConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
215 for (size_t i
= 0; i
< wxStrlen( psz
)+1; i
++)
216 buf
[i
] = (char) psz
[i
];
217 // printf( "libc %s\n", buf );
218 return wxStrlen( psz
);
222 return wxStrlen( psz
);
225 return wxWC2MB(buf
, psz
, n
);
229 const wxWCharBuffer
wxMBConv::cMB2WC(const char *psz
) const
233 size_t nLen
= MB2WC((wchar_t *) NULL
, psz
, 0); // return value excludes /0
234 if (nLen
== (size_t)-1)
235 return wxWCharBuffer((wchar_t *) NULL
);
236 wxWCharBuffer
buf(nLen
); // this allocates nLen1+
237 MB2WC((wchar_t *)(const wchar_t *) buf
, psz
, nLen
+1);
241 return wxWCharBuffer((wchar_t *) NULL
);
244 const wxCharBuffer
wxMBConv::cWC2MB(const wchar_t *psz
) const
248 size_t nLen
= WC2MB((char *) NULL
, psz
, 0); // return value excludes /0
249 if (nLen
== (size_t)-1)
250 return wxCharBuffer((char *) NULL
);
251 wxCharBuffer
buf(nLen
); // this allocates nLen+1
252 WC2MB((char *)(const char *) buf
, psz
, nLen
+1);
253 // printf( "str %s\n", (const char*) buf );
257 return wxCharBuffer((char *) NULL
);
260 // ----------------------------------------------------------------------------
261 // standard gdk conversion
262 // ----------------------------------------------------------------------------
266 WXDLLEXPORT_DATA(wxMBConvGdk
) wxConvGdk
;
270 size_t wxMBConvGdk::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
274 return gdk_mbstowcs((GdkWChar
*)buf
, psz
, n
);
278 GdkWChar
*nbuf
= new GdkWChar
[n
=strlen(psz
)];
279 size_t len
= gdk_mbstowcs(nbuf
, psz
, n
);
285 size_t wxMBConvGdk::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
287 char *mbstr
= gdk_wcstombs((GdkWChar
*)psz
);
288 size_t len
= mbstr
? strlen(mbstr
) : 0;
293 memcpy(buf
, psz
, len
);
302 // ----------------------------------------------------------------------------
304 // ----------------------------------------------------------------------------
306 WXDLLEXPORT_DATA(wxMBConvUTF7
) wxConvUTF7
;
309 static char utf7_setD
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
310 "abcdefghijklmnopqrstuvwxyz"
311 "0123456789'(),-./:?";
312 static char utf7_setO
[]="!\"#$%&*;<=>@[]^_`{|}";
313 static char utf7_setB
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
314 "abcdefghijklmnopqrstuvwxyz"
318 // TODO: write actual implementations of UTF-7 here
319 size_t wxMBConvUTF7::MB2WC(wchar_t * WXUNUSED(buf
),
320 const char * WXUNUSED(psz
),
321 size_t WXUNUSED(n
)) const
326 size_t wxMBConvUTF7::WC2MB(char * WXUNUSED(buf
),
327 const wchar_t * WXUNUSED(psz
),
328 size_t WXUNUSED(n
)) const
333 // ----------------------------------------------------------------------------
335 // ----------------------------------------------------------------------------
337 WXDLLEXPORT_DATA(wxMBConvUTF8
) wxConvUTF8
;
339 static wxUint32 utf8_max
[]=
340 { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff, 0xffffffff };
342 size_t wxMBConvUTF8::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
346 while (*psz
&& ((!buf
) || (len
< n
)))
348 unsigned char cc
= *psz
++, fc
= cc
;
350 for (cnt
= 0; fc
& 0x80; cnt
++)
364 // invalid UTF-8 sequence
369 unsigned ocnt
= cnt
- 1;
370 wxUint32 res
= cc
& (0x3f >> cnt
);
374 if ((cc
& 0xC0) != 0x80)
376 // invalid UTF-8 sequence
379 res
= (res
<< 6) | (cc
& 0x3f);
381 if (res
<= utf8_max
[ocnt
])
383 // illegal UTF-8 encoding
387 size_t pa
= encode_utf16(res
, buf
);
388 if (pa
== (size_t)-1)
397 #endif // WC_UTF16/!WC_UTF16
401 if (buf
&& (len
< n
))
406 size_t wxMBConvUTF8::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
410 while (*psz
&& ((!buf
) || (len
< n
)))
414 size_t pa
= decode_utf16(psz
, cc
);
415 psz
+= (pa
== (size_t)-1) ? 1 : pa
;
417 cc
=(*psz
++) & 0x7fffffff;
420 for (cnt
= 0; cc
> utf8_max
[cnt
]; cnt
++) {}
434 *buf
++ = (char) ((-128 >> cnt
) | ((cc
>> (cnt
* 6)) & (0x3f >> cnt
)));
436 *buf
++ = (char) (0x80 | ((cc
>> (cnt
* 6)) & 0x3f));
441 if (buf
&& (len
<n
)) *buf
= 0;
446 // ============================================================================
447 // wxCharacterSet and derived classes
448 // ============================================================================
450 // ----------------------------------------------------------------------------
451 // wxCharacterSet is the ABC for the classes below
452 // ----------------------------------------------------------------------------
457 wxCharacterSet(const wxChar
*name
) : cname(name
) {}
458 virtual ~wxCharacterSet() {}
459 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) = 0;
460 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) = 0;
461 virtual bool usable() const = 0;
466 // ----------------------------------------------------------------------------
467 // ID_CharSet: implementation of wxCharacterSet using an existing wxMBConv
468 // ----------------------------------------------------------------------------
470 class ID_CharSet
: public wxCharacterSet
473 ID_CharSet(const wxChar
*name
, wxMBConv
*cnv
)
474 : wxCharacterSet(name
), work(cnv
) {}
476 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
477 { return work
? work
->MB2WC(buf
,psz
,n
) : (size_t)-1; }
479 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
480 { return work
? work
->WC2MB(buf
,psz
,n
) : (size_t)-1; }
483 { return work
!=NULL
; }
489 // ============================================================================
490 // The classes doing conversion using the iconv_xxx() functions
491 // ============================================================================
495 // VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with E2BIG
496 // if output buffer is _exactly_ as big as needed. Such case is (unless there's
497 // yet another bug in glibc) the only case when iconv() returns with (size_t)-1
498 // (which means error) and says there are 0 bytes left in the input buffer --
499 // when _real_ error occurs, bytes-left-in-input buffer is non-zero. Hence,
500 // this alternative test for iconv() failure.
501 // [This bug does not appear in glibc 2.2.]
502 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 1
503 #define ICONV_FAILED(cres, bufLeft) ((cres == (size_t)-1) && \
504 (errno != E2BIG || bufLeft != 0))
506 #define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1)
509 #define ICONV_CHAR_CAST(x) ((ICONV_CONST char **)(x))
511 // ----------------------------------------------------------------------------
512 // IC_CharSet: encapsulates an iconv character set
513 // ----------------------------------------------------------------------------
515 class IC_CharSet
: public wxCharacterSet
518 IC_CharSet(const wxChar
*name
);
519 virtual ~IC_CharSet();
521 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
);
522 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
);
525 { return (m2w
!= (iconv_t
)-1) && (w2m
!= (iconv_t
)-1); }
528 // the iconv handlers used to translate from multibyte to wide char and in
529 // the other direction
534 // the name (for iconv_open()) of a wide char charset - if none is
535 // available on this machine, it will remain NULL
536 static const char *ms_wcCharsetName
;
538 // true if the wide char encoding we use (i.e. ms_wcCharsetName) has
539 // different endian-ness than the native one
540 static bool ms_wcNeedsSwap
;
543 const char *IC_CharSet::ms_wcCharsetName
= NULL
;
544 bool IC_CharSet::ms_wcNeedsSwap
= FALSE
;
546 IC_CharSet::IC_CharSet(const wxChar
*name
)
547 : wxCharacterSet(name
)
549 // Do it the hard way
551 for (size_t i
= 0; i
< wxStrlen(name
)+1; i
++)
552 cname
[i
] = (char) name
[i
];
554 // check for charset that represents wchar_t:
555 if (ms_wcCharsetName
== NULL
)
557 ms_wcNeedsSwap
= FALSE
;
559 // try charset with explicit bytesex info (e.g. "UCS-4LE"):
560 ms_wcCharsetName
= WC_NAME_BEST
;
561 m2w
= iconv_open(ms_wcCharsetName
, cname
);
563 if (m2w
== (iconv_t
)-1)
565 // try charset w/o bytesex info (e.g. "UCS4")
566 // and check for bytesex ourselves:
567 ms_wcCharsetName
= WC_NAME
;
568 m2w
= iconv_open(ms_wcCharsetName
, cname
);
570 // last bet, try if it knows WCHAR_T pseudo-charset
571 if (m2w
== (iconv_t
)-1)
573 ms_wcCharsetName
= "WCHAR_T";
574 m2w
= iconv_open(ms_wcCharsetName
, cname
);
577 if (m2w
!= (iconv_t
)-1)
579 char buf
[2], *bufPtr
;
580 wchar_t wbuf
[2], *wbufPtr
;
588 outsz
= SIZEOF_WCHAR_T
* 2;
592 res
= iconv(m2w
, ICONV_CHAR_CAST(&bufPtr
), &insz
,
593 (char**)&wbufPtr
, &outsz
);
595 if (ICONV_FAILED(res
, insz
))
597 ms_wcCharsetName
= NULL
;
598 wxLogLastError(wxT("iconv"));
599 wxLogError(_("Convertion to charset '%s' doesn't work."), name
);
603 ms_wcNeedsSwap
= wbuf
[0] != (wchar_t)buf
[0];
608 ms_wcCharsetName
= NULL
;
610 // VS: we must not output an error here, since wxWindows will safely
611 // fall back to using wxEncodingConverter.
612 wxLogTrace(wxT("strconv"), wxT("Impossible to convert to/from charset '%s' with iconv, falling back to wxEncodingConverter."), name
);
616 wxLogTrace(wxT("strconv"), wxT("wchar_t charset is '%s', needs swap: %i"), ms_wcCharsetName
, ms_wcNeedsSwap
);
618 else // we already have ms_wcCharsetName
620 m2w
= iconv_open(ms_wcCharsetName
, cname
);
623 // NB: don't ever pass NULL to iconv_open(), it may crash!
624 if ( ms_wcCharsetName
)
626 w2m
= iconv_open( cname
, ms_wcCharsetName
);
634 IC_CharSet::~IC_CharSet()
636 if ( m2w
!= (iconv_t
)-1 )
638 if ( w2m
!= (iconv_t
)-1 )
642 size_t IC_CharSet::MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
644 size_t inbuf
= strlen(psz
);
645 size_t outbuf
= n
* SIZEOF_WCHAR_T
;
647 // VS: Use these instead of psz, buf because iconv() modifies its arguments:
648 wchar_t *bufPtr
= buf
;
649 const char *pszPtr
= psz
;
653 // have destination buffer, convert there
655 ICONV_CHAR_CAST(&pszPtr
), &inbuf
,
656 (char**)&bufPtr
, &outbuf
);
657 res
= n
- (outbuf
/ SIZEOF_WCHAR_T
);
661 // convert to native endianness
662 WC_BSWAP(buf
/* _not_ bufPtr */, res
)
665 // NB: iconv was given only strlen(psz) characters on input, and so
666 // it couldn't convert the trailing zero. Let's do it ourselves
667 // if there's some room left for it in the output buffer.
673 // no destination buffer... convert using temp buffer
674 // to calculate destination buffer requirement
679 outbuf
= 8*SIZEOF_WCHAR_T
;
682 ICONV_CHAR_CAST(&pszPtr
), &inbuf
,
683 (char**)&bufPtr
, &outbuf
);
685 res
+= 8-(outbuf
/SIZEOF_WCHAR_T
);
686 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
689 if (ICONV_FAILED(cres
, inbuf
))
691 //VS: it is ok if iconv fails, hence trace only
692 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
699 size_t IC_CharSet::WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
701 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
702 size_t inbuf
= std::wcslen(psz
) * SIZEOF_WCHAR_T
;
704 size_t inbuf
= ::wcslen(psz
) * SIZEOF_WCHAR_T
;
713 // need to copy to temp buffer to switch endianness
714 // this absolutely doesn't rock!
715 // (no, doing WC_BSWAP twice on the original buffer won't help, as it
716 // could be in read-only memory, or be accessed in some other thread)
717 tmpbuf
=(wchar_t*)malloc((inbuf
+1)*SIZEOF_WCHAR_T
);
718 memcpy(tmpbuf
,psz
,(inbuf
+1)*SIZEOF_WCHAR_T
);
719 WC_BSWAP(tmpbuf
, inbuf
)
725 // have destination buffer, convert there
726 cres
= iconv( w2m
, ICONV_CHAR_CAST(&psz
), &inbuf
, &buf
, &outbuf
);
730 // NB: iconv was given only wcslen(psz) characters on input, and so
731 // it couldn't convert the trailing zero. Let's do it ourselves
732 // if there's some room left for it in the output buffer.
738 // no destination buffer... convert using temp buffer
739 // to calculate destination buffer requirement
743 buf
= tbuf
; outbuf
= 16;
745 cres
= iconv( w2m
, ICONV_CHAR_CAST(&psz
), &inbuf
, &buf
, &outbuf
);
748 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
756 if (ICONV_FAILED(cres
, inbuf
))
758 //VS: it is ok if iconv fails, hence trace only
759 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
768 // ============================================================================
769 // Win32 conversion classes
770 // ============================================================================
772 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
774 extern long wxCharsetToCodepage(const wxChar
*charset
); // from utils.cpp
776 class CP_CharSet
: public wxCharacterSet
779 CP_CharSet(const wxChar
* name
)
780 : wxCharacterSet(name
)
782 m_CodePage
= wxCharsetToCodepage(name
);
785 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
788 MultiByteToWideChar(m_CodePage
, 0, psz
, -1, buf
, buf
? n
: 0);
789 //VS: returns # of written chars for buf!=NULL and *size*
790 // needed buffer for buf==NULL
791 return len
? (buf
? len
: len
-1) : (size_t)-1;
794 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
796 size_t len
= WideCharToMultiByte(m_CodePage
, 0, psz
, -1, buf
,
797 buf
? n
: 0, NULL
, NULL
);
798 //VS: returns # of written chars for buf!=NULL and *size*
799 // needed buffer for buf==NULL
800 return len
? (buf
? len
: len
-1) : (size_t)-1;
804 { return m_CodePage
!= -1; }
811 // ============================================================================
812 // wxEncodingConverter based conversion classes
813 // ============================================================================
817 class EC_CharSet
: public wxCharacterSet
820 // temporarily just use wxEncodingConverter stuff,
821 // so that it works while a better implementation is built
822 EC_CharSet(const wxChar
* name
) : wxCharacterSet(name
),
823 enc(wxFONTENCODING_SYSTEM
)
826 enc
= wxFontMapper::Get()->CharsetToEncoding(name
, FALSE
);
828 m_ok
= m2w
.Init(enc
, wxFONTENCODING_UNICODE
) &&
829 w2m
.Init(wxFONTENCODING_UNICODE
, enc
);
832 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t WXUNUSED(n
))
834 size_t inbuf
= strlen(psz
);
836 m2w
.Convert(psz
,buf
);
840 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t WXUNUSED(n
))
842 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
843 || ( defined(__MWERKS__) && defined(__WXMSW__) )
844 size_t inbuf
= std::wcslen(psz
);
846 size_t inbuf
= ::wcslen(psz
);
849 w2m
.Convert(psz
,buf
);
854 bool usable() const { return m_ok
; }
858 wxEncodingConverter m2w
, w2m
;
860 // were we initialized successfully?
864 #endif // wxUSE_FONTMAP
866 // ----------------------------------------------------------------------------
867 // the function creating the wxCharacterSet for the specified charset on the
868 // current system, trying all possibilities
869 // ----------------------------------------------------------------------------
871 static wxCharacterSet
*wxGetCharacterSet(const wxChar
*name
)
873 // check for the special case of ASCII charset
875 if ( wxFontMapper::Get()->CharsetToEncoding(name
) == wxFONTENCODING_DEFAULT
)
876 #else // wxUSE_FONTMAP
878 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
880 // don't convert at all
884 // the test above must have taken care of this case
885 wxCHECK_MSG( name
, NULL
, _T("NULL name must be wxFONTENCODING_DEFAULT") );
887 wxCharacterSet
*cset
;
889 if ( wxStricmp(name
, wxT("UTF8")) == 0 || wxStricmp(name
, wxT("UTF-8")) == 0)
891 cset
= new ID_CharSet(name
, &wxConvUTF8
);
896 cset
= new IC_CharSet(name
);
899 #endif // HAVE_ICONV/!HAVE_ICONV
902 // it can only be NULL in this case
905 #endif // !HAVE_ICONV
907 if ( cset
->usable() )
914 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
915 cset
= new CP_CharSet(name
);
916 if ( cset
->usable() )
924 cset
= new EC_CharSet(name
);
925 if ( cset
->usable() )
930 #endif // wxUSE_FONTMAP
932 wxLogError(_("Cannot convert from encoding '%s'!"), name
);
937 // ============================================================================
938 // wxCSConv implementation
939 // ============================================================================
941 wxCSConv::wxCSConv(const wxChar
*charset
)
943 m_name
= (wxChar
*)NULL
;
944 m_cset
= (wxCharacterSet
*) NULL
;
950 wxCSConv::~wxCSConv()
955 wxCSConv::wxCSConv(const wxCSConv
& conv
)
959 SetName(conv
.m_name
);
962 wxCSConv
& wxCSConv::operator=(const wxCSConv
& conv
)
965 SetName(conv
.m_name
);
969 void wxCSConv::Clear()
979 void wxCSConv::SetName(const wxChar
*charset
)
983 m_name
= wxStrdup(charset
);
988 void wxCSConv::LoadNow()
994 wxString name
= wxLocale::GetSystemEncodingName();
1001 // wxGetCharacterSet() complains about NULL name
1002 m_cset
= m_name
? wxGetCharacterSet(m_name
) : NULL
;
1007 size_t wxCSConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
1009 ((wxCSConv
*)this)->LoadNow(); // discard constness
1012 return m_cset
->MB2WC(buf
, psz
, n
);
1015 size_t len
= strlen(psz
);
1019 for (size_t c
= 0; c
<= len
; c
++)
1020 buf
[c
] = (unsigned char)(psz
[c
]);
1026 size_t wxCSConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
1028 ((wxCSConv
*)this)->LoadNow(); // discard constness
1031 return m_cset
->WC2MB(buf
, psz
, n
);
1034 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
1035 || ( defined(__MWERKS__) && defined(__WXMSW__) )
1036 size_t len
=std::wcslen(psz
);
1038 size_t len
=::wcslen(psz
);
1042 for (size_t c
= 0; c
<= len
; c
++)
1043 buf
[c
] = (psz
[c
] > 0xff) ? '?' : psz
[c
];
1049 #endif // wxUSE_WCHAR_T