]>
git.saurik.com Git - wxWidgets.git/blob - src/common/strconv.cpp
b5e8925decc8f748b9780666c1f2f125e9cc84f1
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 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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"
48 #include "wx/module.h"
49 #include "wx/strconv.h"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
56 WXDLLIMPEXP_DATA_BASE(wxMBConv
) wxConvLibc
;
57 WXDLLIMPEXP_DATA_BASE(wxCSConv
) wxConvLocal((const wxChar
*)NULL
);
58 WXDLLIMPEXP_DATA_BASE(wxCSConv
) wxConvISO8859_1(_T("iso-8859-1"));
60 // stand-ins in absence of wchar_t
61 WXDLLIMPEXP_DATA_BASE(wxMBConv
) wxConvLibc
,
66 #endif // wxUSE_WCHAR_T
68 WXDLLIMPEXP_DATA_BASE(wxMBConv
*) wxConvCurrent
= &wxConvLibc
;
70 class wxStrConvModule
: public wxModule
73 wxStrConvModule() : wxModule() { }
74 virtual bool OnInit() { return TRUE
; }
79 wxConvISO8859_1
.Clear();
83 DECLARE_DYNAMIC_CLASS(wxStrConvModule
)
86 IMPLEMENT_DYNAMIC_CLASS(wxStrConvModule
, wxModule
)
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
103 #include "wx/encconv.h"
104 #include "wx/fontmap.h"
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 #define BSWAP_UCS4(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT32_SWAP_ALWAYS(str[_c]); }
111 #define BSWAP_UTF16(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT16_SWAP_ALWAYS(str[_c]); }
113 // under Unix SIZEOF_WCHAR_T is defined by configure, but under other platforms
114 // it might be not defined - assume the most common value
115 #ifndef SIZEOF_WCHAR_T
116 #define SIZEOF_WCHAR_T 2
117 #endif // !defined(SIZEOF_WCHAR_T)
119 #if SIZEOF_WCHAR_T == 4
120 #define WC_NAME "UCS4"
121 #define WC_BSWAP BSWAP_UCS4
122 #ifdef WORDS_BIGENDIAN
123 #define WC_NAME_BEST "UCS-4BE"
125 #define WC_NAME_BEST "UCS-4LE"
127 #elif SIZEOF_WCHAR_T == 2
128 #define WC_NAME "UTF16"
129 #define WC_BSWAP BSWAP_UTF16
131 #ifdef WORDS_BIGENDIAN
132 #define WC_NAME_BEST "UTF-16BE"
134 #define WC_NAME_BEST "UTF-16LE"
136 #else // sizeof(wchar_t) != 2 nor 4
137 // I don't know what to do about this
138 #error "Weird sizeof(wchar_t): please report your platform details to wx-users mailing list"
141 // ============================================================================
143 // ============================================================================
145 // ----------------------------------------------------------------------------
146 // UTF-16 en/decoding
147 // ----------------------------------------------------------------------------
151 static size_t encode_utf16(wxUint32 input
, wchar_t *output
)
155 if (output
) *output
++ = (wchar_t) input
;
158 else if (input
>=0x110000)
166 *output
++ = (wchar_t) ((input
>> 10)+0xd7c0);
167 *output
++ = (wchar_t) ((input
&0x3ff)+0xdc00);
173 static size_t decode_utf16(const wchar_t* input
, wxUint32
& output
)
175 if ((*input
<0xd800) || (*input
>0xdfff))
180 else if ((input
[1]<0xdc00) || (input
[1]>=0xdfff))
187 output
= ((input
[0] - 0xd7c0) << 10) + (input
[1] - 0xdc00);
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
198 #define IGNORE_LIBC 0
200 wxMBConv::~wxMBConv()
202 // nothing to do here
205 size_t wxMBConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
210 for (size_t i
= 0; i
< strlen( psz
)+1; i
++)
211 buf
[i
] = (wchar_t) psz
[i
];
212 return strlen( psz
);
216 return strlen( psz
);
219 return wxMB2WC(buf
, psz
, n
);
223 size_t wxMBConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
228 for (size_t i
= 0; i
< wxStrlen( psz
)+1; i
++)
229 buf
[i
] = (char) psz
[i
];
230 return wxStrlen( psz
);
234 return wxStrlen( psz
);
237 return wxWC2MB(buf
, psz
, n
);
241 const wxWCharBuffer
wxMBConv::cMB2WC(const char *psz
) const
245 // calculate the length of the buffer needed first
246 size_t nLen
= MB2WC(NULL
, psz
, 0);
247 if ( nLen
!= (size_t)-1 )
249 // now do the actual conversion
250 wxWCharBuffer
buf(nLen
);
251 MB2WC(buf
.data(), psz
, nLen
+ 1); // with the trailing NUL
257 wxWCharBuffer
buf((wchar_t *)NULL
);
262 const wxCharBuffer
wxMBConv::cWC2MB(const wchar_t *pwz
) const
266 size_t nLen
= WC2MB(NULL
, pwz
, 0);
267 if ( nLen
!= (size_t)-1 )
269 wxCharBuffer
buf(nLen
);
270 WC2MB(buf
.data(), pwz
, nLen
+ 1);
276 wxCharBuffer
buf((char *)NULL
);
281 // ----------------------------------------------------------------------------
283 // ----------------------------------------------------------------------------
285 WXDLLIMPEXP_DATA_BASE(wxMBConvUTF7
) wxConvUTF7
;
288 static char utf7_setD
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
289 "abcdefghijklmnopqrstuvwxyz"
290 "0123456789'(),-./:?";
291 static char utf7_setO
[]="!\"#$%&*;<=>@[]^_`{|}";
292 static char utf7_setB
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
293 "abcdefghijklmnopqrstuvwxyz"
297 // TODO: write actual implementations of UTF-7 here
298 size_t wxMBConvUTF7::MB2WC(wchar_t * WXUNUSED(buf
),
299 const char * WXUNUSED(psz
),
300 size_t WXUNUSED(n
)) const
305 size_t wxMBConvUTF7::WC2MB(char * WXUNUSED(buf
),
306 const wchar_t * WXUNUSED(psz
),
307 size_t WXUNUSED(n
)) const
312 // ----------------------------------------------------------------------------
314 // ----------------------------------------------------------------------------
316 WXDLLIMPEXP_DATA_BASE(wxMBConvUTF8
) wxConvUTF8
;
318 static wxUint32 utf8_max
[]=
319 { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff, 0xffffffff };
321 size_t wxMBConvUTF8::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
325 while (*psz
&& ((!buf
) || (len
< n
)))
327 unsigned char cc
= *psz
++, fc
= cc
;
329 for (cnt
= 0; fc
& 0x80; cnt
++)
343 // invalid UTF-8 sequence
348 unsigned ocnt
= cnt
- 1;
349 wxUint32 res
= cc
& (0x3f >> cnt
);
353 if ((cc
& 0xC0) != 0x80)
355 // invalid UTF-8 sequence
358 res
= (res
<< 6) | (cc
& 0x3f);
360 if (res
<= utf8_max
[ocnt
])
362 // illegal UTF-8 encoding
366 size_t pa
= encode_utf16(res
, buf
);
367 if (pa
== (size_t)-1)
376 #endif // WC_UTF16/!WC_UTF16
380 if (buf
&& (len
< n
))
385 size_t wxMBConvUTF8::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
389 while (*psz
&& ((!buf
) || (len
< n
)))
393 size_t pa
= decode_utf16(psz
, cc
);
394 psz
+= (pa
== (size_t)-1) ? 1 : pa
;
396 cc
=(*psz
++) & 0x7fffffff;
399 for (cnt
= 0; cc
> utf8_max
[cnt
]; cnt
++) {}
413 *buf
++ = (char) ((-128 >> cnt
) | ((cc
>> (cnt
* 6)) & (0x3f >> cnt
)));
415 *buf
++ = (char) (0x80 | ((cc
>> (cnt
* 6)) & 0x3f));
420 if (buf
&& (len
<n
)) *buf
= 0;
425 // ============================================================================
426 // wxCharacterSet and derived classes
427 // ============================================================================
429 // ----------------------------------------------------------------------------
430 // wxCharacterSet is the ABC for the classes below
431 // ----------------------------------------------------------------------------
437 virtual ~wxCharacterSet() {}
439 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) = 0;
440 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) = 0;
441 virtual bool usable() const = 0;
444 // ----------------------------------------------------------------------------
445 // ID_CharSet: implementation of wxCharacterSet using an existing wxMBConv
446 // ----------------------------------------------------------------------------
448 class ID_CharSet
: public wxCharacterSet
451 ID_CharSet(wxMBConv
*cnv
) : work(cnv
) {}
453 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
454 { return work
? work
->MB2WC(buf
,psz
,n
) : (size_t)-1; }
456 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
457 { return work
? work
->WC2MB(buf
,psz
,n
) : (size_t)-1; }
460 { return work
!=NULL
; }
466 // ============================================================================
467 // The classes doing conversion using the iconv_xxx() functions
468 // ============================================================================
472 // VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with E2BIG
473 // if output buffer is _exactly_ as big as needed. Such case is (unless there's
474 // yet another bug in glibc) the only case when iconv() returns with (size_t)-1
475 // (which means error) and says there are 0 bytes left in the input buffer --
476 // when _real_ error occurs, bytes-left-in-input buffer is non-zero. Hence,
477 // this alternative test for iconv() failure.
478 // [This bug does not appear in glibc 2.2.]
479 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 1
480 #define ICONV_FAILED(cres, bufLeft) ((cres == (size_t)-1) && \
481 (errno != E2BIG || bufLeft != 0))
483 #define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1)
486 #define ICONV_CHAR_CAST(x) ((ICONV_CONST char **)(x))
488 // ----------------------------------------------------------------------------
489 // IC_CharSet: encapsulates an iconv character set
490 // ----------------------------------------------------------------------------
492 class IC_CharSet
: public wxCharacterSet
495 IC_CharSet(const wxChar
*name
);
496 virtual ~IC_CharSet();
498 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
);
499 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
);
502 { return (m2w
!= (iconv_t
)-1) && (w2m
!= (iconv_t
)-1); }
505 // the iconv handlers used to translate from multibyte to wide char and in
506 // the other direction
511 // the name (for iconv_open()) of a wide char charset - if none is
512 // available on this machine, it will remain NULL
513 static const char *ms_wcCharsetName
;
515 // true if the wide char encoding we use (i.e. ms_wcCharsetName) has
516 // different endian-ness than the native one
517 static bool ms_wcNeedsSwap
;
520 const char *IC_CharSet::ms_wcCharsetName
= NULL
;
521 bool IC_CharSet::ms_wcNeedsSwap
= FALSE
;
523 IC_CharSet::IC_CharSet(const wxChar
*name
)
524 : wxCharacterSet(name
)
526 // Do it the hard way
528 for (size_t i
= 0; i
< wxStrlen(name
)+1; i
++)
529 cname
[i
] = (char) name
[i
];
531 // check for charset that represents wchar_t:
532 if (ms_wcCharsetName
== NULL
)
534 ms_wcNeedsSwap
= FALSE
;
536 // try charset with explicit bytesex info (e.g. "UCS-4LE"):
537 ms_wcCharsetName
= WC_NAME_BEST
;
538 m2w
= iconv_open(ms_wcCharsetName
, cname
);
540 if (m2w
== (iconv_t
)-1)
542 // try charset w/o bytesex info (e.g. "UCS4")
543 // and check for bytesex ourselves:
544 ms_wcCharsetName
= WC_NAME
;
545 m2w
= iconv_open(ms_wcCharsetName
, cname
);
547 // last bet, try if it knows WCHAR_T pseudo-charset
548 if (m2w
== (iconv_t
)-1)
550 ms_wcCharsetName
= "WCHAR_T";
551 m2w
= iconv_open(ms_wcCharsetName
, cname
);
554 if (m2w
!= (iconv_t
)-1)
556 char buf
[2], *bufPtr
;
557 wchar_t wbuf
[2], *wbufPtr
;
565 outsz
= SIZEOF_WCHAR_T
* 2;
569 res
= iconv(m2w
, ICONV_CHAR_CAST(&bufPtr
), &insz
,
570 (char**)&wbufPtr
, &outsz
);
572 if (ICONV_FAILED(res
, insz
))
574 ms_wcCharsetName
= NULL
;
575 wxLogLastError(wxT("iconv"));
576 wxLogError(_("Conversion to charset '%s' doesn't work."), name
);
580 ms_wcNeedsSwap
= wbuf
[0] != (wchar_t)buf
[0];
585 ms_wcCharsetName
= NULL
;
587 // VS: we must not output an error here, since wxWindows will safely
588 // fall back to using wxEncodingConverter.
589 wxLogTrace(wxT("strconv"), wxT("Impossible to convert to/from charset '%s' with iconv, falling back to wxEncodingConverter."), name
);
593 wxLogTrace(wxT("strconv"), wxT("wchar_t charset is '%s', needs swap: %i"), ms_wcCharsetName
, ms_wcNeedsSwap
);
595 else // we already have ms_wcCharsetName
597 m2w
= iconv_open(ms_wcCharsetName
, cname
);
600 // NB: don't ever pass NULL to iconv_open(), it may crash!
601 if ( ms_wcCharsetName
)
603 w2m
= iconv_open( cname
, ms_wcCharsetName
);
611 IC_CharSet::~IC_CharSet()
613 if ( m2w
!= (iconv_t
)-1 )
615 if ( w2m
!= (iconv_t
)-1 )
619 size_t IC_CharSet::MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
621 size_t inbuf
= strlen(psz
);
622 size_t outbuf
= n
* SIZEOF_WCHAR_T
;
624 // VS: Use these instead of psz, buf because iconv() modifies its arguments:
625 wchar_t *bufPtr
= buf
;
626 const char *pszPtr
= psz
;
630 // have destination buffer, convert there
632 ICONV_CHAR_CAST(&pszPtr
), &inbuf
,
633 (char**)&bufPtr
, &outbuf
);
634 res
= n
- (outbuf
/ SIZEOF_WCHAR_T
);
638 // convert to native endianness
639 WC_BSWAP(buf
/* _not_ bufPtr */, res
)
642 // NB: iconv was given only strlen(psz) characters on input, and so
643 // it couldn't convert the trailing zero. Let's do it ourselves
644 // if there's some room left for it in the output buffer.
650 // no destination buffer... convert using temp buffer
651 // to calculate destination buffer requirement
656 outbuf
= 8*SIZEOF_WCHAR_T
;
659 ICONV_CHAR_CAST(&pszPtr
), &inbuf
,
660 (char**)&bufPtr
, &outbuf
);
662 res
+= 8-(outbuf
/SIZEOF_WCHAR_T
);
663 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
666 if (ICONV_FAILED(cres
, inbuf
))
668 //VS: it is ok if iconv fails, hence trace only
669 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
676 size_t IC_CharSet::WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
678 size_t inbuf
= wxWcslen(psz
) * SIZEOF_WCHAR_T
;
686 // need to copy to temp buffer to switch endianness
687 // this absolutely doesn't rock!
688 // (no, doing WC_BSWAP twice on the original buffer won't help, as it
689 // could be in read-only memory, or be accessed in some other thread)
690 tmpbuf
=(wchar_t*)malloc((inbuf
+1)*SIZEOF_WCHAR_T
);
691 memcpy(tmpbuf
,psz
,(inbuf
+1)*SIZEOF_WCHAR_T
);
692 WC_BSWAP(tmpbuf
, inbuf
)
698 // have destination buffer, convert there
699 cres
= iconv( w2m
, ICONV_CHAR_CAST(&psz
), &inbuf
, &buf
, &outbuf
);
703 // NB: iconv was given only wcslen(psz) characters on input, and so
704 // it couldn't convert the trailing zero. Let's do it ourselves
705 // if there's some room left for it in the output buffer.
711 // no destination buffer... convert using temp buffer
712 // to calculate destination buffer requirement
716 buf
= tbuf
; outbuf
= 16;
718 cres
= iconv( w2m
, ICONV_CHAR_CAST(&psz
), &inbuf
, &buf
, &outbuf
);
721 } while ((cres
==(size_t)-1) && (errno
==E2BIG
));
729 if (ICONV_FAILED(cres
, inbuf
))
731 //VS: it is ok if iconv fails, hence trace only
732 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
741 // ============================================================================
742 // Win32 conversion classes
743 // ============================================================================
745 #if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
748 extern WXDLLIMPEXP_BASE
long wxCharsetToCodepage(const wxChar
*charset
);
749 extern WXDLLIMPEXP_BASE
long wxEncodingToCodepage(wxFontEncoding encoding
);
751 class CP_CharSet
: public wxCharacterSet
754 CP_CharSet(const wxChar
* name
)
756 m_CodePage
= wxCharsetToCodepage(name
);
759 CP_CharSet(wxFontEncoding encoding
)
761 m_CodePage
= wxEncodingToCodepage(encoding
);
764 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
)
766 const size_t len
= ::MultiByteToWideChar
768 m_CodePage
, // code page
771 -1, // its length (NUL-terminated)
772 buf
, // output string
773 buf
? n
: 0 // size of output buffer
776 // note that it returns # of written chars for buf != NULL and *size*
777 // of the needed buffer for buf == NULL
778 return len
? (buf
? len
: len
- 1) : (size_t)-1;
781 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
)
783 const size_t len
= ::WideCharToMultiByte
785 m_CodePage
, // code page
788 -1, // it is (wide) NUL-terminated
789 buf
, // output buffer
790 buf
? n
: 0, // and its size
791 NULL
, // default "replacement" char
792 NULL
// [out] was it used?
795 // see the comment above!
796 return len
? (buf
? len
: len
- 1) : (size_t)-1;
800 { return m_CodePage
!= -1; }
805 #endif // defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
807 // ============================================================================
808 // wxEncodingConverter based conversion classes
809 // ============================================================================
813 class EC_CharSet
: public wxCharacterSet
818 m_ok
= m2w
.Init(m_enc
, wxFONTENCODING_UNICODE
) &&
819 w2m
.Init(wxFONTENCODING_UNICODE
, m_enc
);
823 // temporarily just use wxEncodingConverter stuff,
824 // so that it works while a better implementation is built
825 EC_CharSet(const wxChar
* name
)
828 m_enc
= wxFontMapper::Get()->CharsetToEncoding(name
, FALSE
);
830 m_enc
= wxFONTENCODING_SYSTEM
;
835 EC_CharSet(wxFontEncoding enc
)
842 size_t MB2WC(wchar_t *buf
, const char *psz
, size_t WXUNUSED(n
))
844 size_t inbuf
= strlen(psz
);
846 m2w
.Convert(psz
,buf
);
850 size_t WC2MB(char *buf
, const wchar_t *psz
, size_t WXUNUSED(n
))
852 const size_t inbuf
= wxWcslen(psz
);
854 w2m
.Convert(psz
,buf
);
859 bool usable() const { return m_ok
; }
862 wxFontEncoding m_enc
;
863 wxEncodingConverter m2w
, w2m
;
865 // were we initialized successfully?
868 DECLARE_NO_COPY_CLASS(EC_CharSet
)
871 #endif // wxUSE_FONTMAP
873 // ----------------------------------------------------------------------------
874 // the function creating the wxCharacterSet for the specified charset on the
875 // current system, trying all possibilities
877 // it uses the name if it is given or encoding if name == NULL
878 // ----------------------------------------------------------------------------
880 static wxCharacterSet
*
881 wxGetCharacterSet(const wxChar
*name
, wxFontEncoding encoding
)
883 // check for the special case of ASCII charset
884 if ( (!name
&& encoding
== wxFONTENCODING_DEFAULT
)
886 || (name
&& wxFontMapper::Get()->
887 CharsetToEncoding(name
) == wxFONTENCODING_DEFAULT
)
888 #endif // wxUSE_FONTMAP
891 // don't convert at all
895 wxCharacterSet
*cset
;
898 (wxStricmp(name
, wxT("UTF8")) == 0 ||
899 wxStricmp(name
, wxT("UTF-8")) == 0)) ||
900 encoding
== wxFONTENCODING_UTF8
)
902 cset
= new ID_CharSet(&wxConvUTF8
);
909 cset
= new IC_CharSet(name
);
918 // it can only be NULL in this case
921 #endif // !HAVE_ICONV
923 if ( cset
->usable() )
930 #if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
931 cset
= name
? new CP_CharSet(name
) : new CP_CharSet(encoding
);
932 if ( cset
->usable() )
937 #endif // defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
940 cset
= name
? new EC_CharSet(name
) : new EC_CharSet(encoding
);
941 if ( cset
->usable() )
946 #endif // wxUSE_FONTMAP
948 wxLogError(_("Cannot convert from encoding '%s'!"),
952 wxFontMapper::GetEncodingDescription(encoding
).c_str()
953 #else // !wxUSE_FONTMAP
954 wxString::Format(_T("%s"), encoding
).c_str()
955 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
961 // ============================================================================
962 // wxCSConv implementation
963 // ============================================================================
965 void wxCSConv::Init()
967 m_name
= (wxChar
*)NULL
;
968 m_cset
= (wxCharacterSet
*) NULL
;
972 wxCSConv::wxCSConv(const wxChar
*charset
)
975 m_encoding
= wxFONTENCODING_DEFAULT
;
980 wxCSConv::wxCSConv(wxFontEncoding encoding
)
984 m_encoding
= encoding
;
987 wxCSConv::~wxCSConv()
992 wxCSConv::wxCSConv(const wxCSConv
& conv
)
997 SetName(conv
.m_name
);
998 m_encoding
= conv
.m_encoding
;
1001 wxCSConv
& wxCSConv::operator=(const wxCSConv
& conv
)
1005 SetName(conv
.m_name
);
1006 m_encoding
= conv
.m_encoding
;
1011 void wxCSConv::Clear()
1020 void wxCSConv::SetName(const wxChar
*charset
)
1024 m_name
= wxStrdup(charset
);
1029 void wxCSConv::LoadNow()
1033 // it would probably be better to make GetSystemEncodingName() always
1034 // available (i.e. even when wxUSE_INTL == 0)?
1036 if ( !m_name
&& m_encoding
== wxFONTENCODING_DEFAULT
)
1038 wxString name
= wxLocale::GetSystemEncodingName();
1039 if ( !name
.empty() )
1044 #endif // wxUSE_INTL
1046 // wxGetCharacterSet() complains about NULL name
1047 m_cset
= wxGetCharacterSet(m_name
, m_encoding
);
1052 size_t wxCSConv::MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const
1054 ((wxCSConv
*)this)->LoadNow(); // discard constness
1057 return m_cset
->MB2WC(buf
, psz
, n
);
1060 size_t len
= strlen(psz
);
1064 for (size_t c
= 0; c
<= len
; c
++)
1065 buf
[c
] = (unsigned char)(psz
[c
]);
1071 size_t wxCSConv::WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const
1073 ((wxCSConv
*)this)->LoadNow(); // discard constness
1076 return m_cset
->WC2MB(buf
, psz
, n
);
1079 const size_t len
= wxWcslen(psz
);
1082 for (size_t c
= 0; c
<= len
; c
++)
1083 buf
[c
] = (psz
[c
] > 0xff) ? '?' : psz
[c
];
1089 #endif // wxUSE_WCHAR_T