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