replaced recently added wxMBConv::GetMBNul() with a less clever but better
[wxWidgets.git] / include / wx / strconv.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: strconv.h
3 // Purpose: conversion routines for char sets any Unicode
4 // Author: Robert Roebling, Ove Kaaven
5 // Modified by:
6 // Created: 29/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Ove Kaaven, Robert Roebling, Vadim Zeitlin
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_WXSTRCONVH__
13 #define _WX_WXSTRCONVH__
14
15 #include "wx/defs.h"
16 #include "wx/wxchar.h"
17 #include "wx/buffer.h"
18
19 #ifdef __DIGITALMARS__
20 #include "typeinfo.h"
21 #endif
22
23 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
24 # undef __BSEXCPT__
25 #endif
26
27 #include <stdlib.h>
28
29 #if wxUSE_WCHAR_T
30
31 // ----------------------------------------------------------------------------
32 // wxMBConv (abstract base class for conversions)
33 // ----------------------------------------------------------------------------
34
35 class WXDLLIMPEXP_BASE wxMBConv
36 {
37 public:
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.
43 //
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;
49
50 // MB <-> WC
51 const wxWCharBuffer cMB2WC(const char *in) const;
52 const wxCharBuffer cWC2MB(const wchar_t *in) const;
53
54 // Functions converting strings which may contain embedded NULs and don't
55 // have to be NUL-terminated.
56 //
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.
61 //
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)
66 const wxWCharBuffer
67 cMB2WC(const char *in, size_t inLen, size_t *outLen) const;
68 const wxCharBuffer
69 cWC2MB(const wchar_t *in, size_t inLen, size_t *outLen) const;
70
71 // convenience functions for converting MB or WC to/from wxWin default
72 #if wxUSE_UNICODE
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; }
77 #else // ANSI
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
83
84 // virtual dtor for any base class
85 virtual ~wxMBConv();
86
87 private:
88 // this function is used in the implementation of cMB2WC() to distinguish
89 // between the following cases:
90 //
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
97 //
98 // anything else is not supported currently and -1 should be returned
99 virtual size_t GetMinMBCharWidth() const { return 1; }
100 };
101
102 // ----------------------------------------------------------------------------
103 // wxMBConvLibc uses standard mbstowcs() and wcstombs() functions for
104 // conversion (hence it depends on the current locale)
105 // ----------------------------------------------------------------------------
106
107 class WXDLLIMPEXP_BASE wxMBConvLibc : public wxMBConv
108 {
109 public:
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;
112 };
113
114 #ifdef __UNIX__
115
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 // ----------------------------------------------------------------------------
122
123 class WXDLLIMPEXP_BASE wxConvBrokenFileNames : public wxMBConv
124 {
125 public:
126 wxConvBrokenFileNames(const wxChar *charset);
127 virtual ~wxConvBrokenFileNames() { delete m_conv; }
128
129 virtual size_t MB2WC(wchar_t *out, const char *in, size_t outLen) const
130 {
131 return m_conv->MB2WC(out, in, outLen);
132 }
133
134 virtual size_t WC2MB(char *out, const wchar_t *in, size_t outLen) const
135 {
136 return m_conv->WC2MB(out, in, outLen);
137 }
138
139 private:
140 virtual size_t GetMinMBCharWidth() const
141 {
142 // cast needed to call a private function
143 return ((wxConvBrokenFileNames *)m_conv)->GetMinMBCharWidth();
144 }
145
146
147 // the conversion object we forward to
148 wxMBConv *m_conv;
149 };
150
151 #endif // __UNIX__
152
153 // ----------------------------------------------------------------------------
154 // wxMBConvUTF7 (for conversion using UTF7 encoding)
155 // ----------------------------------------------------------------------------
156
157 class WXDLLIMPEXP_BASE wxMBConvUTF7 : public wxMBConv
158 {
159 public:
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;
162 };
163
164 // ----------------------------------------------------------------------------
165 // wxMBConvUTF8 (for conversion using UTF8 encoding)
166 // ----------------------------------------------------------------------------
167
168 class WXDLLIMPEXP_BASE wxMBConvUTF8 : public wxMBConv
169 {
170 public:
171 enum {
172 MAP_INVALID_UTF8_NOT = 0,
173 MAP_INVALID_UTF8_TO_PUA = 1,
174 MAP_INVALID_UTF8_TO_OCTAL = 2
175 };
176
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;
180
181 private:
182 int m_options;
183 };
184
185 // ----------------------------------------------------------------------------
186 // wxMBConvUTF16Base: for both LE and BE variants
187 // ----------------------------------------------------------------------------
188
189 class WXDLLIMPEXP_BASE wxMBConvUTF16Base : public wxMBConv
190 {
191 private:
192 virtual size_t GetMinMBCharWidth() const { return 2; }
193 };
194
195 // ----------------------------------------------------------------------------
196 // wxMBConvUTF16LE (for conversion using UTF16 Little Endian encoding)
197 // ----------------------------------------------------------------------------
198
199 class WXDLLIMPEXP_BASE wxMBConvUTF16LE : public wxMBConvUTF16Base
200 {
201 public:
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;
204 };
205
206 // ----------------------------------------------------------------------------
207 // wxMBConvUTF16BE (for conversion using UTF16 Big Endian encoding)
208 // ----------------------------------------------------------------------------
209
210 class WXDLLIMPEXP_BASE wxMBConvUTF16BE : public wxMBConvUTF16Base
211 {
212 public:
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;
215 };
216
217 // ----------------------------------------------------------------------------
218 // wxMBConvUTF32Base: base class for both LE and BE variants
219 // ----------------------------------------------------------------------------
220
221 class WXDLLIMPEXP_BASE wxMBConvUTF32Base : public wxMBConv
222 {
223 private:
224 virtual size_t GetMinMBCharWidth() const { return 4; }
225 };
226
227 // ----------------------------------------------------------------------------
228 // wxMBConvUTF32LE (for conversion using UTF32 Little Endian encoding)
229 // ----------------------------------------------------------------------------
230
231 class WXDLLIMPEXP_BASE wxMBConvUTF32LE : public wxMBConvUTF32Base
232 {
233 public:
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;
236 };
237
238 // ----------------------------------------------------------------------------
239 // wxMBConvUTF32BE (for conversion using UTF32 Big Endian encoding)
240 // ----------------------------------------------------------------------------
241
242 class WXDLLIMPEXP_BASE wxMBConvUTF32BE : public wxMBConvUTF32Base
243 {
244 public:
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;
247 };
248
249 // ----------------------------------------------------------------------------
250 // wxCSConv (for conversion based on loadable char sets)
251 // ----------------------------------------------------------------------------
252
253 #include "wx/fontenc.h"
254
255 class WXDLLIMPEXP_BASE wxCSConv : public wxMBConv
256 {
257 public:
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);
262
263 wxCSConv(const wxCSConv& conv);
264 virtual ~wxCSConv();
265
266 wxCSConv& operator=(const wxCSConv& conv);
267
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;
270
271 void Clear() ;
272
273 private:
274 // common part of all ctors
275 void Init();
276
277 // creates m_convReal if necessary
278 void CreateConvIfNeeded() const;
279
280 // do create m_convReal (unconditionally)
281 wxMBConv *DoCreate() const;
282
283 // set the name (may be only called when m_name == NULL), makes copy of
284 // charset string
285 void SetName(const wxChar *charset);
286
287 virtual size_t GetMinMBCharWidth() const;
288
289
290 // note that we can't use wxString here because of compilation
291 // dependencies: we're included from wx/string.h
292 wxChar *m_name;
293 wxFontEncoding m_encoding;
294
295 // use CreateConvIfNeeded() before accessing m_convReal!
296 wxMBConv *m_convReal;
297 bool m_deferred;
298 };
299
300
301 // ----------------------------------------------------------------------------
302 // declare predefined conversion objects
303 // ----------------------------------------------------------------------------
304
305 // conversion to be used with all standard functions affected by locale, e.g.
306 // strtol(), strftime(), ...
307 extern WXDLLIMPEXP_DATA_BASE(wxMBConv&) wxConvLibc;
308
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;
313
314 // conversion used for the file names on the systems where they're not Unicode
315 // (basically anything except Windows)
316 //
317 // this is used by all file functions, can be changed by the application
318 //
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;
322
323 // backwards compatible define
324 #define wxConvFile (*wxConvFileName)
325
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;
329
330 // ???
331 extern WXDLLIMPEXP_DATA_BASE(wxCSConv&) wxConvLocal;
332
333
334 // ----------------------------------------------------------------------------
335 // endianness-dependent conversions
336 // ----------------------------------------------------------------------------
337
338 #ifdef WORDS_BIGENDIAN
339 typedef wxMBConvUTF16BE wxMBConvUTF16;
340 typedef wxMBConvUTF32BE wxMBConvUTF32;
341 #else
342 typedef wxMBConvUTF16LE wxMBConvUTF16;
343 typedef wxMBConvUTF32LE wxMBConvUTF32;
344 #endif
345
346 // ----------------------------------------------------------------------------
347 // filename conversion macros
348 // ----------------------------------------------------------------------------
349
350 // filenames are multibyte on Unix and probably widechar on Windows?
351 #if defined(__UNIX__) || defined(__BORLANDC__) || defined(__WXMAC__ )
352 #define wxMBFILES 1
353 #else
354 #define wxMBFILES 0
355 #endif
356
357 #if wxMBFILES && wxUSE_UNICODE
358 #define wxFNCONV(name) wxConvFileName->cWX2MB(name)
359 #define wxFNSTRINGCAST wxMBSTRINGCAST
360 #else
361 #if defined( __WXOSX__ ) && wxMBFILES
362 #define wxFNCONV(name) wxConvFileName->cWC2MB( wxConvLocal.cWX2WC(name) )
363 #else
364 #define wxFNCONV(name) name
365 #endif
366 #define wxFNSTRINGCAST WXSTRINGCAST
367 #endif
368
369 #else // !wxUSE_WCHAR_T
370
371 // ----------------------------------------------------------------------------
372 // stand-ins in absence of wchar_t
373 // ----------------------------------------------------------------------------
374
375 class WXDLLIMPEXP_BASE wxMBConv
376 {
377 public:
378 const char* cMB2WX(const char *psz) const { return psz; }
379 const char* cWX2MB(const char *psz) const { return psz; }
380 };
381
382 #define wxConvFile wxConvLocal
383
384 extern WXDLLIMPEXP_DATA_BASE(wxMBConv) wxConvLibc,
385 wxConvLocal,
386 wxConvISO8859_1,
387 wxConvUTF8;
388 extern WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvCurrent;
389
390 #define wxFNCONV(name) name
391 #define wxFNSTRINGCAST WXSTRINGCAST
392
393 #endif
394 // wxUSE_WCHAR_T
395
396 // ----------------------------------------------------------------------------
397 // macros for the most common conversions
398 // ----------------------------------------------------------------------------
399
400 #if wxUSE_UNICODE
401 #define wxConvertWX2MB(s) wxConvCurrent->cWX2MB(s)
402 #define wxConvertMB2WX(s) wxConvCurrent->cMB2WX(s)
403 #else // ANSI
404 // no conversions to do
405 #define wxConvertWX2MB(s) (s)
406 #define wxConvertMB2WX(s) (s)
407 #endif // Unicode/ANSI
408
409 #endif
410 // _WX_WXSTRCONVH__
411