Described in the comments and documented the semantics of the parameters and
[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 must return the multibyte representation of L'\0'
89 //
90 // on error, nulLen should be set to -1
91 virtual const char *GetMBNul(size_t *nulLen) const
92 {
93 *nulLen = 1;
94
95 return "";
96 }
97 };
98
99 // ----------------------------------------------------------------------------
100 // wxMBConvLibc uses standard mbstowcs() and wcstombs() functions for
101 // conversion (hence it depends on the current locale)
102 // ----------------------------------------------------------------------------
103
104 class WXDLLIMPEXP_BASE wxMBConvLibc : public wxMBConv
105 {
106 public:
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;
109 };
110
111 #ifdef __UNIX__
112
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 // ----------------------------------------------------------------------------
119
120 class WXDLLIMPEXP_BASE wxConvBrokenFileNames : public wxMBConv
121 {
122 public:
123 wxConvBrokenFileNames(const wxChar *charset);
124 virtual ~wxConvBrokenFileNames() { delete m_conv; }
125
126 virtual size_t MB2WC(wchar_t *out, const char *in, size_t outLen) const
127 {
128 return m_conv->MB2WC(out, in, outLen);
129 }
130
131 virtual size_t WC2MB(char *out, const wchar_t *in, size_t outLen) const
132 {
133 return m_conv->WC2MB(out, in, outLen);
134 }
135
136 private:
137 virtual wxCharBuffer GetMBNul(size_t *nulLen) const
138 {
139 return m_conv->GetMBNul(nulLen);
140 }
141
142
143 // the conversion object we forward to
144 wxMBConv *m_conv;
145 };
146
147 #endif // __UNIX__
148
149 // ----------------------------------------------------------------------------
150 // wxMBConvUTF7 (for conversion using UTF7 encoding)
151 // ----------------------------------------------------------------------------
152
153 class WXDLLIMPEXP_BASE wxMBConvUTF7 : public wxMBConv
154 {
155 public:
156 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
157 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
158 };
159
160 // ----------------------------------------------------------------------------
161 // wxMBConvUTF8 (for conversion using UTF8 encoding)
162 // ----------------------------------------------------------------------------
163
164 class WXDLLIMPEXP_BASE wxMBConvUTF8 : public wxMBConv
165 {
166 public:
167 enum {
168 MAP_INVALID_UTF8_NOT = 0,
169 MAP_INVALID_UTF8_TO_PUA = 1,
170 MAP_INVALID_UTF8_TO_OCTAL = 2
171 };
172
173 wxMBConvUTF8(int options = MAP_INVALID_UTF8_NOT) : m_options(options) { }
174 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
175 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
176
177 private:
178 int m_options;
179 };
180
181 // ----------------------------------------------------------------------------
182 // wxMBConvUTF16Base: for both LE and BE variants
183 // ----------------------------------------------------------------------------
184
185 class WXDLLIMPEXP_BASE wxMBConvUTF16Base : public wxMBConv
186 {
187 private:
188 virtual const char *GetMBNul(size_t *nulLen) const
189 {
190 *nulLen = 2;
191 return "\0";
192 }
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 const char *GetMBNul(size_t *nulLen) const
225 {
226 *nulLen = 4;
227 return "\0\0\0";
228 }
229 };
230
231 // ----------------------------------------------------------------------------
232 // wxMBConvUTF32LE (for conversion using UTF32 Little Endian encoding)
233 // ----------------------------------------------------------------------------
234
235 class WXDLLIMPEXP_BASE wxMBConvUTF32LE : public wxMBConvUTF32Base
236 {
237 public:
238 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
239 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
240 };
241
242 // ----------------------------------------------------------------------------
243 // wxMBConvUTF32BE (for conversion using UTF32 Big Endian encoding)
244 // ----------------------------------------------------------------------------
245
246 class WXDLLIMPEXP_BASE wxMBConvUTF32BE : public wxMBConvUTF32Base
247 {
248 public:
249 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
250 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
251 };
252
253 // ----------------------------------------------------------------------------
254 // wxCSConv (for conversion based on loadable char sets)
255 // ----------------------------------------------------------------------------
256
257 #include "wx/fontenc.h"
258
259 class WXDLLIMPEXP_BASE wxCSConv : public wxMBConv
260 {
261 public:
262 // we can be created either from charset name or from an encoding constant
263 // but we can't have both at once
264 wxCSConv(const wxChar *charset);
265 wxCSConv(wxFontEncoding encoding);
266
267 wxCSConv(const wxCSConv& conv);
268 virtual ~wxCSConv();
269
270 wxCSConv& operator=(const wxCSConv& conv);
271
272 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
273 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
274
275 void Clear() ;
276
277 private:
278 // common part of all ctors
279 void Init();
280
281 // creates m_convReal if necessary
282 void CreateConvIfNeeded() const;
283
284 // do create m_convReal (unconditionally)
285 wxMBConv *DoCreate() const;
286
287 // set the name (may be only called when m_name == NULL), makes copy of
288 // charset string
289 void SetName(const wxChar *charset);
290
291 virtual const char *GetMBNul(size_t *nulLen) const;
292
293
294 // note that we can't use wxString here because of compilation
295 // dependencies: we're included from wx/string.h
296 wxChar *m_name;
297 wxFontEncoding m_encoding;
298
299 // use CreateConvIfNeeded() before accessing m_convReal!
300 wxMBConv *m_convReal;
301 bool m_deferred;
302 };
303
304
305 // ----------------------------------------------------------------------------
306 // declare predefined conversion objects
307 // ----------------------------------------------------------------------------
308
309 // conversion to be used with all standard functions affected by locale, e.g.
310 // strtol(), strftime(), ...
311 extern WXDLLIMPEXP_DATA_BASE(wxMBConv&) wxConvLibc;
312
313 // conversion ISO-8859-1/UTF-7/UTF-8 <-> wchar_t
314 extern WXDLLIMPEXP_DATA_BASE(wxCSConv&) wxConvISO8859_1;
315 extern WXDLLIMPEXP_DATA_BASE(wxMBConvUTF7&) wxConvUTF7;
316 extern WXDLLIMPEXP_DATA_BASE(wxMBConvUTF8&) wxConvUTF8;
317
318 // conversion used for the file names on the systems where they're not Unicode
319 // (basically anything except Windows)
320 //
321 // this is used by all file functions, can be changed by the application
322 //
323 // by default UTF-8 under Mac OS X and wxConvLibc elsewhere (but it's not used
324 // under Windows normally)
325 extern WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvFileName;
326
327 // backwards compatible define
328 #define wxConvFile (*wxConvFileName)
329
330 // the current conversion object, may be set to any conversion, is used by
331 // default in a couple of places inside wx (initially same as wxConvLibc)
332 extern WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvCurrent;
333
334 // ???
335 extern WXDLLIMPEXP_DATA_BASE(wxCSConv&) wxConvLocal;
336
337
338 // ----------------------------------------------------------------------------
339 // endianness-dependent conversions
340 // ----------------------------------------------------------------------------
341
342 #ifdef WORDS_BIGENDIAN
343 typedef wxMBConvUTF16BE wxMBConvUTF16;
344 typedef wxMBConvUTF32BE wxMBConvUTF32;
345 #else
346 typedef wxMBConvUTF16LE wxMBConvUTF16;
347 typedef wxMBConvUTF32LE wxMBConvUTF32;
348 #endif
349
350 // ----------------------------------------------------------------------------
351 // filename conversion macros
352 // ----------------------------------------------------------------------------
353
354 // filenames are multibyte on Unix and probably widechar on Windows?
355 #if defined(__UNIX__) || defined(__BORLANDC__) || defined(__WXMAC__ )
356 #define wxMBFILES 1
357 #else
358 #define wxMBFILES 0
359 #endif
360
361 #if wxMBFILES && wxUSE_UNICODE
362 #define wxFNCONV(name) wxConvFileName->cWX2MB(name)
363 #define wxFNSTRINGCAST wxMBSTRINGCAST
364 #else
365 #if defined( __WXOSX__ ) && wxMBFILES
366 #define wxFNCONV(name) wxConvFileName->cWC2MB( wxConvLocal.cWX2WC(name) )
367 #else
368 #define wxFNCONV(name) name
369 #endif
370 #define wxFNSTRINGCAST WXSTRINGCAST
371 #endif
372
373 #else // !wxUSE_WCHAR_T
374
375 // ----------------------------------------------------------------------------
376 // stand-ins in absence of wchar_t
377 // ----------------------------------------------------------------------------
378
379 class WXDLLIMPEXP_BASE wxMBConv
380 {
381 public:
382 const char* cMB2WX(const char *psz) const { return psz; }
383 const char* cWX2MB(const char *psz) const { return psz; }
384 };
385
386 #define wxConvFile wxConvLocal
387
388 extern WXDLLIMPEXP_DATA_BASE(wxMBConv) wxConvLibc,
389 wxConvLocal,
390 wxConvISO8859_1,
391 wxConvUTF8;
392 extern WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvCurrent;
393
394 #define wxFNCONV(name) name
395 #define wxFNSTRINGCAST WXSTRINGCAST
396
397 #endif
398 // wxUSE_WCHAR_T
399
400 // ----------------------------------------------------------------------------
401 // macros for the most common conversions
402 // ----------------------------------------------------------------------------
403
404 #if wxUSE_UNICODE
405 #define wxConvertWX2MB(s) wxConvCurrent->cWX2MB(s)
406 #define wxConvertMB2WX(s) wxConvCurrent->cMB2WX(s)
407 #else // ANSI
408 // no conversions to do
409 #define wxConvertWX2MB(s) (s)
410 #define wxConvertMB2WX(s) (s)
411 #endif // Unicode/ANSI
412
413 #endif
414 // _WX_WXSTRCONVH__
415