added wxMBConv::Clone() to be able to copy conversion objects polymorphically
[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_STRCONV_H_
13 #define _WX_STRCONV_H_
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 // the error value returned by wxMBConv methods
32 #define wxCONV_FAILED ((size_t)-1)
33
34 // ----------------------------------------------------------------------------
35 // wxMBConv (abstract base class for conversions)
36 // ----------------------------------------------------------------------------
37
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
41 // confuse you.
42 //
43 // You also have to implement Clone() to allow copying the conversions
44 // polymorphically.
45 //
46 // And you might need to override GetMBNulLen() as well.
47 class WXDLLIMPEXP_BASE wxMBConv
48 {
49 public:
50 // The functions doing actual conversion from/to narrow to/from wide
51 // character strings.
52 //
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.
58 //
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.
63 //
64 // Typical usage:
65 //
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);
71 //
72 virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
73 const char *src, size_t srcLen = -1) const;
74
75 virtual size_t FromWChar(char *dst, size_t dstLen,
76 const wchar_t *src, size_t srcLen = -1) const;
77
78
79 // Convenience functions for translating NUL-terminated strings: returns
80 // the buffer containing the converted string or NULL pointer if the
81 // conversion failed.
82 const wxWCharBuffer cMB2WC(const char *in) const;
83 const wxCharBuffer cWC2MB(const wchar_t *in) const;
84
85 // Convenience functions for converting strings which may contain embedded
86 // NULs and don't have to be NUL-terminated.
87 //
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.
92 //
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)
97 const wxWCharBuffer
98 cMB2WC(const char *in, size_t inLen, size_t *outLen) const;
99 const wxCharBuffer
100 cWC2MB(const wchar_t *in, size_t inLen, size_t *outLen) const;
101
102 // convenience functions for converting MB or WC to/from wxWin default
103 #if wxUSE_UNICODE
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; }
108 #else // ANSI
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
114
115 // this function is used in the implementation of cMB2WC() to distinguish
116 // between the following cases:
117 //
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
124 //
125 // anything else is not supported currently and -1 should be returned
126 virtual size_t GetMBNulLen() const { return 1; }
127
128 // return the maximal value currently returned by GetMBNulLen() for any
129 // encoding
130 static size_t GetMaxMBNulLen() { return 4 /* for UTF-32 */; }
131
132
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.
139 //
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.
145 //
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;
151
152
153 // make a heap-allocated copy of this object
154 virtual wxMBConv *Clone() const = 0;
155
156 // virtual dtor for any base class
157 virtual ~wxMBConv();
158 };
159
160 // ----------------------------------------------------------------------------
161 // wxMBConvLibc uses standard mbstowcs() and wcstombs() functions for
162 // conversion (hence it depends on the current locale)
163 // ----------------------------------------------------------------------------
164
165 class WXDLLIMPEXP_BASE wxMBConvLibc : public wxMBConv
166 {
167 public:
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;
170
171 virtual wxMBConv *Clone() const { return new wxMBConvLibc; }
172 };
173
174 #ifdef __UNIX__
175
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 // ----------------------------------------------------------------------------
182
183 class WXDLLIMPEXP_BASE wxConvBrokenFileNames : public wxMBConv
184 {
185 public:
186 wxConvBrokenFileNames(const wxChar *charset);
187 wxConvBrokenFileNames(const wxConvBrokenFileNames& conv)
188 : m_conv(conv.m_conv ? conv.m_conv->Clone() : NULL)
189 {
190 }
191 virtual ~wxConvBrokenFileNames() { delete m_conv; }
192
193 virtual size_t MB2WC(wchar_t *out, const char *in, size_t outLen) const
194 {
195 return m_conv->MB2WC(out, in, outLen);
196 }
197
198 virtual size_t WC2MB(char *out, const wchar_t *in, size_t outLen) const
199 {
200 return m_conv->WC2MB(out, in, outLen);
201 }
202
203 virtual size_t GetMBNulLen() const
204 {
205 // cast needed to call a private function
206 return m_conv->GetMBNulLen();
207 }
208
209 virtual wxMBConv *Clone() const { return new wxConvBrokenFileNames(*this); }
210
211 private:
212 // the conversion object we forward to
213 wxMBConv *m_conv;
214
215 DECLARE_NO_ASSIGN_CLASS(wxConvBrokenFileNames)
216 };
217
218 #endif // __UNIX__
219
220 // ----------------------------------------------------------------------------
221 // wxMBConvUTF7 (for conversion using UTF7 encoding)
222 // ----------------------------------------------------------------------------
223
224 class WXDLLIMPEXP_BASE wxMBConvUTF7 : public wxMBConv
225 {
226 public:
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;
229
230 virtual wxMBConv *Clone() const { return new wxMBConvUTF7; }
231 };
232
233 // ----------------------------------------------------------------------------
234 // wxMBConvUTF8 (for conversion using UTF8 encoding)
235 // ----------------------------------------------------------------------------
236
237 class WXDLLIMPEXP_BASE wxMBConvUTF8 : public wxMBConv
238 {
239 public:
240 enum
241 {
242 MAP_INVALID_UTF8_NOT = 0,
243 MAP_INVALID_UTF8_TO_PUA = 1,
244 MAP_INVALID_UTF8_TO_OCTAL = 2
245 };
246
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;
250
251 virtual wxMBConv *Clone() const { return new wxMBConvUTF8(m_options); }
252
253 private:
254 int m_options;
255 };
256
257 // ----------------------------------------------------------------------------
258 // wxMBConvUTF16Base: for both LE and BE variants
259 // ----------------------------------------------------------------------------
260
261 class WXDLLIMPEXP_BASE wxMBConvUTF16Base : public wxMBConv
262 {
263 public:
264 virtual size_t GetMBNulLen() const { return 2; }
265 };
266
267 // ----------------------------------------------------------------------------
268 // wxMBConvUTF16LE (for conversion using UTF16 Little Endian encoding)
269 // ----------------------------------------------------------------------------
270
271 class WXDLLIMPEXP_BASE wxMBConvUTF16LE : public wxMBConvUTF16Base
272 {
273 public:
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; }
277 };
278
279 // ----------------------------------------------------------------------------
280 // wxMBConvUTF16BE (for conversion using UTF16 Big Endian encoding)
281 // ----------------------------------------------------------------------------
282
283 class WXDLLIMPEXP_BASE wxMBConvUTF16BE : public wxMBConvUTF16Base
284 {
285 public:
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; }
289 };
290
291 // ----------------------------------------------------------------------------
292 // wxMBConvUTF32Base: base class for both LE and BE variants
293 // ----------------------------------------------------------------------------
294
295 class WXDLLIMPEXP_BASE wxMBConvUTF32Base : public wxMBConv
296 {
297 public:
298 virtual size_t GetMBNulLen() const { return 4; }
299 };
300
301 // ----------------------------------------------------------------------------
302 // wxMBConvUTF32LE (for conversion using UTF32 Little Endian encoding)
303 // ----------------------------------------------------------------------------
304
305 class WXDLLIMPEXP_BASE wxMBConvUTF32LE : public wxMBConvUTF32Base
306 {
307 public:
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; }
311 };
312
313 // ----------------------------------------------------------------------------
314 // wxMBConvUTF32BE (for conversion using UTF32 Big Endian encoding)
315 // ----------------------------------------------------------------------------
316
317 class WXDLLIMPEXP_BASE wxMBConvUTF32BE : public wxMBConvUTF32Base
318 {
319 public:
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; }
323 };
324
325 // ----------------------------------------------------------------------------
326 // wxCSConv (for conversion based on loadable char sets)
327 // ----------------------------------------------------------------------------
328
329 #include "wx/fontenc.h"
330
331 class WXDLLIMPEXP_BASE wxCSConv : public wxMBConv
332 {
333 public:
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);
338
339 wxCSConv(const wxCSConv& conv);
340 virtual ~wxCSConv();
341
342 wxCSConv& operator=(const wxCSConv& conv);
343
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); }
348
349 void Clear();
350
351 private:
352 // common part of all ctors
353 void Init();
354
355 // creates m_convReal if necessary
356 void CreateConvIfNeeded() const;
357
358 // do create m_convReal (unconditionally)
359 wxMBConv *DoCreate() const;
360
361 // set the name (may be only called when m_name == NULL), makes copy of
362 // charset string
363 void SetName(const wxChar *charset);
364
365
366 // note that we can't use wxString here because of compilation
367 // dependencies: we're included from wx/string.h
368 wxChar *m_name;
369 wxFontEncoding m_encoding;
370
371 // use CreateConvIfNeeded() before accessing m_convReal!
372 wxMBConv *m_convReal;
373 bool m_deferred;
374 };
375
376
377 // ----------------------------------------------------------------------------
378 // declare predefined conversion objects
379 // ----------------------------------------------------------------------------
380
381 // conversion to be used with all standard functions affected by locale, e.g.
382 // strtol(), strftime(), ...
383 extern WXDLLIMPEXP_DATA_BASE(wxMBConv&) wxConvLibc;
384
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;
389
390 // conversion used for the file names on the systems where they're not Unicode
391 // (basically anything except Windows)
392 //
393 // this is used by all file functions, can be changed by the application
394 //
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;
398
399 // backwards compatible define
400 #define wxConvFile (*wxConvFileName)
401
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;
405
406 // ???
407 extern WXDLLIMPEXP_DATA_BASE(wxCSConv&) wxConvLocal;
408
409
410 // ----------------------------------------------------------------------------
411 // endianness-dependent conversions
412 // ----------------------------------------------------------------------------
413
414 #ifdef WORDS_BIGENDIAN
415 typedef wxMBConvUTF16BE wxMBConvUTF16;
416 typedef wxMBConvUTF32BE wxMBConvUTF32;
417 #else
418 typedef wxMBConvUTF16LE wxMBConvUTF16;
419 typedef wxMBConvUTF32LE wxMBConvUTF32;
420 #endif
421
422 // ----------------------------------------------------------------------------
423 // filename conversion macros
424 // ----------------------------------------------------------------------------
425
426 // filenames are multibyte on Unix and probably widechar on Windows?
427 #if defined(__UNIX__) || defined(__BORLANDC__) || defined(__WXMAC__ )
428 #define wxMBFILES 1
429 #else
430 #define wxMBFILES 0
431 #endif
432
433 #if wxMBFILES && wxUSE_UNICODE
434 #define wxFNCONV(name) wxConvFileName->cWX2MB(name)
435 #define wxFNSTRINGCAST wxMBSTRINGCAST
436 #else
437 #if defined( __WXOSX__ ) && wxMBFILES
438 #define wxFNCONV(name) wxConvFileName->cWC2MB( wxConvLocal.cWX2WC(name) )
439 #else
440 #define wxFNCONV(name) name
441 #endif
442 #define wxFNSTRINGCAST WXSTRINGCAST
443 #endif
444
445 #else // !wxUSE_WCHAR_T
446
447 // ----------------------------------------------------------------------------
448 // stand-ins in absence of wchar_t
449 // ----------------------------------------------------------------------------
450
451 class WXDLLIMPEXP_BASE wxMBConv
452 {
453 public:
454 const char* cMB2WX(const char *psz) const { return psz; }
455 const char* cWX2MB(const char *psz) const { return psz; }
456 };
457
458 #define wxConvFile wxConvLocal
459
460 extern WXDLLIMPEXP_DATA_BASE(wxMBConv) wxConvLibc,
461 wxConvLocal,
462 wxConvISO8859_1,
463 wxConvUTF8;
464 extern WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvCurrent;
465
466 #define wxFNCONV(name) name
467 #define wxFNSTRINGCAST WXSTRINGCAST
468
469 #endif
470 // wxUSE_WCHAR_T
471
472 // ----------------------------------------------------------------------------
473 // macros for the most common conversions
474 // ----------------------------------------------------------------------------
475
476 #if wxUSE_UNICODE
477 #define wxConvertWX2MB(s) wxConvCurrent->cWX2MB(s)
478 #define wxConvertMB2WX(s) wxConvCurrent->cMB2WX(s)
479 #else // ANSI
480 // no conversions to do
481 #define wxConvertWX2MB(s) (s)
482 #define wxConvertMB2WX(s) (s)
483 #endif // Unicode/ANSI
484
485 #endif // _WX_STRCONV_H_
486