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 is used in the implementation of cMB2WC() to distinguish
89 // between the following cases:
91 // a) var width encoding with strings terminated by a single NUL
92 // (usual multibyte encodings): return 1 in this case
93 // b) fixed width encoding with 2 bytes/char and so terminated by
94 // 2 NULs (UTF-16/UCS-2 and variants): return 2 in this case
95 // c) fixed width encoding with 4 bytes/char and so terminated by
96 // 4 NULs (UTF-32/UCS-4 and variants): return 4 in this case
98 // anything else is not supported currently and -1 should be returned
99 virtual size_t GetMinMBCharWidth() const { return 1; }
102 // ----------------------------------------------------------------------------
103 // wxMBConvLibc uses standard mbstowcs() and wcstombs() functions for
104 // conversion (hence it depends on the current locale)
105 // ----------------------------------------------------------------------------
107 class WXDLLIMPEXP_BASE wxMBConvLibc
: public wxMBConv
110 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
111 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
116 // ----------------------------------------------------------------------------
117 // wxConvBrokenFileNames is made for Unix in Unicode mode when
118 // files are accidentally written in an encoding which is not
119 // the system encoding. Typically, the system encoding will be
120 // UTF8 but there might be files stored in ISO8859-1 on disk.
121 // ----------------------------------------------------------------------------
123 class WXDLLIMPEXP_BASE wxConvBrokenFileNames
: public wxMBConv
126 wxConvBrokenFileNames(const wxChar
*charset
);
127 virtual ~wxConvBrokenFileNames() { delete m_conv
; }
129 virtual size_t MB2WC(wchar_t *out
, const char *in
, size_t outLen
) const
131 return m_conv
->MB2WC(out
, in
, outLen
);
134 virtual size_t WC2MB(char *out
, const wchar_t *in
, size_t outLen
) const
136 return m_conv
->WC2MB(out
, in
, outLen
);
140 virtual size_t GetMinMBCharWidth() const
142 // cast needed to call a private function
143 return ((wxConvBrokenFileNames
*)m_conv
)->GetMinMBCharWidth();
147 // the conversion object we forward to
153 // ----------------------------------------------------------------------------
154 // wxMBConvUTF7 (for conversion using UTF7 encoding)
155 // ----------------------------------------------------------------------------
157 class WXDLLIMPEXP_BASE wxMBConvUTF7
: public wxMBConv
160 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
161 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
164 // ----------------------------------------------------------------------------
165 // wxMBConvUTF8 (for conversion using UTF8 encoding)
166 // ----------------------------------------------------------------------------
168 class WXDLLIMPEXP_BASE wxMBConvUTF8
: public wxMBConv
172 MAP_INVALID_UTF8_NOT
= 0,
173 MAP_INVALID_UTF8_TO_PUA
= 1,
174 MAP_INVALID_UTF8_TO_OCTAL
= 2
177 wxMBConvUTF8(int options
= MAP_INVALID_UTF8_NOT
) : m_options(options
) { }
178 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
179 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
185 // ----------------------------------------------------------------------------
186 // wxMBConvUTF16Base: for both LE and BE variants
187 // ----------------------------------------------------------------------------
189 class WXDLLIMPEXP_BASE wxMBConvUTF16Base
: public wxMBConv
192 virtual size_t GetMinMBCharWidth() const { return 2; }
195 // ----------------------------------------------------------------------------
196 // wxMBConvUTF16LE (for conversion using UTF16 Little Endian encoding)
197 // ----------------------------------------------------------------------------
199 class WXDLLIMPEXP_BASE wxMBConvUTF16LE
: public wxMBConvUTF16Base
202 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
203 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
206 // ----------------------------------------------------------------------------
207 // wxMBConvUTF16BE (for conversion using UTF16 Big Endian encoding)
208 // ----------------------------------------------------------------------------
210 class WXDLLIMPEXP_BASE wxMBConvUTF16BE
: public wxMBConvUTF16Base
213 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
214 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
217 // ----------------------------------------------------------------------------
218 // wxMBConvUTF32Base: base class for both LE and BE variants
219 // ----------------------------------------------------------------------------
221 class WXDLLIMPEXP_BASE wxMBConvUTF32Base
: public wxMBConv
224 virtual size_t GetMinMBCharWidth() const { return 4; }
227 // ----------------------------------------------------------------------------
228 // wxMBConvUTF32LE (for conversion using UTF32 Little Endian encoding)
229 // ----------------------------------------------------------------------------
231 class WXDLLIMPEXP_BASE wxMBConvUTF32LE
: public wxMBConvUTF32Base
234 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
235 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
238 // ----------------------------------------------------------------------------
239 // wxMBConvUTF32BE (for conversion using UTF32 Big Endian encoding)
240 // ----------------------------------------------------------------------------
242 class WXDLLIMPEXP_BASE wxMBConvUTF32BE
: public wxMBConvUTF32Base
245 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
246 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
249 // ----------------------------------------------------------------------------
250 // wxCSConv (for conversion based on loadable char sets)
251 // ----------------------------------------------------------------------------
253 #include "wx/fontenc.h"
255 class WXDLLIMPEXP_BASE wxCSConv
: public wxMBConv
258 // we can be created either from charset name or from an encoding constant
259 // but we can't have both at once
260 wxCSConv(const wxChar
*charset
);
261 wxCSConv(wxFontEncoding encoding
);
263 wxCSConv(const wxCSConv
& conv
);
266 wxCSConv
& operator=(const wxCSConv
& conv
);
268 virtual size_t MB2WC(wchar_t *outputBuf
, const char *psz
, size_t outputSize
) const;
269 virtual size_t WC2MB(char *outputBuf
, const wchar_t *psz
, size_t outputSize
) const;
274 // common part of all ctors
277 // creates m_convReal if necessary
278 void CreateConvIfNeeded() const;
280 // do create m_convReal (unconditionally)
281 wxMBConv
*DoCreate() const;
283 // set the name (may be only called when m_name == NULL), makes copy of
285 void SetName(const wxChar
*charset
);
287 virtual size_t GetMinMBCharWidth() const;
290 // note that we can't use wxString here because of compilation
291 // dependencies: we're included from wx/string.h
293 wxFontEncoding m_encoding
;
295 // use CreateConvIfNeeded() before accessing m_convReal!
296 wxMBConv
*m_convReal
;
301 // ----------------------------------------------------------------------------
302 // declare predefined conversion objects
303 // ----------------------------------------------------------------------------
305 // conversion to be used with all standard functions affected by locale, e.g.
306 // strtol(), strftime(), ...
307 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
&) wxConvLibc
;
309 // conversion ISO-8859-1/UTF-7/UTF-8 <-> wchar_t
310 extern WXDLLIMPEXP_DATA_BASE(wxCSConv
&) wxConvISO8859_1
;
311 extern WXDLLIMPEXP_DATA_BASE(wxMBConvUTF7
&) wxConvUTF7
;
312 extern WXDLLIMPEXP_DATA_BASE(wxMBConvUTF8
&) wxConvUTF8
;
314 // conversion used for the file names on the systems where they're not Unicode
315 // (basically anything except Windows)
317 // this is used by all file functions, can be changed by the application
319 // by default UTF-8 under Mac OS X and wxConvLibc elsewhere (but it's not used
320 // under Windows normally)
321 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
*) wxConvFileName
;
323 // backwards compatible define
324 #define wxConvFile (*wxConvFileName)
326 // the current conversion object, may be set to any conversion, is used by
327 // default in a couple of places inside wx (initially same as wxConvLibc)
328 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
*) wxConvCurrent
;
331 extern WXDLLIMPEXP_DATA_BASE(wxCSConv
&) wxConvLocal
;
334 // ----------------------------------------------------------------------------
335 // endianness-dependent conversions
336 // ----------------------------------------------------------------------------
338 #ifdef WORDS_BIGENDIAN
339 typedef wxMBConvUTF16BE wxMBConvUTF16
;
340 typedef wxMBConvUTF32BE wxMBConvUTF32
;
342 typedef wxMBConvUTF16LE wxMBConvUTF16
;
343 typedef wxMBConvUTF32LE wxMBConvUTF32
;
346 // ----------------------------------------------------------------------------
347 // filename conversion macros
348 // ----------------------------------------------------------------------------
350 // filenames are multibyte on Unix and probably widechar on Windows?
351 #if defined(__UNIX__) || defined(__BORLANDC__) || defined(__WXMAC__ )
357 #if wxMBFILES && wxUSE_UNICODE
358 #define wxFNCONV(name) wxConvFileName->cWX2MB(name)
359 #define wxFNSTRINGCAST wxMBSTRINGCAST
361 #if defined( __WXOSX__ ) && wxMBFILES
362 #define wxFNCONV(name) wxConvFileName->cWC2MB( wxConvLocal.cWX2WC(name) )
364 #define wxFNCONV(name) name
366 #define wxFNSTRINGCAST WXSTRINGCAST
369 #else // !wxUSE_WCHAR_T
371 // ----------------------------------------------------------------------------
372 // stand-ins in absence of wchar_t
373 // ----------------------------------------------------------------------------
375 class WXDLLIMPEXP_BASE wxMBConv
378 const char* cMB2WX(const char *psz
) const { return psz
; }
379 const char* cWX2MB(const char *psz
) const { return psz
; }
382 #define wxConvFile wxConvLocal
384 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
) wxConvLibc
,
388 extern WXDLLIMPEXP_DATA_BASE(wxMBConv
*) wxConvCurrent
;
390 #define wxFNCONV(name) name
391 #define wxFNSTRINGCAST WXSTRINGCAST
396 // ----------------------------------------------------------------------------
397 // macros for the most common conversions
398 // ----------------------------------------------------------------------------
401 #define wxConvertWX2MB(s) wxConvCurrent->cWX2MB(s)
402 #define wxConvertMB2WX(s) wxConvCurrent->cMB2WX(s)
404 // no conversions to do
405 #define wxConvertWX2MB(s) (s)
406 #define wxConvertMB2WX(s) (s)
407 #endif // Unicode/ANSI