]>
Commit | Line | Data |
---|---|---|
6001e347 RR |
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 | |
65571936 | 9 | // Licence: wxWindows licence |
6001e347 RR |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifndef _WX_WXSTRCONVH__ | |
13 | #define _WX_WXSTRCONVH__ | |
14 | ||
12028905 | 15 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
6001e347 RR |
16 | #pragma interface "strconv.h" |
17 | #endif | |
18 | ||
19 | #include "wx/defs.h" | |
20 | #include "wx/wxchar.h" | |
21 | #include "wx/buffer.h" | |
22 | ||
7db39dd6 CE |
23 | #ifdef __DIGITALMARS__ |
24 | #include "typeinfo.h" | |
25 | #endif | |
26 | ||
9dea36ef DW |
27 | #if defined(__VISAGECPP__) && __IBMCPP__ >= 400 |
28 | # undef __BSEXCPT__ | |
29 | #endif | |
dccce9ea | 30 | |
6001e347 RR |
31 | #include <stdlib.h> |
32 | ||
33 | #if wxUSE_WCHAR_T | |
34 | ||
e90c1d2a | 35 | // ---------------------------------------------------------------------------- |
bde4baac | 36 | // wxMBConv (abstract base class for conversions) |
e90c1d2a | 37 | // ---------------------------------------------------------------------------- |
6001e347 | 38 | |
bddd7a8d | 39 | class WXDLLIMPEXP_BASE wxMBConv |
6001e347 RR |
40 | { |
41 | public: | |
e90c1d2a | 42 | // the actual conversion takes place here |
bde4baac | 43 | // |
e4e3bbb4 | 44 | // note that outputSize is the size of the output buffer, not the length of input |
75736a9c DS |
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; | |
e90c1d2a | 48 | |
bde4baac | 49 | // MB <-> WC |
e90c1d2a VZ |
50 | const wxWCharBuffer cMB2WC(const char *psz) const; |
51 | const wxCharBuffer cWC2MB(const wchar_t *psz) const; | |
6001e347 | 52 | |
f5fb6871 RN |
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 | ||
bde4baac | 60 | // convenience functions for converting MB or WC to/from wxWin default |
6001e347 | 61 | #if wxUSE_UNICODE |
e90c1d2a VZ |
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; } | |
f6bcfd97 | 65 | const wchar_t* cWX2WC(const wchar_t *psz) const { return psz; } |
e90c1d2a VZ |
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 | |
2b5f62a0 VZ |
72 | |
73 | // virtual dtor for any base class | |
e4a4a50b | 74 | virtual ~wxMBConv(); |
6001e347 RR |
75 | }; |
76 | ||
bde4baac VZ |
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: | |
75736a9c DS |
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; | |
bde4baac VZ |
87 | }; |
88 | ||
e90c1d2a | 89 | // ---------------------------------------------------------------------------- |
6001e347 | 90 | // wxMBConvUTF7 (for conversion using UTF7 encoding) |
e90c1d2a | 91 | // ---------------------------------------------------------------------------- |
6001e347 | 92 | |
bddd7a8d | 93 | class WXDLLIMPEXP_BASE wxMBConvUTF7 : public wxMBConv |
6001e347 RR |
94 | { |
95 | public: | |
75736a9c DS |
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; | |
6001e347 RR |
98 | }; |
99 | ||
e90c1d2a | 100 | // ---------------------------------------------------------------------------- |
6001e347 | 101 | // wxMBConvUTF8 (for conversion using UTF8 encoding) |
e90c1d2a | 102 | // ---------------------------------------------------------------------------- |
6001e347 | 103 | |
bddd7a8d | 104 | class WXDLLIMPEXP_BASE wxMBConvUTF8 : public wxMBConv |
6001e347 RR |
105 | { |
106 | public: | |
ea8ce907 RR |
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) { } | |
75736a9c DS |
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; | |
ea8ce907 RR |
116 | |
117 | private: | |
118 | int m_options; | |
6001e347 RR |
119 | }; |
120 | ||
e90c1d2a | 121 | // ---------------------------------------------------------------------------- |
c91830cb VZ |
122 | // wxMBConvUTF16LE (for conversion using UTF16 Little Endian encoding) |
123 | // ---------------------------------------------------------------------------- | |
124 | ||
125 | class WXDLLIMPEXP_BASE wxMBConvUTF16LE : public wxMBConv | |
126 | { | |
127 | public: | |
75736a9c DS |
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; | |
c91830cb VZ |
130 | }; |
131 | ||
132 | // ---------------------------------------------------------------------------- | |
133 | // wxMBConvUTF16BE (for conversion using UTF16 Big Endian encoding) | |
134 | // ---------------------------------------------------------------------------- | |
135 | ||
136 | class WXDLLIMPEXP_BASE wxMBConvUTF16BE : public wxMBConv | |
137 | { | |
138 | public: | |
75736a9c DS |
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; | |
c91830cb VZ |
141 | }; |
142 | ||
143 | // ---------------------------------------------------------------------------- | |
144 | // wxMBConvUCS4LE (for conversion using UTF32 Little Endian encoding) | |
145 | // ---------------------------------------------------------------------------- | |
146 | ||
147 | class WXDLLIMPEXP_BASE wxMBConvUTF32LE : public wxMBConv | |
148 | { | |
149 | public: | |
75736a9c DS |
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; | |
c91830cb VZ |
152 | }; |
153 | ||
154 | // ---------------------------------------------------------------------------- | |
155 | // wxMBConvUCS4BE (for conversion using UTF32 Big Endian encoding) | |
156 | // ---------------------------------------------------------------------------- | |
157 | ||
158 | class WXDLLIMPEXP_BASE wxMBConvUTF32BE : public wxMBConv | |
159 | { | |
160 | public: | |
75736a9c DS |
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; | |
c91830cb VZ |
163 | }; |
164 | ||
165 | // ---------------------------------------------------------------------------- | |
e90c1d2a VZ |
166 | // wxCSConv (for conversion based on loadable char sets) |
167 | // ---------------------------------------------------------------------------- | |
6001e347 | 168 | |
8b04d4c4 VZ |
169 | #include "wx/fontenc.h" |
170 | ||
bddd7a8d | 171 | class WXDLLIMPEXP_BASE wxCSConv : public wxMBConv |
6001e347 | 172 | { |
6001e347 | 173 | public: |
e95354ec VZ |
174 | // we can be created either from charset name or from an encoding constant |
175 | // but we can't have both at once | |
e90c1d2a | 176 | wxCSConv(const wxChar *charset); |
8b04d4c4 | 177 | wxCSConv(wxFontEncoding encoding); |
e95354ec | 178 | |
54380f29 | 179 | wxCSConv(const wxCSConv& conv); |
e90c1d2a VZ |
180 | virtual ~wxCSConv(); |
181 | ||
54380f29 | 182 | wxCSConv& operator=(const wxCSConv& conv); |
2b5f62a0 | 183 | |
75736a9c DS |
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; | |
e90c1d2a | 186 | |
65e50848 JS |
187 | void Clear() ; |
188 | ||
e90c1d2a | 189 | private: |
8b04d4c4 VZ |
190 | // common part of all ctors |
191 | void Init(); | |
192 | ||
e95354ec VZ |
193 | // creates m_convReal if necessary |
194 | void CreateConvIfNeeded() const; | |
195 | ||
196 | // do create m_convReal (unconditionally) | |
197 | wxMBConv *DoCreate() const; | |
198 | ||
bda3d86a VZ |
199 | // set the name (may be only called when m_name == NULL), makes copy of |
200 | // charset string | |
e90c1d2a VZ |
201 | void SetName(const wxChar *charset); |
202 | ||
e95354ec | 203 | |
dccce9ea VZ |
204 | // note that we can't use wxString here because of compilation |
205 | // dependencies: we're included from wx/string.h | |
e90c1d2a | 206 | wxChar *m_name; |
8b04d4c4 | 207 | wxFontEncoding m_encoding; |
e95354ec VZ |
208 | |
209 | // use CreateConvIfNeeded() before accessing m_convReal! | |
210 | wxMBConv *m_convReal; | |
e90c1d2a | 211 | bool m_deferred; |
6001e347 RR |
212 | }; |
213 | ||
f5a1953b VZ |
214 | // ---------------------------------------------------------------------------- |
215 | // declare predefined conversion objects | |
216 | // ---------------------------------------------------------------------------- | |
d5c8817c | 217 | |
f5a1953b VZ |
218 | // conversion to be used with all standard functions affected by locale, e.g. |
219 | // strtol(), strftime(), ... | |
220 | extern WXDLLIMPEXP_DATA_BASE(wxMBConv&) wxConvLibc; | |
221 | ||
222 | // conversion ISO-8859-1/UTF-7/UTF-8 <-> wchar_t | |
16cba29d | 223 | extern WXDLLIMPEXP_DATA_BASE(wxCSConv&) wxConvISO8859_1; |
f5a1953b VZ |
224 | extern WXDLLIMPEXP_DATA_BASE(wxMBConvUTF7&) wxConvUTF7; |
225 | extern WXDLLIMPEXP_DATA_BASE(wxMBConvUTF8&) wxConvUTF8; | |
226 | ||
227 | // conversion used for the file names on the systems where they're not Unicode | |
228 | // (basically anything except Windows) | |
229 | // | |
230 | // this is used by all file functions, can be changed by the application | |
231 | // | |
232 | // by default UTF-8 under Mac OS X and wxConvLibc elsewhere (but it's not used | |
233 | // under Windows normally) | |
234 | extern WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvFileName; | |
235 | ||
236 | // backwards compatible define | |
237 | #define wxConvFile (*wxConvFileName) | |
238 | ||
239 | // the current conversion object, may be set to any conversion, is used by | |
240 | // default in a couple of places inside wx (initially same as wxConvLibc) | |
16cba29d | 241 | extern WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvCurrent; |
6001e347 | 242 | |
f5a1953b VZ |
243 | // ??? |
244 | extern WXDLLIMPEXP_DATA_BASE(wxCSConv&) wxConvLocal; | |
245 | ||
246 | ||
e95354ec VZ |
247 | // ---------------------------------------------------------------------------- |
248 | // endianness-dependent conversions | |
249 | // ---------------------------------------------------------------------------- | |
250 | ||
251 | #ifdef WORDS_BIGENDIAN | |
252 | typedef wxMBConvUTF16BE wxMBConvUTF16; | |
253 | typedef wxMBConvUTF32BE wxMBConvUTF32; | |
254 | #else | |
255 | typedef wxMBConvUTF16LE wxMBConvUTF16; | |
256 | typedef wxMBConvUTF32LE wxMBConvUTF32; | |
257 | #endif | |
258 | ||
e90c1d2a | 259 | // ---------------------------------------------------------------------------- |
6001e347 | 260 | // filename conversion macros |
e90c1d2a | 261 | // ---------------------------------------------------------------------------- |
6001e347 RR |
262 | |
263 | // filenames are multibyte on Unix and probably widechar on Windows? | |
c4e41ce3 | 264 | #if defined(__UNIX__) || defined(__BORLANDC__) || defined(__WXMAC__ ) |
e90c1d2a | 265 | #define wxMBFILES 1 |
6001e347 | 266 | #else |
e90c1d2a | 267 | #define wxMBFILES 0 |
6001e347 RR |
268 | #endif |
269 | ||
80df4d31 | 270 | #if wxMBFILES && wxUSE_UNICODE |
f5a1953b | 271 | #define wxFNCONV(name) wxConvFileName->cWX2MB(name) |
e90c1d2a | 272 | #define wxFNSTRINGCAST wxMBSTRINGCAST |
d5c8817c SC |
273 | #else |
274 | #if defined( __WXOSX__ ) && wxMBFILES | |
f5a1953b | 275 | #define wxFNCONV(name) wxConvFileName->cWC2MB( wxConvLocal.cWX2WC(name) ) |
6001e347 | 276 | #else |
e90c1d2a | 277 | #define wxFNCONV(name) name |
d5c8817c | 278 | #endif |
e90c1d2a | 279 | #define wxFNSTRINGCAST WXSTRINGCAST |
6001e347 RR |
280 | #endif |
281 | ||
f5a1953b | 282 | #else // !wxUSE_WCHAR_T |
6001e347 | 283 | |
e90c1d2a | 284 | // ---------------------------------------------------------------------------- |
6001e347 | 285 | // stand-ins in absence of wchar_t |
e90c1d2a | 286 | // ---------------------------------------------------------------------------- |
6001e347 | 287 | |
bddd7a8d | 288 | class WXDLLIMPEXP_BASE wxMBConv |
6001e347 RR |
289 | { |
290 | public: | |
e90c1d2a VZ |
291 | const char* cMB2WX(const char *psz) const { return psz; } |
292 | const char* cWX2MB(const char *psz) const { return psz; } | |
6001e347 | 293 | }; |
e90c1d2a | 294 | |
bde4baac VZ |
295 | #define wxConvFile wxConvLocal |
296 | ||
16cba29d | 297 | extern WXDLLIMPEXP_DATA_BASE(wxMBConv) wxConvLibc, |
8b04d4c4 VZ |
298 | wxConvLocal, |
299 | wxConvISO8859_1, | |
300 | wxConvUTF8; | |
16cba29d | 301 | extern WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvCurrent; |
6001e347 RR |
302 | |
303 | #define wxFNCONV(name) name | |
e90c1d2a | 304 | #define wxFNSTRINGCAST WXSTRINGCAST |
6001e347 RR |
305 | |
306 | #endif | |
307 | // wxUSE_WCHAR_T | |
308 | ||
e90c1d2a VZ |
309 | // ---------------------------------------------------------------------------- |
310 | // macros for the most common conversions | |
311 | // ---------------------------------------------------------------------------- | |
312 | ||
313 | #if wxUSE_UNICODE | |
314 | #define wxConvertWX2MB(s) wxConvCurrent->cWX2MB(s) | |
315 | #define wxConvertMB2WX(s) wxConvCurrent->cMB2WX(s) | |
316 | #else // ANSI | |
317 | // no conversions to do | |
318 | #define wxConvertWX2MB(s) (s) | |
319 | #define wxConvertMB2WX(s) (s) | |
320 | #endif // Unicode/ANSI | |
321 | ||
322 | #endif | |
6001e347 RR |
323 | // _WX_WXSTRCONVH__ |
324 |