]>
Commit | Line | Data |
---|---|---|
6001e347 RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: strconv.cpp | |
3 | // Purpose: Unicode conversion classes | |
15f2ee32 RN |
4 | // Author: Ove Kaaven, Robert Roebling, Vadim Zeitlin, Vaclav Slavik, |
5 | // Ryan Norton, Fredrik Roubert (UTF7) | |
6001e347 RR |
6 | // Modified by: |
7 | // Created: 29/01/98 | |
8 | // RCS-ID: $Id$ | |
e95354ec VZ |
9 | // Copyright: (c) 1999 Ove Kaaven, Robert Roebling, Vaclav Slavik |
10 | // (c) 2000-2003 Vadim Zeitlin | |
15f2ee32 | 11 | // (c) 2004 Ryan Norton, Fredrik Roubert |
65571936 | 12 | // Licence: wxWindows licence |
6001e347 RR |
13 | ///////////////////////////////////////////////////////////////////////////// |
14 | ||
f6bcfd97 BP |
15 | // ============================================================================ |
16 | // declarations | |
17 | // ============================================================================ | |
18 | ||
19 | // ---------------------------------------------------------------------------- | |
20 | // headers | |
21 | // ---------------------------------------------------------------------------- | |
22 | ||
14f355c2 | 23 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
6001e347 RR |
24 | #pragma implementation "strconv.h" |
25 | #endif | |
26 | ||
27 | // For compilers that support precompilation, includes "wx.h". | |
28 | #include "wx/wxprec.h" | |
29 | ||
30 | #ifdef __BORLANDC__ | |
31 | #pragma hdrstop | |
32 | #endif | |
33 | ||
373658eb VZ |
34 | #ifndef WX_PRECOMP |
35 | #include "wx/intl.h" | |
36 | #include "wx/log.h" | |
37 | #endif // WX_PRECOMP | |
38 | ||
bde4baac VZ |
39 | #include "wx/strconv.h" |
40 | ||
41 | #if wxUSE_WCHAR_T | |
42 | ||
7608a683 | 43 | #ifdef __WINDOWS__ |
532d575b | 44 | #include "wx/msw/private.h" |
13dd924a | 45 | #include "wx/msw/missing.h" |
0a1c1e62 GRG |
46 | #endif |
47 | ||
1c193821 | 48 | #ifndef __WXWINCE__ |
1cd52418 | 49 | #include <errno.h> |
1c193821 JS |
50 | #endif |
51 | ||
6001e347 RR |
52 | #include <ctype.h> |
53 | #include <string.h> | |
54 | #include <stdlib.h> | |
55 | ||
e95354ec VZ |
56 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) |
57 | #define wxHAVE_WIN32_MB2WC | |
58 | #endif // __WIN32__ but !__WXMICROWIN__ | |
59 | ||
373658eb VZ |
60 | // ---------------------------------------------------------------------------- |
61 | // headers | |
62 | // ---------------------------------------------------------------------------- | |
7af284fd | 63 | |
6001e347 | 64 | #ifdef __SALFORDC__ |
373658eb | 65 | #include <clib.h> |
6001e347 RR |
66 | #endif |
67 | ||
b040e242 | 68 | #ifdef HAVE_ICONV |
373658eb | 69 | #include <iconv.h> |
b1d547eb | 70 | #include "wx/thread.h" |
1cd52418 | 71 | #endif |
1cd52418 | 72 | |
373658eb VZ |
73 | #include "wx/encconv.h" |
74 | #include "wx/fontmap.h" | |
7608a683 | 75 | #include "wx/utils.h" |
373658eb | 76 | |
335d31e0 | 77 | #ifdef __WXMAC__ |
40ba2f3b | 78 | #ifndef __DARWIN__ |
4227afa4 SC |
79 | #include <ATSUnicode.h> |
80 | #include <TextCommon.h> | |
81 | #include <TextEncodingConverter.h> | |
40ba2f3b | 82 | #endif |
335d31e0 SC |
83 | |
84 | #include "wx/mac/private.h" // includes mac headers | |
85 | #endif | |
373658eb VZ |
86 | // ---------------------------------------------------------------------------- |
87 | // macros | |
88 | // ---------------------------------------------------------------------------- | |
3e61dfb0 | 89 | |
1cd52418 | 90 | #define BSWAP_UCS4(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT32_SWAP_ALWAYS(str[_c]); } |
3a0d76bc | 91 | #define BSWAP_UTF16(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT16_SWAP_ALWAYS(str[_c]); } |
1cd52418 OK |
92 | |
93 | #if SIZEOF_WCHAR_T == 4 | |
3a0d76bc VS |
94 | #define WC_NAME "UCS4" |
95 | #define WC_BSWAP BSWAP_UCS4 | |
96 | #ifdef WORDS_BIGENDIAN | |
97 | #define WC_NAME_BEST "UCS-4BE" | |
98 | #else | |
99 | #define WC_NAME_BEST "UCS-4LE" | |
100 | #endif | |
1cd52418 | 101 | #elif SIZEOF_WCHAR_T == 2 |
3a0d76bc VS |
102 | #define WC_NAME "UTF16" |
103 | #define WC_BSWAP BSWAP_UTF16 | |
a3f2769e | 104 | #define WC_UTF16 |
3a0d76bc VS |
105 | #ifdef WORDS_BIGENDIAN |
106 | #define WC_NAME_BEST "UTF-16BE" | |
107 | #else | |
108 | #define WC_NAME_BEST "UTF-16LE" | |
109 | #endif | |
bab1e722 | 110 | #else // sizeof(wchar_t) != 2 nor 4 |
bde4baac VZ |
111 | // does this ever happen? |
112 | #error "Unknown sizeof(wchar_t): please report this to wx-dev@lists.wxwindows.org" | |
1cd52418 OK |
113 | #endif |
114 | ||
373658eb VZ |
115 | // ============================================================================ |
116 | // implementation | |
117 | // ============================================================================ | |
118 | ||
119 | // ---------------------------------------------------------------------------- | |
c91830cb | 120 | // UTF-16 en/decoding to/from UCS-4 |
373658eb | 121 | // ---------------------------------------------------------------------------- |
6001e347 | 122 | |
b0a6bb75 | 123 | |
c91830cb | 124 | static size_t encode_utf16(wxUint32 input, wxUint16 *output) |
1cd52418 | 125 | { |
dccce9ea | 126 | if (input<=0xffff) |
4def3b35 | 127 | { |
999836aa VZ |
128 | if (output) |
129 | *output = (wxUint16) input; | |
4def3b35 | 130 | return 1; |
dccce9ea VZ |
131 | } |
132 | else if (input>=0x110000) | |
4def3b35 VS |
133 | { |
134 | return (size_t)-1; | |
dccce9ea VZ |
135 | } |
136 | else | |
4def3b35 | 137 | { |
dccce9ea | 138 | if (output) |
4def3b35 | 139 | { |
c91830cb | 140 | *output++ = (wxUint16) ((input >> 10)+0xd7c0); |
999836aa | 141 | *output = (wxUint16) ((input&0x3ff)+0xdc00); |
4def3b35 VS |
142 | } |
143 | return 2; | |
1cd52418 | 144 | } |
1cd52418 OK |
145 | } |
146 | ||
c91830cb | 147 | static size_t decode_utf16(const wxUint16* input, wxUint32& output) |
1cd52418 | 148 | { |
dccce9ea | 149 | if ((*input<0xd800) || (*input>0xdfff)) |
4def3b35 VS |
150 | { |
151 | output = *input; | |
152 | return 1; | |
dccce9ea | 153 | } |
cdb14ecb | 154 | else if ((input[1]<0xdc00) || (input[1]>0xdfff)) |
4def3b35 VS |
155 | { |
156 | output = *input; | |
157 | return (size_t)-1; | |
dccce9ea VZ |
158 | } |
159 | else | |
4def3b35 VS |
160 | { |
161 | output = ((input[0] - 0xd7c0) << 10) + (input[1] - 0xdc00); | |
162 | return 2; | |
163 | } | |
1cd52418 OK |
164 | } |
165 | ||
b0a6bb75 | 166 | |
f6bcfd97 | 167 | // ---------------------------------------------------------------------------- |
6001e347 | 168 | // wxMBConv |
f6bcfd97 | 169 | // ---------------------------------------------------------------------------- |
2c53a80a WS |
170 | |
171 | wxMBConv::~wxMBConv() | |
172 | { | |
173 | // nothing to do here (necessary for Darwin linking probably) | |
174 | } | |
6001e347 | 175 | |
6001e347 RR |
176 | const wxWCharBuffer wxMBConv::cMB2WC(const char *psz) const |
177 | { | |
2b5f62a0 | 178 | if ( psz ) |
6001e347 | 179 | { |
2b5f62a0 VZ |
180 | // calculate the length of the buffer needed first |
181 | size_t nLen = MB2WC(NULL, psz, 0); | |
182 | if ( nLen != (size_t)-1 ) | |
183 | { | |
184 | // now do the actual conversion | |
185 | wxWCharBuffer buf(nLen); | |
635f33ce VS |
186 | nLen = MB2WC(buf.data(), psz, nLen + 1); // with the trailing NULL |
187 | if ( nLen != (size_t)-1 ) | |
188 | { | |
189 | return buf; | |
190 | } | |
2b5f62a0 | 191 | } |
f6bcfd97 | 192 | } |
2b5f62a0 VZ |
193 | |
194 | wxWCharBuffer buf((wchar_t *)NULL); | |
195 | ||
196 | return buf; | |
6001e347 RR |
197 | } |
198 | ||
e5cceba0 | 199 | const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *pwz) const |
6001e347 | 200 | { |
2b5f62a0 VZ |
201 | if ( pwz ) |
202 | { | |
203 | size_t nLen = WC2MB(NULL, pwz, 0); | |
204 | if ( nLen != (size_t)-1 ) | |
205 | { | |
c91830cb | 206 | wxCharBuffer buf(nLen+3); // space for a wxUint32 trailing zero |
635f33ce VS |
207 | nLen = WC2MB(buf.data(), pwz, nLen + 4); |
208 | if ( nLen != (size_t)-1 ) | |
209 | { | |
210 | return buf; | |
211 | } | |
2b5f62a0 VZ |
212 | } |
213 | } | |
214 | ||
215 | wxCharBuffer buf((char *)NULL); | |
e5cceba0 | 216 | |
e5cceba0 | 217 | return buf; |
6001e347 RR |
218 | } |
219 | ||
f5fb6871 | 220 | const wxWCharBuffer wxMBConv::cMB2WC(const char *szString, size_t nStringLen, size_t* pOutSize) const |
e4e3bbb4 | 221 | { |
f5fb6871 RN |
222 | wxASSERT(pOutSize != NULL); |
223 | ||
e4e3bbb4 RN |
224 | const char* szEnd = szString + nStringLen + 1; |
225 | const char* szPos = szString; | |
226 | const char* szStart = szPos; | |
227 | ||
228 | size_t nActualLength = 0; | |
f5fb6871 RN |
229 | size_t nCurrentSize = nStringLen; //try normal size first (should never resize?) |
230 | ||
231 | wxWCharBuffer theBuffer(nCurrentSize); | |
e4e3bbb4 RN |
232 | |
233 | //Convert the string until the length() is reached, continuing the | |
234 | //loop every time a null character is reached | |
235 | while(szPos != szEnd) | |
236 | { | |
237 | wxASSERT(szPos < szEnd); //something is _really_ screwed up if this rings true | |
238 | ||
239 | //Get the length of the current (sub)string | |
240 | size_t nLen = MB2WC(NULL, szPos, 0); | |
241 | ||
242 | //Invalid conversion? | |
243 | if( nLen == (size_t)-1 ) | |
f5fb6871 RN |
244 | { |
245 | *pOutSize = 0; | |
246 | theBuffer.data()[0u] = wxT('\0'); | |
247 | return theBuffer; | |
248 | } | |
249 | ||
e4e3bbb4 RN |
250 | |
251 | //Increase the actual length (+1 for current null character) | |
252 | nActualLength += nLen + 1; | |
253 | ||
f5fb6871 RN |
254 | //if buffer too big, realloc the buffer |
255 | if (nActualLength > (nCurrentSize+1)) | |
256 | { | |
257 | wxWCharBuffer theNewBuffer(nCurrentSize << 1); | |
258 | memcpy(theNewBuffer.data(), theBuffer.data(), nCurrentSize * sizeof(wchar_t)); | |
259 | theBuffer = theNewBuffer; | |
260 | nCurrentSize <<= 1; | |
261 | } | |
262 | ||
263 | //Convert the current (sub)string | |
264 | if ( MB2WC(&theBuffer.data()[szPos - szStart], szPos, nLen + 1) == (size_t)-1 ) | |
e4e3bbb4 | 265 | { |
f5fb6871 RN |
266 | *pOutSize = 0; |
267 | theBuffer.data()[0u] = wxT('\0'); | |
268 | return theBuffer; | |
e4e3bbb4 RN |
269 | } |
270 | ||
271 | //Increment to next (sub)string | |
3103e8a9 JS |
272 | //Note that we have to use strlen instead of nLen here |
273 | //because XX2XX gives us the size of the output buffer, | |
274 | //which is not necessarily the length of the string | |
e4e3bbb4 RN |
275 | szPos += strlen(szPos) + 1; |
276 | } | |
277 | ||
f5fb6871 RN |
278 | //success - return actual length and the buffer |
279 | *pOutSize = nActualLength; | |
3698ae71 | 280 | return theBuffer; |
e4e3bbb4 RN |
281 | } |
282 | ||
f5fb6871 | 283 | const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *szString, size_t nStringLen, size_t* pOutSize) const |
e4e3bbb4 | 284 | { |
f5fb6871 RN |
285 | wxASSERT(pOutSize != NULL); |
286 | ||
e4e3bbb4 RN |
287 | const wchar_t* szEnd = szString + nStringLen + 1; |
288 | const wchar_t* szPos = szString; | |
289 | const wchar_t* szStart = szPos; | |
290 | ||
291 | size_t nActualLength = 0; | |
f5fb6871 RN |
292 | size_t nCurrentSize = nStringLen << 2; //try * 4 first |
293 | ||
294 | wxCharBuffer theBuffer(nCurrentSize); | |
e4e3bbb4 RN |
295 | |
296 | //Convert the string until the length() is reached, continuing the | |
297 | //loop every time a null character is reached | |
298 | while(szPos != szEnd) | |
299 | { | |
300 | wxASSERT(szPos < szEnd); //something is _really_ screwed up if this rings true | |
301 | ||
302 | //Get the length of the current (sub)string | |
303 | size_t nLen = WC2MB(NULL, szPos, 0); | |
304 | ||
305 | //Invalid conversion? | |
306 | if( nLen == (size_t)-1 ) | |
f5fb6871 RN |
307 | { |
308 | *pOutSize = 0; | |
309 | theBuffer.data()[0u] = wxT('\0'); | |
310 | return theBuffer; | |
311 | } | |
e4e3bbb4 RN |
312 | |
313 | //Increase the actual length (+1 for current null character) | |
314 | nActualLength += nLen + 1; | |
3698ae71 | 315 | |
f5fb6871 RN |
316 | //if buffer too big, realloc the buffer |
317 | if (nActualLength > (nCurrentSize+1)) | |
318 | { | |
319 | wxCharBuffer theNewBuffer(nCurrentSize << 1); | |
320 | memcpy(theNewBuffer.data(), theBuffer.data(), nCurrentSize); | |
321 | theBuffer = theNewBuffer; | |
322 | nCurrentSize <<= 1; | |
323 | } | |
324 | ||
325 | //Convert the current (sub)string | |
326 | if(WC2MB(&theBuffer.data()[szPos - szStart], szPos, nLen + 1) == (size_t)-1 ) | |
e4e3bbb4 | 327 | { |
f5fb6871 RN |
328 | *pOutSize = 0; |
329 | theBuffer.data()[0u] = wxT('\0'); | |
330 | return theBuffer; | |
e4e3bbb4 RN |
331 | } |
332 | ||
333 | //Increment to next (sub)string | |
3103e8a9 JS |
334 | //Note that we have to use wxWcslen instead of nLen here |
335 | //because XX2XX gives us the size of the output buffer, | |
336 | //which is not necessarily the length of the string | |
e4e3bbb4 RN |
337 | szPos += wxWcslen(szPos) + 1; |
338 | } | |
339 | ||
f5fb6871 RN |
340 | //success - return actual length and the buffer |
341 | *pOutSize = nActualLength; | |
3698ae71 | 342 | return theBuffer; |
e4e3bbb4 RN |
343 | } |
344 | ||
6001e347 | 345 | // ---------------------------------------------------------------------------- |
bde4baac | 346 | // wxMBConvLibc |
6001e347 RR |
347 | // ---------------------------------------------------------------------------- |
348 | ||
bde4baac VZ |
349 | size_t wxMBConvLibc::MB2WC(wchar_t *buf, const char *psz, size_t n) const |
350 | { | |
351 | return wxMB2WC(buf, psz, n); | |
352 | } | |
353 | ||
354 | size_t wxMBConvLibc::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
355 | { | |
356 | return wxWC2MB(buf, psz, n); | |
357 | } | |
e1bfe89e | 358 | |
66bf0099 | 359 | #ifdef __UNIX__ |
c12b7f79 | 360 | |
e1bfe89e | 361 | // ---------------------------------------------------------------------------- |
532d575b | 362 | // wxConvBrokenFileNames |
e1bfe89e RR |
363 | // ---------------------------------------------------------------------------- |
364 | ||
845905d5 | 365 | wxConvBrokenFileNames::wxConvBrokenFileNames(const wxChar *charset) |
ea8ce907 | 366 | { |
845905d5 MW |
367 | if ( !charset || wxStricmp(charset, _T("UTF-8")) == 0 |
368 | || wxStricmp(charset, _T("UTF8")) == 0 ) | |
369 | m_conv = new wxMBConvUTF8(wxMBConvUTF8::MAP_INVALID_UTF8_TO_OCTAL); | |
370 | else | |
371 | m_conv = new wxCSConv(charset); | |
ea8ce907 RR |
372 | } |
373 | ||
c12b7f79 VZ |
374 | size_t |
375 | wxConvBrokenFileNames::MB2WC(wchar_t *outputBuf, | |
376 | const char *psz, | |
377 | size_t outputSize) const | |
e1bfe89e | 378 | { |
c12b7f79 | 379 | return m_conv->MB2WC( outputBuf, psz, outputSize ); |
e1bfe89e RR |
380 | } |
381 | ||
c12b7f79 VZ |
382 | size_t |
383 | wxConvBrokenFileNames::WC2MB(char *outputBuf, | |
384 | const wchar_t *psz, | |
385 | size_t outputSize) const | |
e1bfe89e | 386 | { |
c12b7f79 | 387 | return m_conv->WC2MB( outputBuf, psz, outputSize ); |
e1bfe89e RR |
388 | } |
389 | ||
66bf0099 | 390 | #endif |
c12b7f79 | 391 | |
bde4baac | 392 | // ---------------------------------------------------------------------------- |
3698ae71 | 393 | // UTF-7 |
bde4baac | 394 | // ---------------------------------------------------------------------------- |
6001e347 | 395 | |
15f2ee32 | 396 | // Implementation (C) 2004 Fredrik Roubert |
6001e347 | 397 | |
15f2ee32 RN |
398 | // |
399 | // BASE64 decoding table | |
400 | // | |
401 | static const unsigned char utf7unb64[] = | |
6001e347 | 402 | { |
15f2ee32 RN |
403 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
404 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
405 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
406 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
407 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
408 | 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f, | |
409 | 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, | |
410 | 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
411 | 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, | |
412 | 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, | |
413 | 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, | |
414 | 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff, | |
415 | 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, | |
416 | 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, | |
417 | 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, | |
418 | 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, | |
419 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
420 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
421 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
422 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
423 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
424 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
425 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
426 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
427 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
428 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
429 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
430 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
431 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
432 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
433 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
434 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff | |
435 | }; | |
436 | ||
437 | size_t wxMBConvUTF7::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
438 | { | |
15f2ee32 RN |
439 | size_t len = 0; |
440 | ||
441 | while (*psz && ((!buf) || (len < n))) | |
442 | { | |
443 | unsigned char cc = *psz++; | |
444 | if (cc != '+') | |
445 | { | |
446 | // plain ASCII char | |
447 | if (buf) | |
448 | *buf++ = cc; | |
449 | len++; | |
450 | } | |
451 | else if (*psz == '-') | |
452 | { | |
453 | // encoded plus sign | |
454 | if (buf) | |
455 | *buf++ = cc; | |
456 | len++; | |
457 | psz++; | |
458 | } | |
459 | else | |
460 | { | |
461 | // BASE64 encoded string | |
462 | bool lsb; | |
463 | unsigned char c; | |
464 | unsigned int d, l; | |
465 | for (lsb = false, d = 0, l = 0; | |
466 | (cc = utf7unb64[(unsigned char)*psz]) != 0xff; psz++) | |
467 | { | |
468 | d <<= 6; | |
469 | d += cc; | |
470 | for (l += 6; l >= 8; lsb = !lsb) | |
471 | { | |
6356d52a | 472 | c = (unsigned char)((d >> (l -= 8)) % 256); |
15f2ee32 RN |
473 | if (lsb) |
474 | { | |
475 | if (buf) | |
476 | *buf++ |= c; | |
477 | len ++; | |
478 | } | |
479 | else | |
480 | if (buf) | |
6356d52a | 481 | *buf = (wchar_t)(c << 8); |
15f2ee32 RN |
482 | } |
483 | } | |
484 | if (*psz == '-') | |
485 | psz++; | |
486 | } | |
487 | } | |
488 | if (buf && (len < n)) | |
489 | *buf = 0; | |
490 | return len; | |
6001e347 RR |
491 | } |
492 | ||
15f2ee32 RN |
493 | // |
494 | // BASE64 encoding table | |
495 | // | |
496 | static const unsigned char utf7enb64[] = | |
497 | { | |
498 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', | |
499 | 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', | |
500 | 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', | |
501 | 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', | |
502 | 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', | |
503 | 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', | |
504 | 'w', 'x', 'y', 'z', '0', '1', '2', '3', | |
505 | '4', '5', '6', '7', '8', '9', '+', '/' | |
506 | }; | |
507 | ||
508 | // | |
509 | // UTF-7 encoding table | |
510 | // | |
511 | // 0 - Set D (directly encoded characters) | |
512 | // 1 - Set O (optional direct characters) | |
513 | // 2 - whitespace characters (optional) | |
514 | // 3 - special characters | |
515 | // | |
516 | static const unsigned char utf7encode[128] = | |
6001e347 | 517 | { |
15f2ee32 RN |
518 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
519 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, | |
520 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 3, | |
521 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, | |
522 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
523 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, | |
524 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
525 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 3, 3 | |
526 | }; | |
527 | ||
667e5b3e | 528 | size_t wxMBConvUTF7::WC2MB(char *buf, const wchar_t *psz, size_t n) const |
15f2ee32 RN |
529 | { |
530 | ||
531 | ||
532 | size_t len = 0; | |
533 | ||
534 | while (*psz && ((!buf) || (len < n))) | |
535 | { | |
536 | wchar_t cc = *psz++; | |
537 | if (cc < 0x80 && utf7encode[cc] < 1) | |
538 | { | |
539 | // plain ASCII char | |
540 | if (buf) | |
541 | *buf++ = (char)cc; | |
542 | len++; | |
543 | } | |
544 | #ifndef WC_UTF16 | |
79c78d42 | 545 | else if (((wxUint32)cc) > 0xffff) |
b2c13097 | 546 | { |
15f2ee32 RN |
547 | // no surrogate pair generation (yet?) |
548 | return (size_t)-1; | |
549 | } | |
550 | #endif | |
551 | else | |
552 | { | |
553 | if (buf) | |
554 | *buf++ = '+'; | |
555 | len++; | |
556 | if (cc != '+') | |
557 | { | |
558 | // BASE64 encode string | |
559 | unsigned int lsb, d, l; | |
560 | for (d = 0, l = 0;; psz++) | |
561 | { | |
562 | for (lsb = 0; lsb < 2; lsb ++) | |
563 | { | |
564 | d <<= 8; | |
565 | d += lsb ? cc & 0xff : (cc & 0xff00) >> 8; | |
566 | ||
567 | for (l += 8; l >= 6; ) | |
568 | { | |
569 | l -= 6; | |
570 | if (buf) | |
571 | *buf++ = utf7enb64[(d >> l) % 64]; | |
572 | len++; | |
573 | } | |
574 | } | |
575 | cc = *psz; | |
576 | if (!(cc) || (cc < 0x80 && utf7encode[cc] < 1)) | |
577 | break; | |
578 | } | |
579 | if (l != 0) | |
580 | { | |
581 | if (buf) | |
582 | *buf++ = utf7enb64[((d % 16) << (6 - l)) % 64]; | |
583 | len++; | |
584 | } | |
585 | } | |
586 | if (buf) | |
587 | *buf++ = '-'; | |
588 | len++; | |
589 | } | |
590 | } | |
591 | if (buf && (len < n)) | |
592 | *buf = 0; | |
593 | return len; | |
6001e347 RR |
594 | } |
595 | ||
f6bcfd97 | 596 | // ---------------------------------------------------------------------------- |
6001e347 | 597 | // UTF-8 |
f6bcfd97 | 598 | // ---------------------------------------------------------------------------- |
6001e347 | 599 | |
dccce9ea | 600 | static wxUint32 utf8_max[]= |
4def3b35 | 601 | { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff, 0xffffffff }; |
6001e347 | 602 | |
3698ae71 VZ |
603 | // boundaries of the private use area we use to (temporarily) remap invalid |
604 | // characters invalid in a UTF-8 encoded string | |
ea8ce907 RR |
605 | const wxUint32 wxUnicodePUA = 0x100000; |
606 | const wxUint32 wxUnicodePUAEnd = wxUnicodePUA + 256; | |
607 | ||
6001e347 RR |
608 | size_t wxMBConvUTF8::MB2WC(wchar_t *buf, const char *psz, size_t n) const |
609 | { | |
4def3b35 VS |
610 | size_t len = 0; |
611 | ||
dccce9ea | 612 | while (*psz && ((!buf) || (len < n))) |
4def3b35 | 613 | { |
ea8ce907 RR |
614 | const char *opsz = psz; |
615 | bool invalid = false; | |
4def3b35 VS |
616 | unsigned char cc = *psz++, fc = cc; |
617 | unsigned cnt; | |
dccce9ea | 618 | for (cnt = 0; fc & 0x80; cnt++) |
4def3b35 | 619 | fc <<= 1; |
dccce9ea | 620 | if (!cnt) |
4def3b35 VS |
621 | { |
622 | // plain ASCII char | |
dccce9ea | 623 | if (buf) |
4def3b35 VS |
624 | *buf++ = cc; |
625 | len++; | |
561488ef MW |
626 | |
627 | // escape the escape character for octal escapes | |
628 | if ((m_options & MAP_INVALID_UTF8_TO_OCTAL) | |
629 | && cc == '\\' && (!buf || len < n)) | |
630 | { | |
631 | if (buf) | |
632 | *buf++ = cc; | |
633 | len++; | |
634 | } | |
dccce9ea VZ |
635 | } |
636 | else | |
4def3b35 VS |
637 | { |
638 | cnt--; | |
dccce9ea | 639 | if (!cnt) |
4def3b35 VS |
640 | { |
641 | // invalid UTF-8 sequence | |
ea8ce907 | 642 | invalid = true; |
dccce9ea VZ |
643 | } |
644 | else | |
4def3b35 VS |
645 | { |
646 | unsigned ocnt = cnt - 1; | |
647 | wxUint32 res = cc & (0x3f >> cnt); | |
dccce9ea | 648 | while (cnt--) |
4def3b35 | 649 | { |
ea8ce907 | 650 | cc = *psz; |
dccce9ea | 651 | if ((cc & 0xC0) != 0x80) |
4def3b35 VS |
652 | { |
653 | // invalid UTF-8 sequence | |
ea8ce907 RR |
654 | invalid = true; |
655 | break; | |
4def3b35 | 656 | } |
ea8ce907 | 657 | psz++; |
4def3b35 VS |
658 | res = (res << 6) | (cc & 0x3f); |
659 | } | |
ea8ce907 | 660 | if (invalid || res <= utf8_max[ocnt]) |
4def3b35 VS |
661 | { |
662 | // illegal UTF-8 encoding | |
ea8ce907 | 663 | invalid = true; |
4def3b35 | 664 | } |
ea8ce907 RR |
665 | else if ((m_options & MAP_INVALID_UTF8_TO_PUA) && |
666 | res >= wxUnicodePUA && res < wxUnicodePUAEnd) | |
667 | { | |
668 | // if one of our PUA characters turns up externally | |
669 | // it must also be treated as an illegal sequence | |
670 | // (a bit like you have to escape an escape character) | |
671 | invalid = true; | |
672 | } | |
673 | else | |
674 | { | |
1cd52418 | 675 | #ifdef WC_UTF16 |
ea8ce907 RR |
676 | // cast is ok because wchar_t == wxUuint16 if WC_UTF16 |
677 | size_t pa = encode_utf16(res, (wxUint16 *)buf); | |
678 | if (pa == (size_t)-1) | |
679 | { | |
680 | invalid = true; | |
681 | } | |
682 | else | |
683 | { | |
684 | if (buf) | |
685 | buf += pa; | |
686 | len += pa; | |
687 | } | |
373658eb | 688 | #else // !WC_UTF16 |
ea8ce907 RR |
689 | if (buf) |
690 | *buf++ = res; | |
691 | len++; | |
373658eb | 692 | #endif // WC_UTF16/!WC_UTF16 |
ea8ce907 RR |
693 | } |
694 | } | |
695 | if (invalid) | |
696 | { | |
697 | if (m_options & MAP_INVALID_UTF8_TO_PUA) | |
698 | { | |
699 | while (opsz < psz && (!buf || len < n)) | |
700 | { | |
701 | #ifdef WC_UTF16 | |
702 | // cast is ok because wchar_t == wxUuint16 if WC_UTF16 | |
703 | size_t pa = encode_utf16((unsigned char)*opsz + wxUnicodePUA, (wxUint16 *)buf); | |
704 | wxASSERT(pa != (size_t)-1); | |
705 | if (buf) | |
706 | buf += pa; | |
707 | opsz++; | |
708 | len += pa; | |
709 | #else | |
710 | if (buf) | |
711 | *buf++ = wxUnicodePUA + (unsigned char)*opsz; | |
712 | opsz++; | |
713 | len++; | |
714 | #endif | |
715 | } | |
716 | } | |
3698ae71 | 717 | else if (m_options & MAP_INVALID_UTF8_TO_OCTAL) |
ea8ce907 RR |
718 | { |
719 | while (opsz < psz && (!buf || len < n)) | |
720 | { | |
3698ae71 VZ |
721 | if ( buf && len + 3 < n ) |
722 | { | |
723 | unsigned char n = *opsz; | |
724 | *buf++ = L'\\'; | |
b2c13097 WS |
725 | *buf++ = (wchar_t)( L'0' + n / 0100 ); |
726 | *buf++ = (wchar_t)( L'0' + (n % 0100) / 010 ); | |
727 | *buf++ = (wchar_t)( L'0' + n % 010 ); | |
3698ae71 | 728 | } |
ea8ce907 RR |
729 | opsz++; |
730 | len += 4; | |
731 | } | |
732 | } | |
3698ae71 | 733 | else // MAP_INVALID_UTF8_NOT |
ea8ce907 RR |
734 | { |
735 | return (size_t)-1; | |
736 | } | |
4def3b35 VS |
737 | } |
738 | } | |
6001e347 | 739 | } |
dccce9ea | 740 | if (buf && (len < n)) |
4def3b35 VS |
741 | *buf = 0; |
742 | return len; | |
6001e347 RR |
743 | } |
744 | ||
3698ae71 VZ |
745 | static inline bool isoctal(wchar_t wch) |
746 | { | |
747 | return L'0' <= wch && wch <= L'7'; | |
748 | } | |
749 | ||
6001e347 RR |
750 | size_t wxMBConvUTF8::WC2MB(char *buf, const wchar_t *psz, size_t n) const |
751 | { | |
4def3b35 | 752 | size_t len = 0; |
6001e347 | 753 | |
dccce9ea | 754 | while (*psz && ((!buf) || (len < n))) |
4def3b35 VS |
755 | { |
756 | wxUint32 cc; | |
1cd52418 | 757 | #ifdef WC_UTF16 |
b5153fd8 VZ |
758 | // cast is ok for WC_UTF16 |
759 | size_t pa = decode_utf16((const wxUint16 *)psz, cc); | |
4def3b35 | 760 | psz += (pa == (size_t)-1) ? 1 : pa; |
1cd52418 | 761 | #else |
4def3b35 VS |
762 | cc=(*psz++) & 0x7fffffff; |
763 | #endif | |
3698ae71 VZ |
764 | |
765 | if ( (m_options & MAP_INVALID_UTF8_TO_PUA) | |
766 | && cc >= wxUnicodePUA && cc < wxUnicodePUAEnd ) | |
4def3b35 | 767 | { |
dccce9ea | 768 | if (buf) |
ea8ce907 | 769 | *buf++ = (char)(cc - wxUnicodePUA); |
4def3b35 | 770 | len++; |
3698ae71 | 771 | } |
561488ef MW |
772 | else if ( (m_options & MAP_INVALID_UTF8_TO_OCTAL) |
773 | && cc == L'\\' && psz[0] == L'\\' ) | |
774 | { | |
775 | if (buf) | |
776 | *buf++ = (char)cc; | |
777 | psz++; | |
778 | len++; | |
779 | } | |
3698ae71 VZ |
780 | else if ( (m_options & MAP_INVALID_UTF8_TO_OCTAL) && |
781 | cc == L'\\' && | |
782 | isoctal(psz[0]) && isoctal(psz[1]) && isoctal(psz[2]) ) | |
4def3b35 | 783 | { |
dccce9ea | 784 | if (buf) |
3698ae71 | 785 | { |
b2c13097 WS |
786 | *buf++ = (char) ((psz[0] - L'0')*0100 + |
787 | (psz[1] - L'0')*010 + | |
788 | (psz[2] - L'0')); | |
3698ae71 VZ |
789 | } |
790 | ||
791 | psz += 3; | |
ea8ce907 RR |
792 | len++; |
793 | } | |
794 | else | |
795 | { | |
796 | unsigned cnt; | |
797 | for (cnt = 0; cc > utf8_max[cnt]; cnt++) {} | |
798 | if (!cnt) | |
4def3b35 | 799 | { |
ea8ce907 RR |
800 | // plain ASCII char |
801 | if (buf) | |
802 | *buf++ = (char) cc; | |
803 | len++; | |
804 | } | |
805 | ||
806 | else | |
807 | { | |
808 | len += cnt + 1; | |
809 | if (buf) | |
810 | { | |
811 | *buf++ = (char) ((-128 >> cnt) | ((cc >> (cnt * 6)) & (0x3f >> cnt))); | |
812 | while (cnt--) | |
813 | *buf++ = (char) (0x80 | ((cc >> (cnt * 6)) & 0x3f)); | |
814 | } | |
4def3b35 VS |
815 | } |
816 | } | |
6001e347 | 817 | } |
4def3b35 | 818 | |
3698ae71 VZ |
819 | if (buf && (len<n)) |
820 | *buf = 0; | |
adb45366 | 821 | |
4def3b35 | 822 | return len; |
6001e347 RR |
823 | } |
824 | ||
c91830cb VZ |
825 | // ---------------------------------------------------------------------------- |
826 | // UTF-16 | |
827 | // ---------------------------------------------------------------------------- | |
828 | ||
829 | #ifdef WORDS_BIGENDIAN | |
bde4baac VZ |
830 | #define wxMBConvUTF16straight wxMBConvUTF16BE |
831 | #define wxMBConvUTF16swap wxMBConvUTF16LE | |
c91830cb | 832 | #else |
bde4baac VZ |
833 | #define wxMBConvUTF16swap wxMBConvUTF16BE |
834 | #define wxMBConvUTF16straight wxMBConvUTF16LE | |
c91830cb VZ |
835 | #endif |
836 | ||
837 | ||
c91830cb VZ |
838 | #ifdef WC_UTF16 |
839 | ||
c91830cb VZ |
840 | // copy 16bit MB to 16bit String |
841 | size_t wxMBConvUTF16straight::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
842 | { | |
843 | size_t len=0; | |
844 | ||
845 | while (*(wxUint16*)psz && (!buf || len < n)) | |
846 | { | |
847 | if (buf) | |
848 | *buf++ = *(wxUint16*)psz; | |
849 | len++; | |
850 | ||
851 | psz += sizeof(wxUint16); | |
852 | } | |
853 | if (buf && len<n) *buf=0; | |
854 | ||
855 | return len; | |
856 | } | |
857 | ||
858 | ||
859 | // copy 16bit String to 16bit MB | |
860 | size_t wxMBConvUTF16straight::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
861 | { | |
862 | size_t len=0; | |
863 | ||
864 | while (*psz && (!buf || len < n)) | |
865 | { | |
866 | if (buf) | |
867 | { | |
868 | *(wxUint16*)buf = *psz; | |
869 | buf += sizeof(wxUint16); | |
870 | } | |
871 | len += sizeof(wxUint16); | |
872 | psz++; | |
873 | } | |
874 | if (buf && len<=n-sizeof(wxUint16)) *(wxUint16*)buf=0; | |
875 | ||
876 | return len; | |
877 | } | |
878 | ||
879 | ||
880 | // swap 16bit MB to 16bit String | |
881 | size_t wxMBConvUTF16swap::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
882 | { | |
883 | size_t len=0; | |
884 | ||
885 | while (*(wxUint16*)psz && (!buf || len < n)) | |
886 | { | |
887 | if (buf) | |
888 | { | |
889 | ((char *)buf)[0] = psz[1]; | |
890 | ((char *)buf)[1] = psz[0]; | |
891 | buf++; | |
892 | } | |
893 | len++; | |
894 | psz += sizeof(wxUint16); | |
895 | } | |
896 | if (buf && len<n) *buf=0; | |
897 | ||
898 | return len; | |
899 | } | |
900 | ||
901 | ||
902 | // swap 16bit MB to 16bit String | |
903 | size_t wxMBConvUTF16swap::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
904 | { | |
905 | size_t len=0; | |
906 | ||
907 | while (*psz && (!buf || len < n)) | |
908 | { | |
909 | if (buf) | |
910 | { | |
911 | *buf++ = ((char*)psz)[1]; | |
912 | *buf++ = ((char*)psz)[0]; | |
913 | } | |
914 | len += sizeof(wxUint16); | |
915 | psz++; | |
916 | } | |
917 | if (buf && len<=n-sizeof(wxUint16)) *(wxUint16*)buf=0; | |
918 | ||
919 | return len; | |
920 | } | |
921 | ||
922 | ||
923 | #else // WC_UTF16 | |
924 | ||
925 | ||
926 | // copy 16bit MB to 32bit String | |
927 | size_t wxMBConvUTF16straight::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
928 | { | |
929 | size_t len=0; | |
930 | ||
931 | while (*(wxUint16*)psz && (!buf || len < n)) | |
932 | { | |
933 | wxUint32 cc; | |
934 | size_t pa=decode_utf16((wxUint16*)psz, cc); | |
935 | if (pa == (size_t)-1) | |
936 | return pa; | |
937 | ||
938 | if (buf) | |
939 | *buf++ = cc; | |
940 | len++; | |
941 | psz += pa * sizeof(wxUint16); | |
942 | } | |
943 | if (buf && len<n) *buf=0; | |
944 | ||
945 | return len; | |
946 | } | |
947 | ||
948 | ||
949 | // copy 32bit String to 16bit MB | |
950 | size_t wxMBConvUTF16straight::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
951 | { | |
952 | size_t len=0; | |
953 | ||
954 | while (*psz && (!buf || len < n)) | |
955 | { | |
956 | wxUint16 cc[2]; | |
957 | size_t pa=encode_utf16(*psz, cc); | |
958 | ||
959 | if (pa == (size_t)-1) | |
960 | return pa; | |
961 | ||
962 | if (buf) | |
963 | { | |
69b80d28 | 964 | *(wxUint16*)buf = cc[0]; |
b5153fd8 | 965 | buf += sizeof(wxUint16); |
c91830cb | 966 | if (pa > 1) |
69b80d28 VZ |
967 | { |
968 | *(wxUint16*)buf = cc[1]; | |
969 | buf += sizeof(wxUint16); | |
970 | } | |
c91830cb VZ |
971 | } |
972 | ||
973 | len += pa*sizeof(wxUint16); | |
974 | psz++; | |
975 | } | |
976 | if (buf && len<=n-sizeof(wxUint16)) *(wxUint16*)buf=0; | |
977 | ||
978 | return len; | |
979 | } | |
980 | ||
981 | ||
982 | // swap 16bit MB to 32bit String | |
983 | size_t wxMBConvUTF16swap::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
984 | { | |
985 | size_t len=0; | |
986 | ||
987 | while (*(wxUint16*)psz && (!buf || len < n)) | |
988 | { | |
989 | wxUint32 cc; | |
990 | char tmp[4]; | |
991 | tmp[0]=psz[1]; tmp[1]=psz[0]; | |
992 | tmp[2]=psz[3]; tmp[3]=psz[2]; | |
993 | ||
994 | size_t pa=decode_utf16((wxUint16*)tmp, cc); | |
995 | if (pa == (size_t)-1) | |
996 | return pa; | |
997 | ||
998 | if (buf) | |
999 | *buf++ = cc; | |
1000 | ||
1001 | len++; | |
1002 | psz += pa * sizeof(wxUint16); | |
1003 | } | |
1004 | if (buf && len<n) *buf=0; | |
1005 | ||
1006 | return len; | |
1007 | } | |
1008 | ||
1009 | ||
1010 | // swap 32bit String to 16bit MB | |
1011 | size_t wxMBConvUTF16swap::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
1012 | { | |
1013 | size_t len=0; | |
1014 | ||
1015 | while (*psz && (!buf || len < n)) | |
1016 | { | |
1017 | wxUint16 cc[2]; | |
1018 | size_t pa=encode_utf16(*psz, cc); | |
1019 | ||
1020 | if (pa == (size_t)-1) | |
1021 | return pa; | |
1022 | ||
1023 | if (buf) | |
1024 | { | |
1025 | *buf++ = ((char*)cc)[1]; | |
1026 | *buf++ = ((char*)cc)[0]; | |
1027 | if (pa > 1) | |
1028 | { | |
1029 | *buf++ = ((char*)cc)[3]; | |
1030 | *buf++ = ((char*)cc)[2]; | |
1031 | } | |
1032 | } | |
1033 | ||
1034 | len += pa*sizeof(wxUint16); | |
1035 | psz++; | |
1036 | } | |
1037 | if (buf && len<=n-sizeof(wxUint16)) *(wxUint16*)buf=0; | |
1038 | ||
1039 | return len; | |
1040 | } | |
1041 | ||
1042 | #endif // WC_UTF16 | |
1043 | ||
1044 | ||
1045 | // ---------------------------------------------------------------------------- | |
1046 | // UTF-32 | |
1047 | // ---------------------------------------------------------------------------- | |
1048 | ||
1049 | #ifdef WORDS_BIGENDIAN | |
1050 | #define wxMBConvUTF32straight wxMBConvUTF32BE | |
1051 | #define wxMBConvUTF32swap wxMBConvUTF32LE | |
1052 | #else | |
1053 | #define wxMBConvUTF32swap wxMBConvUTF32BE | |
1054 | #define wxMBConvUTF32straight wxMBConvUTF32LE | |
1055 | #endif | |
1056 | ||
1057 | ||
1058 | WXDLLIMPEXP_DATA_BASE(wxMBConvUTF32LE) wxConvUTF32LE; | |
1059 | WXDLLIMPEXP_DATA_BASE(wxMBConvUTF32BE) wxConvUTF32BE; | |
1060 | ||
1061 | ||
1062 | #ifdef WC_UTF16 | |
1063 | ||
1064 | // copy 32bit MB to 16bit String | |
1065 | size_t wxMBConvUTF32straight::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
1066 | { | |
1067 | size_t len=0; | |
1068 | ||
1069 | while (*(wxUint32*)psz && (!buf || len < n)) | |
1070 | { | |
1071 | wxUint16 cc[2]; | |
1072 | ||
1073 | size_t pa=encode_utf16(*(wxUint32*)psz, cc); | |
1074 | if (pa == (size_t)-1) | |
1075 | return pa; | |
1076 | ||
1077 | if (buf) | |
1078 | { | |
1079 | *buf++ = cc[0]; | |
1080 | if (pa > 1) | |
1081 | *buf++ = cc[1]; | |
1082 | } | |
1083 | len += pa; | |
1084 | psz += sizeof(wxUint32); | |
1085 | } | |
1086 | if (buf && len<n) *buf=0; | |
1087 | ||
1088 | return len; | |
1089 | } | |
1090 | ||
1091 | ||
1092 | // copy 16bit String to 32bit MB | |
1093 | size_t wxMBConvUTF32straight::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
1094 | { | |
1095 | size_t len=0; | |
1096 | ||
1097 | while (*psz && (!buf || len < n)) | |
1098 | { | |
1099 | wxUint32 cc; | |
1100 | ||
b5153fd8 VZ |
1101 | // cast is ok for WC_UTF16 |
1102 | size_t pa = decode_utf16((const wxUint16 *)psz, cc); | |
c91830cb VZ |
1103 | if (pa == (size_t)-1) |
1104 | return pa; | |
1105 | ||
1106 | if (buf) | |
1107 | { | |
1108 | *(wxUint32*)buf = cc; | |
1109 | buf += sizeof(wxUint32); | |
1110 | } | |
1111 | len += sizeof(wxUint32); | |
1112 | psz += pa; | |
1113 | } | |
b5153fd8 VZ |
1114 | |
1115 | if (buf && len<=n-sizeof(wxUint32)) | |
1116 | *(wxUint32*)buf=0; | |
c91830cb VZ |
1117 | |
1118 | return len; | |
1119 | } | |
1120 | ||
1121 | ||
1122 | ||
1123 | // swap 32bit MB to 16bit String | |
1124 | size_t wxMBConvUTF32swap::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
1125 | { | |
1126 | size_t len=0; | |
1127 | ||
1128 | while (*(wxUint32*)psz && (!buf || len < n)) | |
1129 | { | |
1130 | char tmp[4]; | |
1131 | tmp[0] = psz[3]; tmp[1] = psz[2]; | |
1132 | tmp[2] = psz[1]; tmp[3] = psz[0]; | |
1133 | ||
1134 | ||
1135 | wxUint16 cc[2]; | |
1136 | ||
1137 | size_t pa=encode_utf16(*(wxUint32*)tmp, cc); | |
1138 | if (pa == (size_t)-1) | |
1139 | return pa; | |
1140 | ||
1141 | if (buf) | |
1142 | { | |
1143 | *buf++ = cc[0]; | |
1144 | if (pa > 1) | |
1145 | *buf++ = cc[1]; | |
1146 | } | |
1147 | len += pa; | |
1148 | psz += sizeof(wxUint32); | |
1149 | } | |
b5153fd8 VZ |
1150 | |
1151 | if (buf && len<n) | |
1152 | *buf=0; | |
c91830cb VZ |
1153 | |
1154 | return len; | |
1155 | } | |
1156 | ||
1157 | ||
1158 | // swap 16bit String to 32bit MB | |
1159 | size_t wxMBConvUTF32swap::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
1160 | { | |
1161 | size_t len=0; | |
1162 | ||
1163 | while (*psz && (!buf || len < n)) | |
1164 | { | |
1165 | char cc[4]; | |
1166 | ||
b5153fd8 VZ |
1167 | // cast is ok for WC_UTF16 |
1168 | size_t pa=decode_utf16((const wxUint16 *)psz, *(wxUint32*)cc); | |
c91830cb VZ |
1169 | if (pa == (size_t)-1) |
1170 | return pa; | |
1171 | ||
1172 | if (buf) | |
1173 | { | |
1174 | *buf++ = cc[3]; | |
1175 | *buf++ = cc[2]; | |
1176 | *buf++ = cc[1]; | |
1177 | *buf++ = cc[0]; | |
1178 | } | |
1179 | len += sizeof(wxUint32); | |
1180 | psz += pa; | |
1181 | } | |
b5153fd8 VZ |
1182 | |
1183 | if (buf && len<=n-sizeof(wxUint32)) | |
1184 | *(wxUint32*)buf=0; | |
c91830cb VZ |
1185 | |
1186 | return len; | |
1187 | } | |
1188 | ||
1189 | #else // WC_UTF16 | |
1190 | ||
1191 | ||
1192 | // copy 32bit MB to 32bit String | |
1193 | size_t wxMBConvUTF32straight::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
1194 | { | |
1195 | size_t len=0; | |
1196 | ||
1197 | while (*(wxUint32*)psz && (!buf || len < n)) | |
1198 | { | |
1199 | if (buf) | |
1200 | *buf++ = *(wxUint32*)psz; | |
1201 | len++; | |
1202 | psz += sizeof(wxUint32); | |
1203 | } | |
b5153fd8 VZ |
1204 | |
1205 | if (buf && len<n) | |
1206 | *buf=0; | |
c91830cb VZ |
1207 | |
1208 | return len; | |
1209 | } | |
1210 | ||
1211 | ||
1212 | // copy 32bit String to 32bit MB | |
1213 | size_t wxMBConvUTF32straight::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
1214 | { | |
1215 | size_t len=0; | |
1216 | ||
1217 | while (*psz && (!buf || len < n)) | |
1218 | { | |
1219 | if (buf) | |
1220 | { | |
1221 | *(wxUint32*)buf = *psz; | |
1222 | buf += sizeof(wxUint32); | |
1223 | } | |
1224 | ||
1225 | len += sizeof(wxUint32); | |
1226 | psz++; | |
1227 | } | |
1228 | ||
b5153fd8 VZ |
1229 | if (buf && len<=n-sizeof(wxUint32)) |
1230 | *(wxUint32*)buf=0; | |
c91830cb VZ |
1231 | |
1232 | return len; | |
1233 | } | |
1234 | ||
1235 | ||
1236 | // swap 32bit MB to 32bit String | |
1237 | size_t wxMBConvUTF32swap::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
1238 | { | |
1239 | size_t len=0; | |
1240 | ||
1241 | while (*(wxUint32*)psz && (!buf || len < n)) | |
1242 | { | |
1243 | if (buf) | |
1244 | { | |
1245 | ((char *)buf)[0] = psz[3]; | |
1246 | ((char *)buf)[1] = psz[2]; | |
1247 | ((char *)buf)[2] = psz[1]; | |
1248 | ((char *)buf)[3] = psz[0]; | |
1249 | buf++; | |
1250 | } | |
1251 | len++; | |
1252 | psz += sizeof(wxUint32); | |
1253 | } | |
b5153fd8 VZ |
1254 | |
1255 | if (buf && len<n) | |
1256 | *buf=0; | |
c91830cb VZ |
1257 | |
1258 | return len; | |
1259 | } | |
1260 | ||
1261 | ||
1262 | // swap 32bit String to 32bit MB | |
1263 | size_t wxMBConvUTF32swap::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
1264 | { | |
1265 | size_t len=0; | |
1266 | ||
1267 | while (*psz && (!buf || len < n)) | |
1268 | { | |
1269 | if (buf) | |
1270 | { | |
1271 | *buf++ = ((char *)psz)[3]; | |
1272 | *buf++ = ((char *)psz)[2]; | |
1273 | *buf++ = ((char *)psz)[1]; | |
1274 | *buf++ = ((char *)psz)[0]; | |
1275 | } | |
1276 | len += sizeof(wxUint32); | |
1277 | psz++; | |
1278 | } | |
b5153fd8 VZ |
1279 | |
1280 | if (buf && len<=n-sizeof(wxUint32)) | |
1281 | *(wxUint32*)buf=0; | |
c91830cb VZ |
1282 | |
1283 | return len; | |
1284 | } | |
1285 | ||
1286 | ||
1287 | #endif // WC_UTF16 | |
1288 | ||
1289 | ||
36acb880 VZ |
1290 | // ============================================================================ |
1291 | // The classes doing conversion using the iconv_xxx() functions | |
1292 | // ============================================================================ | |
3caec1bb | 1293 | |
b040e242 | 1294 | #ifdef HAVE_ICONV |
3a0d76bc | 1295 | |
b1d547eb VS |
1296 | // VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with |
1297 | // E2BIG if output buffer is _exactly_ as big as needed. Such case is | |
1298 | // (unless there's yet another bug in glibc) the only case when iconv() | |
1299 | // returns with (size_t)-1 (which means error) and says there are 0 bytes | |
1300 | // left in the input buffer -- when _real_ error occurs, | |
1301 | // bytes-left-in-input buffer is non-zero. Hence, this alternative test for | |
1302 | // iconv() failure. | |
3caec1bb VS |
1303 | // [This bug does not appear in glibc 2.2.] |
1304 | #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 1 | |
1305 | #define ICONV_FAILED(cres, bufLeft) ((cres == (size_t)-1) && \ | |
1306 | (errno != E2BIG || bufLeft != 0)) | |
1307 | #else | |
1308 | #define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1) | |
1309 | #endif | |
1310 | ||
ab217dba | 1311 | #define ICONV_CHAR_CAST(x) ((ICONV_CONST char **)(x)) |
36acb880 VZ |
1312 | |
1313 | // ---------------------------------------------------------------------------- | |
e95354ec | 1314 | // wxMBConv_iconv: encapsulates an iconv character set |
36acb880 VZ |
1315 | // ---------------------------------------------------------------------------- |
1316 | ||
e95354ec | 1317 | class wxMBConv_iconv : public wxMBConv |
1cd52418 OK |
1318 | { |
1319 | public: | |
e95354ec VZ |
1320 | wxMBConv_iconv(const wxChar *name); |
1321 | virtual ~wxMBConv_iconv(); | |
36acb880 | 1322 | |
bde4baac VZ |
1323 | virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) const; |
1324 | virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) const; | |
36acb880 | 1325 | |
e95354ec | 1326 | bool IsOk() const |
36acb880 VZ |
1327 | { return (m2w != (iconv_t)-1) && (w2m != (iconv_t)-1); } |
1328 | ||
1329 | protected: | |
1330 | // the iconv handlers used to translate from multibyte to wide char and in | |
1331 | // the other direction | |
1332 | iconv_t m2w, | |
1333 | w2m; | |
b1d547eb VS |
1334 | #if wxUSE_THREADS |
1335 | // guards access to m2w and w2m objects | |
1336 | wxMutex m_iconvMutex; | |
1337 | #endif | |
36acb880 VZ |
1338 | |
1339 | private: | |
e95354ec | 1340 | // the name (for iconv_open()) of a wide char charset -- if none is |
36acb880 VZ |
1341 | // available on this machine, it will remain NULL |
1342 | static const char *ms_wcCharsetName; | |
1343 | ||
1344 | // true if the wide char encoding we use (i.e. ms_wcCharsetName) has | |
1345 | // different endian-ness than the native one | |
405d8f46 | 1346 | static bool ms_wcNeedsSwap; |
36acb880 VZ |
1347 | }; |
1348 | ||
8f115891 MW |
1349 | // make the constructor available for unit testing |
1350 | WXDLLIMPEXP_BASE wxMBConv* new_wxMBConv_iconv( const wxChar* name ) | |
1351 | { | |
1352 | wxMBConv_iconv* result = new wxMBConv_iconv( name ); | |
1353 | if ( !result->IsOk() ) | |
1354 | { | |
1355 | delete result; | |
1356 | return 0; | |
1357 | } | |
1358 | return result; | |
1359 | } | |
1360 | ||
e95354ec VZ |
1361 | const char *wxMBConv_iconv::ms_wcCharsetName = NULL; |
1362 | bool wxMBConv_iconv::ms_wcNeedsSwap = false; | |
36acb880 | 1363 | |
e95354ec | 1364 | wxMBConv_iconv::wxMBConv_iconv(const wxChar *name) |
36acb880 | 1365 | { |
0331b385 VZ |
1366 | // iconv operates with chars, not wxChars, but luckily it uses only ASCII |
1367 | // names for the charsets | |
200a9923 | 1368 | const wxCharBuffer cname(wxString(name).ToAscii()); |
04c79127 | 1369 | |
36acb880 VZ |
1370 | // check for charset that represents wchar_t: |
1371 | if (ms_wcCharsetName == NULL) | |
f1339c56 | 1372 | { |
e95354ec | 1373 | ms_wcNeedsSwap = false; |
dccce9ea | 1374 | |
36acb880 VZ |
1375 | // try charset with explicit bytesex info (e.g. "UCS-4LE"): |
1376 | ms_wcCharsetName = WC_NAME_BEST; | |
04c79127 | 1377 | m2w = iconv_open(ms_wcCharsetName, cname); |
3a0d76bc | 1378 | |
36acb880 VZ |
1379 | if (m2w == (iconv_t)-1) |
1380 | { | |
1381 | // try charset w/o bytesex info (e.g. "UCS4") | |
1382 | // and check for bytesex ourselves: | |
1383 | ms_wcCharsetName = WC_NAME; | |
04c79127 | 1384 | m2w = iconv_open(ms_wcCharsetName, cname); |
36acb880 VZ |
1385 | |
1386 | // last bet, try if it knows WCHAR_T pseudo-charset | |
3a0d76bc VS |
1387 | if (m2w == (iconv_t)-1) |
1388 | { | |
36acb880 | 1389 | ms_wcCharsetName = "WCHAR_T"; |
04c79127 | 1390 | m2w = iconv_open(ms_wcCharsetName, cname); |
36acb880 | 1391 | } |
3a0d76bc | 1392 | |
36acb880 VZ |
1393 | if (m2w != (iconv_t)-1) |
1394 | { | |
1395 | char buf[2], *bufPtr; | |
1396 | wchar_t wbuf[2], *wbufPtr; | |
1397 | size_t insz, outsz; | |
1398 | size_t res; | |
1399 | ||
1400 | buf[0] = 'A'; | |
1401 | buf[1] = 0; | |
1402 | wbuf[0] = 0; | |
1403 | insz = 2; | |
1404 | outsz = SIZEOF_WCHAR_T * 2; | |
1405 | wbufPtr = wbuf; | |
1406 | bufPtr = buf; | |
1407 | ||
1408 | res = iconv(m2w, ICONV_CHAR_CAST(&bufPtr), &insz, | |
1409 | (char**)&wbufPtr, &outsz); | |
1410 | ||
1411 | if (ICONV_FAILED(res, insz)) | |
3a0d76bc | 1412 | { |
36acb880 VZ |
1413 | ms_wcCharsetName = NULL; |
1414 | wxLogLastError(wxT("iconv")); | |
2b5f62a0 | 1415 | wxLogError(_("Conversion to charset '%s' doesn't work."), name); |
3a0d76bc VS |
1416 | } |
1417 | else | |
1418 | { | |
36acb880 | 1419 | ms_wcNeedsSwap = wbuf[0] != (wchar_t)buf[0]; |
3a0d76bc VS |
1420 | } |
1421 | } | |
36acb880 VZ |
1422 | else |
1423 | { | |
1424 | ms_wcCharsetName = NULL; | |
373658eb | 1425 | |
77ffb593 | 1426 | // VS: we must not output an error here, since wxWidgets will safely |
957686c8 VS |
1427 | // fall back to using wxEncodingConverter. |
1428 | wxLogTrace(wxT("strconv"), wxT("Impossible to convert to/from charset '%s' with iconv, falling back to wxEncodingConverter."), name); | |
1429 | //wxLogError( | |
36acb880 | 1430 | } |
3a0d76bc | 1431 | } |
36acb880 | 1432 | wxLogTrace(wxT("strconv"), wxT("wchar_t charset is '%s', needs swap: %i"), ms_wcCharsetName, ms_wcNeedsSwap); |
3a0d76bc | 1433 | } |
36acb880 | 1434 | else // we already have ms_wcCharsetName |
3caec1bb | 1435 | { |
04c79127 | 1436 | m2w = iconv_open(ms_wcCharsetName, cname); |
f1339c56 | 1437 | } |
dccce9ea | 1438 | |
36acb880 VZ |
1439 | // NB: don't ever pass NULL to iconv_open(), it may crash! |
1440 | if ( ms_wcCharsetName ) | |
f1339c56 | 1441 | { |
04c79127 | 1442 | w2m = iconv_open( cname, ms_wcCharsetName); |
36acb880 | 1443 | } |
405d8f46 VZ |
1444 | else |
1445 | { | |
1446 | w2m = (iconv_t)-1; | |
1447 | } | |
36acb880 | 1448 | } |
3caec1bb | 1449 | |
e95354ec | 1450 | wxMBConv_iconv::~wxMBConv_iconv() |
36acb880 VZ |
1451 | { |
1452 | if ( m2w != (iconv_t)-1 ) | |
1453 | iconv_close(m2w); | |
1454 | if ( w2m != (iconv_t)-1 ) | |
1455 | iconv_close(w2m); | |
1456 | } | |
3a0d76bc | 1457 | |
bde4baac | 1458 | size_t wxMBConv_iconv::MB2WC(wchar_t *buf, const char *psz, size_t n) const |
36acb880 | 1459 | { |
b1d547eb VS |
1460 | #if wxUSE_THREADS |
1461 | // NB: iconv() is MT-safe, but each thread must use it's own iconv_t handle. | |
1462 | // Unfortunately there is a couple of global wxCSConv objects such as | |
1463 | // wxConvLocal that are used all over wx code, so we have to make sure | |
1464 | // the handle is used by at most one thread at the time. Otherwise | |
1465 | // only a few wx classes would be safe to use from non-main threads | |
1466 | // as MB<->WC conversion would fail "randomly". | |
1467 | wxMutexLocker lock(wxConstCast(this, wxMBConv_iconv)->m_iconvMutex); | |
1468 | #endif | |
3698ae71 | 1469 | |
36acb880 VZ |
1470 | size_t inbuf = strlen(psz); |
1471 | size_t outbuf = n * SIZEOF_WCHAR_T; | |
1472 | size_t res, cres; | |
1473 | // VS: Use these instead of psz, buf because iconv() modifies its arguments: | |
1474 | wchar_t *bufPtr = buf; | |
1475 | const char *pszPtr = psz; | |
1476 | ||
1477 | if (buf) | |
1478 | { | |
1479 | // have destination buffer, convert there | |
1480 | cres = iconv(m2w, | |
1481 | ICONV_CHAR_CAST(&pszPtr), &inbuf, | |
1482 | (char**)&bufPtr, &outbuf); | |
1483 | res = n - (outbuf / SIZEOF_WCHAR_T); | |
dccce9ea | 1484 | |
36acb880 | 1485 | if (ms_wcNeedsSwap) |
3a0d76bc | 1486 | { |
36acb880 VZ |
1487 | // convert to native endianness |
1488 | WC_BSWAP(buf /* _not_ bufPtr */, res) | |
3a0d76bc | 1489 | } |
adb45366 | 1490 | |
49dd9820 VS |
1491 | // NB: iconv was given only strlen(psz) characters on input, and so |
1492 | // it couldn't convert the trailing zero. Let's do it ourselves | |
1493 | // if there's some room left for it in the output buffer. | |
1494 | if (res < n) | |
1495 | buf[res] = 0; | |
36acb880 VZ |
1496 | } |
1497 | else | |
1498 | { | |
1499 | // no destination buffer... convert using temp buffer | |
1500 | // to calculate destination buffer requirement | |
1501 | wchar_t tbuf[8]; | |
1502 | res = 0; | |
1503 | do { | |
1504 | bufPtr = tbuf; | |
1505 | outbuf = 8*SIZEOF_WCHAR_T; | |
1506 | ||
1507 | cres = iconv(m2w, | |
1508 | ICONV_CHAR_CAST(&pszPtr), &inbuf, | |
1509 | (char**)&bufPtr, &outbuf ); | |
1510 | ||
1511 | res += 8-(outbuf/SIZEOF_WCHAR_T); | |
1512 | } while ((cres==(size_t)-1) && (errno==E2BIG)); | |
f1339c56 | 1513 | } |
dccce9ea | 1514 | |
36acb880 | 1515 | if (ICONV_FAILED(cres, inbuf)) |
f1339c56 | 1516 | { |
36acb880 VZ |
1517 | //VS: it is ok if iconv fails, hence trace only |
1518 | wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode())); | |
1519 | return (size_t)-1; | |
1520 | } | |
1521 | ||
1522 | return res; | |
1523 | } | |
1524 | ||
bde4baac | 1525 | size_t wxMBConv_iconv::WC2MB(char *buf, const wchar_t *psz, size_t n) const |
36acb880 | 1526 | { |
b1d547eb VS |
1527 | #if wxUSE_THREADS |
1528 | // NB: explained in MB2WC | |
1529 | wxMutexLocker lock(wxConstCast(this, wxMBConv_iconv)->m_iconvMutex); | |
1530 | #endif | |
3698ae71 | 1531 | |
f8d791e0 | 1532 | size_t inbuf = wxWcslen(psz) * SIZEOF_WCHAR_T; |
36acb880 VZ |
1533 | size_t outbuf = n; |
1534 | size_t res, cres; | |
3a0d76bc | 1535 | |
36acb880 | 1536 | wchar_t *tmpbuf = 0; |
3caec1bb | 1537 | |
36acb880 VZ |
1538 | if (ms_wcNeedsSwap) |
1539 | { | |
1540 | // need to copy to temp buffer to switch endianness | |
1541 | // this absolutely doesn't rock! | |
1542 | // (no, doing WC_BSWAP twice on the original buffer won't help, as it | |
1543 | // could be in read-only memory, or be accessed in some other thread) | |
1544 | tmpbuf=(wchar_t*)malloc((inbuf+1)*SIZEOF_WCHAR_T); | |
1545 | memcpy(tmpbuf,psz,(inbuf+1)*SIZEOF_WCHAR_T); | |
1546 | WC_BSWAP(tmpbuf, inbuf) | |
1547 | psz=tmpbuf; | |
1548 | } | |
3a0d76bc | 1549 | |
36acb880 VZ |
1550 | if (buf) |
1551 | { | |
1552 | // have destination buffer, convert there | |
1553 | cres = iconv( w2m, ICONV_CHAR_CAST(&psz), &inbuf, &buf, &outbuf ); | |
3a0d76bc | 1554 | |
36acb880 | 1555 | res = n-outbuf; |
adb45366 | 1556 | |
49dd9820 VS |
1557 | // NB: iconv was given only wcslen(psz) characters on input, and so |
1558 | // it couldn't convert the trailing zero. Let's do it ourselves | |
1559 | // if there's some room left for it in the output buffer. | |
1560 | if (res < n) | |
1561 | buf[0] = 0; | |
36acb880 VZ |
1562 | } |
1563 | else | |
1564 | { | |
1565 | // no destination buffer... convert using temp buffer | |
1566 | // to calculate destination buffer requirement | |
1567 | char tbuf[16]; | |
1568 | res = 0; | |
1569 | do { | |
1570 | buf = tbuf; outbuf = 16; | |
1571 | ||
1572 | cres = iconv( w2m, ICONV_CHAR_CAST(&psz), &inbuf, &buf, &outbuf ); | |
dccce9ea | 1573 | |
36acb880 VZ |
1574 | res += 16 - outbuf; |
1575 | } while ((cres==(size_t)-1) && (errno==E2BIG)); | |
f1339c56 | 1576 | } |
dccce9ea | 1577 | |
36acb880 VZ |
1578 | if (ms_wcNeedsSwap) |
1579 | { | |
1580 | free(tmpbuf); | |
1581 | } | |
dccce9ea | 1582 | |
36acb880 VZ |
1583 | if (ICONV_FAILED(cres, inbuf)) |
1584 | { | |
1585 | //VS: it is ok if iconv fails, hence trace only | |
1586 | wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode())); | |
1587 | return (size_t)-1; | |
1588 | } | |
1589 | ||
1590 | return res; | |
1591 | } | |
1592 | ||
b040e242 | 1593 | #endif // HAVE_ICONV |
36acb880 | 1594 | |
e95354ec | 1595 | |
36acb880 VZ |
1596 | // ============================================================================ |
1597 | // Win32 conversion classes | |
1598 | // ============================================================================ | |
1cd52418 | 1599 | |
e95354ec | 1600 | #ifdef wxHAVE_WIN32_MB2WC |
373658eb | 1601 | |
8b04d4c4 | 1602 | // from utils.cpp |
d775fa82 | 1603 | #if wxUSE_FONTMAP |
8b04d4c4 VZ |
1604 | extern WXDLLIMPEXP_BASE long wxCharsetToCodepage(const wxChar *charset); |
1605 | extern WXDLLIMPEXP_BASE long wxEncodingToCodepage(wxFontEncoding encoding); | |
7608a683 | 1606 | #endif |
373658eb | 1607 | |
e95354ec | 1608 | class wxMBConv_win32 : public wxMBConv |
1cd52418 OK |
1609 | { |
1610 | public: | |
bde4baac VZ |
1611 | wxMBConv_win32() |
1612 | { | |
1613 | m_CodePage = CP_ACP; | |
1614 | } | |
1615 | ||
7608a683 | 1616 | #if wxUSE_FONTMAP |
e95354ec | 1617 | wxMBConv_win32(const wxChar* name) |
bde4baac VZ |
1618 | { |
1619 | m_CodePage = wxCharsetToCodepage(name); | |
1620 | } | |
dccce9ea | 1621 | |
e95354ec | 1622 | wxMBConv_win32(wxFontEncoding encoding) |
bde4baac VZ |
1623 | { |
1624 | m_CodePage = wxEncodingToCodepage(encoding); | |
1625 | } | |
7608a683 | 1626 | #endif |
8b04d4c4 | 1627 | |
bde4baac | 1628 | size_t MB2WC(wchar_t *buf, const char *psz, size_t n) const |
f1339c56 | 1629 | { |
02272c9c VZ |
1630 | // note that we have to use MB_ERR_INVALID_CHARS flag as it without it |
1631 | // the behaviour is not compatible with the Unix version (using iconv) | |
1632 | // and break the library itself, e.g. wxTextInputStream::NextChar() | |
1633 | // wouldn't work if reading an incomplete MB char didn't result in an | |
1634 | // error | |
667e5b3e VZ |
1635 | // |
1636 | // note however that using MB_ERR_INVALID_CHARS with CP_UTF7 results in | |
1637 | // an error (tested under Windows Server 2003) and apparently it is | |
1638 | // done on purpose, i.e. the function accepts any input in this case | |
1639 | // and although I'd prefer to return error on ill-formed output, our | |
1640 | // own wxMBConvUTF7 doesn't detect errors (e.g. lone "+" which is | |
1641 | // explicitly ill-formed according to RFC 2152) neither so we don't | |
1642 | // even have any fallback here... | |
1643 | int flags = m_CodePage == CP_UTF7 ? 0 : MB_ERR_INVALID_CHARS; | |
1644 | ||
2b5f62a0 VZ |
1645 | const size_t len = ::MultiByteToWideChar |
1646 | ( | |
1647 | m_CodePage, // code page | |
667e5b3e | 1648 | flags, // flags: fall on error |
2b5f62a0 VZ |
1649 | psz, // input string |
1650 | -1, // its length (NUL-terminated) | |
b4da152e | 1651 | buf, // output string |
2b5f62a0 VZ |
1652 | buf ? n : 0 // size of output buffer |
1653 | ); | |
1654 | ||
03a991bc VZ |
1655 | // note that it returns count of written chars for buf != NULL and size |
1656 | // of the needed buffer for buf == NULL so in either case the length of | |
1657 | // the string (which never includes the terminating NUL) is one less | |
1658 | return len ? len - 1 : (size_t)-1; | |
f1339c56 | 1659 | } |
dccce9ea | 1660 | |
13dd924a | 1661 | size_t WC2MB(char *buf, const wchar_t *pwz, size_t n) const |
f1339c56 | 1662 | { |
13dd924a VZ |
1663 | /* |
1664 | we have a problem here: by default, WideCharToMultiByte() may | |
1665 | replace characters unrepresentable in the target code page with bad | |
1666 | quality approximations such as turning "1/2" symbol (U+00BD) into | |
1667 | "1" for the code pages which don't have it and we, obviously, want | |
1668 | to avoid this at any price | |
d775fa82 | 1669 | |
13dd924a VZ |
1670 | the trouble is that this function does it _silently_, i.e. it won't |
1671 | even tell us whether it did or not... Win98/2000 and higher provide | |
1672 | WC_NO_BEST_FIT_CHARS but it doesn't work for the older systems and | |
1673 | we have to resort to a round trip, i.e. check that converting back | |
1674 | results in the same string -- this is, of course, expensive but | |
1675 | otherwise we simply can't be sure to not garble the data. | |
1676 | */ | |
1677 | ||
1678 | // determine if we can rely on WC_NO_BEST_FIT_CHARS: according to MSDN | |
1679 | // it doesn't work with CJK encodings (which we test for rather roughly | |
1680 | // here...) nor with UTF-7/8 nor, of course, with Windows versions not | |
1681 | // supporting it | |
907173e5 WS |
1682 | BOOL usedDef wxDUMMY_INITIALIZE(false); |
1683 | BOOL *pUsedDef; | |
13dd924a VZ |
1684 | int flags; |
1685 | if ( CanUseNoBestFit() && m_CodePage < 50000 ) | |
1686 | { | |
1687 | // it's our lucky day | |
1688 | flags = WC_NO_BEST_FIT_CHARS; | |
1689 | pUsedDef = &usedDef; | |
1690 | } | |
1691 | else // old system or unsupported encoding | |
1692 | { | |
1693 | flags = 0; | |
1694 | pUsedDef = NULL; | |
1695 | } | |
1696 | ||
2b5f62a0 VZ |
1697 | const size_t len = ::WideCharToMultiByte |
1698 | ( | |
1699 | m_CodePage, // code page | |
13dd924a VZ |
1700 | flags, // either none or no best fit |
1701 | pwz, // input string | |
2b5f62a0 VZ |
1702 | -1, // it is (wide) NUL-terminated |
1703 | buf, // output buffer | |
1704 | buf ? n : 0, // and its size | |
1705 | NULL, // default "replacement" char | |
13dd924a | 1706 | pUsedDef // [out] was it used? |
2b5f62a0 VZ |
1707 | ); |
1708 | ||
13dd924a VZ |
1709 | if ( !len ) |
1710 | { | |
1711 | // function totally failed | |
1712 | return (size_t)-1; | |
1713 | } | |
1714 | ||
1715 | // if we were really converting, check if we succeeded | |
1716 | if ( buf ) | |
1717 | { | |
1718 | if ( flags ) | |
1719 | { | |
1720 | // check if the conversion failed, i.e. if any replacements | |
1721 | // were done | |
1722 | if ( usedDef ) | |
1723 | return (size_t)-1; | |
1724 | } | |
1725 | else // we must resort to double tripping... | |
1726 | { | |
1727 | wxWCharBuffer wcBuf(n); | |
1728 | if ( MB2WC(wcBuf.data(), buf, n) == (size_t)-1 || | |
1729 | wcscmp(wcBuf, pwz) != 0 ) | |
1730 | { | |
1731 | // we didn't obtain the same thing we started from, hence | |
1732 | // the conversion was lossy and we consider that it failed | |
1733 | return (size_t)-1; | |
1734 | } | |
1735 | } | |
1736 | } | |
1737 | ||
03a991bc | 1738 | // see the comment above for the reason of "len - 1" |
13dd924a | 1739 | return len - 1; |
f1339c56 | 1740 | } |
dccce9ea | 1741 | |
13dd924a VZ |
1742 | bool IsOk() const { return m_CodePage != -1; } |
1743 | ||
1744 | private: | |
1745 | static bool CanUseNoBestFit() | |
1746 | { | |
1747 | static int s_isWin98Or2k = -1; | |
1748 | ||
1749 | if ( s_isWin98Or2k == -1 ) | |
1750 | { | |
1751 | int verMaj, verMin; | |
1752 | switch ( wxGetOsVersion(&verMaj, &verMin) ) | |
1753 | { | |
1754 | case wxWIN95: | |
1755 | s_isWin98Or2k = verMaj >= 4 && verMin >= 10; | |
1756 | break; | |
1757 | ||
1758 | case wxWINDOWS_NT: | |
1759 | s_isWin98Or2k = verMaj >= 5; | |
1760 | break; | |
1761 | ||
1762 | default: | |
1763 | // unknown, be conseravtive by default | |
1764 | s_isWin98Or2k = 0; | |
1765 | } | |
1766 | ||
1767 | wxASSERT_MSG( s_isWin98Or2k != -1, _T("should be set above") ); | |
1768 | } | |
1769 | ||
1770 | return s_isWin98Or2k == 1; | |
1771 | } | |
f1339c56 | 1772 | |
b1d66b54 | 1773 | long m_CodePage; |
1cd52418 | 1774 | }; |
e95354ec VZ |
1775 | |
1776 | #endif // wxHAVE_WIN32_MB2WC | |
1777 | ||
f7e98dee RN |
1778 | // ============================================================================ |
1779 | // Cocoa conversion classes | |
1780 | // ============================================================================ | |
1781 | ||
1782 | #if defined(__WXCOCOA__) | |
1783 | ||
ecd9653b | 1784 | // RN: There is no UTF-32 support in either Core Foundation or |
f7e98dee RN |
1785 | // Cocoa. Strangely enough, internally Core Foundation uses |
1786 | // UTF 32 internally quite a bit - its just not public (yet). | |
1787 | ||
1788 | #include <CoreFoundation/CFString.h> | |
1789 | #include <CoreFoundation/CFStringEncodingExt.h> | |
1790 | ||
1791 | CFStringEncoding wxCFStringEncFromFontEnc(wxFontEncoding encoding) | |
ecd9653b | 1792 | { |
638357a0 | 1793 | CFStringEncoding enc = kCFStringEncodingInvalidId ; |
ecd9653b WS |
1794 | if ( encoding == wxFONTENCODING_DEFAULT ) |
1795 | { | |
638357a0 | 1796 | enc = CFStringGetSystemEncoding(); |
ecd9653b WS |
1797 | } |
1798 | else switch( encoding) | |
1799 | { | |
1800 | case wxFONTENCODING_ISO8859_1 : | |
1801 | enc = kCFStringEncodingISOLatin1 ; | |
1802 | break ; | |
1803 | case wxFONTENCODING_ISO8859_2 : | |
1804 | enc = kCFStringEncodingISOLatin2; | |
1805 | break ; | |
1806 | case wxFONTENCODING_ISO8859_3 : | |
1807 | enc = kCFStringEncodingISOLatin3 ; | |
1808 | break ; | |
1809 | case wxFONTENCODING_ISO8859_4 : | |
1810 | enc = kCFStringEncodingISOLatin4; | |
1811 | break ; | |
1812 | case wxFONTENCODING_ISO8859_5 : | |
1813 | enc = kCFStringEncodingISOLatinCyrillic; | |
1814 | break ; | |
1815 | case wxFONTENCODING_ISO8859_6 : | |
1816 | enc = kCFStringEncodingISOLatinArabic; | |
1817 | break ; | |
1818 | case wxFONTENCODING_ISO8859_7 : | |
1819 | enc = kCFStringEncodingISOLatinGreek; | |
1820 | break ; | |
1821 | case wxFONTENCODING_ISO8859_8 : | |
1822 | enc = kCFStringEncodingISOLatinHebrew; | |
1823 | break ; | |
1824 | case wxFONTENCODING_ISO8859_9 : | |
1825 | enc = kCFStringEncodingISOLatin5; | |
1826 | break ; | |
1827 | case wxFONTENCODING_ISO8859_10 : | |
1828 | enc = kCFStringEncodingISOLatin6; | |
1829 | break ; | |
1830 | case wxFONTENCODING_ISO8859_11 : | |
1831 | enc = kCFStringEncodingISOLatinThai; | |
1832 | break ; | |
1833 | case wxFONTENCODING_ISO8859_13 : | |
1834 | enc = kCFStringEncodingISOLatin7; | |
1835 | break ; | |
1836 | case wxFONTENCODING_ISO8859_14 : | |
1837 | enc = kCFStringEncodingISOLatin8; | |
1838 | break ; | |
1839 | case wxFONTENCODING_ISO8859_15 : | |
1840 | enc = kCFStringEncodingISOLatin9; | |
1841 | break ; | |
1842 | ||
1843 | case wxFONTENCODING_KOI8 : | |
1844 | enc = kCFStringEncodingKOI8_R; | |
1845 | break ; | |
1846 | case wxFONTENCODING_ALTERNATIVE : // MS-DOS CP866 | |
1847 | enc = kCFStringEncodingDOSRussian; | |
1848 | break ; | |
1849 | ||
1850 | // case wxFONTENCODING_BULGARIAN : | |
1851 | // enc = ; | |
1852 | // break ; | |
1853 | ||
1854 | case wxFONTENCODING_CP437 : | |
1855 | enc =kCFStringEncodingDOSLatinUS ; | |
1856 | break ; | |
1857 | case wxFONTENCODING_CP850 : | |
1858 | enc = kCFStringEncodingDOSLatin1; | |
1859 | break ; | |
1860 | case wxFONTENCODING_CP852 : | |
1861 | enc = kCFStringEncodingDOSLatin2; | |
1862 | break ; | |
1863 | case wxFONTENCODING_CP855 : | |
1864 | enc = kCFStringEncodingDOSCyrillic; | |
1865 | break ; | |
1866 | case wxFONTENCODING_CP866 : | |
1867 | enc =kCFStringEncodingDOSRussian ; | |
1868 | break ; | |
1869 | case wxFONTENCODING_CP874 : | |
1870 | enc = kCFStringEncodingDOSThai; | |
1871 | break ; | |
1872 | case wxFONTENCODING_CP932 : | |
1873 | enc = kCFStringEncodingDOSJapanese; | |
1874 | break ; | |
1875 | case wxFONTENCODING_CP936 : | |
1876 | enc =kCFStringEncodingDOSChineseSimplif ; | |
1877 | break ; | |
1878 | case wxFONTENCODING_CP949 : | |
1879 | enc = kCFStringEncodingDOSKorean; | |
1880 | break ; | |
1881 | case wxFONTENCODING_CP950 : | |
1882 | enc = kCFStringEncodingDOSChineseTrad; | |
1883 | break ; | |
ecd9653b WS |
1884 | case wxFONTENCODING_CP1250 : |
1885 | enc = kCFStringEncodingWindowsLatin2; | |
1886 | break ; | |
1887 | case wxFONTENCODING_CP1251 : | |
1888 | enc =kCFStringEncodingWindowsCyrillic ; | |
1889 | break ; | |
1890 | case wxFONTENCODING_CP1252 : | |
1891 | enc =kCFStringEncodingWindowsLatin1 ; | |
1892 | break ; | |
1893 | case wxFONTENCODING_CP1253 : | |
1894 | enc = kCFStringEncodingWindowsGreek; | |
1895 | break ; | |
1896 | case wxFONTENCODING_CP1254 : | |
1897 | enc = kCFStringEncodingWindowsLatin5; | |
1898 | break ; | |
1899 | case wxFONTENCODING_CP1255 : | |
1900 | enc =kCFStringEncodingWindowsHebrew ; | |
1901 | break ; | |
1902 | case wxFONTENCODING_CP1256 : | |
1903 | enc =kCFStringEncodingWindowsArabic ; | |
1904 | break ; | |
1905 | case wxFONTENCODING_CP1257 : | |
1906 | enc = kCFStringEncodingWindowsBalticRim; | |
1907 | break ; | |
638357a0 RN |
1908 | // This only really encodes to UTF7 (if that) evidently |
1909 | // case wxFONTENCODING_UTF7 : | |
1910 | // enc = kCFStringEncodingNonLossyASCII ; | |
1911 | // break ; | |
ecd9653b WS |
1912 | case wxFONTENCODING_UTF8 : |
1913 | enc = kCFStringEncodingUTF8 ; | |
1914 | break ; | |
1915 | case wxFONTENCODING_EUC_JP : | |
1916 | enc = kCFStringEncodingEUC_JP; | |
1917 | break ; | |
1918 | case wxFONTENCODING_UTF16 : | |
f7e98dee | 1919 | enc = kCFStringEncodingUnicode ; |
ecd9653b | 1920 | break ; |
f7e98dee RN |
1921 | case wxFONTENCODING_MACROMAN : |
1922 | enc = kCFStringEncodingMacRoman ; | |
1923 | break ; | |
1924 | case wxFONTENCODING_MACJAPANESE : | |
1925 | enc = kCFStringEncodingMacJapanese ; | |
1926 | break ; | |
1927 | case wxFONTENCODING_MACCHINESETRAD : | |
1928 | enc = kCFStringEncodingMacChineseTrad ; | |
1929 | break ; | |
1930 | case wxFONTENCODING_MACKOREAN : | |
1931 | enc = kCFStringEncodingMacKorean ; | |
1932 | break ; | |
1933 | case wxFONTENCODING_MACARABIC : | |
1934 | enc = kCFStringEncodingMacArabic ; | |
1935 | break ; | |
1936 | case wxFONTENCODING_MACHEBREW : | |
1937 | enc = kCFStringEncodingMacHebrew ; | |
1938 | break ; | |
1939 | case wxFONTENCODING_MACGREEK : | |
1940 | enc = kCFStringEncodingMacGreek ; | |
1941 | break ; | |
1942 | case wxFONTENCODING_MACCYRILLIC : | |
1943 | enc = kCFStringEncodingMacCyrillic ; | |
1944 | break ; | |
1945 | case wxFONTENCODING_MACDEVANAGARI : | |
1946 | enc = kCFStringEncodingMacDevanagari ; | |
1947 | break ; | |
1948 | case wxFONTENCODING_MACGURMUKHI : | |
1949 | enc = kCFStringEncodingMacGurmukhi ; | |
1950 | break ; | |
1951 | case wxFONTENCODING_MACGUJARATI : | |
1952 | enc = kCFStringEncodingMacGujarati ; | |
1953 | break ; | |
1954 | case wxFONTENCODING_MACORIYA : | |
1955 | enc = kCFStringEncodingMacOriya ; | |
1956 | break ; | |
1957 | case wxFONTENCODING_MACBENGALI : | |
1958 | enc = kCFStringEncodingMacBengali ; | |
1959 | break ; | |
1960 | case wxFONTENCODING_MACTAMIL : | |
1961 | enc = kCFStringEncodingMacTamil ; | |
1962 | break ; | |
1963 | case wxFONTENCODING_MACTELUGU : | |
1964 | enc = kCFStringEncodingMacTelugu ; | |
1965 | break ; | |
1966 | case wxFONTENCODING_MACKANNADA : | |
1967 | enc = kCFStringEncodingMacKannada ; | |
1968 | break ; | |
1969 | case wxFONTENCODING_MACMALAJALAM : | |
1970 | enc = kCFStringEncodingMacMalayalam ; | |
1971 | break ; | |
1972 | case wxFONTENCODING_MACSINHALESE : | |
1973 | enc = kCFStringEncodingMacSinhalese ; | |
1974 | break ; | |
1975 | case wxFONTENCODING_MACBURMESE : | |
1976 | enc = kCFStringEncodingMacBurmese ; | |
1977 | break ; | |
1978 | case wxFONTENCODING_MACKHMER : | |
1979 | enc = kCFStringEncodingMacKhmer ; | |
1980 | break ; | |
1981 | case wxFONTENCODING_MACTHAI : | |
1982 | enc = kCFStringEncodingMacThai ; | |
1983 | break ; | |
1984 | case wxFONTENCODING_MACLAOTIAN : | |
1985 | enc = kCFStringEncodingMacLaotian ; | |
1986 | break ; | |
1987 | case wxFONTENCODING_MACGEORGIAN : | |
1988 | enc = kCFStringEncodingMacGeorgian ; | |
1989 | break ; | |
1990 | case wxFONTENCODING_MACARMENIAN : | |
1991 | enc = kCFStringEncodingMacArmenian ; | |
1992 | break ; | |
1993 | case wxFONTENCODING_MACCHINESESIMP : | |
1994 | enc = kCFStringEncodingMacChineseSimp ; | |
1995 | break ; | |
1996 | case wxFONTENCODING_MACTIBETAN : | |
1997 | enc = kCFStringEncodingMacTibetan ; | |
1998 | break ; | |
1999 | case wxFONTENCODING_MACMONGOLIAN : | |
2000 | enc = kCFStringEncodingMacMongolian ; | |
2001 | break ; | |
2002 | case wxFONTENCODING_MACETHIOPIC : | |
2003 | enc = kCFStringEncodingMacEthiopic ; | |
2004 | break ; | |
2005 | case wxFONTENCODING_MACCENTRALEUR : | |
2006 | enc = kCFStringEncodingMacCentralEurRoman ; | |
2007 | break ; | |
2008 | case wxFONTENCODING_MACVIATNAMESE : | |
2009 | enc = kCFStringEncodingMacVietnamese ; | |
2010 | break ; | |
2011 | case wxFONTENCODING_MACARABICEXT : | |
2012 | enc = kCFStringEncodingMacExtArabic ; | |
2013 | break ; | |
2014 | case wxFONTENCODING_MACSYMBOL : | |
2015 | enc = kCFStringEncodingMacSymbol ; | |
2016 | break ; | |
2017 | case wxFONTENCODING_MACDINGBATS : | |
2018 | enc = kCFStringEncodingMacDingbats ; | |
2019 | break ; | |
2020 | case wxFONTENCODING_MACTURKISH : | |
2021 | enc = kCFStringEncodingMacTurkish ; | |
2022 | break ; | |
2023 | case wxFONTENCODING_MACCROATIAN : | |
2024 | enc = kCFStringEncodingMacCroatian ; | |
2025 | break ; | |
2026 | case wxFONTENCODING_MACICELANDIC : | |
2027 | enc = kCFStringEncodingMacIcelandic ; | |
2028 | break ; | |
2029 | case wxFONTENCODING_MACROMANIAN : | |
2030 | enc = kCFStringEncodingMacRomanian ; | |
2031 | break ; | |
2032 | case wxFONTENCODING_MACCELTIC : | |
2033 | enc = kCFStringEncodingMacCeltic ; | |
2034 | break ; | |
2035 | case wxFONTENCODING_MACGAELIC : | |
2036 | enc = kCFStringEncodingMacGaelic ; | |
2037 | break ; | |
ecd9653b WS |
2038 | // case wxFONTENCODING_MACKEYBOARD : |
2039 | // enc = kCFStringEncodingMacKeyboardGlyphs ; | |
2040 | // break ; | |
2041 | default : | |
2042 | // because gcc is picky | |
2043 | break ; | |
2044 | } ; | |
2045 | return enc ; | |
f7e98dee RN |
2046 | } |
2047 | ||
f7e98dee RN |
2048 | class wxMBConv_cocoa : public wxMBConv |
2049 | { | |
2050 | public: | |
2051 | wxMBConv_cocoa() | |
2052 | { | |
2053 | Init(CFStringGetSystemEncoding()) ; | |
2054 | } | |
2055 | ||
a6900d10 | 2056 | #if wxUSE_FONTMAP |
f7e98dee RN |
2057 | wxMBConv_cocoa(const wxChar* name) |
2058 | { | |
267e11c5 | 2059 | Init( wxCFStringEncFromFontEnc(wxFontMapperBase::Get()->CharsetToEncoding(name, false) ) ) ; |
f7e98dee | 2060 | } |
a6900d10 | 2061 | #endif |
f7e98dee RN |
2062 | |
2063 | wxMBConv_cocoa(wxFontEncoding encoding) | |
2064 | { | |
2065 | Init( wxCFStringEncFromFontEnc(encoding) ); | |
2066 | } | |
2067 | ||
2068 | ~wxMBConv_cocoa() | |
2069 | { | |
2070 | } | |
2071 | ||
2072 | void Init( CFStringEncoding encoding) | |
2073 | { | |
638357a0 | 2074 | m_encoding = encoding ; |
f7e98dee RN |
2075 | } |
2076 | ||
2077 | size_t MB2WC(wchar_t * szOut, const char * szUnConv, size_t nOutSize) const | |
2078 | { | |
2079 | wxASSERT(szUnConv); | |
ecd9653b | 2080 | |
638357a0 RN |
2081 | CFStringRef theString = CFStringCreateWithBytes ( |
2082 | NULL, //the allocator | |
2083 | (const UInt8*)szUnConv, | |
2084 | strlen(szUnConv), | |
2085 | m_encoding, | |
2086 | false //no BOM/external representation | |
f7e98dee RN |
2087 | ); |
2088 | ||
2089 | wxASSERT(theString); | |
2090 | ||
638357a0 RN |
2091 | size_t nOutLength = CFStringGetLength(theString); |
2092 | ||
2093 | if (szOut == NULL) | |
f7e98dee | 2094 | { |
f7e98dee | 2095 | CFRelease(theString); |
638357a0 | 2096 | return nOutLength; |
f7e98dee | 2097 | } |
ecd9653b | 2098 | |
638357a0 | 2099 | CFRange theRange = { 0, nOutSize }; |
ecd9653b | 2100 | |
638357a0 RN |
2101 | #if SIZEOF_WCHAR_T == 4 |
2102 | UniChar* szUniCharBuffer = new UniChar[nOutSize]; | |
2103 | #endif | |
3698ae71 | 2104 | |
f7e98dee | 2105 | CFStringGetCharacters(theString, theRange, szUniCharBuffer); |
3698ae71 | 2106 | |
f7e98dee | 2107 | CFRelease(theString); |
ecd9653b | 2108 | |
638357a0 | 2109 | szUniCharBuffer[nOutLength] = '\0' ; |
f7e98dee RN |
2110 | |
2111 | #if SIZEOF_WCHAR_T == 4 | |
2112 | wxMBConvUTF16 converter ; | |
638357a0 | 2113 | converter.MB2WC(szOut, (const char*)szUniCharBuffer , nOutSize ) ; |
f7e98dee RN |
2114 | delete[] szUniCharBuffer; |
2115 | #endif | |
3698ae71 | 2116 | |
638357a0 | 2117 | return nOutLength; |
f7e98dee RN |
2118 | } |
2119 | ||
2120 | size_t WC2MB(char *szOut, const wchar_t *szUnConv, size_t nOutSize) const | |
2121 | { | |
638357a0 | 2122 | wxASSERT(szUnConv); |
3698ae71 | 2123 | |
f7e98dee | 2124 | size_t nRealOutSize; |
638357a0 | 2125 | size_t nBufSize = wxWcslen(szUnConv); |
f7e98dee | 2126 | UniChar* szUniBuffer = (UniChar*) szUnConv; |
ecd9653b | 2127 | |
f7e98dee | 2128 | #if SIZEOF_WCHAR_T == 4 |
d9d488cf | 2129 | wxMBConvUTF16 converter ; |
f7e98dee RN |
2130 | nBufSize = converter.WC2MB( NULL , szUnConv , 0 ); |
2131 | szUniBuffer = new UniChar[ (nBufSize / sizeof(UniChar)) + 1] ; | |
2132 | converter.WC2MB( (char*) szUniBuffer , szUnConv, nBufSize + sizeof(UniChar)) ; | |
2133 | nBufSize /= sizeof(UniChar); | |
f7e98dee RN |
2134 | #endif |
2135 | ||
2136 | CFStringRef theString = CFStringCreateWithCharactersNoCopy( | |
2137 | NULL, //allocator | |
2138 | szUniBuffer, | |
2139 | nBufSize, | |
638357a0 | 2140 | kCFAllocatorNull //deallocator - we want to deallocate it ourselves |
f7e98dee | 2141 | ); |
ecd9653b | 2142 | |
f7e98dee | 2143 | wxASSERT(theString); |
ecd9653b | 2144 | |
f7e98dee | 2145 | //Note that CER puts a BOM when converting to unicode |
638357a0 RN |
2146 | //so we check and use getchars instead in that case |
2147 | if (m_encoding == kCFStringEncodingUnicode) | |
f7e98dee | 2148 | { |
638357a0 RN |
2149 | if (szOut != NULL) |
2150 | CFStringGetCharacters(theString, CFRangeMake(0, nOutSize - 1), (UniChar*) szOut); | |
3698ae71 | 2151 | |
638357a0 RN |
2152 | nRealOutSize = CFStringGetLength(theString) + 1; |
2153 | } | |
2154 | else | |
2155 | { | |
2156 | CFStringGetBytes( | |
2157 | theString, | |
2158 | CFRangeMake(0, CFStringGetLength(theString)), | |
2159 | m_encoding, | |
2160 | 0, //what to put in characters that can't be converted - | |
2161 | //0 tells CFString to return NULL if it meets such a character | |
2162 | false, //not an external representation | |
2163 | (UInt8*) szOut, | |
3698ae71 | 2164 | nOutSize, |
638357a0 RN |
2165 | (CFIndex*) &nRealOutSize |
2166 | ); | |
f7e98dee | 2167 | } |
ecd9653b | 2168 | |
638357a0 | 2169 | CFRelease(theString); |
ecd9653b | 2170 | |
638357a0 RN |
2171 | #if SIZEOF_WCHAR_T == 4 |
2172 | delete[] szUniBuffer; | |
2173 | #endif | |
ecd9653b | 2174 | |
f7e98dee RN |
2175 | return nRealOutSize - 1; |
2176 | } | |
2177 | ||
2178 | bool IsOk() const | |
ecd9653b | 2179 | { |
3698ae71 | 2180 | return m_encoding != kCFStringEncodingInvalidId && |
638357a0 | 2181 | CFStringIsEncodingAvailable(m_encoding); |
f7e98dee RN |
2182 | } |
2183 | ||
2184 | private: | |
638357a0 | 2185 | CFStringEncoding m_encoding ; |
f7e98dee RN |
2186 | }; |
2187 | ||
2188 | #endif // defined(__WXCOCOA__) | |
2189 | ||
335d31e0 SC |
2190 | // ============================================================================ |
2191 | // Mac conversion classes | |
2192 | // ============================================================================ | |
2193 | ||
2194 | #if defined(__WXMAC__) && defined(TARGET_CARBON) | |
2195 | ||
2196 | class wxMBConv_mac : public wxMBConv | |
2197 | { | |
2198 | public: | |
2199 | wxMBConv_mac() | |
2200 | { | |
2201 | Init(CFStringGetSystemEncoding()) ; | |
2202 | } | |
2203 | ||
2d1659cf | 2204 | #if wxUSE_FONTMAP |
335d31e0 SC |
2205 | wxMBConv_mac(const wxChar* name) |
2206 | { | |
267e11c5 | 2207 | Init( wxMacGetSystemEncFromFontEnc(wxFontMapperBase::Get()->CharsetToEncoding(name, false) ) ) ; |
335d31e0 | 2208 | } |
2d1659cf | 2209 | #endif |
335d31e0 SC |
2210 | |
2211 | wxMBConv_mac(wxFontEncoding encoding) | |
2212 | { | |
d775fa82 WS |
2213 | Init( wxMacGetSystemEncFromFontEnc(encoding) ); |
2214 | } | |
2215 | ||
2216 | ~wxMBConv_mac() | |
2217 | { | |
2218 | OSStatus status = noErr ; | |
2219 | status = TECDisposeConverter(m_MB2WC_converter); | |
2220 | status = TECDisposeConverter(m_WC2MB_converter); | |
2221 | } | |
2222 | ||
2223 | ||
2224 | void Init( TextEncodingBase encoding) | |
2225 | { | |
2226 | OSStatus status = noErr ; | |
2227 | m_char_encoding = encoding ; | |
2228 | m_unicode_encoding = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode16BitFormat) ; | |
2229 | ||
2230 | status = TECCreateConverter(&m_MB2WC_converter, | |
2231 | m_char_encoding, | |
2232 | m_unicode_encoding); | |
2233 | status = TECCreateConverter(&m_WC2MB_converter, | |
2234 | m_unicode_encoding, | |
2235 | m_char_encoding); | |
2236 | } | |
2237 | ||
335d31e0 SC |
2238 | size_t MB2WC(wchar_t *buf, const char *psz, size_t n) const |
2239 | { | |
d775fa82 WS |
2240 | OSStatus status = noErr ; |
2241 | ByteCount byteOutLen ; | |
2242 | ByteCount byteInLen = strlen(psz) ; | |
2243 | wchar_t *tbuf = NULL ; | |
2244 | UniChar* ubuf = NULL ; | |
2245 | size_t res = 0 ; | |
2246 | ||
2247 | if (buf == NULL) | |
2248 | { | |
638357a0 | 2249 | //apple specs say at least 32 |
c543817b | 2250 | n = wxMax( 32 , byteInLen ) ; |
d775fa82 WS |
2251 | tbuf = (wchar_t*) malloc( n * SIZEOF_WCHAR_T) ; |
2252 | } | |
2253 | ByteCount byteBufferLen = n * sizeof( UniChar ) ; | |
f3a355ce | 2254 | #if SIZEOF_WCHAR_T == 4 |
d775fa82 | 2255 | ubuf = (UniChar*) malloc( byteBufferLen + 2 ) ; |
f3a355ce | 2256 | #else |
d775fa82 | 2257 | ubuf = (UniChar*) (buf ? buf : tbuf) ; |
f3a355ce | 2258 | #endif |
d775fa82 WS |
2259 | status = TECConvertText(m_MB2WC_converter, (ConstTextPtr) psz , byteInLen, &byteInLen, |
2260 | (TextPtr) ubuf , byteBufferLen, &byteOutLen); | |
f3a355ce | 2261 | #if SIZEOF_WCHAR_T == 4 |
8471ea90 SC |
2262 | // we have to terminate here, because n might be larger for the trailing zero, and if UniChar |
2263 | // is not properly terminated we get random characters at the end | |
2264 | ubuf[byteOutLen / sizeof( UniChar ) ] = 0 ; | |
d9d488cf | 2265 | wxMBConvUTF16 converter ; |
d775fa82 WS |
2266 | res = converter.MB2WC( (buf ? buf : tbuf) , (const char*)ubuf , n ) ; |
2267 | free( ubuf ) ; | |
f3a355ce | 2268 | #else |
d775fa82 | 2269 | res = byteOutLen / sizeof( UniChar ) ; |
f3a355ce | 2270 | #endif |
d775fa82 WS |
2271 | if ( buf == NULL ) |
2272 | free(tbuf) ; | |
335d31e0 | 2273 | |
335d31e0 SC |
2274 | if ( buf && res < n) |
2275 | buf[res] = 0; | |
2276 | ||
d775fa82 | 2277 | return res ; |
335d31e0 SC |
2278 | } |
2279 | ||
2280 | size_t WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
d775fa82 WS |
2281 | { |
2282 | OSStatus status = noErr ; | |
2283 | ByteCount byteOutLen ; | |
2284 | ByteCount byteInLen = wxWcslen(psz) * SIZEOF_WCHAR_T ; | |
2285 | ||
2286 | char *tbuf = NULL ; | |
2287 | ||
2288 | if (buf == NULL) | |
2289 | { | |
638357a0 | 2290 | //apple specs say at least 32 |
c543817b | 2291 | n = wxMax( 32 , ((byteInLen / SIZEOF_WCHAR_T) * 8) + SIZEOF_WCHAR_T ); |
d775fa82 WS |
2292 | tbuf = (char*) malloc( n ) ; |
2293 | } | |
2294 | ||
2295 | ByteCount byteBufferLen = n ; | |
2296 | UniChar* ubuf = NULL ; | |
f3a355ce | 2297 | #if SIZEOF_WCHAR_T == 4 |
d9d488cf | 2298 | wxMBConvUTF16 converter ; |
d775fa82 WS |
2299 | size_t unicharlen = converter.WC2MB( NULL , psz , 0 ) ; |
2300 | byteInLen = unicharlen ; | |
2301 | ubuf = (UniChar*) malloc( byteInLen + 2 ) ; | |
2302 | converter.WC2MB( (char*) ubuf , psz, unicharlen + 2 ) ; | |
f3a355ce | 2303 | #else |
d775fa82 | 2304 | ubuf = (UniChar*) psz ; |
f3a355ce | 2305 | #endif |
d775fa82 WS |
2306 | status = TECConvertText(m_WC2MB_converter, (ConstTextPtr) ubuf , byteInLen, &byteInLen, |
2307 | (TextPtr) (buf ? buf : tbuf) , byteBufferLen, &byteOutLen); | |
f3a355ce | 2308 | #if SIZEOF_WCHAR_T == 4 |
d775fa82 | 2309 | free( ubuf ) ; |
f3a355ce | 2310 | #endif |
d775fa82 WS |
2311 | if ( buf == NULL ) |
2312 | free(tbuf) ; | |
335d31e0 | 2313 | |
d775fa82 | 2314 | size_t res = byteOutLen ; |
335d31e0 | 2315 | if ( buf && res < n) |
638357a0 | 2316 | { |
335d31e0 | 2317 | buf[res] = 0; |
3698ae71 | 2318 | |
638357a0 RN |
2319 | //we need to double-trip to verify it didn't insert any ? in place |
2320 | //of bogus characters | |
2321 | wxWCharBuffer wcBuf(n); | |
2322 | size_t pszlen = wxWcslen(psz); | |
2323 | if ( MB2WC(wcBuf.data(), buf, n) == (size_t)-1 || | |
2324 | wxWcslen(wcBuf) != pszlen || | |
2325 | memcmp(wcBuf, psz, pszlen * sizeof(wchar_t)) != 0 ) | |
2326 | { | |
2327 | // we didn't obtain the same thing we started from, hence | |
2328 | // the conversion was lossy and we consider that it failed | |
2329 | return (size_t)-1; | |
2330 | } | |
2331 | } | |
335d31e0 | 2332 | |
d775fa82 | 2333 | return res ; |
335d31e0 SC |
2334 | } |
2335 | ||
2336 | bool IsOk() const | |
2337 | { return m_MB2WC_converter != NULL && m_WC2MB_converter != NULL ; } | |
2338 | ||
2339 | private: | |
d775fa82 WS |
2340 | TECObjectRef m_MB2WC_converter ; |
2341 | TECObjectRef m_WC2MB_converter ; | |
2342 | ||
2343 | TextEncodingBase m_char_encoding ; | |
2344 | TextEncodingBase m_unicode_encoding ; | |
335d31e0 SC |
2345 | }; |
2346 | ||
2347 | #endif // defined(__WXMAC__) && defined(TARGET_CARBON) | |
1e6feb95 | 2348 | |
36acb880 VZ |
2349 | // ============================================================================ |
2350 | // wxEncodingConverter based conversion classes | |
2351 | // ============================================================================ | |
2352 | ||
1e6feb95 | 2353 | #if wxUSE_FONTMAP |
1cd52418 | 2354 | |
e95354ec | 2355 | class wxMBConv_wxwin : public wxMBConv |
1cd52418 | 2356 | { |
8b04d4c4 VZ |
2357 | private: |
2358 | void Init() | |
2359 | { | |
2360 | m_ok = m2w.Init(m_enc, wxFONTENCODING_UNICODE) && | |
2361 | w2m.Init(wxFONTENCODING_UNICODE, m_enc); | |
2362 | } | |
2363 | ||
6001e347 | 2364 | public: |
f1339c56 RR |
2365 | // temporarily just use wxEncodingConverter stuff, |
2366 | // so that it works while a better implementation is built | |
e95354ec | 2367 | wxMBConv_wxwin(const wxChar* name) |
f1339c56 RR |
2368 | { |
2369 | if (name) | |
267e11c5 | 2370 | m_enc = wxFontMapperBase::Get()->CharsetToEncoding(name, false); |
8b04d4c4 VZ |
2371 | else |
2372 | m_enc = wxFONTENCODING_SYSTEM; | |
cafbf6fb | 2373 | |
8b04d4c4 VZ |
2374 | Init(); |
2375 | } | |
2376 | ||
e95354ec | 2377 | wxMBConv_wxwin(wxFontEncoding enc) |
8b04d4c4 VZ |
2378 | { |
2379 | m_enc = enc; | |
2380 | ||
2381 | Init(); | |
f1339c56 | 2382 | } |
dccce9ea | 2383 | |
bde4baac | 2384 | size_t MB2WC(wchar_t *buf, const char *psz, size_t WXUNUSED(n)) const |
f1339c56 RR |
2385 | { |
2386 | size_t inbuf = strlen(psz); | |
dccce9ea | 2387 | if (buf) |
c643a977 VS |
2388 | { |
2389 | if (!m2w.Convert(psz,buf)) | |
2390 | return (size_t)-1; | |
2391 | } | |
f1339c56 RR |
2392 | return inbuf; |
2393 | } | |
dccce9ea | 2394 | |
bde4baac | 2395 | size_t WC2MB(char *buf, const wchar_t *psz, size_t WXUNUSED(n)) const |
f1339c56 | 2396 | { |
f8d791e0 | 2397 | const size_t inbuf = wxWcslen(psz); |
f1339c56 | 2398 | if (buf) |
c643a977 VS |
2399 | { |
2400 | if (!w2m.Convert(psz,buf)) | |
2401 | return (size_t)-1; | |
2402 | } | |
dccce9ea | 2403 | |
f1339c56 RR |
2404 | return inbuf; |
2405 | } | |
dccce9ea | 2406 | |
e95354ec | 2407 | bool IsOk() const { return m_ok; } |
f1339c56 RR |
2408 | |
2409 | public: | |
8b04d4c4 | 2410 | wxFontEncoding m_enc; |
f1339c56 | 2411 | wxEncodingConverter m2w, w2m; |
cafbf6fb VZ |
2412 | |
2413 | // were we initialized successfully? | |
2414 | bool m_ok; | |
fc7a2a60 | 2415 | |
e95354ec | 2416 | DECLARE_NO_COPY_CLASS(wxMBConv_wxwin) |
f6bcfd97 | 2417 | }; |
6001e347 | 2418 | |
8f115891 MW |
2419 | // make the constructors available for unit testing |
2420 | WXDLLIMPEXP_BASE wxMBConv* new_wxMBConv_wxwin( const wxChar* name ) | |
2421 | { | |
2422 | wxMBConv_wxwin* result = new wxMBConv_wxwin( name ); | |
2423 | if ( !result->IsOk() ) | |
2424 | { | |
2425 | delete result; | |
2426 | return 0; | |
2427 | } | |
2428 | return result; | |
2429 | } | |
2430 | ||
1e6feb95 VZ |
2431 | #endif // wxUSE_FONTMAP |
2432 | ||
36acb880 VZ |
2433 | // ============================================================================ |
2434 | // wxCSConv implementation | |
2435 | // ============================================================================ | |
2436 | ||
8b04d4c4 | 2437 | void wxCSConv::Init() |
6001e347 | 2438 | { |
e95354ec VZ |
2439 | m_name = NULL; |
2440 | m_convReal = NULL; | |
2441 | m_deferred = true; | |
2442 | } | |
2443 | ||
8b04d4c4 VZ |
2444 | wxCSConv::wxCSConv(const wxChar *charset) |
2445 | { | |
2446 | Init(); | |
82713003 | 2447 | |
e95354ec VZ |
2448 | if ( charset ) |
2449 | { | |
e95354ec VZ |
2450 | SetName(charset); |
2451 | } | |
bda3d86a VZ |
2452 | |
2453 | m_encoding = wxFONTENCODING_SYSTEM; | |
6001e347 RR |
2454 | } |
2455 | ||
8b04d4c4 VZ |
2456 | wxCSConv::wxCSConv(wxFontEncoding encoding) |
2457 | { | |
bda3d86a | 2458 | if ( encoding == wxFONTENCODING_MAX || encoding == wxFONTENCODING_DEFAULT ) |
e95354ec VZ |
2459 | { |
2460 | wxFAIL_MSG( _T("invalid encoding value in wxCSConv ctor") ); | |
2461 | ||
2462 | encoding = wxFONTENCODING_SYSTEM; | |
2463 | } | |
2464 | ||
8b04d4c4 VZ |
2465 | Init(); |
2466 | ||
bda3d86a | 2467 | m_encoding = encoding; |
8b04d4c4 VZ |
2468 | } |
2469 | ||
6001e347 RR |
2470 | wxCSConv::~wxCSConv() |
2471 | { | |
65e50848 JS |
2472 | Clear(); |
2473 | } | |
2474 | ||
54380f29 | 2475 | wxCSConv::wxCSConv(const wxCSConv& conv) |
8b04d4c4 | 2476 | : wxMBConv() |
54380f29 | 2477 | { |
8b04d4c4 VZ |
2478 | Init(); |
2479 | ||
54380f29 | 2480 | SetName(conv.m_name); |
8b04d4c4 | 2481 | m_encoding = conv.m_encoding; |
54380f29 GD |
2482 | } |
2483 | ||
2484 | wxCSConv& wxCSConv::operator=(const wxCSConv& conv) | |
2485 | { | |
2486 | Clear(); | |
8b04d4c4 | 2487 | |
54380f29 | 2488 | SetName(conv.m_name); |
8b04d4c4 VZ |
2489 | m_encoding = conv.m_encoding; |
2490 | ||
54380f29 GD |
2491 | return *this; |
2492 | } | |
2493 | ||
65e50848 JS |
2494 | void wxCSConv::Clear() |
2495 | { | |
8b04d4c4 | 2496 | free(m_name); |
e95354ec | 2497 | delete m_convReal; |
8b04d4c4 | 2498 | |
65e50848 | 2499 | m_name = NULL; |
e95354ec | 2500 | m_convReal = NULL; |
6001e347 RR |
2501 | } |
2502 | ||
2503 | void wxCSConv::SetName(const wxChar *charset) | |
2504 | { | |
f1339c56 RR |
2505 | if (charset) |
2506 | { | |
2507 | m_name = wxStrdup(charset); | |
e95354ec | 2508 | m_deferred = true; |
f1339c56 | 2509 | } |
6001e347 RR |
2510 | } |
2511 | ||
e95354ec VZ |
2512 | wxMBConv *wxCSConv::DoCreate() const |
2513 | { | |
c547282d VZ |
2514 | // check for the special case of ASCII or ISO8859-1 charset: as we have |
2515 | // special knowledge of it anyhow, we don't need to create a special | |
2516 | // conversion object | |
2517 | if ( m_encoding == wxFONTENCODING_ISO8859_1 ) | |
f1339c56 | 2518 | { |
e95354ec VZ |
2519 | // don't convert at all |
2520 | return NULL; | |
2521 | } | |
dccce9ea | 2522 | |
e95354ec VZ |
2523 | // we trust OS to do conversion better than we can so try external |
2524 | // conversion methods first | |
2525 | // | |
2526 | // the full order is: | |
2527 | // 1. OS conversion (iconv() under Unix or Win32 API) | |
2528 | // 2. hard coded conversions for UTF | |
2529 | // 3. wxEncodingConverter as fall back | |
2530 | ||
2531 | // step (1) | |
2532 | #ifdef HAVE_ICONV | |
c547282d | 2533 | #if !wxUSE_FONTMAP |
e95354ec | 2534 | if ( m_name ) |
c547282d | 2535 | #endif // !wxUSE_FONTMAP |
e95354ec | 2536 | { |
c547282d VZ |
2537 | wxString name(m_name); |
2538 | ||
2539 | #if wxUSE_FONTMAP | |
2540 | if ( name.empty() ) | |
d0ee33f5 | 2541 | name = wxFontMapperBase::GetEncodingName(m_encoding); |
c547282d VZ |
2542 | #endif // wxUSE_FONTMAP |
2543 | ||
2544 | wxMBConv_iconv *conv = new wxMBConv_iconv(name); | |
e95354ec VZ |
2545 | if ( conv->IsOk() ) |
2546 | return conv; | |
2547 | ||
2548 | delete conv; | |
2549 | } | |
2550 | #endif // HAVE_ICONV | |
2551 | ||
2552 | #ifdef wxHAVE_WIN32_MB2WC | |
2553 | { | |
7608a683 | 2554 | #if wxUSE_FONTMAP |
e95354ec VZ |
2555 | wxMBConv_win32 *conv = m_name ? new wxMBConv_win32(m_name) |
2556 | : new wxMBConv_win32(m_encoding); | |
2557 | if ( conv->IsOk() ) | |
2558 | return conv; | |
2559 | ||
2560 | delete conv; | |
7608a683 WS |
2561 | #else |
2562 | return NULL; | |
2563 | #endif | |
e95354ec VZ |
2564 | } |
2565 | #endif // wxHAVE_WIN32_MB2WC | |
d775fa82 WS |
2566 | #if defined(__WXMAC__) |
2567 | { | |
5c3c8676 | 2568 | // leave UTF16 and UTF32 to the built-ins of wx |
3698ae71 | 2569 | if ( m_name || ( m_encoding < wxFONTENCODING_UTF16BE || |
5c3c8676 | 2570 | ( m_encoding >= wxFONTENCODING_MACMIN && m_encoding <= wxFONTENCODING_MACMAX ) ) ) |
d775fa82 WS |
2571 | { |
2572 | ||
2d1659cf | 2573 | #if wxUSE_FONTMAP |
d775fa82 WS |
2574 | wxMBConv_mac *conv = m_name ? new wxMBConv_mac(m_name) |
2575 | : new wxMBConv_mac(m_encoding); | |
2d1659cf RN |
2576 | #else |
2577 | wxMBConv_mac *conv = new wxMBConv_mac(m_encoding); | |
2578 | #endif | |
d775fa82 | 2579 | if ( conv->IsOk() ) |
f7e98dee RN |
2580 | return conv; |
2581 | ||
2582 | delete conv; | |
2583 | } | |
2584 | } | |
2585 | #endif | |
2586 | #if defined(__WXCOCOA__) | |
2587 | { | |
2588 | if ( m_name || ( m_encoding <= wxFONTENCODING_UTF16 ) ) | |
2589 | { | |
2590 | ||
a6900d10 | 2591 | #if wxUSE_FONTMAP |
f7e98dee RN |
2592 | wxMBConv_cocoa *conv = m_name ? new wxMBConv_cocoa(m_name) |
2593 | : new wxMBConv_cocoa(m_encoding); | |
a6900d10 RN |
2594 | #else |
2595 | wxMBConv_cocoa *conv = new wxMBConv_cocoa(m_encoding); | |
2596 | #endif | |
f7e98dee | 2597 | if ( conv->IsOk() ) |
d775fa82 WS |
2598 | return conv; |
2599 | ||
2600 | delete conv; | |
2601 | } | |
335d31e0 SC |
2602 | } |
2603 | #endif | |
e95354ec VZ |
2604 | // step (2) |
2605 | wxFontEncoding enc = m_encoding; | |
2606 | #if wxUSE_FONTMAP | |
c547282d VZ |
2607 | if ( enc == wxFONTENCODING_SYSTEM && m_name ) |
2608 | { | |
2609 | // use "false" to suppress interactive dialogs -- we can be called from | |
2610 | // anywhere and popping up a dialog from here is the last thing we want to | |
2611 | // do | |
267e11c5 | 2612 | enc = wxFontMapperBase::Get()->CharsetToEncoding(m_name, false); |
c547282d | 2613 | } |
e95354ec VZ |
2614 | #endif // wxUSE_FONTMAP |
2615 | ||
2616 | switch ( enc ) | |
2617 | { | |
2618 | case wxFONTENCODING_UTF7: | |
2619 | return new wxMBConvUTF7; | |
2620 | ||
2621 | case wxFONTENCODING_UTF8: | |
2622 | return new wxMBConvUTF8; | |
2623 | ||
e95354ec VZ |
2624 | case wxFONTENCODING_UTF16BE: |
2625 | return new wxMBConvUTF16BE; | |
2626 | ||
2627 | case wxFONTENCODING_UTF16LE: | |
2628 | return new wxMBConvUTF16LE; | |
2629 | ||
e95354ec VZ |
2630 | case wxFONTENCODING_UTF32BE: |
2631 | return new wxMBConvUTF32BE; | |
2632 | ||
2633 | case wxFONTENCODING_UTF32LE: | |
2634 | return new wxMBConvUTF32LE; | |
2635 | ||
2636 | default: | |
2637 | // nothing to do but put here to suppress gcc warnings | |
2638 | ; | |
2639 | } | |
2640 | ||
2641 | // step (3) | |
2642 | #if wxUSE_FONTMAP | |
2643 | { | |
2644 | wxMBConv_wxwin *conv = m_name ? new wxMBConv_wxwin(m_name) | |
2645 | : new wxMBConv_wxwin(m_encoding); | |
2646 | if ( conv->IsOk() ) | |
2647 | return conv; | |
2648 | ||
2649 | delete conv; | |
2650 | } | |
2651 | #endif // wxUSE_FONTMAP | |
2652 | ||
a58d4f4d VS |
2653 | // NB: This is a hack to prevent deadlock. What could otherwise happen |
2654 | // in Unicode build: wxConvLocal creation ends up being here | |
2655 | // because of some failure and logs the error. But wxLog will try to | |
2656 | // attach timestamp, for which it will need wxConvLocal (to convert | |
2657 | // time to char* and then wchar_t*), but that fails, tries to log | |
2658 | // error, but wxLog has a (already locked) critical section that | |
2659 | // guards static buffer. | |
2660 | static bool alreadyLoggingError = false; | |
2661 | if (!alreadyLoggingError) | |
2662 | { | |
2663 | alreadyLoggingError = true; | |
2664 | wxLogError(_("Cannot convert from the charset '%s'!"), | |
2665 | m_name ? m_name | |
e95354ec VZ |
2666 | : |
2667 | #if wxUSE_FONTMAP | |
267e11c5 | 2668 | wxFontMapperBase::GetEncodingDescription(m_encoding).c_str() |
e95354ec VZ |
2669 | #else // !wxUSE_FONTMAP |
2670 | wxString::Format(_("encoding %s"), m_encoding).c_str() | |
2671 | #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP | |
2672 | ); | |
a58d4f4d VS |
2673 | alreadyLoggingError = false; |
2674 | } | |
e95354ec VZ |
2675 | |
2676 | return NULL; | |
2677 | } | |
2678 | ||
2679 | void wxCSConv::CreateConvIfNeeded() const | |
2680 | { | |
2681 | if ( m_deferred ) | |
2682 | { | |
2683 | wxCSConv *self = (wxCSConv *)this; // const_cast | |
bda3d86a VZ |
2684 | |
2685 | #if wxUSE_INTL | |
2686 | // if we don't have neither the name nor the encoding, use the default | |
2687 | // encoding for this system | |
2688 | if ( !m_name && m_encoding == wxFONTENCODING_SYSTEM ) | |
2689 | { | |
4d312c22 | 2690 | self->m_name = wxStrdup(wxLocale::GetSystemEncodingName()); |
bda3d86a VZ |
2691 | } |
2692 | #endif // wxUSE_INTL | |
2693 | ||
e95354ec VZ |
2694 | self->m_convReal = DoCreate(); |
2695 | self->m_deferred = false; | |
6001e347 | 2696 | } |
6001e347 RR |
2697 | } |
2698 | ||
2699 | size_t wxCSConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
2700 | { | |
e95354ec | 2701 | CreateConvIfNeeded(); |
dccce9ea | 2702 | |
e95354ec VZ |
2703 | if (m_convReal) |
2704 | return m_convReal->MB2WC(buf, psz, n); | |
f1339c56 RR |
2705 | |
2706 | // latin-1 (direct) | |
4def3b35 | 2707 | size_t len = strlen(psz); |
dccce9ea | 2708 | |
f1339c56 RR |
2709 | if (buf) |
2710 | { | |
4def3b35 | 2711 | for (size_t c = 0; c <= len; c++) |
f1339c56 RR |
2712 | buf[c] = (unsigned char)(psz[c]); |
2713 | } | |
dccce9ea | 2714 | |
f1339c56 | 2715 | return len; |
6001e347 RR |
2716 | } |
2717 | ||
2718 | size_t wxCSConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
2719 | { | |
e95354ec | 2720 | CreateConvIfNeeded(); |
dccce9ea | 2721 | |
e95354ec VZ |
2722 | if (m_convReal) |
2723 | return m_convReal->WC2MB(buf, psz, n); | |
1cd52418 | 2724 | |
f1339c56 | 2725 | // latin-1 (direct) |
f8d791e0 | 2726 | const size_t len = wxWcslen(psz); |
f1339c56 RR |
2727 | if (buf) |
2728 | { | |
4def3b35 | 2729 | for (size_t c = 0; c <= len; c++) |
24642831 VS |
2730 | { |
2731 | if (psz[c] > 0xFF) | |
2732 | return (size_t)-1; | |
907173e5 | 2733 | buf[c] = (char)psz[c]; |
24642831 VS |
2734 | } |
2735 | } | |
2736 | else | |
2737 | { | |
2738 | for (size_t c = 0; c <= len; c++) | |
2739 | { | |
2740 | if (psz[c] > 0xFF) | |
2741 | return (size_t)-1; | |
2742 | } | |
f1339c56 | 2743 | } |
dccce9ea | 2744 | |
f1339c56 | 2745 | return len; |
6001e347 RR |
2746 | } |
2747 | ||
bde4baac VZ |
2748 | // ---------------------------------------------------------------------------- |
2749 | // globals | |
2750 | // ---------------------------------------------------------------------------- | |
2751 | ||
2752 | #ifdef __WINDOWS__ | |
2753 | static wxMBConv_win32 wxConvLibcObj; | |
f81f5901 SC |
2754 | #elif defined(__WXMAC__) && !defined(__MACH__) |
2755 | static wxMBConv_mac wxConvLibcObj ; | |
bde4baac | 2756 | #else |
dcc8fac0 | 2757 | static wxMBConvLibc wxConvLibcObj; |
bde4baac VZ |
2758 | #endif |
2759 | ||
2760 | static wxCSConv wxConvLocalObj(wxFONTENCODING_SYSTEM); | |
2761 | static wxCSConv wxConvISO8859_1Obj(wxFONTENCODING_ISO8859_1); | |
2762 | static wxMBConvUTF7 wxConvUTF7Obj; | |
2763 | static wxMBConvUTF8 wxConvUTF8Obj; | |
c12b7f79 | 2764 | |
bde4baac VZ |
2765 | WXDLLIMPEXP_DATA_BASE(wxMBConv&) wxConvLibc = wxConvLibcObj; |
2766 | WXDLLIMPEXP_DATA_BASE(wxCSConv&) wxConvLocal = wxConvLocalObj; | |
2767 | WXDLLIMPEXP_DATA_BASE(wxCSConv&) wxConvISO8859_1 = wxConvISO8859_1Obj; | |
2768 | WXDLLIMPEXP_DATA_BASE(wxMBConvUTF7&) wxConvUTF7 = wxConvUTF7Obj; | |
2769 | WXDLLIMPEXP_DATA_BASE(wxMBConvUTF8&) wxConvUTF8 = wxConvUTF8Obj; | |
2770 | WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvCurrent = &wxConvLibcObj; | |
f5a1953b VZ |
2771 | WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvFileName = & |
2772 | #ifdef __WXOSX__ | |
ea8ce907 | 2773 | wxConvUTF8Obj; |
f5a1953b | 2774 | #else |
ea8ce907 | 2775 | wxConvLibcObj; |
f5a1953b VZ |
2776 | #endif |
2777 | ||
bde4baac VZ |
2778 | |
2779 | #else // !wxUSE_WCHAR_T | |
2780 | ||
2781 | // stand-ins in absence of wchar_t | |
2782 | WXDLLIMPEXP_DATA_BASE(wxMBConv) wxConvLibc, | |
2783 | wxConvISO8859_1, | |
2784 | wxConvLocal, | |
2785 | wxConvUTF8; | |
2786 | ||
2787 | #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T | |
6001e347 RR |
2788 | |
2789 |