]>
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 licence
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
,
63 #endif // wxUSE_WCHAR_T
65 WXDLLEXPORT_DATA(wxMBConv
*) wxConvCurrent
= &wxConvLibc
;
67 class wxStrConvModule
: public wxModule
70 wxStrConvModule() : wxModule() { }
71 virtual bool OnInit() { return TRUE
; }
76 wxConvISO8859_1
.Clear();
80 DECLARE_DYNAMIC_CLASS(wxStrConvModule
)
83 IMPLEMENT_DYNAMIC_CLASS(wxStrConvModule
, wxModule
)
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
100 #include "wx/encconv.h"
101 #include "wx/fontmap.h"
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 #define BSWAP_UCS4(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT32_SWAP_ALWAYS(str[_c]); }
108 #define BSWAP_UTF16(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT16_SWAP_ALWAYS(str[_c]); }
110 // under Unix SIZEOF_WCHAR_T is defined by configure, but under other platforms
111 // it might be not defined - assume the most common value
112 #ifndef SIZEOF_WCHAR_T
113 #define SIZEOF_WCHAR_T 2
114 #endif // !defined(SIZEOF_WCHAR_T)
116 #if SIZEOF_WCHAR_T == 4
117 #define WC_NAME "UCS4"
118 #define WC_BSWAP BSWAP_UCS4
119 #ifdef WORDS_BIGENDIAN
120 #define WC_NAME_BEST "UCS-4BE"
122 #define WC_NAME_BEST "UCS-4LE"
124 #elif SIZEOF_WCHAR_T == 2
125 #define WC_NAME "UTF16"
126 #define WC_BSWAP BSWAP_UTF16
128 #ifdef WORDS_BIGENDIAN
129 #define WC_NAME_BEST "UTF-16BE"
131 #define WC_NAME_BEST "UTF-16LE"
133 #else // sizeof(wchar_t) != 2 nor 4
134 // I don't know what to do about this
135 #error "Weird sizeof(wchar_t): please report your platform details to wx-users mailing list"
138 // ============================================================================
140 // ============================================================================
142 // ----------------------------------------------------------------------------
143 // UTF-16 en/decoding
144 // ----------------------------------------------------------------------------
148 static size_t encode_utf16(wxUint32 input
, wchar_t *output
)
152 if (output
) *output
++ = (wchar_t) input
;
155 else if (input
>=0x110000)
163 *output
++ = (wchar_t) ((input
>> 10)+0xd7c0);
164 *output
++ = (wchar_t) ((input
&0x3ff)+0xdc00);
170 static size_t decode_utf16(const wchar_t* input
, wxUint32
& output
)
172 if ((*input
<0xd800) || (*input
>0xdfff))
177 else if ((input
[1]<0xdc00) || (input
[1]>=0xdfff))
184 output
= ((input
[0] - 0xd7c0) << 10) + (input
[1] - 0xdc00);
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
195 #define IGNORE_LIBC 0
197 wxMBConv::~wxMBConv()
199 // nothing to do here
202 size_t wxMBConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
207 for (size_t i
= 0; i
< strlen( psz
)+1; i
++)
208 buf
[i
] = (wchar_t) psz
[i
];
209 return strlen( psz
);
213 return strlen( psz
);
216 return wxMB2WC(buf
, psz
, n
);
220 size_t wxMBConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
225 for (size_t i
= 0; i
< wxStrlen( psz
)+1; i
++)
226 buf
[i
] = (char) psz
[i
];
227 return wxStrlen( psz
);
231 return wxStrlen( psz
);
234 return wxWC2MB(buf
, psz
, n
);
238 const wxWCharBuffer
wxMBConv::cMB2WC(const char *psz
) const
242 // calculate the length of the buffer needed first
243 size_t nLen
= MB2WC(NULL
, psz
, 0);
244 if ( nLen
!= (size_t)-1 )
246 // now do the actual conversion
247 wxWCharBuffer
buf(nLen
);
248 MB2WC(buf
.data(), psz
, nLen
+ 1); // with the trailing NUL
254 wxWCharBuffer
buf((wchar_t *)NULL
);
259 const wxCharBuffer
wxMBConv::cWC2MB(const wchar_t *pwz
) const
263 size_t nLen
= WC2MB(NULL
, pwz
, 0);
264 if ( nLen
!= (size_t)-1 )
266 wxCharBuffer
buf(nLen
);
267 WC2MB(buf
.data(), pwz
, nLen
+ 1);
273 wxCharBuffer
buf((char *)NULL
);
278 // ----------------------------------------------------------------------------
279 // standard gdk conversion
280 // ----------------------------------------------------------------------------
284 WXDLLEXPORT_DATA(wxMBConvGdk
) wxConvGdk
;
288 size_t wxMBConvGdk::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
292 return gdk_mbstowcs((GdkWChar
*)buf
, psz
, n
);
296 GdkWChar
*nbuf
= new GdkWChar
[n
=strlen(psz
)];
297 size_t len
= gdk_mbstowcs(nbuf
, psz
, n
);
303 size_t wxMBConvGdk::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
305 char *mbstr
= gdk_wcstombs((GdkWChar
*)psz
);
306 size_t len
= mbstr
? strlen(mbstr
) : 0;
311 memcpy(buf
, psz
, len
);
320 // ----------------------------------------------------------------------------
322 // ----------------------------------------------------------------------------
324 WXDLLEXPORT_DATA(wxMBConvUTF7
) wxConvUTF7
;
327 static char utf7_setD
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
328 "abcdefghijklmnopqrstuvwxyz"
329 "0123456789'(),-./:?";
330 static char utf7_setO
[]="!\"#$%&*;<=>@[]^_`{|}";
331 static char utf7_setB
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
332 "abcdefghijklmnopqrstuvwxyz"
336 // TODO: write actual implementations of UTF-7 here
337 size_t wxMBConvUTF7::MB2WC(wchar_t * WXUNUSED(buf
),
338 const char * WXUNUSED(psz
),
339 size_t WXUNUSED(n
)) const
344 size_t wxMBConvUTF7::WC2MB(char * WXUNUSED(buf
),
345 const wchar_t * WXUNUSED(psz
),
346 size_t WXUNUSED(n
)) const
351 // ----------------------------------------------------------------------------
353 // ----------------------------------------------------------------------------
355 WXDLLEXPORT_DATA(wxMBConvUTF8
) wxConvUTF8
;
357 static wxUint32 utf8_max
[]=
358 { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff, 0xffffffff };
360 size_t wxMBConvUTF8::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
364 while (*psz
&& ((!buf
) || (len
< n
)))
366 unsigned char cc
= *psz
++, fc
= cc
;
368 for (cnt
= 0; fc
& 0x80; cnt
++)
382 // invalid UTF-8 sequence
387 unsigned ocnt
= cnt
- 1;
388 wxUint32 res
= cc
& (0x3f >> cnt
);
392 if ((cc
& 0xC0) != 0x80)
394 // invalid UTF-8 sequence
397 res
= (res
<< 6) | (cc
& 0x3f);
399 if (res
<= utf8_max
[ocnt
])
401 // illegal UTF-8 encoding
405 size_t pa
= encode_utf16(res
, buf
);
406 if (pa
== (size_t)-1)
415 #endif // WC_UTF16/!WC_UTF16
419 if (buf
&& (len
< n
))
424 size_t wxMBConvUTF8::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
428 while (*psz
&& ((!buf
) || (len
< n
)))
432 size_t pa
= decode_utf16(psz
, cc
);
433 psz
+= (pa
== (size_t)-1) ? 1 : pa
;
435 cc
=(*psz
++) & 0x7fffffff;
438 for (cnt
= 0; cc
> utf8_max
[cnt
]; cnt
++) {}
452 *buf
++ = (char) ((-128 >> cnt
) | ((cc
>> (cnt
* 6)) & (0x3f >> cnt
)));
454 *buf
++ = (char) (0x80 | ((cc
>> (cnt
* 6)) & 0x3f));
459 if (buf
&& (len
<n
)) *buf
= 0;
464 // ============================================================================
465 // wxCharacterSet and derived classes
466 // ============================================================================
468 // ----------------------------------------------------------------------------
469 // wxCharacterSet is the ABC for the classes below
470 // ----------------------------------------------------------------------------
475 wxCharacterSet(const wxChar
*name
) : cname(name
) {}
476 virtual ~wxCharacterSet() {}
477 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) = 0;
478 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) = 0;
479 virtual bool usable() const = 0;
484 // ----------------------------------------------------------------------------
485 // ID_CharSet: implementation of wxCharacterSet using an existing wxMBConv
486 // ----------------------------------------------------------------------------
488 class ID_CharSet
: public wxCharacterSet
491 ID_CharSet(const wxChar
*name
, wxMBConv
*cnv
)
492 : wxCharacterSet(name
), work(cnv
) {}
494 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
495 { return work
? work
->MB2WC(buf
,psz
,n
) : (size_t)-1; }
497 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
498 { return work
? work
->WC2MB(buf
,psz
,n
) : (size_t)-1; }
501 { return work
!=NULL
; }
507 // ============================================================================
508 // The classes doing conversion using the iconv_xxx() functions
509 // ============================================================================
513 // VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with E2BIG
514 // if output buffer is _exactly_ as big as needed. Such case is (unless there's
515 // yet another bug in glibc) the only case when iconv() returns with (size_t)-1
516 // (which means error) and says there are 0 bytes left in the input buffer --
517 // when _real_ error occurs, bytes-left-in-input buffer is non-zero. Hence,
518 // this alternative test for iconv() failure.
519 // [This bug does not appear in glibc 2.2.]
520 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 1
521 #define ICONV_FAILED(cres, bufLeft) ((cres == (size_t)-1) && \
522 (errno != E2BIG || bufLeft != 0))
524 #define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1)
527 #define ICONV_CHAR_CAST(x) ((ICONV_CONST char **)(x))
529 // ----------------------------------------------------------------------------
530 // IC_CharSet: encapsulates an iconv character set
531 // ----------------------------------------------------------------------------
533 class IC_CharSet
: public wxCharacterSet
536 IC_CharSet(const wxChar
*name
);
537 virtual ~IC_CharSet();
539 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
);
540 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
);
543 { return (m2w
!= (iconv_t
)-1) && (w2m
!= (iconv_t
)-1); }
546 // the iconv handlers used to translate from multibyte to wide char and in
547 // the other direction
552 // the name (for iconv_open()) of a wide char charset - if none is
553 // available on this machine, it will remain NULL
554 static const char *ms_wcCharsetName
;
556 // true if the wide char encoding we use (i.e. ms_wcCharsetName) has
557 // different endian-ness than the native one
558 static bool ms_wcNeedsSwap
;
561 const char *IC_CharSet::ms_wcCharsetName
= NULL
;
562 bool IC_CharSet::ms_wcNeedsSwap
= FALSE
;
564 IC_CharSet::IC_CharSet(const wxChar
*name
)
565 : wxCharacterSet(name
)
567 // Do it the hard way
569 for (size_t i
= 0; i
< wxStrlen(name
)+1; i
++)
570 cname
[i
] = (char) name
[i
];
572 // check for charset that represents wchar_t:
573 if (ms_wcCharsetName
== NULL
)
575 ms_wcNeedsSwap
= FALSE
;
577 // try charset with explicit bytesex info (e.g. "UCS-4LE"):
578 ms_wcCharsetName
= WC_NAME_BEST
;
579 m2w
= iconv_open(ms_wcCharsetName
, cname
);
581 if (m2w
== (iconv_t
)-1)
583 // try charset w/o bytesex info (e.g. "UCS4")
584 // and check for bytesex ourselves:
585 ms_wcCharsetName
= WC_NAME
;
586 m2w
= iconv_open(ms_wcCharsetName
, cname
);
588 // last bet, try if it knows WCHAR_T pseudo-charset
589 if (m2w
== (iconv_t
)-1)
591 ms_wcCharsetName
= "WCHAR_T";
592 m2w
= iconv_open(ms_wcCharsetName
, cname
);
595 if (m2w
!= (iconv_t
)-1)
597 char buf
[2], *bufPtr
;
598 wchar_t wbuf
[2], *wbufPtr
;
606 outsz
= SIZEOF_WCHAR_T
* 2;
610 res
= iconv(m2w
, ICONV_CHAR_CAST(&bufPtr
), &insz
,
611 (char**)&wbufPtr
, &outsz
);
613 if (ICONV_FAILED(res
, insz
))
615 ms_wcCharsetName
= NULL
;
616 wxLogLastError(wxT("iconv"));
617 wxLogError(_("Conversion to charset '%s' doesn't work."), name
);
621 ms_wcNeedsSwap
= wbuf
[0] != (wchar_t)buf
[0];
626 ms_wcCharsetName
= NULL
;
628 // VS: we must not output an error here, since wxWindows will safely
629 // fall back to using wxEncodingConverter.
630 wxLogTrace(wxT("strconv"), wxT("Impossible to convert to/from charset '%s' with iconv, falling back to wxEncodingConverter."), name
);
634 wxLogTrace(wxT("strconv"), wxT("wchar_t charset is '%s', needs swap: %i"), ms_wcCharsetName
, ms_wcNeedsSwap
);
636 else // we already have ms_wcCharsetName
638 m2w
= iconv_open(ms_wcCharsetName
, cname
);
641 // NB: don't ever pass NULL to iconv_open(), it may crash!
642 if ( ms_wcCharsetName
)
644 w2m
= iconv_open( cname
, ms_wcCharsetName
);
652 IC_CharSet::~IC_CharSet()
654 if ( m2w
!= (iconv_t
)-1 )
656 if ( w2m
!= (iconv_t
)-1 )
660 size_t IC_CharSet::MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
662 size_t inbuf
= strlen(psz
);
663 size_t outbuf
= n
* SIZEOF_WCHAR_T
;
665 // VS: Use these instead of psz, buf because iconv() modifies its arguments:
666 wchar_t *bufPtr
= buf
;
667 const char *pszPtr
= psz
;
671 // have destination buffer, convert there
673 ICONV_CHAR_CAST(&pszPtr
), &inbuf
,
674 (char**)&bufPtr
, &outbuf
);
675 res
= n
- (outbuf
/ SIZEOF_WCHAR_T
);
679 // convert to native endianness
680 WC_BSWAP(buf
/* _not_ bufPtr */, res
)
683 // NB: iconv was given only strlen(psz) characters on input, and so
684 // it couldn't convert the trailing zero. Let's do it ourselves
685 // if there's some room left for it in the output buffer.
691 // no destination buffer... convert using temp buffer
692 // to calculate destination buffer requirement
697 outbuf
= 8*SIZEOF_WCHAR_T
;
700 ICONV_CHAR_CAST(&pszPtr
), &inbuf
,
701 (char**)&bufPtr
, &outbuf
);
703 res
+= 8-(outbuf
/SIZEOF_WCHAR_T
);
704 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
707 if (ICONV_FAILED(cres
, inbuf
))
709 //VS: it is ok if iconv fails, hence trace only
710 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
717 size_t IC_CharSet::WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
719 size_t inbuf
= wxWcslen(psz
) * SIZEOF_WCHAR_T
;
727 // need to copy to temp buffer to switch endianness
728 // this absolutely doesn't rock!
729 // (no, doing WC_BSWAP twice on the original buffer won't help, as it
730 // could be in read-only memory, or be accessed in some other thread)
731 tmpbuf
=(wchar_t*)malloc((inbuf
+1)*SIZEOF_WCHAR_T
);
732 memcpy(tmpbuf
,psz
,(inbuf
+1)*SIZEOF_WCHAR_T
);
733 WC_BSWAP(tmpbuf
, inbuf
)
739 // have destination buffer, convert there
740 cres
= iconv( w2m
, ICONV_CHAR_CAST(&psz
), &inbuf
, &buf
, &outbuf
);
744 // NB: iconv was given only wcslen(psz) characters on input, and so
745 // it couldn't convert the trailing zero. Let's do it ourselves
746 // if there's some room left for it in the output buffer.
752 // no destination buffer... convert using temp buffer
753 // to calculate destination buffer requirement
757 buf
= tbuf
; outbuf
= 16;
759 cres
= iconv( w2m
, ICONV_CHAR_CAST(&psz
), &inbuf
, &buf
, &outbuf
);
762 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
770 if (ICONV_FAILED(cres
, inbuf
))
772 //VS: it is ok if iconv fails, hence trace only
773 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
782 // ============================================================================
783 // Win32 conversion classes
784 // ============================================================================
786 #if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
788 extern long wxCharsetToCodepage(const wxChar
*charset
); // from utils.cpp
790 class CP_CharSet
: public wxCharacterSet
793 CP_CharSet(const wxChar
* name
)
794 : wxCharacterSet(name
)
796 m_CodePage
= wxCharsetToCodepage(name
);
799 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
801 const size_t len
= ::MultiByteToWideChar
803 m_CodePage
, // code page
806 -1, // its length (NUL-terminated)
807 buf
, // output string
808 buf
? n
: 0 // size of output buffer
811 // note that it returns # of written chars for buf != NULL and *size*
812 // of the needed buffer for buf == NULL
813 return len
? (buf
? len
: len
- 1) : (size_t)-1;
816 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
818 const size_t len
= ::WideCharToMultiByte
820 m_CodePage
, // code page
823 -1, // it is (wide) NUL-terminated
824 buf
, // output buffer
825 buf
? n
: 0, // and its size
826 NULL
, // default "replacement" char
827 NULL
// [out] was it used?
830 // see the comment above!
831 return len
? (buf
? len
: len
- 1) : (size_t)-1;
835 { return m_CodePage
!= -1; }
840 #endif // defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
842 // ============================================================================
843 // wxEncodingConverter based conversion classes
844 // ============================================================================
848 class EC_CharSet
: public wxCharacterSet
851 // temporarily just use wxEncodingConverter stuff,
852 // so that it works while a better implementation is built
853 EC_CharSet(const wxChar
* name
) : wxCharacterSet(name
),
854 enc(wxFONTENCODING_SYSTEM
)
857 enc
= wxFontMapper::Get()->CharsetToEncoding(name
, FALSE
);
859 m_ok
= m2w
.Init(enc
, wxFONTENCODING_UNICODE
) &&
860 w2m
.Init(wxFONTENCODING_UNICODE
, enc
);
863 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t WXUNUSED(n
))
865 size_t inbuf
= strlen(psz
);
867 m2w
.Convert(psz
,buf
);
871 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t WXUNUSED(n
))
873 const size_t inbuf
= wxWcslen(psz
);
875 w2m
.Convert(psz
,buf
);
880 bool usable() const { return m_ok
; }
884 wxEncodingConverter m2w
, w2m
;
886 // were we initialized successfully?
890 #endif // wxUSE_FONTMAP
892 // ----------------------------------------------------------------------------
893 // the function creating the wxCharacterSet for the specified charset on the
894 // current system, trying all possibilities
895 // ----------------------------------------------------------------------------
897 static wxCharacterSet
*wxGetCharacterSet(const wxChar
*name
)
899 // check for the special case of ASCII charset
901 if ( wxFontMapper::Get()->CharsetToEncoding(name
) == wxFONTENCODING_DEFAULT
)
902 #else // wxUSE_FONTMAP
904 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
906 // don't convert at all
910 // the test above must have taken care of this case
911 wxCHECK_MSG( name
, NULL
, _T("NULL name must be wxFONTENCODING_DEFAULT") );
913 wxCharacterSet
*cset
;
915 if ( wxStricmp(name
, wxT("UTF8")) == 0 || wxStricmp(name
, wxT("UTF-8")) == 0)
917 cset
= new ID_CharSet(name
, &wxConvUTF8
);
922 cset
= new IC_CharSet(name
);
925 #endif // HAVE_ICONV/!HAVE_ICONV
928 // it can only be NULL in this case
931 #endif // !HAVE_ICONV
933 if ( cset
->usable() )
940 #if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
941 cset
= new CP_CharSet(name
);
942 if ( cset
->usable() )
947 #endif // defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
950 cset
= new EC_CharSet(name
);
951 if ( cset
->usable() )
956 #endif // wxUSE_FONTMAP
958 wxLogError(_("Cannot convert from encoding '%s'!"), name
);
963 // ============================================================================
964 // wxCSConv implementation
965 // ============================================================================
967 wxCSConv::wxCSConv(const wxChar
*charset
)
969 m_name
= (wxChar
*)NULL
;
970 m_cset
= (wxCharacterSet
*) NULL
;
976 wxCSConv::~wxCSConv()
981 wxCSConv::wxCSConv(const wxCSConv
& conv
)
985 SetName(conv
.m_name
);
988 wxCSConv
& wxCSConv::operator=(const wxCSConv
& conv
)
991 SetName(conv
.m_name
);
995 void wxCSConv::Clear()
1005 void wxCSConv::SetName(const wxChar
*charset
)
1009 m_name
= wxStrdup(charset
);
1014 void wxCSConv::LoadNow()
1018 // it would probably be better to make GetSystemEncodingName() always
1019 // available (i.e. even when wxUSE_INTL == 0)?
1023 wxString name
= wxLocale::GetSystemEncodingName();
1024 if ( !name
.empty() )
1029 #endif // wxUSE_INTL
1031 // wxGetCharacterSet() complains about NULL name
1032 m_cset
= m_name
? wxGetCharacterSet(m_name
) : NULL
;
1037 size_t wxCSConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
1039 ((wxCSConv
*)this)->LoadNow(); // discard constness
1042 return m_cset
->MB2WC(buf
, psz
, n
);
1045 size_t len
= strlen(psz
);
1049 for (size_t c
= 0; c
<= len
; c
++)
1050 buf
[c
] = (unsigned char)(psz
[c
]);
1056 size_t wxCSConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
1058 ((wxCSConv
*)this)->LoadNow(); // discard constness
1061 return m_cset
->WC2MB(buf
, psz
, n
);
1064 const size_t len
= wxWcslen(psz
);
1067 for (size_t c
= 0; c
<= len
; c
++)
1068 buf
[c
] = (psz
[c
] > 0xff) ? '?' : psz
[c
];
1074 #endif // wxUSE_WCHAR_T