]>
Commit | Line | Data |
---|---|---|
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 | // 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 | // And you might need to override GetMBNulLen() as well. | |
44 | class WXDLLIMPEXP_BASE wxMBConv | |
45 | { | |
46 | public: | |
47 | // The functions doing actual conversion from/to narrow to/from wide | |
48 | // character strings. | |
49 | // | |
50 | // On success, the return value is the length (i.e. the number of | |
51 | // characters, not bytes) of the converted string including any trailing | |
52 | // L'\0' or (possibly multiple) '\0'(s). If the conversion fails or if | |
53 | // there is not enough space for everything, including the trailing NUL | |
54 | // character(s), in the output buffer, (size_t)-1 is returned. | |
55 | // | |
56 | // In the special case when dstLen is 0 (outputBuf may be NULL then) the | |
57 | // return value is the length of the needed buffer but nothing happens | |
58 | // otherwise. If srcLen is -1, the entire string, up to and including the | |
59 | // trailing NUL(s), is converted, otherwise exactly srcLen bytes are. | |
60 | // | |
61 | // Typical usage: | |
62 | // | |
63 | // size_t dstLen = conv.ToWChar(NULL, 0, src); | |
64 | // if ( dstLen != wxCONV_FAILED ) | |
65 | // ... handle error ... | |
66 | // wchar_t *wbuf = new wchar_t[dstLen]; | |
67 | // conv.ToWChar(wbuf, dstLen, src); | |
68 | // | |
69 | virtual size_t ToWChar(wchar_t *dst, size_t dstLen, | |
70 | const char *src, size_t srcLen = -1) const; | |
71 | ||
72 | virtual size_t FromWChar(char *dst, size_t dstLen, | |
73 | const wchar_t *src, size_t srcLen = -1) const; | |
74 | ||
75 | ||
76 | // Convenience functions for translating NUL-terminated strings: returns | |
77 | // the buffer containing the converted string or NULL pointer if the | |
78 | // conversion failed. | |
79 | const wxWCharBuffer cMB2WC(const char *in) const; | |
80 | const wxCharBuffer cWC2MB(const wchar_t *in) const; | |
81 | ||
82 | // Convenience functions for converting strings which may contain embedded | |
83 | // NULs and don't have to be NUL-terminated. | |
84 | // | |
85 | // inLen is the length of the buffer including trailing NUL if any: if the | |
86 | // last 4 bytes of the buffer are all NULs, these functions are more | |
87 | // efficient as they avoid copying the string, but otherwise a copy is made | |
88 | // internally which could be quite bad for (very) long strings. | |
89 | // | |
90 | // outLen receives, if not NULL, the length of the converted string or 0 if | |
91 | // the conversion failed (returning 0 and not -1 in this case makes it | |
92 | // difficult to distinguish between failed conversion and empty input but | |
93 | // this is done for backwards compatibility) | |
94 | const wxWCharBuffer | |
95 | cMB2WC(const char *in, size_t inLen, size_t *outLen) const; | |
96 | const wxCharBuffer | |
97 | cWC2MB(const wchar_t *in, size_t inLen, size_t *outLen) const; | |
98 | ||
99 | // convenience functions for converting MB or WC to/from wxWin default | |
100 | #if wxUSE_UNICODE | |
101 | const wxWCharBuffer cMB2WX(const char *psz) const { return cMB2WC(psz); } | |
102 | const wxCharBuffer cWX2MB(const wchar_t *psz) const { return cWC2MB(psz); } | |
103 | const wchar_t* cWC2WX(const wchar_t *psz) const { return psz; } | |
104 | const wchar_t* cWX2WC(const wchar_t *psz) const { return psz; } | |
105 | #else // ANSI | |
106 | const char* cMB2WX(const char *psz) const { return psz; } | |
107 | const char* cWX2MB(const char *psz) const { return psz; } | |
108 | const wxCharBuffer cWC2WX(const wchar_t *psz) const { return cWC2MB(psz); } | |
109 | const wxWCharBuffer cWX2WC(const char *psz) const { return cMB2WC(psz); } | |
110 | #endif // Unicode/ANSI | |
111 | ||
112 | // this function is used in the implementation of cMB2WC() to distinguish | |
113 | // between the following cases: | |
114 | // | |
115 | // a) var width encoding with strings terminated by a single NUL | |
116 | // (usual multibyte encodings): return 1 in this case | |
117 | // b) fixed width encoding with 2 bytes/char and so terminated by | |
118 | // 2 NULs (UTF-16/UCS-2 and variants): return 2 in this case | |
119 | // c) fixed width encoding with 4 bytes/char and so terminated by | |
120 | // 4 NULs (UTF-32/UCS-4 and variants): return 4 in this case | |
121 | // | |
122 | // anything else is not supported currently and -1 should be returned | |
123 | virtual size_t GetMBNulLen() const { return 1; } | |
124 | ||
125 | // return the maximal value currently returned by GetMBNulLen() for any | |
126 | // encoding | |
127 | static size_t GetMaxMBNulLen() { return 4 /* for UTF-32 */; } | |
128 | ||
129 | ||
130 | // The old conversion functions. The existing classes currently mostly | |
131 | // implement these ones but we're in transition to using To/FromWChar() | |
132 | // instead and any new classes should implement just the new functions. | |
133 | // For now, however, we provide default implementation of To/FromWChar() in | |
134 | // this base class in terms of MB2WC/WC2MB() to avoid having to rewrite all | |
135 | // the conversions at once. | |
136 | // | |
137 | // On success, the return value is the length (i.e. the number of | |
138 | // characters, not bytes) not counting the trailing NUL(s) of the converted | |
139 | // string. On failure, (size_t)-1 is returned. In the special case when | |
140 | // outputBuf is NULL the return value is the same one but nothing is | |
141 | // written to the buffer. | |
142 | // | |
143 | // Note that outLen is the length of the output buffer, not the length of | |
144 | // the input (which is always supposed to be terminated by one or more | |
145 | // NULs, as appropriate for the encoding)! | |
146 | virtual size_t MB2WC(wchar_t *out, const char *in, size_t outLen) const; | |
147 | virtual size_t WC2MB(char *out, const wchar_t *in, size_t outLen) const; | |
148 | ||
149 | ||
150 | // virtual dtor for any base class | |
151 | virtual ~wxMBConv(); | |
152 | }; | |
153 | ||
154 | // ---------------------------------------------------------------------------- | |
155 | // wxMBConvLibc uses standard mbstowcs() and wcstombs() functions for | |
156 | // conversion (hence it depends on the current locale) | |
157 | // ---------------------------------------------------------------------------- | |
158 | ||
159 | class WXDLLIMPEXP_BASE wxMBConvLibc : public wxMBConv | |
160 | { | |
161 | public: | |
162 | virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const; | |
163 | virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const; | |
164 | }; | |
165 | ||
166 | #ifdef __UNIX__ | |
167 | ||
168 | // ---------------------------------------------------------------------------- | |
169 | // wxConvBrokenFileNames is made for Unix in Unicode mode when | |
170 | // files are accidentally written in an encoding which is not | |
171 | // the system encoding. Typically, the system encoding will be | |
172 | // UTF8 but there might be files stored in ISO8859-1 on disk. | |
173 | // ---------------------------------------------------------------------------- | |
174 | ||
175 | class WXDLLIMPEXP_BASE wxConvBrokenFileNames : public wxMBConv | |
176 | { | |
177 | public: | |
178 | wxConvBrokenFileNames(const wxChar *charset); | |
179 | virtual ~wxConvBrokenFileNames() { delete m_conv; } | |
180 | ||
181 | virtual size_t MB2WC(wchar_t *out, const char *in, size_t outLen) const | |
182 | { | |
183 | return m_conv->MB2WC(out, in, outLen); | |
184 | } | |
185 | ||
186 | virtual size_t WC2MB(char *out, const wchar_t *in, size_t outLen) const | |
187 | { | |
188 | return m_conv->WC2MB(out, in, outLen); | |
189 | } | |
190 | ||
191 | virtual size_t GetMBNulLen() const | |
192 | { | |
193 | // cast needed to call a private function | |
194 | return m_conv->GetMBNulLen(); | |
195 | } | |
196 | ||
197 | private: | |
198 | // the conversion object we forward to | |
199 | wxMBConv *m_conv; | |
200 | }; | |
201 | ||
202 | #endif // __UNIX__ | |
203 | ||
204 | // ---------------------------------------------------------------------------- | |
205 | // wxMBConvUTF7 (for conversion using UTF7 encoding) | |
206 | // ---------------------------------------------------------------------------- | |
207 | ||
208 | class WXDLLIMPEXP_BASE wxMBConvUTF7 : public wxMBConv | |
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 | // wxMBConvUTF8 (for conversion using UTF8 encoding) | |
217 | // ---------------------------------------------------------------------------- | |
218 | ||
219 | class WXDLLIMPEXP_BASE wxMBConvUTF8 : public wxMBConv | |
220 | { | |
221 | public: | |
222 | enum { | |
223 | MAP_INVALID_UTF8_NOT = 0, | |
224 | MAP_INVALID_UTF8_TO_PUA = 1, | |
225 | MAP_INVALID_UTF8_TO_OCTAL = 2 | |
226 | }; | |
227 | ||
228 | wxMBConvUTF8(int options = MAP_INVALID_UTF8_NOT) : m_options(options) { } | |
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 | int m_options; | |
234 | }; | |
235 | ||
236 | // ---------------------------------------------------------------------------- | |
237 | // wxMBConvUTF16Base: for both LE and BE variants | |
238 | // ---------------------------------------------------------------------------- | |
239 | ||
240 | class WXDLLIMPEXP_BASE wxMBConvUTF16Base : public wxMBConv | |
241 | { | |
242 | public: | |
243 | virtual size_t GetMBNulLen() const { return 2; } | |
244 | }; | |
245 | ||
246 | // ---------------------------------------------------------------------------- | |
247 | // wxMBConvUTF16LE (for conversion using UTF16 Little Endian encoding) | |
248 | // ---------------------------------------------------------------------------- | |
249 | ||
250 | class WXDLLIMPEXP_BASE wxMBConvUTF16LE : public wxMBConvUTF16Base | |
251 | { | |
252 | public: | |
253 | virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const; | |
254 | virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const; | |
255 | }; | |
256 | ||
257 | // ---------------------------------------------------------------------------- | |
258 | // wxMBConvUTF16BE (for conversion using UTF16 Big Endian encoding) | |
259 | // ---------------------------------------------------------------------------- | |
260 | ||
261 | class WXDLLIMPEXP_BASE wxMBConvUTF16BE : public wxMBConvUTF16Base | |
262 | { | |
263 | public: | |
264 | virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const; | |
265 | virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const; | |
266 | }; | |
267 | ||
268 | // ---------------------------------------------------------------------------- | |
269 | // wxMBConvUTF32Base: base class for both LE and BE variants | |
270 | // ---------------------------------------------------------------------------- | |
271 | ||
272 | class WXDLLIMPEXP_BASE wxMBConvUTF32Base : public wxMBConv | |
273 | { | |
274 | public: | |
275 | virtual size_t GetMBNulLen() const { return 4; } | |
276 | }; | |
277 | ||
278 | // ---------------------------------------------------------------------------- | |
279 | // wxMBConvUTF32LE (for conversion using UTF32 Little Endian encoding) | |
280 | // ---------------------------------------------------------------------------- | |
281 | ||
282 | class WXDLLIMPEXP_BASE wxMBConvUTF32LE : public wxMBConvUTF32Base | |
283 | { | |
284 | public: | |
285 | virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const; | |
286 | virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const; | |
287 | }; | |
288 | ||
289 | // ---------------------------------------------------------------------------- | |
290 | // wxMBConvUTF32BE (for conversion using UTF32 Big Endian encoding) | |
291 | // ---------------------------------------------------------------------------- | |
292 | ||
293 | class WXDLLIMPEXP_BASE wxMBConvUTF32BE : public wxMBConvUTF32Base | |
294 | { | |
295 | public: | |
296 | virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const; | |
297 | virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const; | |
298 | }; | |
299 | ||
300 | // ---------------------------------------------------------------------------- | |
301 | // wxCSConv (for conversion based on loadable char sets) | |
302 | // ---------------------------------------------------------------------------- | |
303 | ||
304 | #include "wx/fontenc.h" | |
305 | ||
306 | class WXDLLIMPEXP_BASE wxCSConv : public wxMBConv | |
307 | { | |
308 | public: | |
309 | // we can be created either from charset name or from an encoding constant | |
310 | // but we can't have both at once | |
311 | wxCSConv(const wxChar *charset); | |
312 | wxCSConv(wxFontEncoding encoding); | |
313 | ||
314 | wxCSConv(const wxCSConv& conv); | |
315 | virtual ~wxCSConv(); | |
316 | ||
317 | wxCSConv& operator=(const wxCSConv& conv); | |
318 | ||
319 | virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const; | |
320 | virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const; | |
321 | virtual size_t GetMBNulLen() const; | |
322 | ||
323 | void Clear() ; | |
324 | ||
325 | private: | |
326 | // common part of all ctors | |
327 | void Init(); | |
328 | ||
329 | // creates m_convReal if necessary | |
330 | void CreateConvIfNeeded() const; | |
331 | ||
332 | // do create m_convReal (unconditionally) | |
333 | wxMBConv *DoCreate() const; | |
334 | ||
335 | // set the name (may be only called when m_name == NULL), makes copy of | |
336 | // charset string | |
337 | void SetName(const wxChar *charset); | |
338 | ||
339 | ||
340 | // note that we can't use wxString here because of compilation | |
341 | // dependencies: we're included from wx/string.h | |
342 | wxChar *m_name; | |
343 | wxFontEncoding m_encoding; | |
344 | ||
345 | // use CreateConvIfNeeded() before accessing m_convReal! | |
346 | wxMBConv *m_convReal; | |
347 | bool m_deferred; | |
348 | }; | |
349 | ||
350 | ||
351 | // ---------------------------------------------------------------------------- | |
352 | // declare predefined conversion objects | |
353 | // ---------------------------------------------------------------------------- | |
354 | ||
355 | // conversion to be used with all standard functions affected by locale, e.g. | |
356 | // strtol(), strftime(), ... | |
357 | extern WXDLLIMPEXP_DATA_BASE(wxMBConv&) wxConvLibc; | |
358 | ||
359 | // conversion ISO-8859-1/UTF-7/UTF-8 <-> wchar_t | |
360 | extern WXDLLIMPEXP_DATA_BASE(wxCSConv&) wxConvISO8859_1; | |
361 | extern WXDLLIMPEXP_DATA_BASE(wxMBConvUTF7&) wxConvUTF7; | |
362 | extern WXDLLIMPEXP_DATA_BASE(wxMBConvUTF8&) wxConvUTF8; | |
363 | ||
364 | // conversion used for the file names on the systems where they're not Unicode | |
365 | // (basically anything except Windows) | |
366 | // | |
367 | // this is used by all file functions, can be changed by the application | |
368 | // | |
369 | // by default UTF-8 under Mac OS X and wxConvLibc elsewhere (but it's not used | |
370 | // under Windows normally) | |
371 | extern WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvFileName; | |
372 | ||
373 | // backwards compatible define | |
374 | #define wxConvFile (*wxConvFileName) | |
375 | ||
376 | // the current conversion object, may be set to any conversion, is used by | |
377 | // default in a couple of places inside wx (initially same as wxConvLibc) | |
378 | extern WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvCurrent; | |
379 | ||
380 | // ??? | |
381 | extern WXDLLIMPEXP_DATA_BASE(wxCSConv&) wxConvLocal; | |
382 | ||
383 | ||
384 | // ---------------------------------------------------------------------------- | |
385 | // endianness-dependent conversions | |
386 | // ---------------------------------------------------------------------------- | |
387 | ||
388 | #ifdef WORDS_BIGENDIAN | |
389 | typedef wxMBConvUTF16BE wxMBConvUTF16; | |
390 | typedef wxMBConvUTF32BE wxMBConvUTF32; | |
391 | #else | |
392 | typedef wxMBConvUTF16LE wxMBConvUTF16; | |
393 | typedef wxMBConvUTF32LE wxMBConvUTF32; | |
394 | #endif | |
395 | ||
396 | // ---------------------------------------------------------------------------- | |
397 | // filename conversion macros | |
398 | // ---------------------------------------------------------------------------- | |
399 | ||
400 | // filenames are multibyte on Unix and probably widechar on Windows? | |
401 | #if defined(__UNIX__) || defined(__BORLANDC__) || defined(__WXMAC__ ) | |
402 | #define wxMBFILES 1 | |
403 | #else | |
404 | #define wxMBFILES 0 | |
405 | #endif | |
406 | ||
407 | #if wxMBFILES && wxUSE_UNICODE | |
408 | #define wxFNCONV(name) wxConvFileName->cWX2MB(name) | |
409 | #define wxFNSTRINGCAST wxMBSTRINGCAST | |
410 | #else | |
411 | #if defined( __WXOSX__ ) && wxMBFILES | |
412 | #define wxFNCONV(name) wxConvFileName->cWC2MB( wxConvLocal.cWX2WC(name) ) | |
413 | #else | |
414 | #define wxFNCONV(name) name | |
415 | #endif | |
416 | #define wxFNSTRINGCAST WXSTRINGCAST | |
417 | #endif | |
418 | ||
419 | #else // !wxUSE_WCHAR_T | |
420 | ||
421 | // ---------------------------------------------------------------------------- | |
422 | // stand-ins in absence of wchar_t | |
423 | // ---------------------------------------------------------------------------- | |
424 | ||
425 | class WXDLLIMPEXP_BASE wxMBConv | |
426 | { | |
427 | public: | |
428 | const char* cMB2WX(const char *psz) const { return psz; } | |
429 | const char* cWX2MB(const char *psz) const { return psz; } | |
430 | }; | |
431 | ||
432 | #define wxConvFile wxConvLocal | |
433 | ||
434 | extern WXDLLIMPEXP_DATA_BASE(wxMBConv) wxConvLibc, | |
435 | wxConvLocal, | |
436 | wxConvISO8859_1, | |
437 | wxConvUTF8; | |
438 | extern WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvCurrent; | |
439 | ||
440 | #define wxFNCONV(name) name | |
441 | #define wxFNSTRINGCAST WXSTRINGCAST | |
442 | ||
443 | #endif | |
444 | // wxUSE_WCHAR_T | |
445 | ||
446 | // ---------------------------------------------------------------------------- | |
447 | // macros for the most common conversions | |
448 | // ---------------------------------------------------------------------------- | |
449 | ||
450 | #if wxUSE_UNICODE | |
451 | #define wxConvertWX2MB(s) wxConvCurrent->cWX2MB(s) | |
452 | #define wxConvertMB2WX(s) wxConvCurrent->cMB2WX(s) | |
453 | #else // ANSI | |
454 | // no conversions to do | |
455 | #define wxConvertWX2MB(s) (s) | |
456 | #define wxConvertMB2WX(s) (s) | |
457 | #endif // Unicode/ANSI | |
458 | ||
459 | #endif | |
460 | // _WX_WXSTRCONVH__ | |
461 |