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