1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: conversion routines for char sets any Unicode
4 // Author: Robert Roebling, Ove Kaaven
8 // Copyright: (c) 1998 Ove Kaaven, Robert Roebling, Vadim Zeitlin
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_WXSTRCONVH__
13 #define _WX_WXSTRCONVH__
16 #include "wx/wxchar.h"
17 #include "wx/buffer.h"
19 #ifdef __DIGITALMARS__
23 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
31 // the error value returned by wxMBConv methods
32 #define wxCONV_FAILED ((size_t)-1)
34 // ----------------------------------------------------------------------------
35 // wxMBConv (abstract base class for conversions)
36 // ----------------------------------------------------------------------------
38 // When deriving a new class from wxMBConv you must reimplement ToWChar() and
39 // FromWChar() methods which are not pure virtual only for historical reasons,
40 // don't let the fact that the existing classes implement MB2WC/WC2MB() instead
43 // And you might need to override GetMBNulLen() as well.
44 class WXDLLIMPEXP_BASE wxMBConv
47 // The functions doing actual conversion from/to narrow to/from wide
50 // On success, the return value is the length (i.e. the number of
51 // characters, not bytes) of the converted string including any trailing
52 // L'\0' or (possibly multiple) '\0'(s). If the conversion fails or if
53 // there is not enough space for everything, including the trailing NUL
54 // character(s), in the output buffer, (size_t)-1 is returned.
56 // In the special case when dstLen is 0 (outputBuf may be NULL then) the
57 // return value is the length of the needed buffer but nothing happens
58 // otherwise. If srcLen is -1, the entire string, up to and including the
59 // trailing NUL(s), is converted, otherwise exactly srcLen bytes are.
63 // size_t dstLen = conv.ToWChar(NULL, 0, src);
64 // if ( dstLen != wxCONV_FAILED )
65 // ... handle error ...
66 // wchar_t *wbuf = new wchar_t[dstLen];
67 // conv.ToWChar(wbuf, dstLen, src);
69 virtual size_t ToWChar(wchar_t *dst
, size_t dstLen
,
70 const char *src
, size_t srcLen
= -1) const;
72 virtual size_t FromWChar(char *dst
, size_t dstLen
,
73 const wchar_t *src
, size_t srcLen
= -1) const;
76 // Convenience functions for translating NUL-terminated strings: returns
77 // the buffer containing the converted string or NULL pointer if the
79 const wxWCharBuffer
cMB2WC(const char *in
) const;
80 const wxCharBuffer
cWC2MB(const wchar_t *in
) const;
82 // Convenience functions for converting strings which may contain embedded
83 // NULs and don't have to be NUL-terminated.
85 // inLen is the length of the buffer including trailing NUL if any: if the
86 // last 4 bytes of the buffer are all NULs, these functions are more
87 // efficient as they avoid copying the string, but otherwise a copy is made
88 // internally which could be quite bad for (very) long strings.
90 // outLen receives, if not NULL, the length of the converted string or 0 if
91 // the conversion failed (returning 0 and not -1 in this case makes it
92 // difficult to distinguish between failed conversion and empty input but
93 // this is done for backwards compatibility)
95 cMB2WC(const char *in
, size_t inLen
, size_t *outLen
) const;
97 cWC2MB(const wchar_t *in
, size_t inLen
, size_t *outLen
) const;
99 // convenience functions for converting MB or WC to/from wxWin default
101 const wxWCharBuffer
cMB2WX(const char *psz
) const { return cMB2WC(psz
); }
102 const wxCharBuffer
cWX2MB(const wchar_t *psz
) const { return cWC2MB(psz
); }
103 const wchar_t* cWC2WX(const wchar_t *psz
) const { return psz
; }
104 const wchar_t* cWX2WC(const wchar_t *psz
) const { return psz
; }
106 const char* cMB2WX(const char *psz
) const { return psz
; }
107 const char* cWX2MB(const char *psz
) const { return psz
; }
108 const wxCharBuffer
cWC2WX(const wchar_t *psz
) const { return cWC2MB(psz
); }
109 const wxWCharBuffer
cWX2WC(const char *psz
) const { return cMB2WC(psz
); }
110 #endif // Unicode/ANSI
112 // this function is used in the implementation of cMB2WC() to distinguish
113 // between the following cases:
115 // a) var width encoding with strings terminated by a single NUL
116 // (usual multibyte encodings): return 1 in this case
117 // b) fixed width encoding with 2 bytes/char and so terminated by
118 // 2 NULs (UTF-16/UCS-2 and variants): return 2 in this case
119 // c) fixed width encoding with 4 bytes/char and so terminated by
120 // 4 NULs (UTF-32/UCS-4 and variants): return 4 in this case
122 // anything else is not supported currently and -1 should be returned
123 virtual size_t GetMBNulLen() const { return 1; }
125 // return the maximal value currently returned by GetMBNulLen() for any
127 static size_t GetMaxMBNulLen() { return 4 /* for UTF-32 */; }
130 // The old conversion functions. The existing classes currently mostly
131 // implement these ones but we're in transition to using To/FromWChar()
132 // instead and any new classes should implement just the new functions.
133 // For now, however, we provide default implementation of To/FromWChar() in
134 // this base class in terms of MB2WC/WC2MB() to avoid having to rewrite all
135 // the conversions at once.
137 // On success, the return value is the length (i.e. the number of
138 // characters, not bytes) not counting the trailing NUL(s) of the converted
139 // string. On failure, (size_t)-1 is returned. In the special case when
140 // outputBuf is NULL the return value is the same one but nothing is
141 // written to the buffer.
143 // Note that outLen is the length of the output buffer, not the length of
144 // the input (which is always supposed to be terminated by one or more
145 // NULs, as appropriate for the encoding)!
146 virtual size_t MB2WC(wchar_t *out
, const char *in
, size_t outLen
) const;
147 virtual size_t WC2MB(char *out
, const wchar_t *in
, size_t outLen
) const;
150 // virtual dtor for any base class
154 // ----------------------------------------------------------------------------
155 // wxMBConvLibc uses standard mbstowcs() and wcstombs() functions for
156 // conversion (hence it depends on the current locale)
157 // ----------------------------------------------------------------------------
159 class WXDLLIMPEXP_BASE wxMBConvLibc
: public wxMBConv
162 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
163 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
168 // ----------------------------------------------------------------------------
169 // wxConvBrokenFileNames is made for Unix in Unicode mode when
170 // files are accidentally written in an encoding which is not
171 // the system encoding. Typically, the system encoding will be
172 // UTF8 but there might be files stored in ISO8859-1 on disk.
173 // ----------------------------------------------------------------------------
175 class WXDLLIMPEXP_BASE wxConvBrokenFileNames
: public wxMBConv
178 wxConvBrokenFileNames(const wxChar
*charset
);
179 virtual ~wxConvBrokenFileNames() { delete m_conv
; }
181 virtual size_t MB2WC(wchar_t *out
, const char *in
, size_t outLen
) const
183 return m_conv
->MB2WC(out
, in
, outLen
);
186 virtual size_t WC2MB(char *out
, const wchar_t *in
, size_t outLen
) const
188 return m_conv
->WC2MB(out
, in
, outLen
);
191 virtual size_t GetMBNulLen() const
193 // cast needed to call a private function
194 return m_conv
->GetMBNulLen();
198 // the conversion object we forward to
204 // ----------------------------------------------------------------------------
205 // wxMBConvUTF7 (for conversion using UTF7 encoding)
206 // ----------------------------------------------------------------------------
208 class WXDLLIMPEXP_BASE wxMBConvUTF7
: public wxMBConv
211 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
212 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
215 // ----------------------------------------------------------------------------
216 // wxMBConvUTF8 (for conversion using UTF8 encoding)
217 // ----------------------------------------------------------------------------
219 class WXDLLIMPEXP_BASE wxMBConvUTF8
: public wxMBConv
223 MAP_INVALID_UTF8_NOT
= 0,
224 MAP_INVALID_UTF8_TO_PUA
= 1,
225 MAP_INVALID_UTF8_TO_OCTAL
= 2
228 wxMBConvUTF8(int options
= MAP_INVALID_UTF8_NOT
) : m_options(options
) { }
229 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
230 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
236 // ----------------------------------------------------------------------------
237 // wxMBConvUTF16Base: for both LE and BE variants
238 // ----------------------------------------------------------------------------
240 class WXDLLIMPEXP_BASE wxMBConvUTF16Base
: public wxMBConv
243 virtual size_t GetMBNulLen() const { return 2; }
246 // ----------------------------------------------------------------------------
247 // wxMBConvUTF16LE (for conversion using UTF16 Little Endian encoding)
248 // ----------------------------------------------------------------------------
250 class WXDLLIMPEXP_BASE wxMBConvUTF16LE
: public wxMBConvUTF16Base
253 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
254 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
257 // ----------------------------------------------------------------------------
258 // wxMBConvUTF16BE (for conversion using UTF16 Big Endian encoding)
259 // ----------------------------------------------------------------------------
261 class WXDLLIMPEXP_BASE wxMBConvUTF16BE
: public wxMBConvUTF16Base
264 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
265 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
268 // ----------------------------------------------------------------------------
269 // wxMBConvUTF32Base: base class for both LE and BE variants
270 // ----------------------------------------------------------------------------
272 class WXDLLIMPEXP_BASE wxMBConvUTF32Base
: public wxMBConv
275 virtual size_t GetMBNulLen() const { return 4; }
278 // ----------------------------------------------------------------------------
279 // wxMBConvUTF32LE (for conversion using UTF32 Little Endian encoding)
280 // ----------------------------------------------------------------------------
282 class WXDLLIMPEXP_BASE wxMBConvUTF32LE
: public wxMBConvUTF32Base
285 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
286 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
289 // ----------------------------------------------------------------------------
290 // wxMBConvUTF32BE (for conversion using UTF32 Big Endian encoding)
291 // ----------------------------------------------------------------------------
293 class WXDLLIMPEXP_BASE wxMBConvUTF32BE
: public wxMBConvUTF32Base
296 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
297 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
300 // ----------------------------------------------------------------------------
301 // wxCSConv (for conversion based on loadable char sets)
302 // ----------------------------------------------------------------------------
304 #include "wx/fontenc.h"
306 class WXDLLIMPEXP_BASE wxCSConv
: public wxMBConv
309 // we can be created either from charset name or from an encoding constant
310 // but we can't have both at once
311 wxCSConv(const wxChar
*charset
);
312 wxCSConv(wxFontEncoding encoding
);
314 wxCSConv(const wxCSConv
& conv
);
317 wxCSConv
& operator=(const wxCSConv
& conv
);
319 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
320 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
321 virtual size_t GetMBNulLen() const;
326 // common part of all ctors
329 // creates m_convReal if necessary
330 void CreateConvIfNeeded() const;
332 // do create m_convReal (unconditionally)
333 wxMBConv
*DoCreate() const;
335 // set the name (may be only called when m_name == NULL), makes copy of
337 void SetName(const wxChar
*charset
);
340 // note that we can't use wxString here because of compilation
341 // dependencies: we're included from wx/string.h
343 wxFontEncoding m_encoding
;
345 // use CreateConvIfNeeded() before accessing m_convReal!
346 wxMBConv
*m_convReal
;
351 // ----------------------------------------------------------------------------
352 // declare predefined conversion objects
353 // ----------------------------------------------------------------------------
355 // conversion to be used with all standard functions affected by locale, e.g.
356 // strtol(), strftime(), ...
357 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
&) wxConvLibc
;
359 // conversion ISO-8859-1/UTF-7/UTF-8 <-> wchar_t
360 extern WXDLLIMPEXP_DATA_BASE(wxCSConv
&) wxConvISO8859_1
;
361 extern WXDLLIMPEXP_DATA_BASE(wxMBConvUTF7
&) wxConvUTF7
;
362 extern WXDLLIMPEXP_DATA_BASE(wxMBConvUTF8
&) wxConvUTF8
;
364 // conversion used for the file names on the systems where they're not Unicode
365 // (basically anything except Windows)
367 // this is used by all file functions, can be changed by the application
369 // by default UTF-8 under Mac OS X and wxConvLibc elsewhere (but it's not used
370 // under Windows normally)
371 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
*) wxConvFileName
;
373 // backwards compatible define
374 #define wxConvFile (*wxConvFileName)
376 // the current conversion object, may be set to any conversion, is used by
377 // default in a couple of places inside wx (initially same as wxConvLibc)
378 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
*) wxConvCurrent
;
381 extern WXDLLIMPEXP_DATA_BASE(wxCSConv
&) wxConvLocal
;
384 // ----------------------------------------------------------------------------
385 // endianness-dependent conversions
386 // ----------------------------------------------------------------------------
388 #ifdef WORDS_BIGENDIAN
389 typedef wxMBConvUTF16BE wxMBConvUTF16
;
390 typedef wxMBConvUTF32BE wxMBConvUTF32
;
392 typedef wxMBConvUTF16LE wxMBConvUTF16
;
393 typedef wxMBConvUTF32LE wxMBConvUTF32
;
396 // ----------------------------------------------------------------------------
397 // filename conversion macros
398 // ----------------------------------------------------------------------------
400 // filenames are multibyte on Unix and probably widechar on Windows?
401 #if defined(__UNIX__) || defined(__BORLANDC__) || defined(__WXMAC__ )
407 #if wxMBFILES && wxUSE_UNICODE
408 #define wxFNCONV(name) wxConvFileName->cWX2MB(name)
409 #define wxFNSTRINGCAST wxMBSTRINGCAST
411 #if defined( __WXOSX__ ) && wxMBFILES
412 #define wxFNCONV(name) wxConvFileName->cWC2MB( wxConvLocal.cWX2WC(name) )
414 #define wxFNCONV(name) name
416 #define wxFNSTRINGCAST WXSTRINGCAST
419 #else // !wxUSE_WCHAR_T
421 // ----------------------------------------------------------------------------
422 // stand-ins in absence of wchar_t
423 // ----------------------------------------------------------------------------
425 class WXDLLIMPEXP_BASE wxMBConv
428 const char* cMB2WX(const char *psz
) const { return psz
; }
429 const char* cWX2MB(const char *psz
) const { return psz
; }
432 #define wxConvFile wxConvLocal
434 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
) wxConvLibc
,
438 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
*) wxConvCurrent
;
440 #define wxFNCONV(name) name
441 #define wxFNSTRINGCAST WXSTRINGCAST
446 // ----------------------------------------------------------------------------
447 // macros for the most common conversions
448 // ----------------------------------------------------------------------------
451 #define wxConvertWX2MB(s) wxConvCurrent->cWX2MB(s)
452 #define wxConvertMB2WX(s) wxConvCurrent->cMB2WX(s)
454 // no conversions to do
455 #define wxConvertWX2MB(s) (s)
456 #define wxConvertMB2WX(s) (s)
457 #endif // Unicode/ANSI