Attempted compile fix
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "strconv.h"
17 #endif
18
19 #include "wx/defs.h"
20 #include "wx/wxchar.h"
21 #include "wx/buffer.h"
22
23 #ifdef __DIGITALMARS__
24 #include "typeinfo.h"
25 #endif
26
27 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
28 # undef __BSEXCPT__
29 #endif
30
31 #include <stdlib.h>
32
33 #if wxUSE_WCHAR_T
34
35 // ----------------------------------------------------------------------------
36 // wxMBConv (abstract base class for conversions)
37 // ----------------------------------------------------------------------------
38
39 class WXDLLIMPEXP_BASE wxMBConv
40 {
41 public:
42 // the actual conversion takes place here
43 //
44 // note that outputSize is the size of the output buffer, not the length of input
45 // (the latter is always supposed to be NUL-terminated)
46 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const = 0;
47 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const = 0;
48
49 // MB <-> WC
50 const wxWCharBuffer cMB2WC(const char *psz) const;
51 const wxCharBuffer cWC2MB(const wchar_t *psz) const;
52
53 // MB <-> WC for strings with embedded null characters
54 //
55 // pszLen length of the input string
56 // pOutSize gets the final size of the converted string
57 const wxWCharBuffer cMB2WC(const char *psz, size_t pszLen, size_t* pOutSize) const;
58 const wxCharBuffer cWC2MB(const wchar_t *psz, size_t pszLen, size_t* pOutSize) const;
59
60 // convenience functions for converting MB or WC to/from wxWin default
61 #if wxUSE_UNICODE
62 const wxWCharBuffer cMB2WX(const char *psz) const { return cMB2WC(psz); }
63 const wxCharBuffer cWX2MB(const wchar_t *psz) const { return cWC2MB(psz); }
64 const wchar_t* cWC2WX(const wchar_t *psz) const { return psz; }
65 const wchar_t* cWX2WC(const wchar_t *psz) const { return psz; }
66 #else // ANSI
67 const char* cMB2WX(const char *psz) const { return psz; }
68 const char* cWX2MB(const char *psz) const { return psz; }
69 const wxCharBuffer cWC2WX(const wchar_t *psz) const { return cWC2MB(psz); }
70 const wxWCharBuffer cWX2WC(const char *psz) const { return cMB2WC(psz); }
71 #endif // Unicode/ANSI
72
73 // virtual dtor for any base class
74 virtual ~wxMBConv();
75 };
76
77 // ----------------------------------------------------------------------------
78 // wxMBConvLibc uses standard mbstowcs() and wcstombs() functions for
79 // conversion (hence it depends on the current locale)
80 // ----------------------------------------------------------------------------
81
82 class WXDLLIMPEXP_BASE wxMBConvLibc : public wxMBConv
83 {
84 public:
85 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
86 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
87 };
88
89 // ----------------------------------------------------------------------------
90 // wxMBConvUTF7 (for conversion using UTF7 encoding)
91 // ----------------------------------------------------------------------------
92
93 class WXDLLIMPEXP_BASE wxMBConvUTF7 : public wxMBConv
94 {
95 public:
96 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
97 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
98 };
99
100 // ----------------------------------------------------------------------------
101 // wxMBConvUTF8 (for conversion using UTF8 encoding)
102 // ----------------------------------------------------------------------------
103
104 class WXDLLIMPEXP_BASE wxMBConvUTF8 : public wxMBConv
105 {
106 public:
107 enum {
108 MAP_INVALID_UTF8_NOT = 0,
109 MAP_INVALID_UTF8_TO_PUA = 1,
110 MAP_INVALID_UTF8_TO_OCTAL = 2
111 };
112
113 wxMBConvUTF8(int options = MAP_INVALID_UTF8_NOT) : m_options(options) { }
114 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
115 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
116
117 private:
118 int m_options;
119 };
120
121 // ----------------------------------------------------------------------------
122 // wxMBConvUTF16LE (for conversion using UTF16 Little Endian encoding)
123 // ----------------------------------------------------------------------------
124
125 class WXDLLIMPEXP_BASE wxMBConvUTF16LE : public wxMBConv
126 {
127 public:
128 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
129 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
130 };
131
132 // ----------------------------------------------------------------------------
133 // wxMBConvUTF16BE (for conversion using UTF16 Big Endian encoding)
134 // ----------------------------------------------------------------------------
135
136 class WXDLLIMPEXP_BASE wxMBConvUTF16BE : public wxMBConv
137 {
138 public:
139 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
140 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
141 };
142
143 // ----------------------------------------------------------------------------
144 // wxMBConvUCS4LE (for conversion using UTF32 Little Endian encoding)
145 // ----------------------------------------------------------------------------
146
147 class WXDLLIMPEXP_BASE wxMBConvUTF32LE : public wxMBConv
148 {
149 public:
150 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
151 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
152 };
153
154 // ----------------------------------------------------------------------------
155 // wxMBConvUCS4BE (for conversion using UTF32 Big Endian encoding)
156 // ----------------------------------------------------------------------------
157
158 class WXDLLIMPEXP_BASE wxMBConvUTF32BE : public wxMBConv
159 {
160 public:
161 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
162 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
163 };
164
165 // ----------------------------------------------------------------------------
166 // wxCSConv (for conversion based on loadable char sets)
167 // ----------------------------------------------------------------------------
168
169 #include "wx/fontenc.h"
170
171 class WXDLLIMPEXP_BASE wxCSConv : public wxMBConv
172 {
173 public:
174 // we can be created either from charset name or from an encoding constant
175 // but we can't have both at once
176 wxCSConv(const wxChar *charset);
177 wxCSConv(wxFontEncoding encoding);
178
179 wxCSConv(const wxCSConv& conv);
180 virtual ~wxCSConv();
181
182 wxCSConv& operator=(const wxCSConv& conv);
183
184 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
185 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
186
187 void Clear() ;
188
189 private:
190 // common part of all ctors
191 void Init();
192
193 // creates m_convReal if necessary
194 void CreateConvIfNeeded() const;
195
196 // do create m_convReal (unconditionally)
197 wxMBConv *DoCreate() const;
198
199 // set the name (may be only called when m_name == NULL), makes copy of
200 // charset string
201 void SetName(const wxChar *charset);
202
203
204 // note that we can't use wxString here because of compilation
205 // dependencies: we're included from wx/string.h
206 wxChar *m_name;
207 wxFontEncoding m_encoding;
208
209 // use CreateConvIfNeeded() before accessing m_convReal!
210 wxMBConv *m_convReal;
211 bool m_deferred;
212 };
213
214 #ifdef __WXGTK20__
215
216 // ----------------------------------------------------------------------------
217 // wxConvBrokenFileNames is made for GTK2 in Unicode mode when
218 // files are accidentally written in an encoding which is not
219 // the system encoding. Typically, the system encoding will be
220 // UTF8 but there might be files stored in ISO8859-1 on disk.
221 // ----------------------------------------------------------------------------
222
223 class wxConvBrokenFileNames : public wxMBConv
224 {
225 public:
226 wxConvBrokenFileNames();
227 virtual ~wxConvBrokenFileNames() { delete m_conv; }
228
229 virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
230 virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
231
232 private:
233 // the conversion object we forward to
234 wxMBConv *m_conv;
235 };
236 #endif
237 // __WXGTK20__
238
239 // ----------------------------------------------------------------------------
240 // declare predefined conversion objects
241 // ----------------------------------------------------------------------------
242
243 // conversion to be used with all standard functions affected by locale, e.g.
244 // strtol(), strftime(), ...
245 extern WXDLLIMPEXP_DATA_BASE(wxMBConv&) wxConvLibc;
246
247 // conversion ISO-8859-1/UTF-7/UTF-8 <-> wchar_t
248 extern WXDLLIMPEXP_DATA_BASE(wxCSConv&) wxConvISO8859_1;
249 extern WXDLLIMPEXP_DATA_BASE(wxMBConvUTF7&) wxConvUTF7;
250 extern WXDLLIMPEXP_DATA_BASE(wxMBConvUTF8&) wxConvUTF8;
251
252 // conversion used for the file names on the systems where they're not Unicode
253 // (basically anything except Windows)
254 //
255 // this is used by all file functions, can be changed by the application
256 //
257 // by default UTF-8 under Mac OS X and wxConvLibc elsewhere (but it's not used
258 // under Windows normally)
259 extern WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvFileName;
260
261 // backwards compatible define
262 #define wxConvFile (*wxConvFileName)
263
264 // the current conversion object, may be set to any conversion, is used by
265 // default in a couple of places inside wx (initially same as wxConvLibc)
266 extern WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvCurrent;
267
268 // ???
269 extern WXDLLIMPEXP_DATA_BASE(wxCSConv&) wxConvLocal;
270
271
272 // ----------------------------------------------------------------------------
273 // endianness-dependent conversions
274 // ----------------------------------------------------------------------------
275
276 #ifdef WORDS_BIGENDIAN
277 typedef wxMBConvUTF16BE wxMBConvUTF16;
278 typedef wxMBConvUTF32BE wxMBConvUTF32;
279 #else
280 typedef wxMBConvUTF16LE wxMBConvUTF16;
281 typedef wxMBConvUTF32LE wxMBConvUTF32;
282 #endif
283
284 // ----------------------------------------------------------------------------
285 // filename conversion macros
286 // ----------------------------------------------------------------------------
287
288 // filenames are multibyte on Unix and probably widechar on Windows?
289 #if defined(__UNIX__) || defined(__BORLANDC__) || defined(__WXMAC__ )
290 #define wxMBFILES 1
291 #else
292 #define wxMBFILES 0
293 #endif
294
295 #if wxMBFILES && wxUSE_UNICODE
296 #define wxFNCONV(name) wxConvFileName->cWX2MB(name)
297 #define wxFNSTRINGCAST wxMBSTRINGCAST
298 #else
299 #if defined( __WXOSX__ ) && wxMBFILES
300 #define wxFNCONV(name) wxConvFileName->cWC2MB( wxConvLocal.cWX2WC(name) )
301 #else
302 #define wxFNCONV(name) name
303 #endif
304 #define wxFNSTRINGCAST WXSTRINGCAST
305 #endif
306
307 #else // !wxUSE_WCHAR_T
308
309 // ----------------------------------------------------------------------------
310 // stand-ins in absence of wchar_t
311 // ----------------------------------------------------------------------------
312
313 class WXDLLIMPEXP_BASE wxMBConv
314 {
315 public:
316 const char* cMB2WX(const char *psz) const { return psz; }
317 const char* cWX2MB(const char *psz) const { return psz; }
318 };
319
320 #define wxConvFile wxConvLocal
321
322 extern WXDLLIMPEXP_DATA_BASE(wxMBConv) wxConvLibc,
323 wxConvLocal,
324 wxConvISO8859_1,
325 wxConvUTF8;
326 extern WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvCurrent;
327
328 #define wxFNCONV(name) name
329 #define wxFNSTRINGCAST WXSTRINGCAST
330
331 #endif
332 // wxUSE_WCHAR_T
333
334 // ----------------------------------------------------------------------------
335 // macros for the most common conversions
336 // ----------------------------------------------------------------------------
337
338 #if wxUSE_UNICODE
339 #define wxConvertWX2MB(s) wxConvCurrent->cWX2MB(s)
340 #define wxConvertMB2WX(s) wxConvCurrent->cMB2WX(s)
341 #else // ANSI
342 // no conversions to do
343 #define wxConvertWX2MB(s) (s)
344 #define wxConvertMB2WX(s) (s)
345 #endif // Unicode/ANSI
346
347 #endif
348 // _WX_WXSTRCONVH__
349