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 // ----------------------------------------------------------------------------
32 // wxMBConv (abstract base class for conversions)
33 // ----------------------------------------------------------------------------
35 class WXDLLIMPEXP_BASE wxMBConv
38 // The functions doing actual conversion. On success, the return value is
39 // the length (i.e. the number of characters, not bytes, and not counting
40 // the trailing L'\0') of the converted string. On failure, (size_t)-1 is
41 // returned. In the special case when outputBuf is NULL the return value is
42 // the same one but nothing is written to the buffer.
44 // Note that outLen is the length of the output buffer, not the length of
45 // the input (which is always supposed to be terminated by one or more
46 // NULs, as appropriate for the encoding)!
47 virtual size_t MB2WC(wchar_t *out
, const char *in
, size_t outLen
) const = 0;
48 virtual size_t WC2MB(char *out
, const wchar_t *in
, size_t outLen
) const = 0;
51 const wxWCharBuffer
cMB2WC(const char *in
) const;
52 const wxCharBuffer
cWC2MB(const wchar_t *in
) const;
54 // Functions converting strings which may contain embedded NULs and don't
55 // have to be NUL-terminated.
57 // inLen is the length of the buffer including trailing NUL if any: if the
58 // last 4 bytes of the buffer are all NULs, these functions are more
59 // efficient as they avoid copying the string, but otherwise a copy is made
60 // internally which could be quite bad for (very) long strings.
62 // outLen receives, if not NULL, the length of the converted string or 0 if
63 // the conversion failed (returning 0 and not -1 in this case makes it
64 // difficult to distinguish between failed conversion and empty input but
65 // this is done for backwards compatibility)
67 cMB2WC(const char *in
, size_t inLen
, size_t *outLen
) const;
69 cWC2MB(const wchar_t *in
, size_t inLen
, size_t *outLen
) const;
71 // convenience functions for converting MB or WC to/from wxWin default
73 const wxWCharBuffer
cMB2WX(const char *psz
) const { return cMB2WC(psz
); }
74 const wxCharBuffer
cWX2MB(const wchar_t *psz
) const { return cWC2MB(psz
); }
75 const wchar_t* cWC2WX(const wchar_t *psz
) const { return psz
; }
76 const wchar_t* cWX2WC(const wchar_t *psz
) const { return psz
; }
78 const char* cMB2WX(const char *psz
) const { return psz
; }
79 const char* cWX2MB(const char *psz
) const { return psz
; }
80 const wxCharBuffer
cWC2WX(const wchar_t *psz
) const { return cWC2MB(psz
); }
81 const wxWCharBuffer
cWX2WC(const char *psz
) const { return cMB2WC(psz
); }
82 #endif // Unicode/ANSI
84 // virtual dtor for any base class
88 // this function must return the multibyte representation of L'\0'
90 // on error, nulLen should be set to -1
91 virtual const char *GetMBNul(size_t *nulLen
) const
99 // ----------------------------------------------------------------------------
100 // wxMBConvLibc uses standard mbstowcs() and wcstombs() functions for
101 // conversion (hence it depends on the current locale)
102 // ----------------------------------------------------------------------------
104 class WXDLLIMPEXP_BASE wxMBConvLibc
: public wxMBConv
107 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
108 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
113 // ----------------------------------------------------------------------------
114 // wxConvBrokenFileNames is made for Unix in Unicode mode when
115 // files are accidentally written in an encoding which is not
116 // the system encoding. Typically, the system encoding will be
117 // UTF8 but there might be files stored in ISO8859-1 on disk.
118 // ----------------------------------------------------------------------------
120 class WXDLLIMPEXP_BASE wxConvBrokenFileNames
: public wxMBConv
123 wxConvBrokenFileNames(const wxChar
*charset
);
124 virtual ~wxConvBrokenFileNames() { delete m_conv
; }
126 virtual size_t MB2WC(wchar_t *out
, const char *in
, size_t outLen
) const
128 return m_conv
->MB2WC(out
, in
, outLen
);
131 virtual size_t WC2MB(char *out
, const wchar_t *in
, size_t outLen
) const
133 return m_conv
->WC2MB(out
, in
, outLen
);
137 virtual const char *GetMBNul(size_t *nulLen
) const
139 // cast needed to call a private function
140 return ((wxConvBrokenFileNames
*)m_conv
)->GetMBNul(nulLen
);
144 // the conversion object we forward to
150 // ----------------------------------------------------------------------------
151 // wxMBConvUTF7 (for conversion using UTF7 encoding)
152 // ----------------------------------------------------------------------------
154 class WXDLLIMPEXP_BASE wxMBConvUTF7
: public wxMBConv
157 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
158 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
161 // ----------------------------------------------------------------------------
162 // wxMBConvUTF8 (for conversion using UTF8 encoding)
163 // ----------------------------------------------------------------------------
165 class WXDLLIMPEXP_BASE wxMBConvUTF8
: public wxMBConv
169 MAP_INVALID_UTF8_NOT
= 0,
170 MAP_INVALID_UTF8_TO_PUA
= 1,
171 MAP_INVALID_UTF8_TO_OCTAL
= 2
174 wxMBConvUTF8(int options
= MAP_INVALID_UTF8_NOT
) : m_options(options
) { }
175 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
176 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
182 // ----------------------------------------------------------------------------
183 // wxMBConvUTF16Base: for both LE and BE variants
184 // ----------------------------------------------------------------------------
186 class WXDLLIMPEXP_BASE wxMBConvUTF16Base
: public wxMBConv
189 virtual const char *GetMBNul(size_t *nulLen
) const
196 // ----------------------------------------------------------------------------
197 // wxMBConvUTF16LE (for conversion using UTF16 Little Endian encoding)
198 // ----------------------------------------------------------------------------
200 class WXDLLIMPEXP_BASE wxMBConvUTF16LE
: public wxMBConvUTF16Base
203 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
204 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
207 // ----------------------------------------------------------------------------
208 // wxMBConvUTF16BE (for conversion using UTF16 Big Endian encoding)
209 // ----------------------------------------------------------------------------
211 class WXDLLIMPEXP_BASE wxMBConvUTF16BE
: public wxMBConvUTF16Base
214 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
215 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
218 // ----------------------------------------------------------------------------
219 // wxMBConvUTF32Base: base class for both LE and BE variants
220 // ----------------------------------------------------------------------------
222 class WXDLLIMPEXP_BASE wxMBConvUTF32Base
: public wxMBConv
225 virtual const char *GetMBNul(size_t *nulLen
) const
232 // ----------------------------------------------------------------------------
233 // wxMBConvUTF32LE (for conversion using UTF32 Little Endian encoding)
234 // ----------------------------------------------------------------------------
236 class WXDLLIMPEXP_BASE wxMBConvUTF32LE
: public wxMBConvUTF32Base
239 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
240 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
243 // ----------------------------------------------------------------------------
244 // wxMBConvUTF32BE (for conversion using UTF32 Big Endian encoding)
245 // ----------------------------------------------------------------------------
247 class WXDLLIMPEXP_BASE wxMBConvUTF32BE
: public wxMBConvUTF32Base
250 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
251 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
254 // ----------------------------------------------------------------------------
255 // wxCSConv (for conversion based on loadable char sets)
256 // ----------------------------------------------------------------------------
258 #include "wx/fontenc.h"
260 class WXDLLIMPEXP_BASE wxCSConv
: public wxMBConv
263 // we can be created either from charset name or from an encoding constant
264 // but we can't have both at once
265 wxCSConv(const wxChar
*charset
);
266 wxCSConv(wxFontEncoding encoding
);
268 wxCSConv(const wxCSConv
& conv
);
271 wxCSConv
& operator=(const wxCSConv
& conv
);
273 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
274 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
279 // common part of all ctors
282 // creates m_convReal if necessary
283 void CreateConvIfNeeded() const;
285 // do create m_convReal (unconditionally)
286 wxMBConv
*DoCreate() const;
288 // set the name (may be only called when m_name == NULL), makes copy of
290 void SetName(const wxChar
*charset
);
292 virtual const char *GetMBNul(size_t *nulLen
) const;
295 // note that we can't use wxString here because of compilation
296 // dependencies: we're included from wx/string.h
298 wxFontEncoding m_encoding
;
300 // use CreateConvIfNeeded() before accessing m_convReal!
301 wxMBConv
*m_convReal
;
306 // ----------------------------------------------------------------------------
307 // declare predefined conversion objects
308 // ----------------------------------------------------------------------------
310 // conversion to be used with all standard functions affected by locale, e.g.
311 // strtol(), strftime(), ...
312 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
&) wxConvLibc
;
314 // conversion ISO-8859-1/UTF-7/UTF-8 <-> wchar_t
315 extern WXDLLIMPEXP_DATA_BASE(wxCSConv
&) wxConvISO8859_1
;
316 extern WXDLLIMPEXP_DATA_BASE(wxMBConvUTF7
&) wxConvUTF7
;
317 extern WXDLLIMPEXP_DATA_BASE(wxMBConvUTF8
&) wxConvUTF8
;
319 // conversion used for the file names on the systems where they're not Unicode
320 // (basically anything except Windows)
322 // this is used by all file functions, can be changed by the application
324 // by default UTF-8 under Mac OS X and wxConvLibc elsewhere (but it's not used
325 // under Windows normally)
326 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
*) wxConvFileName
;
328 // backwards compatible define
329 #define wxConvFile (*wxConvFileName)
331 // the current conversion object, may be set to any conversion, is used by
332 // default in a couple of places inside wx (initially same as wxConvLibc)
333 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
*) wxConvCurrent
;
336 extern WXDLLIMPEXP_DATA_BASE(wxCSConv
&) wxConvLocal
;
339 // ----------------------------------------------------------------------------
340 // endianness-dependent conversions
341 // ----------------------------------------------------------------------------
343 #ifdef WORDS_BIGENDIAN
344 typedef wxMBConvUTF16BE wxMBConvUTF16
;
345 typedef wxMBConvUTF32BE wxMBConvUTF32
;
347 typedef wxMBConvUTF16LE wxMBConvUTF16
;
348 typedef wxMBConvUTF32LE wxMBConvUTF32
;
351 // ----------------------------------------------------------------------------
352 // filename conversion macros
353 // ----------------------------------------------------------------------------
355 // filenames are multibyte on Unix and probably widechar on Windows?
356 #if defined(__UNIX__) || defined(__BORLANDC__) || defined(__WXMAC__ )
362 #if wxMBFILES && wxUSE_UNICODE
363 #define wxFNCONV(name) wxConvFileName->cWX2MB(name)
364 #define wxFNSTRINGCAST wxMBSTRINGCAST
366 #if defined( __WXOSX__ ) && wxMBFILES
367 #define wxFNCONV(name) wxConvFileName->cWC2MB( wxConvLocal.cWX2WC(name) )
369 #define wxFNCONV(name) name
371 #define wxFNSTRINGCAST WXSTRINGCAST
374 #else // !wxUSE_WCHAR_T
376 // ----------------------------------------------------------------------------
377 // stand-ins in absence of wchar_t
378 // ----------------------------------------------------------------------------
380 class WXDLLIMPEXP_BASE wxMBConv
383 const char* cMB2WX(const char *psz
) const { return psz
; }
384 const char* cWX2MB(const char *psz
) const { return psz
; }
387 #define wxConvFile wxConvLocal
389 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
) wxConvLibc
,
393 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
*) wxConvCurrent
;
395 #define wxFNCONV(name) name
396 #define wxFNSTRINGCAST WXSTRINGCAST
401 // ----------------------------------------------------------------------------
402 // macros for the most common conversions
403 // ----------------------------------------------------------------------------
406 #define wxConvertWX2MB(s) wxConvCurrent->cWX2MB(s)
407 #define wxConvertMB2WX(s) wxConvCurrent->cMB2WX(s)
409 // no conversions to do
410 #define wxConvertWX2MB(s) (s)
411 #define wxConvertMB2WX(s) (s)
412 #endif // Unicode/ANSI