]>
Commit | Line | Data |
---|---|---|
6001e347 RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: strconv.cpp | |
3 | // Purpose: Unicode conversion classes | |
3a0d76bc | 4 | // Author: Ove Kaaven, Robert Roebling, Vadim Zeitlin, Vaclav Slavik |
6001e347 RR |
5 | // Modified by: |
6 | // Created: 29/01/98 | |
7 | // RCS-ID: $Id$ | |
3a0d76bc | 8 | // Copyright: (c) 1999 Ove Kaaven, Robert Roebling, Vadim Zeitlin, Vaclav Slavik |
6001e347 RR |
9 | // Licence: wxWindows license |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
f6bcfd97 BP |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
6001e347 RR |
20 | #ifdef __GNUG__ |
21 | #pragma implementation "strconv.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
0a1c1e62 GRG |
31 | #ifdef __WXMSW__ |
32 | #include "wx/msw/private.h" | |
33 | #endif | |
34 | ||
1cd52418 | 35 | #include <errno.h> |
6001e347 RR |
36 | #include <ctype.h> |
37 | #include <string.h> | |
38 | #include <stdlib.h> | |
39 | ||
7af284fd VS |
40 | |
41 | #include "wx/debug.h" | |
42 | #include "wx/strconv.h" | |
43 | #include "wx/intl.h" | |
44 | #include "wx/log.h" | |
45 | ||
46 | // ---------------------------------------------------------------------------- | |
47 | // globals | |
48 | // ---------------------------------------------------------------------------- | |
49 | ||
50 | WXDLLEXPORT_DATA(wxMBConv *) wxConvCurrent = &wxConvLibc; | |
51 | ||
52 | ||
53 | // ============================================================================ | |
54 | // implementation | |
55 | // ============================================================================ | |
56 | ||
57 | #if wxUSE_WCHAR_T | |
58 | ||
6001e347 RR |
59 | #ifdef __SALFORDC__ |
60 | #include <clib.h> | |
61 | #endif | |
62 | ||
b040e242 | 63 | #ifdef HAVE_ICONV |
1cd52418 OK |
64 | #include <iconv.h> |
65 | #endif | |
1cd52418 | 66 | |
3e61dfb0 OK |
67 | #ifdef __WXMSW__ |
68 | #include <windows.h> | |
69 | #endif | |
70 | ||
1cd52418 | 71 | #define BSWAP_UCS4(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT32_SWAP_ALWAYS(str[_c]); } |
3a0d76bc | 72 | #define BSWAP_UTF16(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT16_SWAP_ALWAYS(str[_c]); } |
1cd52418 | 73 | |
a3f2769e VZ |
74 | // under Unix SIZEOF_WCHAR_T is defined by configure, but under other platforms |
75 | // it might be not defined - assume the most common value | |
76 | #ifndef SIZEOF_WCHAR_T | |
77 | #define SIZEOF_WCHAR_T 2 | |
78 | #endif // !defined(SIZEOF_WCHAR_T) | |
79 | ||
1cd52418 | 80 | #if SIZEOF_WCHAR_T == 4 |
3a0d76bc VS |
81 | #define WC_NAME "UCS4" |
82 | #define WC_BSWAP BSWAP_UCS4 | |
83 | #ifdef WORDS_BIGENDIAN | |
84 | #define WC_NAME_BEST "UCS-4BE" | |
85 | #else | |
86 | #define WC_NAME_BEST "UCS-4LE" | |
87 | #endif | |
1cd52418 | 88 | #elif SIZEOF_WCHAR_T == 2 |
3a0d76bc VS |
89 | #define WC_NAME "UTF16" |
90 | #define WC_BSWAP BSWAP_UTF16 | |
a3f2769e | 91 | #define WC_UTF16 |
3a0d76bc VS |
92 | #ifdef WORDS_BIGENDIAN |
93 | #define WC_NAME_BEST "UTF-16BE" | |
94 | #else | |
95 | #define WC_NAME_BEST "UTF-16LE" | |
96 | #endif | |
bab1e722 | 97 | #else // sizeof(wchar_t) != 2 nor 4 |
a3f2769e VZ |
98 | // I don't know what to do about this |
99 | #error "Weird sizeof(wchar_t): please report your platform details to wx-users mailing list" | |
1cd52418 OK |
100 | #endif |
101 | ||
6001e347 | 102 | |
b0a6bb75 VZ |
103 | #ifdef WC_UTF16 |
104 | ||
eccf1b2c | 105 | static size_t encode_utf16(wxUint32 input, wchar_t *output) |
1cd52418 | 106 | { |
dccce9ea | 107 | if (input<=0xffff) |
4def3b35 VS |
108 | { |
109 | if (output) *output++ = input; | |
110 | return 1; | |
dccce9ea VZ |
111 | } |
112 | else if (input>=0x110000) | |
4def3b35 VS |
113 | { |
114 | return (size_t)-1; | |
dccce9ea VZ |
115 | } |
116 | else | |
4def3b35 | 117 | { |
dccce9ea | 118 | if (output) |
4def3b35 VS |
119 | { |
120 | *output++ = (input >> 10)+0xd7c0; | |
121 | *output++ = (input&0x3ff)+0xdc00; | |
122 | } | |
123 | return 2; | |
1cd52418 | 124 | } |
1cd52418 OK |
125 | } |
126 | ||
eccf1b2c | 127 | static size_t decode_utf16(const wchar_t* input, wxUint32& output) |
1cd52418 | 128 | { |
dccce9ea | 129 | if ((*input<0xd800) || (*input>0xdfff)) |
4def3b35 VS |
130 | { |
131 | output = *input; | |
132 | return 1; | |
dccce9ea VZ |
133 | } |
134 | else if ((input[1]<0xdc00) || (input[1]>=0xdfff)) | |
4def3b35 VS |
135 | { |
136 | output = *input; | |
137 | return (size_t)-1; | |
dccce9ea VZ |
138 | } |
139 | else | |
4def3b35 VS |
140 | { |
141 | output = ((input[0] - 0xd7c0) << 10) + (input[1] - 0xdc00); | |
142 | return 2; | |
143 | } | |
1cd52418 OK |
144 | } |
145 | ||
b0a6bb75 VZ |
146 | #endif // WC_UTF16 |
147 | ||
f6bcfd97 | 148 | // ---------------------------------------------------------------------------- |
6001e347 | 149 | // wxMBConv |
f6bcfd97 | 150 | // ---------------------------------------------------------------------------- |
6001e347 RR |
151 | |
152 | WXDLLEXPORT_DATA(wxMBConv) wxConvLibc; | |
153 | ||
154 | size_t wxMBConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
155 | { | |
156 | return wxMB2WC(buf, psz, n); | |
157 | } | |
158 | ||
159 | size_t wxMBConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
160 | { | |
161 | return wxWC2MB(buf, psz, n); | |
162 | } | |
163 | ||
164 | const wxWCharBuffer wxMBConv::cMB2WC(const char *psz) const | |
165 | { | |
f6bcfd97 | 166 | if (psz) |
6001e347 RR |
167 | { |
168 | size_t nLen = MB2WC((wchar_t *) NULL, psz, 0); | |
f6bcfd97 BP |
169 | if (nLen == (size_t)-1) |
170 | return wxWCharBuffer((wchar_t *) NULL); | |
6001e347 RR |
171 | wxWCharBuffer buf(nLen); |
172 | MB2WC((wchar_t *)(const wchar_t *) buf, psz, nLen); | |
173 | return buf; | |
f6bcfd97 BP |
174 | } |
175 | else | |
6001e347 RR |
176 | return wxWCharBuffer((wchar_t *) NULL); |
177 | } | |
178 | ||
179 | const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *psz) const | |
180 | { | |
f6bcfd97 | 181 | if (psz) |
6001e347 RR |
182 | { |
183 | size_t nLen = WC2MB((char *) NULL, psz, 0); | |
f6bcfd97 BP |
184 | if (nLen == (size_t)-1) |
185 | return wxCharBuffer((char *) NULL); | |
6001e347 RR |
186 | wxCharBuffer buf(nLen); |
187 | WC2MB((char *)(const char *) buf, psz, nLen); | |
188 | return buf; | |
f6bcfd97 BP |
189 | } |
190 | else | |
6001e347 RR |
191 | return wxCharBuffer((char *) NULL); |
192 | } | |
193 | ||
f6bcfd97 | 194 | // ---------------------------------------------------------------------------- |
6001e347 | 195 | // standard file conversion |
f6bcfd97 | 196 | // ---------------------------------------------------------------------------- |
6001e347 RR |
197 | |
198 | WXDLLEXPORT_DATA(wxMBConvFile) wxConvFile; | |
199 | ||
200 | // just use the libc conversion for now | |
201 | size_t wxMBConvFile::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
202 | { | |
203 | return wxMB2WC(buf, psz, n); | |
204 | } | |
205 | ||
206 | size_t wxMBConvFile::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
207 | { | |
208 | return wxWC2MB(buf, psz, n); | |
209 | } | |
210 | ||
f6bcfd97 | 211 | // ---------------------------------------------------------------------------- |
6001e347 | 212 | // standard gdk conversion |
f6bcfd97 BP |
213 | // ---------------------------------------------------------------------------- |
214 | ||
215 | #ifdef __WXGTK12__ | |
6001e347 RR |
216 | |
217 | WXDLLEXPORT_DATA(wxMBConvGdk) wxConvGdk; | |
218 | ||
219 | #include <gdk/gdk.h> | |
220 | ||
221 | size_t wxMBConvGdk::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
222 | { | |
dccce9ea | 223 | if (buf) |
4def3b35 VS |
224 | { |
225 | return gdk_mbstowcs((GdkWChar *)buf, psz, n); | |
dccce9ea VZ |
226 | } |
227 | else | |
4def3b35 VS |
228 | { |
229 | GdkWChar *nbuf = new GdkWChar[n=strlen(psz)]; | |
230 | size_t len = gdk_mbstowcs(nbuf, psz, n); | |
231 | delete[] nbuf; | |
232 | return len; | |
233 | } | |
6001e347 RR |
234 | } |
235 | ||
236 | size_t wxMBConvGdk::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
237 | { | |
4def3b35 VS |
238 | char *mbstr = gdk_wcstombs((GdkWChar *)psz); |
239 | size_t len = mbstr ? strlen(mbstr) : 0; | |
dccce9ea | 240 | if (buf) |
4def3b35 | 241 | { |
dccce9ea | 242 | if (len > n) |
4def3b35 VS |
243 | len = n; |
244 | memcpy(buf, psz, len); | |
dccce9ea | 245 | if (len < n) |
4def3b35 VS |
246 | buf[len] = 0; |
247 | } | |
248 | return len; | |
6001e347 | 249 | } |
f6bcfd97 | 250 | |
6001e347 RR |
251 | #endif // GTK > 1.0 |
252 | ||
253 | // ---------------------------------------------------------------------------- | |
254 | // UTF-7 | |
255 | // ---------------------------------------------------------------------------- | |
256 | ||
257 | WXDLLEXPORT_DATA(wxMBConvUTF7) wxConvUTF7; | |
258 | ||
259 | #if 0 | |
260 | static char utf7_setD[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
261 | "abcdefghijklmnopqrstuvwxyz" | |
262 | "0123456789'(),-./:?"; | |
263 | static char utf7_setO[]="!\"#$%&*;<=>@[]^_`{|}"; | |
264 | static char utf7_setB[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
265 | "abcdefghijklmnopqrstuvwxyz" | |
266 | "0123456789+/"; | |
267 | #endif | |
268 | ||
269 | // TODO: write actual implementations of UTF-7 here | |
270 | size_t wxMBConvUTF7::MB2WC(wchar_t * WXUNUSED(buf), | |
271 | const char * WXUNUSED(psz), | |
272 | size_t WXUNUSED(n)) const | |
273 | { | |
274 | return 0; | |
275 | } | |
276 | ||
277 | size_t wxMBConvUTF7::WC2MB(char * WXUNUSED(buf), | |
278 | const wchar_t * WXUNUSED(psz), | |
279 | size_t WXUNUSED(n)) const | |
280 | { | |
281 | return 0; | |
282 | } | |
283 | ||
f6bcfd97 | 284 | // ---------------------------------------------------------------------------- |
6001e347 | 285 | // UTF-8 |
f6bcfd97 | 286 | // ---------------------------------------------------------------------------- |
6001e347 RR |
287 | |
288 | WXDLLEXPORT_DATA(wxMBConvUTF8) wxConvUTF8; | |
289 | ||
dccce9ea | 290 | static wxUint32 utf8_max[]= |
4def3b35 | 291 | { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff, 0xffffffff }; |
6001e347 RR |
292 | |
293 | size_t wxMBConvUTF8::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
294 | { | |
4def3b35 VS |
295 | size_t len = 0; |
296 | ||
dccce9ea | 297 | while (*psz && ((!buf) || (len < n))) |
4def3b35 VS |
298 | { |
299 | unsigned char cc = *psz++, fc = cc; | |
300 | unsigned cnt; | |
dccce9ea | 301 | for (cnt = 0; fc & 0x80; cnt++) |
4def3b35 | 302 | fc <<= 1; |
dccce9ea | 303 | if (!cnt) |
4def3b35 VS |
304 | { |
305 | // plain ASCII char | |
dccce9ea | 306 | if (buf) |
4def3b35 VS |
307 | *buf++ = cc; |
308 | len++; | |
dccce9ea VZ |
309 | } |
310 | else | |
4def3b35 VS |
311 | { |
312 | cnt--; | |
dccce9ea | 313 | if (!cnt) |
4def3b35 VS |
314 | { |
315 | // invalid UTF-8 sequence | |
316 | return (size_t)-1; | |
dccce9ea VZ |
317 | } |
318 | else | |
4def3b35 VS |
319 | { |
320 | unsigned ocnt = cnt - 1; | |
321 | wxUint32 res = cc & (0x3f >> cnt); | |
dccce9ea | 322 | while (cnt--) |
4def3b35 VS |
323 | { |
324 | cc = *psz++; | |
dccce9ea | 325 | if ((cc & 0xC0) != 0x80) |
4def3b35 VS |
326 | { |
327 | // invalid UTF-8 sequence | |
328 | return (size_t)-1; | |
329 | } | |
330 | res = (res << 6) | (cc & 0x3f); | |
331 | } | |
dccce9ea | 332 | if (res <= utf8_max[ocnt]) |
4def3b35 VS |
333 | { |
334 | // illegal UTF-8 encoding | |
335 | return (size_t)-1; | |
336 | } | |
1cd52418 | 337 | #ifdef WC_UTF16 |
4def3b35 VS |
338 | size_t pa = encode_utf16(res, buf); |
339 | if (pa == (size_t)-1) | |
340 | return (size_t)-1; | |
dccce9ea | 341 | if (buf) |
4def3b35 VS |
342 | buf += pa; |
343 | len += pa; | |
1cd52418 | 344 | #else |
dccce9ea | 345 | if (buf) |
4def3b35 VS |
346 | *buf++ = res; |
347 | len++; | |
1cd52418 | 348 | #endif |
4def3b35 VS |
349 | } |
350 | } | |
6001e347 | 351 | } |
dccce9ea | 352 | if (buf && (len < n)) |
4def3b35 VS |
353 | *buf = 0; |
354 | return len; | |
6001e347 RR |
355 | } |
356 | ||
357 | size_t wxMBConvUTF8::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
358 | { | |
4def3b35 | 359 | size_t len = 0; |
6001e347 | 360 | |
dccce9ea | 361 | while (*psz && ((!buf) || (len < n))) |
4def3b35 VS |
362 | { |
363 | wxUint32 cc; | |
1cd52418 | 364 | #ifdef WC_UTF16 |
eccf1b2c | 365 | size_t pa = decode_utf16(psz, cc); |
4def3b35 | 366 | psz += (pa == (size_t)-1) ? 1 : pa; |
1cd52418 | 367 | #else |
4def3b35 VS |
368 | cc=(*psz++) & 0x7fffffff; |
369 | #endif | |
370 | unsigned cnt; | |
371 | for (cnt = 0; cc > utf8_max[cnt]; cnt++) {} | |
dccce9ea | 372 | if (!cnt) |
4def3b35 VS |
373 | { |
374 | // plain ASCII char | |
dccce9ea | 375 | if (buf) |
4def3b35 VS |
376 | *buf++ = cc; |
377 | len++; | |
dccce9ea VZ |
378 | } |
379 | ||
380 | else | |
4def3b35 VS |
381 | { |
382 | len += cnt + 1; | |
dccce9ea | 383 | if (buf) |
4def3b35 VS |
384 | { |
385 | *buf++ = (-128 >> cnt) | ((cc >> (cnt * 6)) & (0x3f >> cnt)); | |
386 | while (cnt--) | |
387 | *buf++ = 0x80 | ((cc >> (cnt * 6)) & 0x3f); | |
388 | } | |
389 | } | |
6001e347 | 390 | } |
4def3b35 VS |
391 | |
392 | if (buf && (len<n)) *buf = 0; | |
393 | return len; | |
6001e347 RR |
394 | } |
395 | ||
396 | // ---------------------------------------------------------------------------- | |
397 | // specified character set | |
398 | // ---------------------------------------------------------------------------- | |
399 | ||
f6bcfd97 BP |
400 | WXDLLEXPORT_DATA(wxCSConv) wxConvLocal((const wxChar *)NULL); |
401 | ||
402 | #include "wx/encconv.h" | |
403 | #include "wx/fontmap.h" | |
6001e347 | 404 | |
1cd52418 OK |
405 | // TODO: add some tables here |
406 | // - perhaps common encodings to common codepages (for Win32) | |
407 | // - perhaps common encodings to objects ("UTF8" -> wxConvUTF8) | |
408 | // - move wxEncodingConverter meat in here | |
409 | ||
04ef50df | 410 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) |
b1d66b54 | 411 | |
dc7c3244 VZ |
412 | #if wxUSE_GUI |
413 | ||
b1d66b54 VZ |
414 | // VZ: the new version of wxCharsetToCodepage() is more politically correct |
415 | // and should work on other Windows versions as well but the old version is | |
dc7c3244 | 416 | // still needed for !wxUSE_FONTMAP || !wxUSE_GUI case |
b1d66b54 VZ |
417 | |
418 | extern long wxEncodingToCodepage(wxFontEncoding encoding) | |
419 | { | |
420 | // translate encoding into the Windows CHARSET | |
421 | wxNativeEncodingInfo natveEncInfo; | |
422 | if ( !wxGetNativeFontEncoding(encoding, &natveEncInfo) ) | |
423 | return -1; | |
424 | ||
425 | // translate CHARSET to code page | |
426 | CHARSETINFO csetInfo; | |
427 | if ( !::TranslateCharsetInfo((DWORD *)(DWORD)natveEncInfo.charset, | |
428 | &csetInfo, | |
429 | TCI_SRCCHARSET) ) | |
430 | { | |
431 | wxLogLastError(_T("TranslateCharsetInfo(TCI_SRCCHARSET)")); | |
432 | ||
433 | return -1; | |
434 | } | |
435 | ||
436 | return csetInfo.ciACP; | |
437 | } | |
438 | ||
dc7c3244 | 439 | #if wxUSE_FONTMAP |
b1d66b54 VZ |
440 | |
441 | extern long wxCharsetToCodepage(const wxChar *name) | |
442 | { | |
443 | // first get the font encoding for this charset | |
444 | if ( !name ) | |
445 | return -1; | |
446 | ||
447 | wxFontEncoding enc = wxTheFontMapper->CharsetToEncoding(name, FALSE); | |
448 | if ( enc == wxFONTENCODING_SYSTEM ) | |
449 | return -1; | |
450 | ||
451 | // the use the helper function | |
452 | return wxEncodingToCodepage(enc); | |
453 | } | |
454 | ||
dc7c3244 VZ |
455 | #endif // wxUSE_FONTMAP |
456 | ||
457 | #endif // wxUSE_GUI | |
458 | ||
459 | // include old wxCharsetToCodepage() by OK if needed | |
460 | #if !wxUSE_GUI || !wxUSE_FONTMAP | |
b1d66b54 | 461 | |
1cd52418 | 462 | #include "wx/msw/registry.h" |
b1d66b54 VZ |
463 | |
464 | // this should work if Internet Exploiter is installed | |
465 | extern long wxCharsetToCodepage(const wxChar *name) | |
1cd52418 | 466 | { |
dccce9ea | 467 | if (!name) |
f1339c56 | 468 | return GetACP(); |
dccce9ea | 469 | |
f1339c56 | 470 | long CP=-1; |
dccce9ea | 471 | |
f1339c56 RR |
472 | wxString cn(name); |
473 | do { | |
5ce0e4ac | 474 | wxString path(wxT("MIME\\Database\\Charset\\")); |
f1339c56 | 475 | path += cn; |
5ce0e4ac VS |
476 | wxRegKey key(wxRegKey::HKCR, path); |
477 | ||
9c904e25 | 478 | if (!key.Exists()) break; |
dccce9ea | 479 | |
5ce0e4ac VS |
480 | // two cases: either there's an AliasForCharset string, |
481 | // or there are Codepage and InternetEncoding dwords. | |
482 | // The InternetEncoding gives us the actual encoding, | |
483 | // the Codepage just says which Windows character set to | |
484 | // use when displaying the data. | |
485 | if (key.HasValue(wxT("InternetEncoding")) && | |
486 | key.QueryValue(wxT("InternetEncoding"), &CP)) break; | |
dccce9ea | 487 | |
f1339c56 | 488 | // no encoding, see if it's an alias |
5ce0e4ac VS |
489 | if (!key.HasValue(wxT("AliasForCharset")) || |
490 | !key.QueryValue(wxT("AliasForCharset"), cn)) break; | |
f1339c56 | 491 | } while (1); |
dccce9ea | 492 | |
f1339c56 | 493 | return CP; |
1cd52418 | 494 | } |
b1d66b54 | 495 | |
dc7c3244 | 496 | #endif // !wxUSE_GUI || !wxUSE_FONTMAP |
b1d66b54 VZ |
497 | |
498 | #endif // Win32 | |
1cd52418 | 499 | |
36acb880 VZ |
500 | // ============================================================================ |
501 | // wxCharacterSet and derived classes | |
502 | // ============================================================================ | |
503 | ||
504 | // ---------------------------------------------------------------------------- | |
505 | // wxCharacterSet is the ABC for the classes below | |
506 | // ---------------------------------------------------------------------------- | |
507 | ||
6001e347 RR |
508 | class wxCharacterSet |
509 | { | |
1cd52418 | 510 | public: |
4f61e22c VS |
511 | wxCharacterSet(const wxChar*name) : cname(name) {} |
512 | virtual ~wxCharacterSet() {} | |
513 | virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) = 0; | |
514 | virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) = 0; | |
515 | virtual bool usable() const = 0; | |
f1339c56 RR |
516 | public: |
517 | const wxChar*cname; | |
1cd52418 OK |
518 | }; |
519 | ||
36acb880 VZ |
520 | // ---------------------------------------------------------------------------- |
521 | // ID_CharSet: implementation of wxCharacterSet using an existing wxMBConv | |
522 | // ---------------------------------------------------------------------------- | |
523 | ||
1cd52418 OK |
524 | class ID_CharSet : public wxCharacterSet |
525 | { | |
526 | public: | |
36acb880 | 527 | ID_CharSet(const wxChar *name, wxMBConv *cnv) |
f1339c56 | 528 | : wxCharacterSet(name), work(cnv) {} |
dccce9ea | 529 | |
4def3b35 | 530 | size_t MB2WC(wchar_t *buf, const char *psz, size_t n) |
f1339c56 | 531 | { return work ? work->MB2WC(buf,psz,n) : (size_t)-1; } |
dccce9ea | 532 | |
4def3b35 | 533 | size_t WC2MB(char *buf, const wchar_t *psz, size_t n) |
f1339c56 | 534 | { return work ? work->WC2MB(buf,psz,n) : (size_t)-1; } |
dccce9ea | 535 | |
4f61e22c | 536 | bool usable() const |
f1339c56 RR |
537 | { return work!=NULL; } |
538 | public: | |
539 | wxMBConv*work; | |
1cd52418 OK |
540 | }; |
541 | ||
3caec1bb | 542 | |
36acb880 VZ |
543 | // ============================================================================ |
544 | // The classes doing conversion using the iconv_xxx() functions | |
545 | // ============================================================================ | |
3caec1bb | 546 | |
b040e242 | 547 | #ifdef HAVE_ICONV |
3a0d76bc | 548 | |
3caec1bb VS |
549 | // VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with E2BIG |
550 | // if output buffer is _exactly_ as big as needed. Such case is (unless there's | |
551 | // yet another bug in glibc) the only case when iconv() returns with (size_t)-1 | |
552 | // (which means error) and says there are 0 bytes left in the input buffer -- | |
553 | // when _real_ error occurs, bytes-left-in-input buffer is non-zero. Hence, | |
554 | // this alternative test for iconv() failure. | |
555 | // [This bug does not appear in glibc 2.2.] | |
556 | #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 1 | |
557 | #define ICONV_FAILED(cres, bufLeft) ((cres == (size_t)-1) && \ | |
558 | (errno != E2BIG || bufLeft != 0)) | |
559 | #else | |
560 | #define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1) | |
561 | #endif | |
562 | ||
b040e242 | 563 | #define ICONV_CHAR_CAST(x) ((ICONV_CONST char **)(x)) |
36acb880 VZ |
564 | |
565 | // ---------------------------------------------------------------------------- | |
566 | // IC_CharSet: encapsulates an iconv character set | |
567 | // ---------------------------------------------------------------------------- | |
568 | ||
1cd52418 OK |
569 | class IC_CharSet : public wxCharacterSet |
570 | { | |
571 | public: | |
36acb880 VZ |
572 | IC_CharSet(const wxChar *name); |
573 | virtual ~IC_CharSet(); | |
574 | ||
575 | virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n); | |
576 | virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n); | |
577 | ||
578 | bool usable() const | |
579 | { return (m2w != (iconv_t)-1) && (w2m != (iconv_t)-1); } | |
580 | ||
581 | protected: | |
582 | // the iconv handlers used to translate from multibyte to wide char and in | |
583 | // the other direction | |
584 | iconv_t m2w, | |
585 | w2m; | |
586 | ||
587 | private: | |
588 | // the name (for iconv_open()) of a wide char charset - if none is | |
589 | // available on this machine, it will remain NULL | |
590 | static const char *ms_wcCharsetName; | |
591 | ||
592 | // true if the wide char encoding we use (i.e. ms_wcCharsetName) has | |
593 | // different endian-ness than the native one | |
405d8f46 | 594 | static bool ms_wcNeedsSwap; |
36acb880 VZ |
595 | }; |
596 | ||
597 | const char *IC_CharSet::ms_wcCharsetName = NULL; | |
405d8f46 | 598 | bool IC_CharSet::ms_wcNeedsSwap = FALSE; |
36acb880 VZ |
599 | |
600 | IC_CharSet::IC_CharSet(const wxChar *name) | |
601 | : wxCharacterSet(name) | |
602 | { | |
603 | // check for charset that represents wchar_t: | |
604 | if (ms_wcCharsetName == NULL) | |
f1339c56 | 605 | { |
36acb880 | 606 | ms_wcNeedsSwap = FALSE; |
dccce9ea | 607 | |
36acb880 VZ |
608 | // try charset with explicit bytesex info (e.g. "UCS-4LE"): |
609 | ms_wcCharsetName = WC_NAME_BEST; | |
610 | m2w = iconv_open(ms_wcCharsetName, wxConvLibc.cWX2MB(name)); | |
3a0d76bc | 611 | |
36acb880 VZ |
612 | if (m2w == (iconv_t)-1) |
613 | { | |
614 | // try charset w/o bytesex info (e.g. "UCS4") | |
615 | // and check for bytesex ourselves: | |
616 | ms_wcCharsetName = WC_NAME; | |
617 | m2w = iconv_open(ms_wcCharsetName, wxConvLibc.cWX2MB(name)); | |
618 | ||
619 | // last bet, try if it knows WCHAR_T pseudo-charset | |
3a0d76bc VS |
620 | if (m2w == (iconv_t)-1) |
621 | { | |
36acb880 VZ |
622 | ms_wcCharsetName = "WCHAR_T"; |
623 | m2w = iconv_open(ms_wcCharsetName, wxConvLibc.cWX2MB(name)); | |
624 | } | |
3a0d76bc | 625 | |
36acb880 VZ |
626 | if (m2w != (iconv_t)-1) |
627 | { | |
628 | char buf[2], *bufPtr; | |
629 | wchar_t wbuf[2], *wbufPtr; | |
630 | size_t insz, outsz; | |
631 | size_t res; | |
632 | ||
633 | buf[0] = 'A'; | |
634 | buf[1] = 0; | |
635 | wbuf[0] = 0; | |
636 | insz = 2; | |
637 | outsz = SIZEOF_WCHAR_T * 2; | |
638 | wbufPtr = wbuf; | |
639 | bufPtr = buf; | |
640 | ||
641 | res = iconv(m2w, ICONV_CHAR_CAST(&bufPtr), &insz, | |
642 | (char**)&wbufPtr, &outsz); | |
643 | ||
644 | if (ICONV_FAILED(res, insz)) | |
3a0d76bc | 645 | { |
36acb880 VZ |
646 | ms_wcCharsetName = NULL; |
647 | wxLogLastError(wxT("iconv")); | |
648 | wxLogError(_("Convertion to charset '%s' doesn't work."), name); | |
3a0d76bc VS |
649 | } |
650 | else | |
651 | { | |
36acb880 | 652 | ms_wcNeedsSwap = wbuf[0] != (wchar_t)buf[0]; |
3a0d76bc VS |
653 | } |
654 | } | |
36acb880 VZ |
655 | else |
656 | { | |
657 | ms_wcCharsetName = NULL; | |
957686c8 VS |
658 | |
659 | // VS: we must not output an error here, since wxWindows will safely | |
660 | // fall back to using wxEncodingConverter. | |
661 | wxLogTrace(wxT("strconv"), wxT("Impossible to convert to/from charset '%s' with iconv, falling back to wxEncodingConverter."), name); | |
662 | //wxLogError( | |
36acb880 | 663 | } |
3a0d76bc | 664 | } |
36acb880 | 665 | wxLogTrace(wxT("strconv"), wxT("wchar_t charset is '%s', needs swap: %i"), ms_wcCharsetName, ms_wcNeedsSwap); |
3a0d76bc | 666 | } |
36acb880 | 667 | else // we already have ms_wcCharsetName |
3caec1bb | 668 | { |
36acb880 | 669 | m2w = iconv_open(ms_wcCharsetName, wxConvLibc.cWX2MB(name)); |
f1339c56 | 670 | } |
dccce9ea | 671 | |
36acb880 VZ |
672 | // NB: don't ever pass NULL to iconv_open(), it may crash! |
673 | if ( ms_wcCharsetName ) | |
f1339c56 | 674 | { |
36acb880 VZ |
675 | w2m = iconv_open(wxConvLibc.cWX2MB(name), ms_wcCharsetName); |
676 | } | |
405d8f46 VZ |
677 | else |
678 | { | |
679 | w2m = (iconv_t)-1; | |
680 | } | |
36acb880 | 681 | } |
3caec1bb | 682 | |
36acb880 VZ |
683 | IC_CharSet::~IC_CharSet() |
684 | { | |
685 | if ( m2w != (iconv_t)-1 ) | |
686 | iconv_close(m2w); | |
687 | if ( w2m != (iconv_t)-1 ) | |
688 | iconv_close(w2m); | |
689 | } | |
3a0d76bc | 690 | |
36acb880 VZ |
691 | size_t IC_CharSet::MB2WC(wchar_t *buf, const char *psz, size_t n) |
692 | { | |
693 | size_t inbuf = strlen(psz); | |
694 | size_t outbuf = n * SIZEOF_WCHAR_T; | |
695 | size_t res, cres; | |
696 | // VS: Use these instead of psz, buf because iconv() modifies its arguments: | |
697 | wchar_t *bufPtr = buf; | |
698 | const char *pszPtr = psz; | |
699 | ||
700 | if (buf) | |
701 | { | |
702 | // have destination buffer, convert there | |
703 | cres = iconv(m2w, | |
704 | ICONV_CHAR_CAST(&pszPtr), &inbuf, | |
705 | (char**)&bufPtr, &outbuf); | |
706 | res = n - (outbuf / SIZEOF_WCHAR_T); | |
dccce9ea | 707 | |
36acb880 | 708 | if (ms_wcNeedsSwap) |
3a0d76bc | 709 | { |
36acb880 VZ |
710 | // convert to native endianness |
711 | WC_BSWAP(buf /* _not_ bufPtr */, res) | |
3a0d76bc | 712 | } |
36acb880 VZ |
713 | } |
714 | else | |
715 | { | |
716 | // no destination buffer... convert using temp buffer | |
717 | // to calculate destination buffer requirement | |
718 | wchar_t tbuf[8]; | |
719 | res = 0; | |
720 | do { | |
721 | bufPtr = tbuf; | |
722 | outbuf = 8*SIZEOF_WCHAR_T; | |
723 | ||
724 | cres = iconv(m2w, | |
725 | ICONV_CHAR_CAST(&pszPtr), &inbuf, | |
726 | (char**)&bufPtr, &outbuf ); | |
727 | ||
728 | res += 8-(outbuf/SIZEOF_WCHAR_T); | |
729 | } while ((cres==(size_t)-1) && (errno==E2BIG)); | |
f1339c56 | 730 | } |
dccce9ea | 731 | |
36acb880 | 732 | if (ICONV_FAILED(cres, inbuf)) |
f1339c56 | 733 | { |
36acb880 VZ |
734 | //VS: it is ok if iconv fails, hence trace only |
735 | wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode())); | |
736 | return (size_t)-1; | |
737 | } | |
738 | ||
739 | return res; | |
740 | } | |
741 | ||
742 | size_t IC_CharSet::WC2MB(char *buf, const wchar_t *psz, size_t n) | |
743 | { | |
1cd52418 | 744 | #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530) |
36acb880 | 745 | size_t inbuf = std::wcslen(psz) * SIZEOF_WCHAR_T; |
1cd52418 | 746 | #else |
36acb880 | 747 | size_t inbuf = ::wcslen(psz) * SIZEOF_WCHAR_T; |
1cd52418 | 748 | #endif |
36acb880 VZ |
749 | size_t outbuf = n; |
750 | size_t res, cres; | |
3a0d76bc | 751 | |
36acb880 | 752 | wchar_t *tmpbuf = 0; |
3caec1bb | 753 | |
36acb880 VZ |
754 | if (ms_wcNeedsSwap) |
755 | { | |
756 | // need to copy to temp buffer to switch endianness | |
757 | // this absolutely doesn't rock! | |
758 | // (no, doing WC_BSWAP twice on the original buffer won't help, as it | |
759 | // could be in read-only memory, or be accessed in some other thread) | |
760 | tmpbuf=(wchar_t*)malloc((inbuf+1)*SIZEOF_WCHAR_T); | |
761 | memcpy(tmpbuf,psz,(inbuf+1)*SIZEOF_WCHAR_T); | |
762 | WC_BSWAP(tmpbuf, inbuf) | |
763 | psz=tmpbuf; | |
764 | } | |
3a0d76bc | 765 | |
36acb880 VZ |
766 | if (buf) |
767 | { | |
768 | // have destination buffer, convert there | |
769 | cres = iconv( w2m, ICONV_CHAR_CAST(&psz), &inbuf, &buf, &outbuf ); | |
3a0d76bc | 770 | |
36acb880 VZ |
771 | res = n-outbuf; |
772 | } | |
773 | else | |
774 | { | |
775 | // no destination buffer... convert using temp buffer | |
776 | // to calculate destination buffer requirement | |
777 | char tbuf[16]; | |
778 | res = 0; | |
779 | do { | |
780 | buf = tbuf; outbuf = 16; | |
781 | ||
782 | cres = iconv( w2m, ICONV_CHAR_CAST(&psz), &inbuf, &buf, &outbuf ); | |
dccce9ea | 783 | |
36acb880 VZ |
784 | res += 16 - outbuf; |
785 | } while ((cres==(size_t)-1) && (errno==E2BIG)); | |
f1339c56 | 786 | } |
dccce9ea | 787 | |
36acb880 VZ |
788 | if (ms_wcNeedsSwap) |
789 | { | |
790 | free(tmpbuf); | |
791 | } | |
dccce9ea | 792 | |
36acb880 VZ |
793 | if (ICONV_FAILED(cres, inbuf)) |
794 | { | |
795 | //VS: it is ok if iconv fails, hence trace only | |
796 | wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode())); | |
797 | return (size_t)-1; | |
798 | } | |
799 | ||
800 | return res; | |
801 | } | |
802 | ||
b040e242 | 803 | #endif // HAVE_ICONV |
36acb880 VZ |
804 | |
805 | // ============================================================================ | |
806 | // Win32 conversion classes | |
807 | // ============================================================================ | |
1cd52418 | 808 | |
04ef50df | 809 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) |
1cd52418 OK |
810 | class CP_CharSet : public wxCharacterSet |
811 | { | |
812 | public: | |
b1d66b54 VZ |
813 | CP_CharSet(const wxChar* name) |
814 | : wxCharacterSet(name) | |
815 | { | |
816 | m_CodePage = wxCharsetToCodepage(name); | |
817 | } | |
dccce9ea | 818 | |
4def3b35 | 819 | size_t MB2WC(wchar_t *buf, const char *psz, size_t n) |
f1339c56 | 820 | { |
dccce9ea | 821 | size_t len = |
b1d66b54 | 822 | MultiByteToWideChar(m_CodePage, 0, psz, -1, buf, buf ? n : 0); |
1e6feb95 | 823 | //VS: returns # of written chars for buf!=NULL and *size* |
35d764b0 VS |
824 | // needed buffer for buf==NULL |
825 | return len ? (buf ? len : len-1) : (size_t)-1; | |
f1339c56 | 826 | } |
dccce9ea | 827 | |
4def3b35 | 828 | size_t WC2MB(char *buf, const wchar_t *psz, size_t n) |
f1339c56 | 829 | { |
b1d66b54 | 830 | size_t len = WideCharToMultiByte(m_CodePage, 0, psz, -1, buf, |
4def3b35 | 831 | buf ? n : 0, NULL, NULL); |
1e6feb95 | 832 | //VS: returns # of written chars for buf!=NULL and *size* |
35d764b0 VS |
833 | // needed buffer for buf==NULL |
834 | return len ? (buf ? len : len-1) : (size_t)-1; | |
f1339c56 | 835 | } |
dccce9ea | 836 | |
4f61e22c | 837 | bool usable() const |
b1d66b54 | 838 | { return m_CodePage != -1; } |
f1339c56 RR |
839 | |
840 | public: | |
b1d66b54 | 841 | long m_CodePage; |
1cd52418 | 842 | }; |
1e6feb95 VZ |
843 | #endif // __WIN32__ |
844 | ||
36acb880 VZ |
845 | // ============================================================================ |
846 | // wxEncodingConverter based conversion classes | |
847 | // ============================================================================ | |
848 | ||
1e6feb95 | 849 | #if wxUSE_FONTMAP |
1cd52418 OK |
850 | |
851 | class EC_CharSet : public wxCharacterSet | |
852 | { | |
6001e347 | 853 | public: |
f1339c56 RR |
854 | // temporarily just use wxEncodingConverter stuff, |
855 | // so that it works while a better implementation is built | |
b1d66b54 VZ |
856 | EC_CharSet(const wxChar* name) : wxCharacterSet(name), |
857 | enc(wxFONTENCODING_SYSTEM) | |
f1339c56 RR |
858 | { |
859 | if (name) | |
860 | enc = wxTheFontMapper->CharsetToEncoding(name, FALSE); | |
cafbf6fb VZ |
861 | |
862 | m_ok = m2w.Init(enc, wxFONTENCODING_UNICODE) && | |
863 | w2m.Init(wxFONTENCODING_UNICODE, enc); | |
f1339c56 | 864 | } |
dccce9ea | 865 | |
4def3b35 | 866 | size_t MB2WC(wchar_t *buf, const char *psz, size_t n) |
f1339c56 RR |
867 | { |
868 | size_t inbuf = strlen(psz); | |
dccce9ea | 869 | if (buf) |
4def3b35 | 870 | m2w.Convert(psz,buf); |
f1339c56 RR |
871 | return inbuf; |
872 | } | |
dccce9ea | 873 | |
4def3b35 | 874 | size_t WC2MB(char *buf, const wchar_t *psz, size_t n) |
f1339c56 | 875 | { |
de85a884 VZ |
876 | #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \ |
877 | || ( defined(__MWERKS__) && defined(__WXMSW__) ) | |
f1339c56 | 878 | size_t inbuf = std::wcslen(psz); |
1cd52418 | 879 | #else |
f1339c56 | 880 | size_t inbuf = ::wcslen(psz); |
1cd52418 | 881 | #endif |
f1339c56 RR |
882 | if (buf) |
883 | w2m.Convert(psz,buf); | |
dccce9ea | 884 | |
f1339c56 RR |
885 | return inbuf; |
886 | } | |
dccce9ea | 887 | |
cafbf6fb | 888 | bool usable() const { return m_ok; } |
f1339c56 RR |
889 | |
890 | public: | |
891 | wxFontEncoding enc; | |
892 | wxEncodingConverter m2w, w2m; | |
cafbf6fb VZ |
893 | |
894 | // were we initialized successfully? | |
895 | bool m_ok; | |
f6bcfd97 | 896 | }; |
6001e347 | 897 | |
1e6feb95 VZ |
898 | #endif // wxUSE_FONTMAP |
899 | ||
36acb880 VZ |
900 | // ---------------------------------------------------------------------------- |
901 | // the function creating the wxCharacterSet for the specified charset on the | |
902 | // current system, trying all possibilities | |
903 | // ---------------------------------------------------------------------------- | |
904 | ||
f6bcfd97 | 905 | static wxCharacterSet *wxGetCharacterSet(const wxChar *name) |
6001e347 | 906 | { |
cafbf6fb VZ |
907 | // check for the special case of ASCII charset |
908 | #if wxUSE_FONTMAP | |
909 | if ( wxTheFontMapper->CharsetToEncoding(name) == wxFONTENCODING_DEFAULT ) | |
910 | #else // wxUSE_FONTMAP | |
911 | if ( !name ) | |
912 | #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP | |
f1339c56 | 913 | { |
cafbf6fb VZ |
914 | // don't convert at all |
915 | return NULL; | |
1cd52418 | 916 | } |
dccce9ea | 917 | |
cafbf6fb VZ |
918 | // the test above must have taken care of this case |
919 | wxCHECK_MSG( name, NULL, _T("NULL name must be wxFONTENCODING_DEFAULT") ); | |
1e6feb95 | 920 | |
cafbf6fb VZ |
921 | wxCharacterSet *cset; |
922 | ||
923 | if ( wxStricmp(name, wxT("UTF8")) == 0 || wxStricmp(name, wxT("UTF-8")) == 0) | |
924 | { | |
925 | cset = new ID_CharSet(name, &wxConvUTF8); | |
926 | } | |
927 | else | |
dccce9ea | 928 | { |
cafbf6fb VZ |
929 | #ifdef HAVE_ICONV |
930 | cset = new IC_CharSet(name); | |
931 | #else // !HAVE_ICONV | |
dccce9ea | 932 | cset = NULL; |
cafbf6fb | 933 | #endif // HAVE_ICONV/!HAVE_ICONV |
dccce9ea VZ |
934 | } |
935 | ||
734eda8a VZ |
936 | // it can only be NULL in this case |
937 | #ifndef HAVE_ICONV | |
938 | if ( cset ) | |
939 | #endif // !HAVE_ICONV | |
940 | { | |
941 | if ( cset->usable() ) | |
942 | return cset; | |
cafbf6fb | 943 | |
734eda8a VZ |
944 | delete cset; |
945 | cset = NULL; | |
946 | } | |
cafbf6fb | 947 | |
04ef50df | 948 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) |
cafbf6fb VZ |
949 | cset = new CP_CharSet(name); |
950 | if ( cset->usable() ) | |
dccce9ea VZ |
951 | return cset; |
952 | ||
953 | delete cset; | |
cafbf6fb | 954 | cset = NULL; |
dccce9ea VZ |
955 | #endif // __WIN32__ |
956 | ||
1e6feb95 | 957 | #if wxUSE_FONTMAP |
f1339c56 | 958 | cset = new EC_CharSet(name); |
cafbf6fb | 959 | if ( cset->usable() ) |
dccce9ea VZ |
960 | return cset; |
961 | ||
f1339c56 | 962 | delete cset; |
cafbf6fb VZ |
963 | cset = NULL; |
964 | #endif // wxUSE_FONTMAP | |
965 | ||
966 | wxLogError(_("Cannot convert from encoding '%s'!"), name); | |
967 | ||
f1339c56 | 968 | return NULL; |
6001e347 RR |
969 | } |
970 | ||
36acb880 VZ |
971 | // ============================================================================ |
972 | // wxCSConv implementation | |
973 | // ============================================================================ | |
974 | ||
6001e347 RR |
975 | wxCSConv::wxCSConv(const wxChar *charset) |
976 | { | |
dccce9ea | 977 | m_name = (wxChar *)NULL; |
f1339c56 | 978 | m_cset = (wxCharacterSet *) NULL; |
82713003 VZ |
979 | m_deferred = TRUE; |
980 | ||
f1339c56 | 981 | SetName(charset); |
6001e347 RR |
982 | } |
983 | ||
984 | wxCSConv::~wxCSConv() | |
985 | { | |
dccce9ea VZ |
986 | free(m_name); |
987 | delete m_cset; | |
6001e347 RR |
988 | } |
989 | ||
990 | void wxCSConv::SetName(const wxChar *charset) | |
991 | { | |
f1339c56 RR |
992 | if (charset) |
993 | { | |
994 | m_name = wxStrdup(charset); | |
995 | m_deferred = TRUE; | |
996 | } | |
6001e347 RR |
997 | } |
998 | ||
999 | void wxCSConv::LoadNow() | |
1000 | { | |
f1339c56 RR |
1001 | if (m_deferred) |
1002 | { | |
dccce9ea | 1003 | if ( !m_name ) |
f1339c56 | 1004 | { |
dccce9ea VZ |
1005 | wxString name = wxLocale::GetSystemEncodingName(); |
1006 | if ( !name.empty() ) | |
1007 | SetName(name); | |
f1339c56 | 1008 | } |
dccce9ea | 1009 | |
a45a98fb VZ |
1010 | // wxGetCharacterSet() complains about NULL name |
1011 | m_cset = m_name ? wxGetCharacterSet(m_name) : NULL; | |
f1339c56 | 1012 | m_deferred = FALSE; |
6001e347 | 1013 | } |
6001e347 RR |
1014 | } |
1015 | ||
1016 | size_t wxCSConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
1017 | { | |
f1339c56 | 1018 | ((wxCSConv *)this)->LoadNow(); // discard constness |
dccce9ea | 1019 | |
f1339c56 RR |
1020 | if (m_cset) |
1021 | return m_cset->MB2WC(buf, psz, n); | |
1022 | ||
1023 | // latin-1 (direct) | |
4def3b35 | 1024 | size_t len = strlen(psz); |
dccce9ea | 1025 | |
f1339c56 RR |
1026 | if (buf) |
1027 | { | |
4def3b35 | 1028 | for (size_t c = 0; c <= len; c++) |
f1339c56 RR |
1029 | buf[c] = (unsigned char)(psz[c]); |
1030 | } | |
dccce9ea | 1031 | |
f1339c56 | 1032 | return len; |
6001e347 RR |
1033 | } |
1034 | ||
1035 | size_t wxCSConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
1036 | { | |
f1339c56 | 1037 | ((wxCSConv *)this)->LoadNow(); // discard constness |
dccce9ea | 1038 | |
f1339c56 RR |
1039 | if (m_cset) |
1040 | return m_cset->WC2MB(buf, psz, n); | |
1cd52418 | 1041 | |
f1339c56 | 1042 | // latin-1 (direct) |
de85a884 VZ |
1043 | #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \ |
1044 | || ( defined(__MWERKS__) && defined(__WXMSW__) ) | |
f1339c56 | 1045 | size_t len=std::wcslen(psz); |
d834f22c | 1046 | #else |
f1339c56 | 1047 | size_t len=::wcslen(psz); |
d834f22c | 1048 | #endif |
f1339c56 RR |
1049 | if (buf) |
1050 | { | |
4def3b35 VS |
1051 | for (size_t c = 0; c <= len; c++) |
1052 | buf[c] = (psz[c] > 0xff) ? '?' : psz[c]; | |
f1339c56 | 1053 | } |
dccce9ea | 1054 | |
f1339c56 | 1055 | return len; |
6001e347 RR |
1056 | } |
1057 | ||
f6bcfd97 BP |
1058 | #else // !wxUSE_WCHAR_T |
1059 | ||
1060 | // ---------------------------------------------------------------------------- | |
1061 | // stand-ins in absence of wchar_t | |
1062 | // ---------------------------------------------------------------------------- | |
1063 | ||
1064 | WXDLLEXPORT_DATA(wxMBConv) wxConvLibc, wxConvFile; | |
1065 | ||
1066 | #endif // wxUSE_WCHAR_T | |
6001e347 RR |
1067 | |
1068 |