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