]>
Commit | Line | Data |
---|---|---|
6001e347 RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: strconv.cpp | |
3 | // Purpose: Unicode conversion classes | |
4 | // Author: Ove Kaaven, Robert Roebling, Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 29/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1999 Ove Kaaven, Robert Roebling, Vadim Zeitlin | |
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 | ||
40 | #ifdef __SALFORDC__ | |
41 | #include <clib.h> | |
42 | #endif | |
43 | ||
1cd52418 OK |
44 | #ifdef HAVE_ICONV_H |
45 | #include <iconv.h> | |
46 | #endif | |
47 | #ifdef HAVE_LANGINFO_H | |
48 | #include <langinfo.h> | |
49 | #endif | |
50 | ||
3e61dfb0 OK |
51 | #ifdef __WXMSW__ |
52 | #include <windows.h> | |
53 | #endif | |
54 | ||
6001e347 RR |
55 | #include "wx/debug.h" |
56 | #include "wx/strconv.h" | |
3caec1bb VS |
57 | #include "wx/intl.h" |
58 | #include "wx/log.h" | |
6001e347 | 59 | |
d43088ee | 60 | #if defined(WORDS_BIGENDIAN) || defined(__STDC_ISO_10646__) |
1cd52418 OK |
61 | #define BSWAP_UCS4(str, len) |
62 | #define BSWAP_UCS2(str, len) | |
63 | #else | |
64 | #define BSWAP_UCS4(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT32_SWAP_ALWAYS(str[_c]); } | |
65 | #define BSWAP_UCS2(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT16_SWAP_ALWAYS(str[_c]); } | |
66 | #define WC_NEED_BSWAP | |
67 | #endif | |
68 | #define BSWAP_UTF32(str, len) BSWAP_UCS4(str, len) | |
69 | #define BSWAP_UTF16(str, len) BSWAP_UCS2(str, len) | |
70 | ||
71 | #if SIZEOF_WCHAR_T == 4 | |
72 | #define WC_NAME "UCS4" | |
73 | #define WC_BSWAP BSWAP_UCS4 | |
74 | #elif SIZEOF_WCHAR_T == 2 | |
75 | #define WC_NAME "UTF16" | |
76 | #define WC_BSWAP BSWAP_UTF16 | |
77 | #define WC_UTF16 | |
78 | #endif | |
79 | ||
f6bcfd97 BP |
80 | // ---------------------------------------------------------------------------- |
81 | // globals | |
82 | // ---------------------------------------------------------------------------- | |
6001e347 RR |
83 | |
84 | WXDLLEXPORT_DATA(wxMBConv *) wxConvCurrent = &wxConvLibc; | |
85 | ||
f6bcfd97 BP |
86 | // ============================================================================ |
87 | // implementation | |
88 | // ============================================================================ | |
6001e347 | 89 | |
f6bcfd97 | 90 | #if wxUSE_WCHAR_T |
6001e347 | 91 | |
b0a6bb75 VZ |
92 | #ifdef WC_UTF16 |
93 | ||
1cd52418 OK |
94 | static size_t encode_utf16(wxUint32 input,wxUint16*output) |
95 | { | |
4def3b35 VS |
96 | if (input<=0xffff) |
97 | { | |
98 | if (output) *output++ = input; | |
99 | return 1; | |
100 | } | |
101 | else if (input>=0x110000) | |
102 | { | |
103 | return (size_t)-1; | |
104 | } | |
105 | else | |
106 | { | |
107 | if (output) | |
108 | { | |
109 | *output++ = (input >> 10)+0xd7c0; | |
110 | *output++ = (input&0x3ff)+0xdc00; | |
111 | } | |
112 | return 2; | |
1cd52418 | 113 | } |
1cd52418 OK |
114 | } |
115 | ||
116 | static size_t decode_utf16(wxUint16*input,wxUint32&output) | |
117 | { | |
4def3b35 VS |
118 | if ((*input<0xd800) || (*input>0xdfff)) |
119 | { | |
120 | output = *input; | |
121 | return 1; | |
122 | } | |
123 | else if ((input[1]<0xdc00) || (input[1]>=0xdfff)) | |
124 | { | |
125 | output = *input; | |
126 | return (size_t)-1; | |
127 | } | |
128 | else | |
129 | { | |
130 | output = ((input[0] - 0xd7c0) << 10) + (input[1] - 0xdc00); | |
131 | return 2; | |
132 | } | |
1cd52418 OK |
133 | } |
134 | ||
b0a6bb75 VZ |
135 | #endif // WC_UTF16 |
136 | ||
f6bcfd97 | 137 | // ---------------------------------------------------------------------------- |
6001e347 | 138 | // wxMBConv |
f6bcfd97 | 139 | // ---------------------------------------------------------------------------- |
6001e347 RR |
140 | |
141 | WXDLLEXPORT_DATA(wxMBConv) wxConvLibc; | |
142 | ||
143 | size_t wxMBConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
144 | { | |
145 | return wxMB2WC(buf, psz, n); | |
146 | } | |
147 | ||
148 | size_t wxMBConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
149 | { | |
150 | return wxWC2MB(buf, psz, n); | |
151 | } | |
152 | ||
153 | const wxWCharBuffer wxMBConv::cMB2WC(const char *psz) const | |
154 | { | |
f6bcfd97 | 155 | if (psz) |
6001e347 RR |
156 | { |
157 | size_t nLen = MB2WC((wchar_t *) NULL, psz, 0); | |
f6bcfd97 BP |
158 | if (nLen == (size_t)-1) |
159 | return wxWCharBuffer((wchar_t *) NULL); | |
6001e347 RR |
160 | wxWCharBuffer buf(nLen); |
161 | MB2WC((wchar_t *)(const wchar_t *) buf, psz, nLen); | |
162 | return buf; | |
f6bcfd97 BP |
163 | } |
164 | else | |
6001e347 RR |
165 | return wxWCharBuffer((wchar_t *) NULL); |
166 | } | |
167 | ||
168 | const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *psz) const | |
169 | { | |
f6bcfd97 | 170 | if (psz) |
6001e347 RR |
171 | { |
172 | size_t nLen = WC2MB((char *) NULL, psz, 0); | |
f6bcfd97 BP |
173 | if (nLen == (size_t)-1) |
174 | return wxCharBuffer((char *) NULL); | |
6001e347 RR |
175 | wxCharBuffer buf(nLen); |
176 | WC2MB((char *)(const char *) buf, psz, nLen); | |
177 | return buf; | |
f6bcfd97 BP |
178 | } |
179 | else | |
6001e347 RR |
180 | return wxCharBuffer((char *) NULL); |
181 | } | |
182 | ||
f6bcfd97 | 183 | // ---------------------------------------------------------------------------- |
6001e347 | 184 | // standard file conversion |
f6bcfd97 | 185 | // ---------------------------------------------------------------------------- |
6001e347 RR |
186 | |
187 | WXDLLEXPORT_DATA(wxMBConvFile) wxConvFile; | |
188 | ||
189 | // just use the libc conversion for now | |
190 | size_t wxMBConvFile::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
191 | { | |
192 | return wxMB2WC(buf, psz, n); | |
193 | } | |
194 | ||
195 | size_t wxMBConvFile::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
196 | { | |
197 | return wxWC2MB(buf, psz, n); | |
198 | } | |
199 | ||
f6bcfd97 | 200 | // ---------------------------------------------------------------------------- |
6001e347 | 201 | // standard gdk conversion |
f6bcfd97 BP |
202 | // ---------------------------------------------------------------------------- |
203 | ||
204 | #ifdef __WXGTK12__ | |
6001e347 RR |
205 | |
206 | WXDLLEXPORT_DATA(wxMBConvGdk) wxConvGdk; | |
207 | ||
208 | #include <gdk/gdk.h> | |
209 | ||
210 | size_t wxMBConvGdk::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
211 | { | |
4def3b35 VS |
212 | if (buf) |
213 | { | |
214 | return gdk_mbstowcs((GdkWChar *)buf, psz, n); | |
215 | } | |
216 | else | |
217 | { | |
218 | GdkWChar *nbuf = new GdkWChar[n=strlen(psz)]; | |
219 | size_t len = gdk_mbstowcs(nbuf, psz, n); | |
220 | delete[] nbuf; | |
221 | return len; | |
222 | } | |
6001e347 RR |
223 | } |
224 | ||
225 | size_t wxMBConvGdk::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
226 | { | |
4def3b35 VS |
227 | char *mbstr = gdk_wcstombs((GdkWChar *)psz); |
228 | size_t len = mbstr ? strlen(mbstr) : 0; | |
229 | if (buf) | |
230 | { | |
231 | if (len > n) | |
232 | len = n; | |
233 | memcpy(buf, psz, len); | |
234 | if (len < n) | |
235 | buf[len] = 0; | |
236 | } | |
237 | return len; | |
6001e347 | 238 | } |
f6bcfd97 | 239 | |
6001e347 RR |
240 | #endif // GTK > 1.0 |
241 | ||
242 | // ---------------------------------------------------------------------------- | |
243 | // UTF-7 | |
244 | // ---------------------------------------------------------------------------- | |
245 | ||
246 | WXDLLEXPORT_DATA(wxMBConvUTF7) wxConvUTF7; | |
247 | ||
248 | #if 0 | |
249 | static char utf7_setD[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
250 | "abcdefghijklmnopqrstuvwxyz" | |
251 | "0123456789'(),-./:?"; | |
252 | static char utf7_setO[]="!\"#$%&*;<=>@[]^_`{|}"; | |
253 | static char utf7_setB[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
254 | "abcdefghijklmnopqrstuvwxyz" | |
255 | "0123456789+/"; | |
256 | #endif | |
257 | ||
258 | // TODO: write actual implementations of UTF-7 here | |
259 | size_t wxMBConvUTF7::MB2WC(wchar_t * WXUNUSED(buf), | |
260 | const char * WXUNUSED(psz), | |
261 | size_t WXUNUSED(n)) const | |
262 | { | |
263 | return 0; | |
264 | } | |
265 | ||
266 | size_t wxMBConvUTF7::WC2MB(char * WXUNUSED(buf), | |
267 | const wchar_t * WXUNUSED(psz), | |
268 | size_t WXUNUSED(n)) const | |
269 | { | |
270 | return 0; | |
271 | } | |
272 | ||
f6bcfd97 | 273 | // ---------------------------------------------------------------------------- |
6001e347 | 274 | // UTF-8 |
f6bcfd97 | 275 | // ---------------------------------------------------------------------------- |
6001e347 RR |
276 | |
277 | WXDLLEXPORT_DATA(wxMBConvUTF8) wxConvUTF8; | |
278 | ||
4def3b35 VS |
279 | static wxUint32 utf8_max[]= |
280 | { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff, 0xffffffff }; | |
6001e347 RR |
281 | |
282 | size_t wxMBConvUTF8::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
283 | { | |
4def3b35 VS |
284 | size_t len = 0; |
285 | ||
286 | while (*psz && ((!buf) || (len < n))) | |
287 | { | |
288 | unsigned char cc = *psz++, fc = cc; | |
289 | unsigned cnt; | |
290 | for (cnt = 0; fc & 0x80; cnt++) | |
291 | fc <<= 1; | |
292 | if (!cnt) | |
293 | { | |
294 | // plain ASCII char | |
295 | if (buf) | |
296 | *buf++ = cc; | |
297 | len++; | |
298 | } | |
299 | else | |
300 | { | |
301 | cnt--; | |
302 | if (!cnt) | |
303 | { | |
304 | // invalid UTF-8 sequence | |
305 | return (size_t)-1; | |
306 | } | |
307 | else | |
308 | { | |
309 | unsigned ocnt = cnt - 1; | |
310 | wxUint32 res = cc & (0x3f >> cnt); | |
311 | while (cnt--) | |
312 | { | |
313 | cc = *psz++; | |
314 | if ((cc & 0xC0) != 0x80) | |
315 | { | |
316 | // invalid UTF-8 sequence | |
317 | return (size_t)-1; | |
318 | } | |
319 | res = (res << 6) | (cc & 0x3f); | |
320 | } | |
321 | if (res <= utf8_max[ocnt]) | |
322 | { | |
323 | // illegal UTF-8 encoding | |
324 | return (size_t)-1; | |
325 | } | |
1cd52418 | 326 | #ifdef WC_UTF16 |
4def3b35 VS |
327 | size_t pa = encode_utf16(res, buf); |
328 | if (pa == (size_t)-1) | |
329 | return (size_t)-1; | |
330 | if (buf) | |
331 | buf += pa; | |
332 | len += pa; | |
1cd52418 | 333 | #else |
4def3b35 VS |
334 | if (buf) |
335 | *buf++ = res; | |
336 | len++; | |
1cd52418 | 337 | #endif |
4def3b35 VS |
338 | } |
339 | } | |
6001e347 | 340 | } |
4def3b35 VS |
341 | if (buf && (len < n)) |
342 | *buf = 0; | |
343 | return len; | |
6001e347 RR |
344 | } |
345 | ||
346 | size_t wxMBConvUTF8::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
347 | { | |
4def3b35 | 348 | size_t len = 0; |
6001e347 | 349 | |
4def3b35 VS |
350 | while (*psz && ((!buf) || (len < n))) |
351 | { | |
352 | wxUint32 cc; | |
1cd52418 | 353 | #ifdef WC_UTF16 |
4def3b35 VS |
354 | size_t pa = decode_utf16(psz,cc); |
355 | psz += (pa == (size_t)-1) ? 1 : pa; | |
1cd52418 | 356 | #else |
4def3b35 VS |
357 | cc=(*psz++) & 0x7fffffff; |
358 | #endif | |
359 | unsigned cnt; | |
360 | for (cnt = 0; cc > utf8_max[cnt]; cnt++) {} | |
361 | if (!cnt) | |
362 | { | |
363 | // plain ASCII char | |
364 | if (buf) | |
365 | *buf++ = cc; | |
366 | len++; | |
367 | } | |
368 | ||
369 | else | |
370 | { | |
371 | len += cnt + 1; | |
372 | if (buf) | |
373 | { | |
374 | *buf++ = (-128 >> cnt) | ((cc >> (cnt * 6)) & (0x3f >> cnt)); | |
375 | while (cnt--) | |
376 | *buf++ = 0x80 | ((cc >> (cnt * 6)) & 0x3f); | |
377 | } | |
378 | } | |
6001e347 | 379 | } |
4def3b35 VS |
380 | |
381 | if (buf && (len<n)) *buf = 0; | |
382 | return len; | |
6001e347 RR |
383 | } |
384 | ||
385 | // ---------------------------------------------------------------------------- | |
386 | // specified character set | |
387 | // ---------------------------------------------------------------------------- | |
388 | ||
f6bcfd97 BP |
389 | WXDLLEXPORT_DATA(wxCSConv) wxConvLocal((const wxChar *)NULL); |
390 | ||
391 | #include "wx/encconv.h" | |
392 | #include "wx/fontmap.h" | |
6001e347 | 393 | |
1cd52418 OK |
394 | // TODO: add some tables here |
395 | // - perhaps common encodings to common codepages (for Win32) | |
396 | // - perhaps common encodings to objects ("UTF8" -> wxConvUTF8) | |
397 | // - move wxEncodingConverter meat in here | |
398 | ||
399 | #ifdef __WIN32__ | |
400 | #include "wx/msw/registry.h" | |
401 | // this should work if M$ Internet Exploiter is installed | |
402 | static long CharsetToCodepage(const wxChar *name) | |
403 | { | |
f1339c56 RR |
404 | if (!name) |
405 | return GetACP(); | |
406 | ||
407 | long CP=-1; | |
408 | ||
409 | wxString cn(name); | |
410 | do { | |
5ce0e4ac | 411 | wxString path(wxT("MIME\\Database\\Charset\\")); |
f1339c56 | 412 | path += cn; |
5ce0e4ac VS |
413 | wxRegKey key(wxRegKey::HKCR, path); |
414 | ||
415 | if (!key.Exists()) continue; | |
416 | ||
417 | // two cases: either there's an AliasForCharset string, | |
418 | // or there are Codepage and InternetEncoding dwords. | |
419 | // The InternetEncoding gives us the actual encoding, | |
420 | // the Codepage just says which Windows character set to | |
421 | // use when displaying the data. | |
422 | if (key.HasValue(wxT("InternetEncoding")) && | |
423 | key.QueryValue(wxT("InternetEncoding"), &CP)) break; | |
f1339c56 RR |
424 | |
425 | // no encoding, see if it's an alias | |
5ce0e4ac VS |
426 | if (!key.HasValue(wxT("AliasForCharset")) || |
427 | !key.QueryValue(wxT("AliasForCharset"), cn)) break; | |
f1339c56 RR |
428 | } while (1); |
429 | ||
430 | return CP; | |
1cd52418 OK |
431 | } |
432 | #endif | |
433 | ||
6001e347 RR |
434 | class wxCharacterSet |
435 | { | |
1cd52418 | 436 | public: |
f1339c56 RR |
437 | wxCharacterSet(const wxChar*name) |
438 | : cname(name) {} | |
439 | virtual ~wxCharacterSet() | |
440 | {} | |
4def3b35 | 441 | virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) |
f1339c56 | 442 | { return (size_t)-1; } |
4def3b35 | 443 | virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) |
f1339c56 RR |
444 | { return (size_t)-1; } |
445 | virtual bool usable() | |
446 | { return FALSE; } | |
447 | public: | |
448 | const wxChar*cname; | |
1cd52418 OK |
449 | }; |
450 | ||
451 | class ID_CharSet : public wxCharacterSet | |
452 | { | |
453 | public: | |
4def3b35 | 454 | ID_CharSet(const wxChar *name,wxMBConv *cnv) |
f1339c56 RR |
455 | : wxCharacterSet(name), work(cnv) {} |
456 | ||
4def3b35 | 457 | size_t MB2WC(wchar_t *buf, const char *psz, size_t n) |
f1339c56 RR |
458 | { return work ? work->MB2WC(buf,psz,n) : (size_t)-1; } |
459 | ||
4def3b35 | 460 | size_t WC2MB(char *buf, const wchar_t *psz, size_t n) |
f1339c56 RR |
461 | { return work ? work->WC2MB(buf,psz,n) : (size_t)-1; } |
462 | ||
463 | bool usable() | |
464 | { return work!=NULL; } | |
465 | public: | |
466 | wxMBConv*work; | |
1cd52418 OK |
467 | }; |
468 | ||
3caec1bb | 469 | |
1cd52418 | 470 | #ifdef HAVE_ICONV_H |
3caec1bb VS |
471 | |
472 | // VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with E2BIG | |
473 | // if output buffer is _exactly_ as big as needed. Such case is (unless there's | |
474 | // yet another bug in glibc) the only case when iconv() returns with (size_t)-1 | |
475 | // (which means error) and says there are 0 bytes left in the input buffer -- | |
476 | // when _real_ error occurs, bytes-left-in-input buffer is non-zero. Hence, | |
477 | // this alternative test for iconv() failure. | |
478 | // [This bug does not appear in glibc 2.2.] | |
479 | #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 1 | |
480 | #define ICONV_FAILED(cres, bufLeft) ((cres == (size_t)-1) && \ | |
481 | (errno != E2BIG || bufLeft != 0)) | |
482 | #else | |
483 | #define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1) | |
484 | #endif | |
485 | ||
1cd52418 OK |
486 | class IC_CharSet : public wxCharacterSet |
487 | { | |
488 | public: | |
4def3b35 | 489 | IC_CharSet(const wxChar *name) |
3caec1bb | 490 | : wxCharacterSet(name) |
f1339c56 | 491 | { |
3caec1bb VS |
492 | m2w = iconv_open(WC_NAME, wxConvLibc.cWX2MB(cname)); |
493 | w2m = iconv_open(wxConvLibc.cWX2MB(cname), WC_NAME); | |
f1339c56 RR |
494 | } |
495 | ||
3caec1bb VS |
496 | ~IC_CharSet() |
497 | { | |
498 | if ( m2w != (iconv_t)-1 ) | |
499 | iconv_close(m2w); | |
500 | if ( w2m != (iconv_t)-1 ) | |
501 | iconv_close(w2m); | |
f1339c56 RR |
502 | } |
503 | ||
3caec1bb | 504 | size_t MB2WC(wchar_t *buf, const char *psz, size_t n) |
f1339c56 | 505 | { |
f1339c56 | 506 | size_t inbuf = strlen(psz); |
3caec1bb | 507 | size_t outbuf = n * SIZEOF_WCHAR_T; |
f1339c56 | 508 | size_t res, cres; |
3caec1bb VS |
509 | // VS: Use these instead of psz, buf because iconv() modifies its arguments: |
510 | wchar_t *bufPtr = buf; | |
511 | const char *pszPtr = psz; | |
512 | ||
f1339c56 RR |
513 | if (buf) |
514 | { | |
515 | // have destination buffer, convert there | |
95c8801c | 516 | #ifdef WX_ICONV_TAKES_CHAR |
3caec1bb | 517 | cres = iconv(m2w, (char**)&pszPtr, &inbuf, (char**)&bufPtr, &outbuf); |
95c8801c | 518 | #else |
3caec1bb | 519 | cres = iconv(m2w, &pszPtr, &inbuf, (char**)&bufPtr, &outbuf); |
95c8801c | 520 | #endif |
3caec1bb | 521 | res = n - (outbuf / SIZEOF_WCHAR_T); |
f1339c56 | 522 | // convert to native endianness |
3caec1bb VS |
523 | #ifdef WC_NEED_BSWAP |
524 | WC_BSWAP(buf /* _not_ bufPtr */, res) | |
525 | #endif | |
f1339c56 RR |
526 | } |
527 | else | |
528 | { | |
529 | // no destination buffer... convert using temp buffer | |
530 | // to calculate destination buffer requirement | |
531 | wchar_t tbuf[8]; | |
532 | res = 0; | |
533 | do { | |
3caec1bb | 534 | bufPtr = tbuf; outbuf = 8*SIZEOF_WCHAR_T; |
95c8801c | 535 | #ifdef WX_ICONV_TAKES_CHAR |
3caec1bb | 536 | cres = iconv( m2w, (char**)&pszPtr, &inbuf, (char**)&bufPtr, &outbuf ); |
95c8801c | 537 | #else |
3caec1bb | 538 | cres = iconv( m2w, &pszPtr, &inbuf, (char**)&bufPtr, &outbuf ); |
95c8801c | 539 | #endif |
f1339c56 RR |
540 | res += 8-(outbuf/SIZEOF_WCHAR_T); |
541 | } while ((cres==(size_t)-1) && (errno==E2BIG)); | |
542 | } | |
543 | ||
3caec1bb | 544 | if (ICONV_FAILED(cres, inbuf)) |
f1339c56 | 545 | return (size_t)-1; |
3caec1bb | 546 | |
f1339c56 RR |
547 | return res; |
548 | } | |
549 | ||
4def3b35 | 550 | size_t WC2MB(char *buf, const wchar_t *psz, size_t n) |
f1339c56 | 551 | { |
1cd52418 | 552 | #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530) |
3caec1bb | 553 | size_t inbuf = std::wcslen(psz) * SIZEOF_WCHAR_T; |
1cd52418 | 554 | #else |
3caec1bb | 555 | size_t inbuf = ::wcslen(psz) * SIZEOF_WCHAR_T; |
1cd52418 | 556 | #endif |
f1339c56 RR |
557 | size_t outbuf = n; |
558 | size_t res, cres; | |
3caec1bb | 559 | |
1cd52418 | 560 | #ifdef WC_NEED_BSWAP |
f1339c56 RR |
561 | // need to copy to temp buffer to switch endianness |
562 | // this absolutely doesn't rock! | |
563 | // (no, doing WC_BSWAP twice on the original buffer won't help, as it | |
564 | // could be in read-only memory, or be accessed in some other thread) | |
4def3b35 | 565 | wchar_t *tmpbuf=(wchar_t*)malloc((inbuf+1)*SIZEOF_WCHAR_T); |
f1339c56 RR |
566 | memcpy(tmpbuf,psz,(inbuf+1)*SIZEOF_WCHAR_T); |
567 | WC_BSWAP(tmpbuf, inbuf) | |
568 | psz=tmpbuf; | |
1cd52418 | 569 | #endif |
f1339c56 RR |
570 | if (buf) |
571 | { | |
572 | // have destination buffer, convert there | |
95c8801c | 573 | #ifdef WX_ICONV_TAKES_CHAR |
f1339c56 | 574 | cres = iconv( w2m, (char**)&psz, &inbuf, &buf, &outbuf ); |
95c8801c VS |
575 | #else |
576 | cres = iconv( w2m, (const char**)&psz, &inbuf, &buf, &outbuf ); | |
577 | #endif | |
f1339c56 RR |
578 | res = n-outbuf; |
579 | } | |
580 | else | |
581 | { | |
582 | // no destination buffer... convert using temp buffer | |
583 | // to calculate destination buffer requirement | |
584 | char tbuf[16]; | |
585 | res = 0; | |
586 | do { | |
587 | buf = tbuf; outbuf = 16; | |
95c8801c | 588 | #ifdef WX_ICONV_TAKES_CHAR |
f1339c56 | 589 | cres = iconv( w2m, (char**)&psz, &inbuf, &buf, &outbuf ); |
95c8801c VS |
590 | #else |
591 | cres = iconv( w2m, (const char**)&psz, &inbuf, &buf, &outbuf ); | |
592 | #endif | |
f1339c56 RR |
593 | res += 16 - outbuf; |
594 | } while ((cres==(size_t)-1) && (errno==E2BIG)); | |
595 | } | |
1cd52418 | 596 | #ifdef WC_NEED_BSWAP |
f1339c56 | 597 | free(tmpbuf); |
1cd52418 | 598 | #endif |
3caec1bb | 599 | if (ICONV_FAILED(cres, inbuf)) |
f1339c56 RR |
600 | return (size_t)-1; |
601 | ||
602 | return res; | |
603 | } | |
604 | ||
605 | bool usable() | |
3caec1bb | 606 | { return (m2w != (iconv_t)-1) && (w2m != (iconv_t)-1); } |
f1339c56 RR |
607 | |
608 | public: | |
609 | iconv_t m2w, w2m; | |
1cd52418 OK |
610 | }; |
611 | #endif | |
612 | ||
613 | #ifdef __WIN32__ | |
614 | class CP_CharSet : public wxCharacterSet | |
615 | { | |
616 | public: | |
f1339c56 RR |
617 | CP_CharSet(const wxChar*name) |
618 | : wxCharacterSet(name), CodePage(CharsetToCodepage(name)) {} | |
619 | ||
4def3b35 | 620 | size_t MB2WC(wchar_t *buf, const char *psz, size_t n) |
f1339c56 | 621 | { |
4def3b35 VS |
622 | size_t len = |
623 | MultiByteToWideChar(CodePage, 0, psz, -1, buf, buf ? n : 0); | |
f1339c56 RR |
624 | return len ? len : (size_t)-1; |
625 | } | |
626 | ||
4def3b35 | 627 | size_t WC2MB(char *buf, const wchar_t *psz, size_t n) |
f1339c56 | 628 | { |
4def3b35 VS |
629 | size_t len = WideCharToMultiByte(CodePage, 0, psz, -1, buf, |
630 | buf ? n : 0, NULL, NULL); | |
f1339c56 RR |
631 | return len ? len : (size_t)-1; |
632 | } | |
633 | ||
634 | bool usable() | |
4def3b35 | 635 | { return CodePage != -1; } |
f1339c56 RR |
636 | |
637 | public: | |
638 | long CodePage; | |
1cd52418 OK |
639 | }; |
640 | #endif | |
641 | ||
642 | class EC_CharSet : public wxCharacterSet | |
643 | { | |
6001e347 | 644 | public: |
f1339c56 RR |
645 | // temporarily just use wxEncodingConverter stuff, |
646 | // so that it works while a better implementation is built | |
4def3b35 VS |
647 | EC_CharSet(const wxChar*name) : wxCharacterSet(name), |
648 | enc(wxFONTENCODING_SYSTEM) | |
f1339c56 RR |
649 | { |
650 | if (name) | |
651 | enc = wxTheFontMapper->CharsetToEncoding(name, FALSE); | |
652 | m2w.Init(enc, wxFONTENCODING_UNICODE); | |
653 | w2m.Init(wxFONTENCODING_UNICODE, enc); | |
654 | } | |
655 | ||
4def3b35 | 656 | size_t MB2WC(wchar_t *buf, const char *psz, size_t n) |
f1339c56 RR |
657 | { |
658 | size_t inbuf = strlen(psz); | |
4def3b35 VS |
659 | if (buf) |
660 | m2w.Convert(psz,buf); | |
f1339c56 RR |
661 | return inbuf; |
662 | } | |
663 | ||
4def3b35 | 664 | size_t WC2MB(char *buf, const wchar_t *psz, size_t n) |
f1339c56 | 665 | { |
1cd52418 | 666 | #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530) |
f1339c56 | 667 | size_t inbuf = std::wcslen(psz); |
1cd52418 | 668 | #else |
f1339c56 | 669 | size_t inbuf = ::wcslen(psz); |
1cd52418 | 670 | #endif |
f1339c56 RR |
671 | if (buf) |
672 | w2m.Convert(psz,buf); | |
673 | ||
674 | return inbuf; | |
675 | } | |
676 | ||
677 | bool usable() | |
678 | { return (enc!=wxFONTENCODING_SYSTEM) && (enc!=wxFONTENCODING_DEFAULT); } | |
679 | ||
680 | public: | |
681 | wxFontEncoding enc; | |
682 | wxEncodingConverter m2w, w2m; | |
f6bcfd97 | 683 | }; |
6001e347 | 684 | |
f6bcfd97 | 685 | static wxCharacterSet *wxGetCharacterSet(const wxChar *name) |
6001e347 | 686 | { |
f1339c56 RR |
687 | wxCharacterSet *cset = NULL; |
688 | if (name) | |
689 | { | |
4def3b35 | 690 | if (wxStricmp(name, wxT("UTF8")) == 0 || wxStricmp(name, wxT("UTF-8")) == 0) |
f1339c56 RR |
691 | { |
692 | cset = new ID_CharSet(name, &wxConvUTF8); | |
693 | } | |
694 | else | |
695 | { | |
1cd52418 | 696 | #ifdef HAVE_ICONV_H |
f1339c56 | 697 | cset = new IC_CharSet(name); // may not take NULL |
1cd52418 | 698 | #endif |
f1339c56 | 699 | } |
1cd52418 | 700 | } |
f1339c56 RR |
701 | |
702 | if (cset && cset->usable()) return cset; | |
703 | if (cset) delete cset; | |
3caec1bb | 704 | cset = NULL; |
1cd52418 | 705 | #ifdef __WIN32__ |
f1339c56 RR |
706 | cset = new CP_CharSet(name); // may take NULL |
707 | if (cset->usable()) return cset; | |
1cd52418 | 708 | #endif |
f1339c56 RR |
709 | if (cset) delete cset; |
710 | cset = new EC_CharSet(name); | |
711 | if (cset->usable()) return cset; | |
712 | delete cset; | |
3caec1bb | 713 | wxLogError(_("Unknown encoding '%s'!"), name); |
f1339c56 | 714 | return NULL; |
6001e347 RR |
715 | } |
716 | ||
6001e347 RR |
717 | wxCSConv::wxCSConv(const wxChar *charset) |
718 | { | |
f1339c56 RR |
719 | m_name = (wxChar *) NULL; |
720 | m_cset = (wxCharacterSet *) NULL; | |
721 | m_deferred = TRUE; | |
722 | SetName(charset); | |
6001e347 RR |
723 | } |
724 | ||
725 | wxCSConv::~wxCSConv() | |
726 | { | |
f1339c56 RR |
727 | if (m_name) free(m_name); |
728 | if (m_cset) delete m_cset; | |
6001e347 RR |
729 | } |
730 | ||
731 | void wxCSConv::SetName(const wxChar *charset) | |
732 | { | |
f1339c56 RR |
733 | if (charset) |
734 | { | |
735 | m_name = wxStrdup(charset); | |
736 | m_deferred = TRUE; | |
737 | } | |
6001e347 RR |
738 | } |
739 | ||
740 | void wxCSConv::LoadNow() | |
741 | { | |
f1339c56 RR |
742 | if (m_deferred) |
743 | { | |
744 | if (!m_name) | |
745 | { | |
6001e347 | 746 | #ifdef __UNIX__ |
1cd52418 | 747 | #if defined(HAVE_LANGINFO_H) && defined(CODESET) |
f1339c56 | 748 | // GNU libc provides current character set this way |
4def3b35 | 749 | char *alang = nl_langinfo(CODESET); |
f1339c56 RR |
750 | if (alang) |
751 | { | |
752 | SetName(wxConvLibc.cMB2WX(alang)); | |
753 | } | |
754 | else | |
1cd52418 | 755 | #endif |
f1339c56 RR |
756 | { |
757 | // if we can't get at the character set directly, | |
758 | // try to see if it's in the environment variables | |
759 | // (in most cases this won't work, but I was out of ideas) | |
760 | wxChar *lang = wxGetenv(wxT("LC_ALL")); | |
23ca6e20 VS |
761 | wxChar *dot = lang ? wxStrchr(lang, wxT('.')) : (wxChar *)NULL; |
762 | if (!dot) | |
763 | { | |
4def3b35 | 764 | lang = wxGetenv(wxT("LC_CTYPE")); |
23ca6e20 VS |
765 | dot = lang ? wxStrchr(lang, wxT('.')) : (wxChar *)NULL; |
766 | } | |
767 | if (!dot) | |
768 | { | |
4def3b35 | 769 | lang = wxGetenv(wxT("LANG")); |
23ca6e20 VS |
770 | dot = lang ? wxStrchr(lang, wxT('.')) : (wxChar *)NULL; |
771 | } | |
772 | if (dot) | |
4def3b35 | 773 | SetName(dot+1); |
f1339c56 | 774 | } |
6001e347 | 775 | #endif |
f1339c56 RR |
776 | } |
777 | m_cset = wxGetCharacterSet(m_name); | |
778 | m_deferred = FALSE; | |
6001e347 | 779 | } |
6001e347 RR |
780 | } |
781 | ||
782 | size_t wxCSConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const | |
783 | { | |
f1339c56 RR |
784 | ((wxCSConv *)this)->LoadNow(); // discard constness |
785 | ||
786 | if (m_cset) | |
787 | return m_cset->MB2WC(buf, psz, n); | |
788 | ||
789 | // latin-1 (direct) | |
4def3b35 | 790 | size_t len = strlen(psz); |
f1339c56 RR |
791 | |
792 | if (buf) | |
793 | { | |
4def3b35 | 794 | for (size_t c = 0; c <= len; c++) |
f1339c56 RR |
795 | buf[c] = (unsigned char)(psz[c]); |
796 | } | |
797 | ||
798 | return len; | |
6001e347 RR |
799 | } |
800 | ||
801 | size_t wxCSConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const | |
802 | { | |
f1339c56 RR |
803 | ((wxCSConv *)this)->LoadNow(); // discard constness |
804 | ||
805 | if (m_cset) | |
806 | return m_cset->WC2MB(buf, psz, n); | |
1cd52418 | 807 | |
f1339c56 | 808 | // latin-1 (direct) |
d834f22c | 809 | #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530) |
f1339c56 | 810 | size_t len=std::wcslen(psz); |
d834f22c | 811 | #else |
f1339c56 | 812 | size_t len=::wcslen(psz); |
d834f22c | 813 | #endif |
f1339c56 RR |
814 | if (buf) |
815 | { | |
4def3b35 VS |
816 | for (size_t c = 0; c <= len; c++) |
817 | buf[c] = (psz[c] > 0xff) ? '?' : psz[c]; | |
f1339c56 RR |
818 | } |
819 | ||
820 | return len; | |
6001e347 RR |
821 | } |
822 | ||
1cd52418 OK |
823 | #ifdef HAVE_ICONV_H |
824 | class IC_CharSetConverter | |
825 | { | |
826 | public: | |
4def3b35 VS |
827 | IC_CharSetConverter(IC_CharSet *from, IC_CharSet *to) |
828 | { | |
829 | cnv = iconv_open(wxConvLibc.cWX2MB(to->cname), | |
830 | wxConvLibc.cWX2MB(from->cname)); | |
831 | } | |
f1339c56 RR |
832 | |
833 | ~IC_CharSetConverter() | |
4def3b35 VS |
834 | { |
835 | if (cnv != (iconv_t)-1) | |
836 | iconv_close(cnv); | |
837 | } | |
f1339c56 | 838 | |
4def3b35 | 839 | size_t Convert(char *buf, const char *psz, size_t n) |
f1339c56 RR |
840 | { |
841 | size_t inbuf = strlen(psz); | |
842 | size_t outbuf = n; | |
95c8801c | 843 | #ifdef WX_ICONV_TAKES_CHAR |
f1339c56 | 844 | size_t res = iconv( cnv, (char**)&psz, &inbuf, &buf, &outbuf ); |
95c8801c VS |
845 | #else |
846 | size_t res = iconv( cnv, &psz, &inbuf, &buf, &outbuf ); | |
847 | #endif | |
4def3b35 VS |
848 | if (res == (size_t)-1) |
849 | return (size_t)-1; | |
850 | return (n - outbuf); | |
f1339c56 RR |
851 | } |
852 | ||
853 | public: | |
854 | iconv_t cnv; | |
1cd52418 OK |
855 | }; |
856 | #endif | |
857 | ||
858 | class EC_CharSetConverter | |
859 | { | |
860 | public: | |
f1339c56 RR |
861 | EC_CharSetConverter(EC_CharSet*from,EC_CharSet*to) |
862 | { cnv.Init(from->enc,to->enc); } | |
863 | ||
864 | size_t Convert(char*buf, const char*psz, size_t n) | |
865 | { | |
866 | size_t inbuf = strlen(psz); | |
867 | if (buf) cnv.Convert(psz,buf); | |
868 | return inbuf; | |
869 | } | |
870 | ||
871 | public: | |
872 | wxEncodingConverter cnv; | |
1cd52418 OK |
873 | }; |
874 | ||
f6bcfd97 BP |
875 | #else // !wxUSE_WCHAR_T |
876 | ||
877 | // ---------------------------------------------------------------------------- | |
878 | // stand-ins in absence of wchar_t | |
879 | // ---------------------------------------------------------------------------- | |
880 | ||
881 | WXDLLEXPORT_DATA(wxMBConv) wxConvLibc, wxConvFile; | |
882 | ||
883 | #endif // wxUSE_WCHAR_T | |
6001e347 RR |
884 | |
885 |