]>
git.saurik.com Git - wxWidgets.git/blob - src/common/strconv.cpp
cf96614eef8034e73e0f9cce3ec9c62508e2a28d
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 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 *psz
) const
246 size_t nLen
= WC2MB((char *) NULL
, psz
, 0); // return value excludes /0
247 if (nLen
== (size_t)-1)
248 return wxCharBuffer((char *) NULL
);
249 wxCharBuffer
buf(nLen
); // this allocates nLen+1
250 WC2MB((char *)(const char *) buf
, psz
, nLen
+1);
254 return wxCharBuffer((char *) NULL
);
257 // ----------------------------------------------------------------------------
258 // standard gdk conversion
259 // ----------------------------------------------------------------------------
263 WXDLLEXPORT_DATA(wxMBConvGdk
) wxConvGdk
;
267 size_t wxMBConvGdk::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
271 return gdk_mbstowcs((GdkWChar
*)buf
, psz
, n
);
275 GdkWChar
*nbuf
= new GdkWChar
[n
=strlen(psz
)];
276 size_t len
= gdk_mbstowcs(nbuf
, psz
, n
);
282 size_t wxMBConvGdk::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
284 char *mbstr
= gdk_wcstombs((GdkWChar
*)psz
);
285 size_t len
= mbstr
? strlen(mbstr
) : 0;
290 memcpy(buf
, psz
, len
);
299 // ----------------------------------------------------------------------------
301 // ----------------------------------------------------------------------------
303 WXDLLEXPORT_DATA(wxMBConvUTF7
) wxConvUTF7
;
306 static char utf7_setD
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
307 "abcdefghijklmnopqrstuvwxyz"
308 "0123456789'(),-./:?";
309 static char utf7_setO
[]="!\"#$%&*;<=>@[]^_`{|}";
310 static char utf7_setB
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
311 "abcdefghijklmnopqrstuvwxyz"
315 // TODO: write actual implementations of UTF-7 here
316 size_t wxMBConvUTF7::MB2WC(wchar_t * WXUNUSED(buf
),
317 const char * WXUNUSED(psz
),
318 size_t WXUNUSED(n
)) const
323 size_t wxMBConvUTF7::WC2MB(char * WXUNUSED(buf
),
324 const wchar_t * WXUNUSED(psz
),
325 size_t WXUNUSED(n
)) const
330 // ----------------------------------------------------------------------------
332 // ----------------------------------------------------------------------------
334 WXDLLEXPORT_DATA(wxMBConvUTF8
) wxConvUTF8
;
336 static wxUint32 utf8_max
[]=
337 { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff, 0xffffffff };
339 size_t wxMBConvUTF8::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
343 while (*psz
&& ((!buf
) || (len
< n
)))
345 unsigned char cc
= *psz
++, fc
= cc
;
347 for (cnt
= 0; fc
& 0x80; cnt
++)
361 // invalid UTF-8 sequence
366 unsigned ocnt
= cnt
- 1;
367 wxUint32 res
= cc
& (0x3f >> cnt
);
371 if ((cc
& 0xC0) != 0x80)
373 // invalid UTF-8 sequence
376 res
= (res
<< 6) | (cc
& 0x3f);
378 if (res
<= utf8_max
[ocnt
])
380 // illegal UTF-8 encoding
384 size_t pa
= encode_utf16(res
, buf
);
385 if (pa
== (size_t)-1)
394 #endif // WC_UTF16/!WC_UTF16
398 if (buf
&& (len
< n
))
403 size_t wxMBConvUTF8::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
407 while (*psz
&& ((!buf
) || (len
< n
)))
411 size_t pa
= decode_utf16(psz
, cc
);
412 psz
+= (pa
== (size_t)-1) ? 1 : pa
;
414 cc
=(*psz
++) & 0x7fffffff;
417 for (cnt
= 0; cc
> utf8_max
[cnt
]; cnt
++) {}
431 *buf
++ = (char) ((-128 >> cnt
) | ((cc
>> (cnt
* 6)) & (0x3f >> cnt
)));
433 *buf
++ = (char) (0x80 | ((cc
>> (cnt
* 6)) & 0x3f));
438 if (buf
&& (len
<n
)) *buf
= 0;
443 // ============================================================================
444 // wxCharacterSet and derived classes
445 // ============================================================================
447 // ----------------------------------------------------------------------------
448 // wxCharacterSet is the ABC for the classes below
449 // ----------------------------------------------------------------------------
454 wxCharacterSet(const wxChar
*name
) : cname(name
) {}
455 virtual ~wxCharacterSet() {}
456 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) = 0;
457 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) = 0;
458 virtual bool usable() const = 0;
463 // ----------------------------------------------------------------------------
464 // ID_CharSet: implementation of wxCharacterSet using an existing wxMBConv
465 // ----------------------------------------------------------------------------
467 class ID_CharSet
: public wxCharacterSet
470 ID_CharSet(const wxChar
*name
, wxMBConv
*cnv
)
471 : wxCharacterSet(name
), work(cnv
) {}
473 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
474 { return work
? work
->MB2WC(buf
,psz
,n
) : (size_t)-1; }
476 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
477 { return work
? work
->WC2MB(buf
,psz
,n
) : (size_t)-1; }
480 { return work
!=NULL
; }
486 // ============================================================================
487 // The classes doing conversion using the iconv_xxx() functions
488 // ============================================================================
492 // VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with E2BIG
493 // if output buffer is _exactly_ as big as needed. Such case is (unless there's
494 // yet another bug in glibc) the only case when iconv() returns with (size_t)-1
495 // (which means error) and says there are 0 bytes left in the input buffer --
496 // when _real_ error occurs, bytes-left-in-input buffer is non-zero. Hence,
497 // this alternative test for iconv() failure.
498 // [This bug does not appear in glibc 2.2.]
499 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 1
500 #define ICONV_FAILED(cres, bufLeft) ((cres == (size_t)-1) && \
501 (errno != E2BIG || bufLeft != 0))
503 #define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1)
506 #define ICONV_CHAR_CAST(x) ((ICONV_CONST char **)(x))
508 // ----------------------------------------------------------------------------
509 // IC_CharSet: encapsulates an iconv character set
510 // ----------------------------------------------------------------------------
512 class IC_CharSet
: public wxCharacterSet
515 IC_CharSet(const wxChar
*name
);
516 virtual ~IC_CharSet();
518 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
);
519 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
);
522 { return (m2w
!= (iconv_t
)-1) && (w2m
!= (iconv_t
)-1); }
525 // the iconv handlers used to translate from multibyte to wide char and in
526 // the other direction
531 // the name (for iconv_open()) of a wide char charset - if none is
532 // available on this machine, it will remain NULL
533 static const char *ms_wcCharsetName
;
535 // true if the wide char encoding we use (i.e. ms_wcCharsetName) has
536 // different endian-ness than the native one
537 static bool ms_wcNeedsSwap
;
540 const char *IC_CharSet::ms_wcCharsetName
= NULL
;
541 bool IC_CharSet::ms_wcNeedsSwap
= FALSE
;
543 IC_CharSet::IC_CharSet(const wxChar
*name
)
544 : wxCharacterSet(name
)
546 // Do it the hard way
548 for (size_t i
= 0; i
< wxStrlen(name
)+1; i
++)
549 cname
[i
] = (char) name
[i
];
551 // check for charset that represents wchar_t:
552 if (ms_wcCharsetName
== NULL
)
554 ms_wcNeedsSwap
= FALSE
;
556 // try charset with explicit bytesex info (e.g. "UCS-4LE"):
557 ms_wcCharsetName
= WC_NAME_BEST
;
558 m2w
= iconv_open(ms_wcCharsetName
, cname
);
560 if (m2w
== (iconv_t
)-1)
562 // try charset w/o bytesex info (e.g. "UCS4")
563 // and check for bytesex ourselves:
564 ms_wcCharsetName
= WC_NAME
;
565 m2w
= iconv_open(ms_wcCharsetName
, cname
);
567 // last bet, try if it knows WCHAR_T pseudo-charset
568 if (m2w
== (iconv_t
)-1)
570 ms_wcCharsetName
= "WCHAR_T";
571 m2w
= iconv_open(ms_wcCharsetName
, cname
);
574 if (m2w
!= (iconv_t
)-1)
576 char buf
[2], *bufPtr
;
577 wchar_t wbuf
[2], *wbufPtr
;
585 outsz
= SIZEOF_WCHAR_T
* 2;
589 res
= iconv(m2w
, ICONV_CHAR_CAST(&bufPtr
), &insz
,
590 (char**)&wbufPtr
, &outsz
);
592 if (ICONV_FAILED(res
, insz
))
594 ms_wcCharsetName
= NULL
;
595 wxLogLastError(wxT("iconv"));
596 wxLogError(_("Convertion to charset '%s' doesn't work."), name
);
600 ms_wcNeedsSwap
= wbuf
[0] != (wchar_t)buf
[0];
605 ms_wcCharsetName
= NULL
;
607 // VS: we must not output an error here, since wxWindows will safely
608 // fall back to using wxEncodingConverter.
609 wxLogTrace(wxT("strconv"), wxT("Impossible to convert to/from charset '%s' with iconv, falling back to wxEncodingConverter."), name
);
613 wxLogTrace(wxT("strconv"), wxT("wchar_t charset is '%s', needs swap: %i"), ms_wcCharsetName
, ms_wcNeedsSwap
);
615 else // we already have ms_wcCharsetName
617 m2w
= iconv_open(ms_wcCharsetName
, cname
);
620 // NB: don't ever pass NULL to iconv_open(), it may crash!
621 if ( ms_wcCharsetName
)
623 w2m
= iconv_open( cname
, ms_wcCharsetName
);
631 IC_CharSet::~IC_CharSet()
633 if ( m2w
!= (iconv_t
)-1 )
635 if ( w2m
!= (iconv_t
)-1 )
639 size_t IC_CharSet::MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
641 size_t inbuf
= strlen(psz
);
642 size_t outbuf
= n
* SIZEOF_WCHAR_T
;
644 // VS: Use these instead of psz, buf because iconv() modifies its arguments:
645 wchar_t *bufPtr
= buf
;
646 const char *pszPtr
= psz
;
650 // have destination buffer, convert there
652 ICONV_CHAR_CAST(&pszPtr
), &inbuf
,
653 (char**)&bufPtr
, &outbuf
);
654 res
= n
- (outbuf
/ SIZEOF_WCHAR_T
);
658 // convert to native endianness
659 WC_BSWAP(buf
/* _not_ bufPtr */, res
)
662 // NB: iconv was given only strlen(psz) characters on input, and so
663 // it couldn't convert the trailing zero. Let's do it ourselves
664 // if there's some room left for it in the output buffer.
670 // no destination buffer... convert using temp buffer
671 // to calculate destination buffer requirement
676 outbuf
= 8*SIZEOF_WCHAR_T
;
679 ICONV_CHAR_CAST(&pszPtr
), &inbuf
,
680 (char**)&bufPtr
, &outbuf
);
682 res
+= 8-(outbuf
/SIZEOF_WCHAR_T
);
683 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
686 if (ICONV_FAILED(cres
, inbuf
))
688 //VS: it is ok if iconv fails, hence trace only
689 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
696 size_t IC_CharSet::WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
698 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
699 size_t inbuf
= std::wcslen(psz
) * SIZEOF_WCHAR_T
;
701 size_t inbuf
= ::wcslen(psz
) * SIZEOF_WCHAR_T
;
710 // need to copy to temp buffer to switch endianness
711 // this absolutely doesn't rock!
712 // (no, doing WC_BSWAP twice on the original buffer won't help, as it
713 // could be in read-only memory, or be accessed in some other thread)
714 tmpbuf
=(wchar_t*)malloc((inbuf
+1)*SIZEOF_WCHAR_T
);
715 memcpy(tmpbuf
,psz
,(inbuf
+1)*SIZEOF_WCHAR_T
);
716 WC_BSWAP(tmpbuf
, inbuf
)
722 // have destination buffer, convert there
723 cres
= iconv( w2m
, ICONV_CHAR_CAST(&psz
), &inbuf
, &buf
, &outbuf
);
727 // NB: iconv was given only wcslen(psz) characters on input, and so
728 // it couldn't convert the trailing zero. Let's do it ourselves
729 // if there's some room left for it in the output buffer.
735 // no destination buffer... convert using temp buffer
736 // to calculate destination buffer requirement
740 buf
= tbuf
; outbuf
= 16;
742 cres
= iconv( w2m
, ICONV_CHAR_CAST(&psz
), &inbuf
, &buf
, &outbuf
);
745 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
753 if (ICONV_FAILED(cres
, inbuf
))
755 //VS: it is ok if iconv fails, hence trace only
756 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
765 // ============================================================================
766 // Win32 conversion classes
767 // ============================================================================
769 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
771 extern long wxCharsetToCodepage(const wxChar
*charset
); // from utils.cpp
773 class CP_CharSet
: public wxCharacterSet
776 CP_CharSet(const wxChar
* name
)
777 : wxCharacterSet(name
)
779 m_CodePage
= wxCharsetToCodepage(name
);
782 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
785 MultiByteToWideChar(m_CodePage
, 0, psz
, -1, buf
, buf
? n
: 0);
786 //VS: returns # of written chars for buf!=NULL and *size*
787 // needed buffer for buf==NULL
788 return len
? (buf
? len
: len
-1) : (size_t)-1;
791 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
793 size_t len
= WideCharToMultiByte(m_CodePage
, 0, psz
, -1, buf
,
794 buf
? n
: 0, NULL
, NULL
);
795 //VS: returns # of written chars for buf!=NULL and *size*
796 // needed buffer for buf==NULL
797 return len
? (buf
? len
: len
-1) : (size_t)-1;
801 { return m_CodePage
!= -1; }
808 // ============================================================================
809 // wxEncodingConverter based conversion classes
810 // ============================================================================
814 class EC_CharSet
: public wxCharacterSet
817 // temporarily just use wxEncodingConverter stuff,
818 // so that it works while a better implementation is built
819 EC_CharSet(const wxChar
* name
) : wxCharacterSet(name
),
820 enc(wxFONTENCODING_SYSTEM
)
823 enc
= wxFontMapper::Get()->CharsetToEncoding(name
, FALSE
);
825 m_ok
= m2w
.Init(enc
, wxFONTENCODING_UNICODE
) &&
826 w2m
.Init(wxFONTENCODING_UNICODE
, enc
);
829 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t WXUNUSED(n
))
831 size_t inbuf
= strlen(psz
);
833 m2w
.Convert(psz
,buf
);
837 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t WXUNUSED(n
))
839 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
840 || ( defined(__MWERKS__) && defined(__WXMSW__) )
841 size_t inbuf
= std::wcslen(psz
);
843 size_t inbuf
= ::wcslen(psz
);
846 w2m
.Convert(psz
,buf
);
851 bool usable() const { return m_ok
; }
855 wxEncodingConverter m2w
, w2m
;
857 // were we initialized successfully?
861 #endif // wxUSE_FONTMAP
863 // ----------------------------------------------------------------------------
864 // the function creating the wxCharacterSet for the specified charset on the
865 // current system, trying all possibilities
866 // ----------------------------------------------------------------------------
868 static wxCharacterSet
*wxGetCharacterSet(const wxChar
*name
)
870 // check for the special case of ASCII charset
872 if ( wxFontMapper::Get()->CharsetToEncoding(name
) == wxFONTENCODING_DEFAULT
)
873 #else // wxUSE_FONTMAP
875 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
877 // don't convert at all
881 // the test above must have taken care of this case
882 wxCHECK_MSG( name
, NULL
, _T("NULL name must be wxFONTENCODING_DEFAULT") );
884 wxCharacterSet
*cset
;
886 if ( wxStricmp(name
, wxT("UTF8")) == 0 || wxStricmp(name
, wxT("UTF-8")) == 0)
888 cset
= new ID_CharSet(name
, &wxConvUTF8
);
893 cset
= new IC_CharSet(name
);
896 #endif // HAVE_ICONV/!HAVE_ICONV
899 // it can only be NULL in this case
902 #endif // !HAVE_ICONV
904 if ( cset
->usable() )
911 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
912 cset
= new CP_CharSet(name
);
913 if ( cset
->usable() )
921 cset
= new EC_CharSet(name
);
922 if ( cset
->usable() )
927 #endif // wxUSE_FONTMAP
929 wxLogError(_("Cannot convert from encoding '%s'!"), name
);
934 // ============================================================================
935 // wxCSConv implementation
936 // ============================================================================
938 wxCSConv::wxCSConv(const wxChar
*charset
)
940 m_name
= (wxChar
*)NULL
;
941 m_cset
= (wxCharacterSet
*) NULL
;
947 wxCSConv::~wxCSConv()
952 wxCSConv::wxCSConv(const wxCSConv
& conv
)
956 SetName(conv
.m_name
);
959 wxCSConv
& wxCSConv::operator=(const wxCSConv
& conv
)
962 SetName(conv
.m_name
);
966 void wxCSConv::Clear()
976 void wxCSConv::SetName(const wxChar
*charset
)
980 m_name
= wxStrdup(charset
);
985 void wxCSConv::LoadNow()
991 wxString name
= wxLocale::GetSystemEncodingName();
998 // wxGetCharacterSet() complains about NULL name
999 m_cset
= m_name
? wxGetCharacterSet(m_name
) : NULL
;
1004 size_t wxCSConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
1006 ((wxCSConv
*)this)->LoadNow(); // discard constness
1009 return m_cset
->MB2WC(buf
, psz
, n
);
1012 size_t len
= strlen(psz
);
1016 for (size_t c
= 0; c
<= len
; c
++)
1017 buf
[c
] = (unsigned char)(psz
[c
]);
1023 size_t wxCSConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
1025 ((wxCSConv
*)this)->LoadNow(); // discard constness
1028 return m_cset
->WC2MB(buf
, psz
, n
);
1031 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
1032 || ( defined(__MWERKS__) && defined(__WXMSW__) )
1033 size_t len
=std::wcslen(psz
);
1035 size_t len
=::wcslen(psz
);
1039 for (size_t c
= 0; c
<= len
; c
++)
1040 buf
[c
] = (psz
[c
] > 0xff) ? '?' : psz
[c
];
1046 #endif // wxUSE_WCHAR_T