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_STRCONV_H_
13 #define _WX_STRCONV_H_
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 // You also have to implement Clone() to allow copying the conversions
46 // And you might need to override GetMBNulLen() as well.
47 class WXDLLIMPEXP_BASE wxMBConv
50 // The functions doing actual conversion from/to narrow to/from wide
53 // On success, the return value is the length (i.e. the number of
54 // characters, not bytes) of the converted string including any trailing
55 // L'\0' or (possibly multiple) '\0'(s). If the conversion fails or if
56 // there is not enough space for everything, including the trailing NUL
57 // character(s), in the output buffer, (size_t)-1 is returned.
59 // In the special case when dstLen is 0 (outputBuf may be NULL then) the
60 // return value is the length of the needed buffer but nothing happens
61 // otherwise. If srcLen is -1, the entire string, up to and including the
62 // trailing NUL(s), is converted, otherwise exactly srcLen bytes are.
66 // size_t dstLen = conv.ToWChar(NULL, 0, src);
67 // if ( dstLen != wxCONV_FAILED )
68 // ... handle error ...
69 // wchar_t *wbuf = new wchar_t[dstLen];
70 // conv.ToWChar(wbuf, dstLen, src);
72 virtual size_t ToWChar(wchar_t *dst
, size_t dstLen
,
73 const char *src
, size_t srcLen
= -1) const;
75 virtual size_t FromWChar(char *dst
, size_t dstLen
,
76 const wchar_t *src
, size_t srcLen
= -1) const;
79 // Convenience functions for translating NUL-terminated strings: returns
80 // the buffer containing the converted string or NULL pointer if the
82 const wxWCharBuffer
cMB2WC(const char *in
) const;
83 const wxCharBuffer
cWC2MB(const wchar_t *in
) const;
85 // Convenience functions for converting strings which may contain embedded
86 // NULs and don't have to be NUL-terminated.
88 // inLen is the length of the buffer including trailing NUL if any: if the
89 // last 4 bytes of the buffer are all NULs, these functions are more
90 // efficient as they avoid copying the string, but otherwise a copy is made
91 // internally which could be quite bad for (very) long strings.
93 // outLen receives, if not NULL, the length of the converted string or 0 if
94 // the conversion failed (returning 0 and not -1 in this case makes it
95 // difficult to distinguish between failed conversion and empty input but
96 // this is done for backwards compatibility)
98 cMB2WC(const char *in
, size_t inLen
, size_t *outLen
) const;
100 cWC2MB(const wchar_t *in
, size_t inLen
, size_t *outLen
) const;
102 // convenience functions for converting MB or WC to/from wxWin default
104 const wxWCharBuffer
cMB2WX(const char *psz
) const { return cMB2WC(psz
); }
105 const wxCharBuffer
cWX2MB(const wchar_t *psz
) const { return cWC2MB(psz
); }
106 const wchar_t* cWC2WX(const wchar_t *psz
) const { return psz
; }
107 const wchar_t* cWX2WC(const wchar_t *psz
) const { return psz
; }
109 const char* cMB2WX(const char *psz
) const { return psz
; }
110 const char* cWX2MB(const char *psz
) const { return psz
; }
111 const wxCharBuffer
cWC2WX(const wchar_t *psz
) const { return cWC2MB(psz
); }
112 const wxWCharBuffer
cWX2WC(const char *psz
) const { return cMB2WC(psz
); }
113 #endif // Unicode/ANSI
115 // this function is used in the implementation of cMB2WC() to distinguish
116 // between the following cases:
118 // a) var width encoding with strings terminated by a single NUL
119 // (usual multibyte encodings): return 1 in this case
120 // b) fixed width encoding with 2 bytes/char and so terminated by
121 // 2 NULs (UTF-16/UCS-2 and variants): return 2 in this case
122 // c) fixed width encoding with 4 bytes/char and so terminated by
123 // 4 NULs (UTF-32/UCS-4 and variants): return 4 in this case
125 // anything else is not supported currently and -1 should be returned
126 virtual size_t GetMBNulLen() const { return 1; }
128 // return the maximal value currently returned by GetMBNulLen() for any
130 static size_t GetMaxMBNulLen() { return 4 /* for UTF-32 */; }
133 // The old conversion functions. The existing classes currently mostly
134 // implement these ones but we're in transition to using To/FromWChar()
135 // instead and any new classes should implement just the new functions.
136 // For now, however, we provide default implementation of To/FromWChar() in
137 // this base class in terms of MB2WC/WC2MB() to avoid having to rewrite all
138 // the conversions at once.
140 // On success, the return value is the length (i.e. the number of
141 // characters, not bytes) not counting the trailing NUL(s) of the converted
142 // string. On failure, (size_t)-1 is returned. In the special case when
143 // outputBuf is NULL the return value is the same one but nothing is
144 // written to the buffer.
146 // Note that outLen is the length of the output buffer, not the length of
147 // the input (which is always supposed to be terminated by one or more
148 // NULs, as appropriate for the encoding)!
149 virtual size_t MB2WC(wchar_t *out
, const char *in
, size_t outLen
) const;
150 virtual size_t WC2MB(char *out
, const wchar_t *in
, size_t outLen
) const;
153 // make a heap-allocated copy of this object
154 virtual wxMBConv
*Clone() const = 0;
156 // virtual dtor for any base class
160 // ----------------------------------------------------------------------------
161 // wxMBConvLibc uses standard mbstowcs() and wcstombs() functions for
162 // conversion (hence it depends on the current locale)
163 // ----------------------------------------------------------------------------
165 class WXDLLIMPEXP_BASE wxMBConvLibc
: public wxMBConv
168 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
169 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
171 virtual wxMBConv
*Clone() const { return new wxMBConvLibc
; }
176 // ----------------------------------------------------------------------------
177 // wxConvBrokenFileNames is made for Unix in Unicode mode when
178 // files are accidentally written in an encoding which is not
179 // the system encoding. Typically, the system encoding will be
180 // UTF8 but there might be files stored in ISO8859-1 on disk.
181 // ----------------------------------------------------------------------------
183 class WXDLLIMPEXP_BASE wxConvBrokenFileNames
: public wxMBConv
186 wxConvBrokenFileNames(const wxChar
*charset
);
187 wxConvBrokenFileNames(const wxConvBrokenFileNames
& conv
)
188 : m_conv(conv
.m_conv
? conv
.m_conv
->Clone() : NULL
)
191 virtual ~wxConvBrokenFileNames() { delete m_conv
; }
193 virtual size_t MB2WC(wchar_t *out
, const char *in
, size_t outLen
) const
195 return m_conv
->MB2WC(out
, in
, outLen
);
198 virtual size_t WC2MB(char *out
, const wchar_t *in
, size_t outLen
) const
200 return m_conv
->WC2MB(out
, in
, outLen
);
203 virtual size_t GetMBNulLen() const
205 // cast needed to call a private function
206 return m_conv
->GetMBNulLen();
209 virtual wxMBConv
*Clone() const { return new wxConvBrokenFileNames(*this); }
212 // the conversion object we forward to
215 DECLARE_NO_ASSIGN_CLASS(wxConvBrokenFileNames
)
220 // ----------------------------------------------------------------------------
221 // wxMBConvUTF7 (for conversion using UTF7 encoding)
222 // ----------------------------------------------------------------------------
224 class WXDLLIMPEXP_BASE wxMBConvUTF7
: public wxMBConv
227 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
228 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
230 virtual wxMBConv
*Clone() const { return new wxMBConvUTF7
; }
233 // ----------------------------------------------------------------------------
234 // wxMBConvUTF8 (for conversion using UTF8 encoding)
235 // ----------------------------------------------------------------------------
237 class WXDLLIMPEXP_BASE wxMBConvUTF8
: public wxMBConv
242 MAP_INVALID_UTF8_NOT
= 0,
243 MAP_INVALID_UTF8_TO_PUA
= 1,
244 MAP_INVALID_UTF8_TO_OCTAL
= 2
247 wxMBConvUTF8(int options
= MAP_INVALID_UTF8_NOT
) : m_options(options
) { }
248 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
249 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
251 virtual wxMBConv
*Clone() const { return new wxMBConvUTF8(m_options
); }
257 // ----------------------------------------------------------------------------
258 // wxMBConvUTF16Base: for both LE and BE variants
259 // ----------------------------------------------------------------------------
261 class WXDLLIMPEXP_BASE wxMBConvUTF16Base
: public wxMBConv
264 virtual size_t GetMBNulLen() const { return 2; }
267 // ----------------------------------------------------------------------------
268 // wxMBConvUTF16LE (for conversion using UTF16 Little Endian encoding)
269 // ----------------------------------------------------------------------------
271 class WXDLLIMPEXP_BASE wxMBConvUTF16LE
: public wxMBConvUTF16Base
274 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
275 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
276 virtual wxMBConv
*Clone() const { return new wxMBConvUTF16LE
; }
279 // ----------------------------------------------------------------------------
280 // wxMBConvUTF16BE (for conversion using UTF16 Big Endian encoding)
281 // ----------------------------------------------------------------------------
283 class WXDLLIMPEXP_BASE wxMBConvUTF16BE
: public wxMBConvUTF16Base
286 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
287 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
288 virtual wxMBConv
*Clone() const { return new wxMBConvUTF16BE
; }
291 // ----------------------------------------------------------------------------
292 // wxMBConvUTF32Base: base class for both LE and BE variants
293 // ----------------------------------------------------------------------------
295 class WXDLLIMPEXP_BASE wxMBConvUTF32Base
: public wxMBConv
298 virtual size_t GetMBNulLen() const { return 4; }
301 // ----------------------------------------------------------------------------
302 // wxMBConvUTF32LE (for conversion using UTF32 Little Endian encoding)
303 // ----------------------------------------------------------------------------
305 class WXDLLIMPEXP_BASE wxMBConvUTF32LE
: public wxMBConvUTF32Base
308 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
309 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
310 virtual wxMBConv
*Clone() const { return new wxMBConvUTF32LE
; }
313 // ----------------------------------------------------------------------------
314 // wxMBConvUTF32BE (for conversion using UTF32 Big Endian encoding)
315 // ----------------------------------------------------------------------------
317 class WXDLLIMPEXP_BASE wxMBConvUTF32BE
: public wxMBConvUTF32Base
320 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
321 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
322 virtual wxMBConv
*Clone() const { return new wxMBConvUTF32BE
; }
325 // ----------------------------------------------------------------------------
326 // wxCSConv (for conversion based on loadable char sets)
327 // ----------------------------------------------------------------------------
329 #include "wx/fontenc.h"
331 class WXDLLIMPEXP_BASE wxCSConv
: public wxMBConv
334 // we can be created either from charset name or from an encoding constant
335 // but we can't have both at once
336 wxCSConv(const wxChar
*charset
);
337 wxCSConv(wxFontEncoding encoding
);
339 wxCSConv(const wxCSConv
& conv
);
342 wxCSConv
& operator=(const wxCSConv
& conv
);
344 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
345 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
346 virtual size_t GetMBNulLen() const;
347 virtual wxMBConv
*Clone() const { return new wxCSConv(*this); }
352 // common part of all ctors
355 // creates m_convReal if necessary
356 void CreateConvIfNeeded() const;
358 // do create m_convReal (unconditionally)
359 wxMBConv
*DoCreate() const;
361 // set the name (may be only called when m_name == NULL), makes copy of
363 void SetName(const wxChar
*charset
);
366 // note that we can't use wxString here because of compilation
367 // dependencies: we're included from wx/string.h
369 wxFontEncoding m_encoding
;
371 // use CreateConvIfNeeded() before accessing m_convReal!
372 wxMBConv
*m_convReal
;
377 // ----------------------------------------------------------------------------
378 // declare predefined conversion objects
379 // ----------------------------------------------------------------------------
381 // conversion to be used with all standard functions affected by locale, e.g.
382 // strtol(), strftime(), ...
383 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
&) wxConvLibc
;
385 // conversion ISO-8859-1/UTF-7/UTF-8 <-> wchar_t
386 extern WXDLLIMPEXP_DATA_BASE(wxCSConv
&) wxConvISO8859_1
;
387 extern WXDLLIMPEXP_DATA_BASE(wxMBConvUTF7
&) wxConvUTF7
;
388 extern WXDLLIMPEXP_DATA_BASE(wxMBConvUTF8
&) wxConvUTF8
;
390 // conversion used for the file names on the systems where they're not Unicode
391 // (basically anything except Windows)
393 // this is used by all file functions, can be changed by the application
395 // by default UTF-8 under Mac OS X and wxConvLibc elsewhere (but it's not used
396 // under Windows normally)
397 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
*) wxConvFileName
;
399 // backwards compatible define
400 #define wxConvFile (*wxConvFileName)
402 // the current conversion object, may be set to any conversion, is used by
403 // default in a couple of places inside wx (initially same as wxConvLibc)
404 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
*) wxConvCurrent
;
407 extern WXDLLIMPEXP_DATA_BASE(wxCSConv
&) wxConvLocal
;
410 // ----------------------------------------------------------------------------
411 // endianness-dependent conversions
412 // ----------------------------------------------------------------------------
414 #ifdef WORDS_BIGENDIAN
415 typedef wxMBConvUTF16BE wxMBConvUTF16
;
416 typedef wxMBConvUTF32BE wxMBConvUTF32
;
418 typedef wxMBConvUTF16LE wxMBConvUTF16
;
419 typedef wxMBConvUTF32LE wxMBConvUTF32
;
422 // ----------------------------------------------------------------------------
423 // filename conversion macros
424 // ----------------------------------------------------------------------------
426 // filenames are multibyte on Unix and probably widechar on Windows?
427 #if defined(__UNIX__) || defined(__BORLANDC__) || defined(__WXMAC__ )
433 #if wxMBFILES && wxUSE_UNICODE
434 #define wxFNCONV(name) wxConvFileName->cWX2MB(name)
435 #define wxFNSTRINGCAST wxMBSTRINGCAST
437 #if defined( __WXOSX__ ) && wxMBFILES
438 #define wxFNCONV(name) wxConvFileName->cWC2MB( wxConvLocal.cWX2WC(name) )
440 #define wxFNCONV(name) name
442 #define wxFNSTRINGCAST WXSTRINGCAST
445 #else // !wxUSE_WCHAR_T
447 // ----------------------------------------------------------------------------
448 // stand-ins in absence of wchar_t
449 // ----------------------------------------------------------------------------
451 class WXDLLIMPEXP_BASE wxMBConv
454 const char* cMB2WX(const char *psz
) const { return psz
; }
455 const char* cWX2MB(const char *psz
) const { return psz
; }
458 #define wxConvFile wxConvLocal
460 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
) wxConvLibc
,
464 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
*) wxConvCurrent
;
466 #define wxFNCONV(name) name
467 #define wxFNSTRINGCAST WXSTRINGCAST
472 // ----------------------------------------------------------------------------
473 // macros for the most common conversions
474 // ----------------------------------------------------------------------------
477 #define wxConvertWX2MB(s) wxConvCurrent->cWX2MB(s)
478 #define wxConvertMB2WX(s) wxConvCurrent->cMB2WX(s)
480 // no conversions to do
481 #define wxConvertWX2MB(s) (s)
482 #define wxConvertMB2WX(s) (s)
483 #endif // Unicode/ANSI
485 #endif // _WX_STRCONV_H_