1. use a manifest constant wxNO_LEN instead of -1 for lengths everywhere
[wxWidgets.git] / include / wx / strconv.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: strconv.h
3 // Purpose: conversion routines for char sets any Unicode
4 // Author: Ove Kaaven, Robert Roebling, Vadim Zeitlin
5 // Modified by:
6 // Created: 29/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Ove Kaaven, Robert Roebling
9 // (c) 1998-2006 Vadim Zeitlin
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_STRCONV_H_
14 #define _WX_STRCONV_H_
15
16 #include "wx/defs.h"
17 #include "wx/wxchar.h"
18 #include "wx/buffer.h"
19
20 #ifdef __DIGITALMARS__
21 #include "typeinfo.h"
22 #endif
23
24 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
25 # undef __BSEXCPT__
26 #endif
27
28 #include <stdlib.h>
29
30 #if wxUSE_WCHAR_T
31
32 // the error value returned by wxMBConv methods
33 #define wxCONV_FAILED ((size_t)-1)
34
35 // the default value for some length parameters meaning that the string is
36 // NUL-terminated
37 #define wxNO_LEN ((size_t)-1)
38
39 // ----------------------------------------------------------------------------
40 // wxMBConv (abstract base class for conversions)
41 // ----------------------------------------------------------------------------
42
43 // When deriving a new class from wxMBConv you must reimplement ToWChar() and
44 // FromWChar() methods which are not pure virtual only for historical reasons,
45 // don't let the fact that the existing classes implement MB2WC/WC2MB() instead
46 // confuse you.
47 //
48 // You also have to implement Clone() to allow copying the conversions
49 // polymorphically.
50 //
51 // And you might need to override GetMBNulLen() as well.
52 class WXDLLIMPEXP_BASE wxMBConv
53 {
54 public:
55 // The functions doing actual conversion from/to narrow to/from wide
56 // character strings.
57 //
58 // On success, the return value is the length (i.e. the number of
59 // characters, not bytes) of the converted string including any trailing
60 // L'\0' or (possibly multiple) '\0'(s). If the conversion fails or if
61 // there is not enough space for everything, including the trailing NUL
62 // character(s), in the output buffer, wxCONV_FAILED is returned.
63 //
64 // In the special case when dstLen is 0 (outputBuf may be NULL then) the
65 // return value is the length of the needed buffer but nothing happens
66 // otherwise. If srcLen is wxNO_LEN, the entire string, up to and
67 // including the trailing NUL(s), is converted, otherwise exactly srcLen
68 // bytes are.
69 //
70 // Typical usage:
71 //
72 // size_t dstLen = conv.ToWChar(NULL, 0, src);
73 // if ( dstLen != wxCONV_FAILED )
74 // ... handle error ...
75 // wchar_t *wbuf = new wchar_t[dstLen];
76 // conv.ToWChar(wbuf, dstLen, src);
77 //
78 virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
79 const char *src, size_t srcLen = wxNO_LEN) const;
80
81 virtual size_t FromWChar(char *dst, size_t dstLen,
82 const wchar_t *src, size_t srcLen = wxNO_LEN) const;
83
84
85 // Convenience functions for translating NUL-terminated strings: returns
86 // the buffer containing the converted string or NULL pointer if the
87 // conversion failed.
88 const wxWCharBuffer cMB2WC(const char *in) const;
89 const wxCharBuffer cWC2MB(const wchar_t *in) const;
90
91 // Convenience functions for converting strings which may contain embedded
92 // NULs and don't have to be NUL-terminated.
93 //
94 // inLen is the length of the buffer including trailing NUL if any: if the
95 // last 4 bytes of the buffer are all NULs, these functions are more
96 // efficient as they avoid copying the string, but otherwise a copy is made
97 // internally which could be quite bad for (very) long strings.
98 //
99 // outLen receives, if not NULL, the length of the converted string or 0 if
100 // the conversion failed (returning 0 and not -1 in this case makes it
101 // difficult to distinguish between failed conversion and empty input but
102 // this is done for backwards compatibility)
103 const wxWCharBuffer
104 cMB2WC(const char *in, size_t inLen, size_t *outLen) const;
105 const wxCharBuffer
106 cWC2MB(const wchar_t *in, size_t inLen, size_t *outLen) const;
107
108 // convenience functions for converting MB or WC to/from wxWin default
109 #if wxUSE_UNICODE
110 const wxWCharBuffer cMB2WX(const char *psz) const { return cMB2WC(psz); }
111 const wxCharBuffer cWX2MB(const wchar_t *psz) const { return cWC2MB(psz); }
112 const wchar_t* cWC2WX(const wchar_t *psz) const { return psz; }
113 const wchar_t* cWX2WC(const wchar_t *psz) const { return psz; }
114 #else // ANSI
115 const char* cMB2WX(const char *psz) const { return psz; }
116 const char* cWX2MB(const char *psz) const { return psz; }
117 const wxCharBuffer cWC2WX(const wchar_t *psz) const { return cWC2MB(psz); }
118 const wxWCharBuffer cWX2WC(const char *psz) const { return cMB2WC(psz); }
119 #endif // Unicode/ANSI
120
121 // this function is used in the implementation of cMB2WC() to distinguish
122 // between the following cases:
123 //
124 // a) var width encoding with strings terminated by a single NUL
125 // (usual multibyte encodings): return 1 in this case
126 // b) fixed width encoding with 2 bytes/char and so terminated by
127 // 2 NULs (UTF-16/UCS-2 and variants): return 2 in this case
128 // c) fixed width encoding with 4 bytes/char and so terminated by
129 // 4 NULs (UTF-32/UCS-4 and variants): return 4 in this case
130 //
131 // anything else is not supported currently and -1 should be returned
132 virtual size_t GetMBNulLen() const { return 1; }
133
134 // return the maximal value currently returned by GetMBNulLen() for any
135 // encoding
136 static size_t GetMaxMBNulLen() { return 4 /* for UTF-32 */; }
137
138
139 // The old conversion functions. The existing classes currently mostly
140 // implement these ones but we're in transition to using To/FromWChar()
141 // instead and any new classes should implement just the new functions.
142 // For now, however, we provide default implementation of To/FromWChar() in
143 // this base class in terms of MB2WC/WC2MB() to avoid having to rewrite all
144 // the conversions at once.
145 //
146 // On success, the return value is the length (i.e. the number of
147 // characters, not bytes) not counting the trailing NUL(s) of the converted
148 // string. On failure, (size_t)-1 is returned. In the special case when
149 // outputBuf is NULL the return value is the same one but nothing is
150 // written to the buffer.
151 //
152 // Note that outLen is the length of the output buffer, not the length of
153 // the input (which is always supposed to be terminated by one or more
154 // NULs, as appropriate for the encoding)!
155 virtual size_t MB2WC(wchar_t *out, const char *in, size_t outLen) const;
156 virtual size_t WC2MB(char *out, const wchar_t *in, size_t outLen) const;
157
158
159 // make a heap-allocated copy of this object
160 virtual wxMBConv *Clone() const = 0;
161
162 // virtual dtor for any base class
163 virtual ~wxMBConv();
164 };
165
166 // ----------------------------------------------------------------------------
167 // wxMBConvLibc uses standard mbstowcs() and wcstombs() functions for
168 // conversion (hence it depends on the current locale)
169 // ----------------------------------------------------------------------------
170
171 class WXDLLIMPEXP_BASE wxMBConvLibc : public wxMBConv
172 {
173 public:
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 virtual wxMBConv *Clone() const { return new wxMBConvLibc; }
178 };
179
180 #ifdef __UNIX__
181
182 // ----------------------------------------------------------------------------
183 // wxConvBrokenFileNames is made for Unix in Unicode mode when
184 // files are accidentally written in an encoding which is not
185 // the system encoding. Typically, the system encoding will be
186 // UTF8 but there might be files stored in ISO8859-1 on disk.
187 // ----------------------------------------------------------------------------
188
189 class WXDLLIMPEXP_BASE wxConvBrokenFileNames : public wxMBConv
190 {
191 public:
192 wxConvBrokenFileNames(const wxChar *charset);
193 wxConvBrokenFileNames(const wxConvBrokenFileNames& conv)
194 : m_conv(conv.m_conv ? conv.m_conv->Clone() : NULL)
195 {
196 }
197 virtual ~wxConvBrokenFileNames() { delete m_conv; }
198
199 virtual size_t MB2WC(wchar_t *out, const char *in, size_t outLen) const
200 {
201 return m_conv->MB2WC(out, in, outLen);
202 }
203
204 virtual size_t WC2MB(char *out, const wchar_t *in, size_t outLen) const
205 {
206 return m_conv->WC2MB(out, in, outLen);
207 }
208
209 virtual size_t GetMBNulLen() const
210 {
211 // cast needed to call a private function
212 return m_conv->GetMBNulLen();
213 }
214
215 virtual wxMBConv *Clone() const { return new wxConvBrokenFileNames(*this); }
216
217 private:
218 // the conversion object we forward to
219 wxMBConv *m_conv;
220
221 DECLARE_NO_ASSIGN_CLASS(wxConvBrokenFileNames)
222 };
223
224 #endif // __UNIX__
225
226 // ----------------------------------------------------------------------------
227 // wxMBConvUTF7 (for conversion using UTF7 encoding)
228 // ----------------------------------------------------------------------------
229
230 class WXDLLIMPEXP_BASE wxMBConvUTF7 : public wxMBConv
231 {
232 public:
233 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
234 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
235
236 virtual wxMBConv *Clone() const { return new wxMBConvUTF7; }
237 };
238
239 // ----------------------------------------------------------------------------
240 // wxMBConvUTF8 (for conversion using UTF8 encoding)
241 // ----------------------------------------------------------------------------
242
243 class WXDLLIMPEXP_BASE wxMBConvUTF8 : public wxMBConv
244 {
245 public:
246 enum
247 {
248 MAP_INVALID_UTF8_NOT = 0,
249 MAP_INVALID_UTF8_TO_PUA = 1,
250 MAP_INVALID_UTF8_TO_OCTAL = 2
251 };
252
253 wxMBConvUTF8(int options = MAP_INVALID_UTF8_NOT) : m_options(options) { }
254 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
255 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
256
257 virtual wxMBConv *Clone() const { return new wxMBConvUTF8(m_options); }
258
259 private:
260 int m_options;
261 };
262
263 // ----------------------------------------------------------------------------
264 // wxMBConvUTF16Base: for both LE and BE variants
265 // ----------------------------------------------------------------------------
266
267 class WXDLLIMPEXP_BASE wxMBConvUTF16Base : public wxMBConv
268 {
269 public:
270 enum { BYTES_PER_CHAR = 2 };
271
272 virtual size_t GetMBNulLen() const { return BYTES_PER_CHAR; }
273
274 protected:
275 // return the length of the buffer using srcLen if it's not wxNO_LEN and
276 // computing the length ourselves if it is; also checks that the length is
277 // even if specified as we need an entire number of UTF-16 characters and
278 // returns wxNO_LEN which indicates error if it is odd
279 static size_t GetLength(const char *src, size_t srcLen);
280 };
281
282 // ----------------------------------------------------------------------------
283 // wxMBConvUTF16LE (for conversion using UTF16 Little Endian encoding)
284 // ----------------------------------------------------------------------------
285
286 class WXDLLIMPEXP_BASE wxMBConvUTF16LE : public wxMBConvUTF16Base
287 {
288 public:
289 #if SIZEOF_WCHAR_T == 2
290 virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
291 const char *src, size_t srcLen = wxNO_LEN) const;
292 virtual size_t FromWChar(char *dst, size_t dstLen,
293 const wchar_t *src, size_t srcLen = wxNO_LEN) const;
294 #else
295 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
296 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
297 #endif
298 virtual wxMBConv *Clone() const { return new wxMBConvUTF16LE; }
299 };
300
301 // ----------------------------------------------------------------------------
302 // wxMBConvUTF16BE (for conversion using UTF16 Big Endian encoding)
303 // ----------------------------------------------------------------------------
304
305 class WXDLLIMPEXP_BASE wxMBConvUTF16BE : public wxMBConvUTF16Base
306 {
307 public:
308 #if SIZEOF_WCHAR_T == 2
309 virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
310 const char *src, size_t srcLen = wxNO_LEN) const;
311 virtual size_t FromWChar(char *dst, size_t dstLen,
312 const wchar_t *src, size_t srcLen = wxNO_LEN) const;
313 #else
314 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
315 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
316 #endif
317 virtual wxMBConv *Clone() const { return new wxMBConvUTF16BE; }
318 };
319
320 // ----------------------------------------------------------------------------
321 // wxMBConvUTF32Base: base class for both LE and BE variants
322 // ----------------------------------------------------------------------------
323
324 class WXDLLIMPEXP_BASE wxMBConvUTF32Base : public wxMBConv
325 {
326 public:
327 enum { BYTES_PER_CHAR = 4 };
328
329 virtual size_t GetMBNulLen() const { return BYTES_PER_CHAR; }
330
331 protected:
332 // this is similar to wxMBConvUTF16Base method with the same name except
333 // that, of course, it verifies that length is divisible by 4 if given and
334 // not by 2
335 static size_t GetLength(const char *src, size_t srcLen);
336 };
337
338 // ----------------------------------------------------------------------------
339 // wxMBConvUTF32LE (for conversion using UTF32 Little Endian encoding)
340 // ----------------------------------------------------------------------------
341
342 class WXDLLIMPEXP_BASE wxMBConvUTF32LE : public wxMBConvUTF32Base
343 {
344 public:
345 #if SIZEOF_WCHAR_T == 2
346 virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
347 const char *src, size_t srcLen = wxNO_LEN) const;
348 virtual size_t FromWChar(char *dst, size_t dstLen,
349 const wchar_t *src, size_t srcLen = wxNO_LEN) const;
350 #else
351 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
352 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
353 #endif
354 virtual wxMBConv *Clone() const { return new wxMBConvUTF32LE; }
355 };
356
357 // ----------------------------------------------------------------------------
358 // wxMBConvUTF32BE (for conversion using UTF32 Big Endian encoding)
359 // ----------------------------------------------------------------------------
360
361 class WXDLLIMPEXP_BASE wxMBConvUTF32BE : public wxMBConvUTF32Base
362 {
363 public:
364 #if SIZEOF_WCHAR_T == 2
365 virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
366 const char *src, size_t srcLen = wxNO_LEN) const;
367 virtual size_t FromWChar(char *dst, size_t dstLen,
368 const wchar_t *src, size_t srcLen = wxNO_LEN) const;
369 #else
370 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
371 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
372 #endif
373 virtual wxMBConv *Clone() const { return new wxMBConvUTF32BE; }
374 };
375
376 // ----------------------------------------------------------------------------
377 // wxCSConv (for conversion based on loadable char sets)
378 // ----------------------------------------------------------------------------
379
380 #include "wx/fontenc.h"
381
382 class WXDLLIMPEXP_BASE wxCSConv : public wxMBConv
383 {
384 public:
385 // we can be created either from charset name or from an encoding constant
386 // but we can't have both at once
387 wxCSConv(const wxChar *charset);
388 wxCSConv(wxFontEncoding encoding);
389
390 wxCSConv(const wxCSConv& conv);
391 virtual ~wxCSConv();
392
393 wxCSConv& operator=(const wxCSConv& conv);
394
395 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
396 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
397 virtual size_t GetMBNulLen() const;
398 virtual wxMBConv *Clone() const { return new wxCSConv(*this); }
399
400 void Clear();
401
402 private:
403 // common part of all ctors
404 void Init();
405
406 // creates m_convReal if necessary
407 void CreateConvIfNeeded() const;
408
409 // do create m_convReal (unconditionally)
410 wxMBConv *DoCreate() const;
411
412 // set the name (may be only called when m_name == NULL), makes copy of
413 // charset string
414 void SetName(const wxChar *charset);
415
416
417 // note that we can't use wxString here because of compilation
418 // dependencies: we're included from wx/string.h
419 wxChar *m_name;
420 wxFontEncoding m_encoding;
421
422 // use CreateConvIfNeeded() before accessing m_convReal!
423 wxMBConv *m_convReal;
424 bool m_deferred;
425 };
426
427
428 // ----------------------------------------------------------------------------
429 // declare predefined conversion objects
430 // ----------------------------------------------------------------------------
431
432 // conversion to be used with all standard functions affected by locale, e.g.
433 // strtol(), strftime(), ...
434 extern WXDLLIMPEXP_DATA_BASE(wxMBConv&) wxConvLibc;
435
436 // conversion ISO-8859-1/UTF-7/UTF-8 <-> wchar_t
437 extern WXDLLIMPEXP_DATA_BASE(wxCSConv&) wxConvISO8859_1;
438 extern WXDLLIMPEXP_DATA_BASE(wxMBConvUTF7&) wxConvUTF7;
439 extern WXDLLIMPEXP_DATA_BASE(wxMBConvUTF8&) wxConvUTF8;
440
441 // conversion used for the file names on the systems where they're not Unicode
442 // (basically anything except Windows)
443 //
444 // this is used by all file functions, can be changed by the application
445 //
446 // by default UTF-8 under Mac OS X and wxConvLibc elsewhere (but it's not used
447 // under Windows normally)
448 extern WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvFileName;
449
450 // backwards compatible define
451 #define wxConvFile (*wxConvFileName)
452
453 // the current conversion object, may be set to any conversion, is used by
454 // default in a couple of places inside wx (initially same as wxConvLibc)
455 extern WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvCurrent;
456
457 // ???
458 extern WXDLLIMPEXP_DATA_BASE(wxCSConv&) wxConvLocal;
459
460
461 // ----------------------------------------------------------------------------
462 // endianness-dependent conversions
463 // ----------------------------------------------------------------------------
464
465 #ifdef WORDS_BIGENDIAN
466 typedef wxMBConvUTF16BE wxMBConvUTF16;
467 typedef wxMBConvUTF32BE wxMBConvUTF32;
468 #else
469 typedef wxMBConvUTF16LE wxMBConvUTF16;
470 typedef wxMBConvUTF32LE wxMBConvUTF32;
471 #endif
472
473 // ----------------------------------------------------------------------------
474 // filename conversion macros
475 // ----------------------------------------------------------------------------
476
477 // filenames are multibyte on Unix and probably widechar on Windows?
478 #if defined(__UNIX__) || defined(__BORLANDC__) || defined(__WXMAC__ )
479 #define wxMBFILES 1
480 #else
481 #define wxMBFILES 0
482 #endif
483
484 #if wxMBFILES && wxUSE_UNICODE
485 #define wxFNCONV(name) wxConvFileName->cWX2MB(name)
486 #define wxFNSTRINGCAST wxMBSTRINGCAST
487 #else
488 #if defined( __WXOSX__ ) && wxMBFILES
489 #define wxFNCONV(name) wxConvFileName->cWC2MB( wxConvLocal.cWX2WC(name) )
490 #else
491 #define wxFNCONV(name) name
492 #endif
493 #define wxFNSTRINGCAST WXSTRINGCAST
494 #endif
495
496 #else // !wxUSE_WCHAR_T
497
498 // ----------------------------------------------------------------------------
499 // stand-ins in absence of wchar_t
500 // ----------------------------------------------------------------------------
501
502 class WXDLLIMPEXP_BASE wxMBConv
503 {
504 public:
505 const char* cMB2WX(const char *psz) const { return psz; }
506 const char* cWX2MB(const char *psz) const { return psz; }
507 };
508
509 #define wxConvFile wxConvLocal
510
511 extern WXDLLIMPEXP_DATA_BASE(wxMBConv) wxConvLibc,
512 wxConvLocal,
513 wxConvISO8859_1,
514 wxConvUTF8;
515 extern WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvCurrent;
516
517 #define wxFNCONV(name) name
518 #define wxFNSTRINGCAST WXSTRINGCAST
519
520 #endif
521 // wxUSE_WCHAR_T
522
523 // ----------------------------------------------------------------------------
524 // macros for the most common conversions
525 // ----------------------------------------------------------------------------
526
527 #if wxUSE_UNICODE
528 #define wxConvertWX2MB(s) wxConvCurrent->cWX2MB(s)
529 #define wxConvertMB2WX(s) wxConvCurrent->cMB2WX(s)
530 #else // ANSI
531 // no conversions to do
532 #define wxConvertWX2MB(s) (s)
533 #define wxConvertMB2WX(s) (s)
534 #endif // Unicode/ANSI
535
536 #endif // _WX_STRCONV_H_
537