]>
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
);
55 WXDLLEXPORT_DATA(wxCSConv
) wxConvISO8859_1(_T("iso-8859-1"));
57 // stand-ins in absence of wchar_t
58 WXDLLEXPORT_DATA(wxMBConv
) wxConvLibc
,
62 #endif // wxUSE_WCHAR_T
64 WXDLLEXPORT_DATA(wxMBConv
*) wxConvCurrent
= &wxConvLibc
;
66 class wxStrConvModule
: public wxModule
69 wxStrConvModule() : wxModule() { }
70 virtual bool OnInit() { return TRUE
; }
75 wxConvISO8859_1
.Clear();
79 DECLARE_DYNAMIC_CLASS(wxStrConvModule
)
82 IMPLEMENT_DYNAMIC_CLASS(wxStrConvModule
, wxModule
)
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
99 #include "wx/encconv.h"
100 #include "wx/fontmap.h"
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 #define BSWAP_UCS4(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT32_SWAP_ALWAYS(str[_c]); }
107 #define BSWAP_UTF16(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT16_SWAP_ALWAYS(str[_c]); }
109 // under Unix SIZEOF_WCHAR_T is defined by configure, but under other platforms
110 // it might be not defined - assume the most common value
111 #ifndef SIZEOF_WCHAR_T
112 #define SIZEOF_WCHAR_T 2
113 #endif // !defined(SIZEOF_WCHAR_T)
115 #if SIZEOF_WCHAR_T == 4
116 #define WC_NAME "UCS4"
117 #define WC_BSWAP BSWAP_UCS4
118 #ifdef WORDS_BIGENDIAN
119 #define WC_NAME_BEST "UCS-4BE"
121 #define WC_NAME_BEST "UCS-4LE"
123 #elif SIZEOF_WCHAR_T == 2
124 #define WC_NAME "UTF16"
125 #define WC_BSWAP BSWAP_UTF16
127 #ifdef WORDS_BIGENDIAN
128 #define WC_NAME_BEST "UTF-16BE"
130 #define WC_NAME_BEST "UTF-16LE"
132 #else // sizeof(wchar_t) != 2 nor 4
133 // I don't know what to do about this
134 #error "Weird sizeof(wchar_t): please report your platform details to wx-users mailing list"
137 // ============================================================================
139 // ============================================================================
141 // ----------------------------------------------------------------------------
142 // UTF-16 en/decoding
143 // ----------------------------------------------------------------------------
147 static size_t encode_utf16(wxUint32 input
, wchar_t *output
)
151 if (output
) *output
++ = (wchar_t) input
;
154 else if (input
>=0x110000)
162 *output
++ = (wchar_t) ((input
>> 10)+0xd7c0);
163 *output
++ = (wchar_t) ((input
&0x3ff)+0xdc00);
169 static size_t decode_utf16(const wchar_t* input
, wxUint32
& output
)
171 if ((*input
<0xd800) || (*input
>0xdfff))
176 else if ((input
[1]<0xdc00) || (input
[1]>=0xdfff))
183 output
= ((input
[0] - 0xd7c0) << 10) + (input
[1] - 0xdc00);
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
194 #define IGNORE_LIBC 0
196 wxMBConv::~wxMBConv()
198 // nothing to do here
201 size_t wxMBConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
206 for (size_t i
= 0; i
< strlen( psz
)+1; i
++)
207 buf
[i
] = (wchar_t) psz
[i
];
208 return strlen( psz
);
212 return strlen( psz
);
215 return wxMB2WC(buf
, psz
, n
);
219 size_t wxMBConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
224 for (size_t i
= 0; i
< wxStrlen( psz
)+1; i
++)
225 buf
[i
] = (char) psz
[i
];
226 return wxStrlen( psz
);
230 return wxStrlen( psz
);
233 return wxWC2MB(buf
, psz
, n
);
237 const wxWCharBuffer
wxMBConv::cMB2WC(const char *psz
) const
241 // calculate the length of the buffer needed first
242 size_t nLen
= MB2WC(NULL
, psz
, 0);
243 if ( nLen
!= (size_t)-1 )
245 // now do the actual conversion
246 wxWCharBuffer
buf(nLen
);
247 MB2WC(buf
.data(), psz
, nLen
+ 1); // with the trailing NUL
253 wxWCharBuffer
buf((wchar_t *)NULL
);
258 const wxCharBuffer
wxMBConv::cWC2MB(const wchar_t *pwz
) const
262 size_t nLen
= WC2MB(NULL
, pwz
, 0);
263 if ( nLen
!= (size_t)-1 )
265 wxCharBuffer
buf(nLen
);
266 WC2MB(buf
.data(), pwz
, nLen
+ 1);
272 wxCharBuffer
buf((char *)NULL
);
277 // ----------------------------------------------------------------------------
278 // standard gdk conversion
279 // ----------------------------------------------------------------------------
283 WXDLLEXPORT_DATA(wxMBConvGdk
) wxConvGdk
;
287 size_t wxMBConvGdk::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
291 return gdk_mbstowcs((GdkWChar
*)buf
, psz
, n
);
295 GdkWChar
*nbuf
= new GdkWChar
[n
=strlen(psz
)];
296 size_t len
= gdk_mbstowcs(nbuf
, psz
, n
);
302 size_t wxMBConvGdk::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
304 char *mbstr
= gdk_wcstombs((GdkWChar
*)psz
);
305 size_t len
= mbstr
? strlen(mbstr
) : 0;
310 memcpy(buf
, psz
, len
);
319 // ----------------------------------------------------------------------------
321 // ----------------------------------------------------------------------------
323 WXDLLEXPORT_DATA(wxMBConvUTF7
) wxConvUTF7
;
326 static char utf7_setD
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
327 "abcdefghijklmnopqrstuvwxyz"
328 "0123456789'(),-./:?";
329 static char utf7_setO
[]="!\"#$%&*;<=>@[]^_`{|}";
330 static char utf7_setB
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
331 "abcdefghijklmnopqrstuvwxyz"
335 // TODO: write actual implementations of UTF-7 here
336 size_t wxMBConvUTF7::MB2WC(wchar_t * WXUNUSED(buf
),
337 const char * WXUNUSED(psz
),
338 size_t WXUNUSED(n
)) const
343 size_t wxMBConvUTF7::WC2MB(char * WXUNUSED(buf
),
344 const wchar_t * WXUNUSED(psz
),
345 size_t WXUNUSED(n
)) const
350 // ----------------------------------------------------------------------------
352 // ----------------------------------------------------------------------------
354 WXDLLEXPORT_DATA(wxMBConvUTF8
) wxConvUTF8
;
356 static wxUint32 utf8_max
[]=
357 { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff, 0xffffffff };
359 size_t wxMBConvUTF8::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
363 while (*psz
&& ((!buf
) || (len
< n
)))
365 unsigned char cc
= *psz
++, fc
= cc
;
367 for (cnt
= 0; fc
& 0x80; cnt
++)
381 // invalid UTF-8 sequence
386 unsigned ocnt
= cnt
- 1;
387 wxUint32 res
= cc
& (0x3f >> cnt
);
391 if ((cc
& 0xC0) != 0x80)
393 // invalid UTF-8 sequence
396 res
= (res
<< 6) | (cc
& 0x3f);
398 if (res
<= utf8_max
[ocnt
])
400 // illegal UTF-8 encoding
404 size_t pa
= encode_utf16(res
, buf
);
405 if (pa
== (size_t)-1)
414 #endif // WC_UTF16/!WC_UTF16
418 if (buf
&& (len
< n
))
423 size_t wxMBConvUTF8::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
427 while (*psz
&& ((!buf
) || (len
< n
)))
431 size_t pa
= decode_utf16(psz
, cc
);
432 psz
+= (pa
== (size_t)-1) ? 1 : pa
;
434 cc
=(*psz
++) & 0x7fffffff;
437 for (cnt
= 0; cc
> utf8_max
[cnt
]; cnt
++) {}
451 *buf
++ = (char) ((-128 >> cnt
) | ((cc
>> (cnt
* 6)) & (0x3f >> cnt
)));
453 *buf
++ = (char) (0x80 | ((cc
>> (cnt
* 6)) & 0x3f));
458 if (buf
&& (len
<n
)) *buf
= 0;
463 // ============================================================================
464 // wxCharacterSet and derived classes
465 // ============================================================================
467 // ----------------------------------------------------------------------------
468 // wxCharacterSet is the ABC for the classes below
469 // ----------------------------------------------------------------------------
474 wxCharacterSet(const wxChar
*name
) : cname(name
) {}
475 virtual ~wxCharacterSet() {}
476 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) = 0;
477 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) = 0;
478 virtual bool usable() const = 0;
483 // ----------------------------------------------------------------------------
484 // ID_CharSet: implementation of wxCharacterSet using an existing wxMBConv
485 // ----------------------------------------------------------------------------
487 class ID_CharSet
: public wxCharacterSet
490 ID_CharSet(const wxChar
*name
, wxMBConv
*cnv
)
491 : wxCharacterSet(name
), work(cnv
) {}
493 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
494 { return work
? work
->MB2WC(buf
,psz
,n
) : (size_t)-1; }
496 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
497 { return work
? work
->WC2MB(buf
,psz
,n
) : (size_t)-1; }
500 { return work
!=NULL
; }
506 // ============================================================================
507 // The classes doing conversion using the iconv_xxx() functions
508 // ============================================================================
512 // VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with E2BIG
513 // if output buffer is _exactly_ as big as needed. Such case is (unless there's
514 // yet another bug in glibc) the only case when iconv() returns with (size_t)-1
515 // (which means error) and says there are 0 bytes left in the input buffer --
516 // when _real_ error occurs, bytes-left-in-input buffer is non-zero. Hence,
517 // this alternative test for iconv() failure.
518 // [This bug does not appear in glibc 2.2.]
519 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 1
520 #define ICONV_FAILED(cres, bufLeft) ((cres == (size_t)-1) && \
521 (errno != E2BIG || bufLeft != 0))
523 #define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1)
526 #define ICONV_CHAR_CAST(x) ((ICONV_CONST char **)(x))
528 // ----------------------------------------------------------------------------
529 // IC_CharSet: encapsulates an iconv character set
530 // ----------------------------------------------------------------------------
532 class IC_CharSet
: public wxCharacterSet
535 IC_CharSet(const wxChar
*name
);
536 virtual ~IC_CharSet();
538 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
);
539 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
);
542 { return (m2w
!= (iconv_t
)-1) && (w2m
!= (iconv_t
)-1); }
545 // the iconv handlers used to translate from multibyte to wide char and in
546 // the other direction
551 // the name (for iconv_open()) of a wide char charset - if none is
552 // available on this machine, it will remain NULL
553 static const char *ms_wcCharsetName
;
555 // true if the wide char encoding we use (i.e. ms_wcCharsetName) has
556 // different endian-ness than the native one
557 static bool ms_wcNeedsSwap
;
560 const char *IC_CharSet::ms_wcCharsetName
= NULL
;
561 bool IC_CharSet::ms_wcNeedsSwap
= FALSE
;
563 IC_CharSet::IC_CharSet(const wxChar
*name
)
564 : wxCharacterSet(name
)
566 // Do it the hard way
568 for (size_t i
= 0; i
< wxStrlen(name
)+1; i
++)
569 cname
[i
] = (char) name
[i
];
571 // check for charset that represents wchar_t:
572 if (ms_wcCharsetName
== NULL
)
574 ms_wcNeedsSwap
= FALSE
;
576 // try charset with explicit bytesex info (e.g. "UCS-4LE"):
577 ms_wcCharsetName
= WC_NAME_BEST
;
578 m2w
= iconv_open(ms_wcCharsetName
, cname
);
580 if (m2w
== (iconv_t
)-1)
582 // try charset w/o bytesex info (e.g. "UCS4")
583 // and check for bytesex ourselves:
584 ms_wcCharsetName
= WC_NAME
;
585 m2w
= iconv_open(ms_wcCharsetName
, cname
);
587 // last bet, try if it knows WCHAR_T pseudo-charset
588 if (m2w
== (iconv_t
)-1)
590 ms_wcCharsetName
= "WCHAR_T";
591 m2w
= iconv_open(ms_wcCharsetName
, cname
);
594 if (m2w
!= (iconv_t
)-1)
596 char buf
[2], *bufPtr
;
597 wchar_t wbuf
[2], *wbufPtr
;
605 outsz
= SIZEOF_WCHAR_T
* 2;
609 res
= iconv(m2w
, ICONV_CHAR_CAST(&bufPtr
), &insz
,
610 (char**)&wbufPtr
, &outsz
);
612 if (ICONV_FAILED(res
, insz
))
614 ms_wcCharsetName
= NULL
;
615 wxLogLastError(wxT("iconv"));
616 wxLogError(_("Conversion to charset '%s' doesn't work."), name
);
620 ms_wcNeedsSwap
= wbuf
[0] != (wchar_t)buf
[0];
625 ms_wcCharsetName
= NULL
;
627 // VS: we must not output an error here, since wxWindows will safely
628 // fall back to using wxEncodingConverter.
629 wxLogTrace(wxT("strconv"), wxT("Impossible to convert to/from charset '%s' with iconv, falling back to wxEncodingConverter."), name
);
633 wxLogTrace(wxT("strconv"), wxT("wchar_t charset is '%s', needs swap: %i"), ms_wcCharsetName
, ms_wcNeedsSwap
);
635 else // we already have ms_wcCharsetName
637 m2w
= iconv_open(ms_wcCharsetName
, cname
);
640 // NB: don't ever pass NULL to iconv_open(), it may crash!
641 if ( ms_wcCharsetName
)
643 w2m
= iconv_open( cname
, ms_wcCharsetName
);
651 IC_CharSet::~IC_CharSet()
653 if ( m2w
!= (iconv_t
)-1 )
655 if ( w2m
!= (iconv_t
)-1 )
659 size_t IC_CharSet::MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
661 size_t inbuf
= strlen(psz
);
662 size_t outbuf
= n
* SIZEOF_WCHAR_T
;
664 // VS: Use these instead of psz, buf because iconv() modifies its arguments:
665 wchar_t *bufPtr
= buf
;
666 const char *pszPtr
= psz
;
670 // have destination buffer, convert there
672 ICONV_CHAR_CAST(&pszPtr
), &inbuf
,
673 (char**)&bufPtr
, &outbuf
);
674 res
= n
- (outbuf
/ SIZEOF_WCHAR_T
);
678 // convert to native endianness
679 WC_BSWAP(buf
/* _not_ bufPtr */, res
)
682 // NB: iconv was given only strlen(psz) characters on input, and so
683 // it couldn't convert the trailing zero. Let's do it ourselves
684 // if there's some room left for it in the output buffer.
690 // no destination buffer... convert using temp buffer
691 // to calculate destination buffer requirement
696 outbuf
= 8*SIZEOF_WCHAR_T
;
699 ICONV_CHAR_CAST(&pszPtr
), &inbuf
,
700 (char**)&bufPtr
, &outbuf
);
702 res
+= 8-(outbuf
/SIZEOF_WCHAR_T
);
703 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
706 if (ICONV_FAILED(cres
, inbuf
))
708 //VS: it is ok if iconv fails, hence trace only
709 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
716 size_t IC_CharSet::WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
718 size_t inbuf
= wxWcslen(psz
) * SIZEOF_WCHAR_T
;
726 // need to copy to temp buffer to switch endianness
727 // this absolutely doesn't rock!
728 // (no, doing WC_BSWAP twice on the original buffer won't help, as it
729 // could be in read-only memory, or be accessed in some other thread)
730 tmpbuf
=(wchar_t*)malloc((inbuf
+1)*SIZEOF_WCHAR_T
);
731 memcpy(tmpbuf
,psz
,(inbuf
+1)*SIZEOF_WCHAR_T
);
732 WC_BSWAP(tmpbuf
, inbuf
)
738 // have destination buffer, convert there
739 cres
= iconv( w2m
, ICONV_CHAR_CAST(&psz
), &inbuf
, &buf
, &outbuf
);
743 // NB: iconv was given only wcslen(psz) characters on input, and so
744 // it couldn't convert the trailing zero. Let's do it ourselves
745 // if there's some room left for it in the output buffer.
751 // no destination buffer... convert using temp buffer
752 // to calculate destination buffer requirement
756 buf
= tbuf
; outbuf
= 16;
758 cres
= iconv( w2m
, ICONV_CHAR_CAST(&psz
), &inbuf
, &buf
, &outbuf
);
761 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
769 if (ICONV_FAILED(cres
, inbuf
))
771 //VS: it is ok if iconv fails, hence trace only
772 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
781 // ============================================================================
782 // Win32 conversion classes
783 // ============================================================================
785 #if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
788 #define WINE_CAST (WCHAR *)
793 extern long wxCharsetToCodepage(const wxChar
*charset
); // from utils.cpp
795 class CP_CharSet
: public wxCharacterSet
798 CP_CharSet(const wxChar
* name
)
799 : wxCharacterSet(name
)
801 m_CodePage
= wxCharsetToCodepage(name
);
804 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
806 const size_t len
= ::MultiByteToWideChar
808 m_CodePage
, // code page
811 -1, // its length (NUL-terminated)
812 WINE_CAST buf
, // output string
813 buf
? n
: 0 // size of output buffer
816 // note that it returns # of written chars for buf != NULL and *size*
817 // of the needed buffer for buf == NULL
818 return len
? (buf
? len
: len
- 1) : (size_t)-1;
821 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
823 const size_t len
= ::WideCharToMultiByte
825 m_CodePage
, // code page
827 WINE_CAST psz
, // input string
828 -1, // it is (wide) NUL-terminated
829 buf
, // output buffer
830 buf
? n
: 0, // and its size
831 NULL
, // default "replacement" char
832 NULL
// [out] was it used?
835 // see the comment above!
836 return len
? (buf
? len
: len
- 1) : (size_t)-1;
840 { return m_CodePage
!= -1; }
845 #endif // defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
847 // ============================================================================
848 // wxEncodingConverter based conversion classes
849 // ============================================================================
853 class EC_CharSet
: public wxCharacterSet
856 // temporarily just use wxEncodingConverter stuff,
857 // so that it works while a better implementation is built
858 EC_CharSet(const wxChar
* name
) : wxCharacterSet(name
),
859 enc(wxFONTENCODING_SYSTEM
)
862 enc
= wxFontMapper::Get()->CharsetToEncoding(name
, FALSE
);
864 m_ok
= m2w
.Init(enc
, wxFONTENCODING_UNICODE
) &&
865 w2m
.Init(wxFONTENCODING_UNICODE
, enc
);
868 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t WXUNUSED(n
))
870 size_t inbuf
= strlen(psz
);
872 m2w
.Convert(psz
,buf
);
876 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t WXUNUSED(n
))
878 const size_t inbuf
= wxWcslen(psz
);
880 w2m
.Convert(psz
,buf
);
885 bool usable() const { return m_ok
; }
889 wxEncodingConverter m2w
, w2m
;
891 // were we initialized successfully?
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 // check for the special case of ASCII charset
906 if ( wxFontMapper::Get()->CharsetToEncoding(name
) == wxFONTENCODING_DEFAULT
)
907 #else // wxUSE_FONTMAP
909 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
911 // don't convert at all
915 // the test above must have taken care of this case
916 wxCHECK_MSG( name
, NULL
, _T("NULL name must be wxFONTENCODING_DEFAULT") );
918 wxCharacterSet
*cset
;
920 if ( wxStricmp(name
, wxT("UTF8")) == 0 || wxStricmp(name
, wxT("UTF-8")) == 0)
922 cset
= new ID_CharSet(name
, &wxConvUTF8
);
927 cset
= new IC_CharSet(name
);
930 #endif // HAVE_ICONV/!HAVE_ICONV
933 // it can only be NULL in this case
936 #endif // !HAVE_ICONV
938 if ( cset
->usable() )
945 #if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
946 cset
= new CP_CharSet(name
);
947 if ( cset
->usable() )
952 #endif // defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
955 cset
= new EC_CharSet(name
);
956 if ( cset
->usable() )
961 #endif // wxUSE_FONTMAP
963 wxLogError(_("Cannot convert from encoding '%s'!"), name
);
968 // ============================================================================
969 // wxCSConv implementation
970 // ============================================================================
972 wxCSConv::wxCSConv(const wxChar
*charset
)
974 m_name
= (wxChar
*)NULL
;
975 m_cset
= (wxCharacterSet
*) NULL
;
981 wxCSConv::~wxCSConv()
986 wxCSConv::wxCSConv(const wxCSConv
& conv
)
990 SetName(conv
.m_name
);
993 wxCSConv
& wxCSConv::operator=(const wxCSConv
& conv
)
996 SetName(conv
.m_name
);
1000 void wxCSConv::Clear()
1010 void wxCSConv::SetName(const wxChar
*charset
)
1014 m_name
= wxStrdup(charset
);
1019 void wxCSConv::LoadNow()
1023 // it would probably be better to make GetSystemEncodingName() always
1024 // available (i.e. even when wxUSE_INTL == 0)?
1028 wxString name
= wxLocale::GetSystemEncodingName();
1029 if ( !name
.empty() )
1034 #endif // wxUSE_INTL
1036 // wxGetCharacterSet() complains about NULL name
1037 m_cset
= m_name
? wxGetCharacterSet(m_name
) : NULL
;
1042 size_t wxCSConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
1044 ((wxCSConv
*)this)->LoadNow(); // discard constness
1047 return m_cset
->MB2WC(buf
, psz
, n
);
1050 size_t len
= strlen(psz
);
1054 for (size_t c
= 0; c
<= len
; c
++)
1055 buf
[c
] = (unsigned char)(psz
[c
]);
1061 size_t wxCSConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
1063 ((wxCSConv
*)this)->LoadNow(); // discard constness
1066 return m_cset
->WC2MB(buf
, psz
, n
);
1069 const size_t len
= wxWcslen(psz
);
1072 for (size_t c
= 0; c
<= len
; c
++)
1073 buf
[c
] = (psz
[c
] > 0xff) ? '?' : psz
[c
];
1079 #endif // wxUSE_WCHAR_T