]> git.saurik.com Git - wxWidgets.git/blame - src/common/strconv.cpp
yet another attempt to wxFrame's last focus bug
[wxWidgets.git] / src / common / strconv.cpp
CommitLineData
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
84WXDLLEXPORT_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
94static size_t encode_utf16(wxUint32 input,wxUint16*output)
95{
dccce9ea 96 if (input<=0xffff)
4def3b35
VS
97 {
98 if (output) *output++ = input;
99 return 1;
dccce9ea
VZ
100 }
101 else if (input>=0x110000)
4def3b35
VS
102 {
103 return (size_t)-1;
dccce9ea
VZ
104 }
105 else
4def3b35 106 {
dccce9ea 107 if (output)
4def3b35
VS
108 {
109 *output++ = (input >> 10)+0xd7c0;
110 *output++ = (input&0x3ff)+0xdc00;
111 }
112 return 2;
1cd52418 113 }
1cd52418
OK
114}
115
116static size_t decode_utf16(wxUint16*input,wxUint32&output)
117{
dccce9ea 118 if ((*input<0xd800) || (*input>0xdfff))
4def3b35
VS
119 {
120 output = *input;
121 return 1;
dccce9ea
VZ
122 }
123 else if ((input[1]<0xdc00) || (input[1]>=0xdfff))
4def3b35
VS
124 {
125 output = *input;
126 return (size_t)-1;
dccce9ea
VZ
127 }
128 else
4def3b35
VS
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
141WXDLLEXPORT_DATA(wxMBConv) wxConvLibc;
142
143size_t wxMBConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const
144{
145 return wxMB2WC(buf, psz, n);
146}
147
148size_t wxMBConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
149{
150 return wxWC2MB(buf, psz, n);
151}
152
153const 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
168const 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
187WXDLLEXPORT_DATA(wxMBConvFile) wxConvFile;
188
189// just use the libc conversion for now
190size_t wxMBConvFile::MB2WC(wchar_t *buf, const char *psz, size_t n) const
191{
192 return wxMB2WC(buf, psz, n);
193}
194
195size_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
206WXDLLEXPORT_DATA(wxMBConvGdk) wxConvGdk;
207
208#include <gdk/gdk.h>
209
210size_t wxMBConvGdk::MB2WC(wchar_t *buf, const char *psz, size_t n) const
211{
dccce9ea 212 if (buf)
4def3b35
VS
213 {
214 return gdk_mbstowcs((GdkWChar *)buf, psz, n);
dccce9ea
VZ
215 }
216 else
4def3b35
VS
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
225size_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;
dccce9ea 229 if (buf)
4def3b35 230 {
dccce9ea 231 if (len > n)
4def3b35
VS
232 len = n;
233 memcpy(buf, psz, len);
dccce9ea 234 if (len < n)
4def3b35
VS
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
246WXDLLEXPORT_DATA(wxMBConvUTF7) wxConvUTF7;
247
248#if 0
249static char utf7_setD[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
250 "abcdefghijklmnopqrstuvwxyz"
251 "0123456789'(),-./:?";
252static char utf7_setO[]="!\"#$%&*;<=>@[]^_`{|}";
253static char utf7_setB[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
254 "abcdefghijklmnopqrstuvwxyz"
255 "0123456789+/";
256#endif
257
258// TODO: write actual implementations of UTF-7 here
259size_t wxMBConvUTF7::MB2WC(wchar_t * WXUNUSED(buf),
260 const char * WXUNUSED(psz),
261 size_t WXUNUSED(n)) const
262{
263 return 0;
264}
265
266size_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
277WXDLLEXPORT_DATA(wxMBConvUTF8) wxConvUTF8;
278
dccce9ea 279static wxUint32 utf8_max[]=
4def3b35 280 { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff, 0xffffffff };
6001e347
RR
281
282size_t wxMBConvUTF8::MB2WC(wchar_t *buf, const char *psz, size_t n) const
283{
4def3b35
VS
284 size_t len = 0;
285
dccce9ea 286 while (*psz && ((!buf) || (len < n)))
4def3b35
VS
287 {
288 unsigned char cc = *psz++, fc = cc;
289 unsigned cnt;
dccce9ea 290 for (cnt = 0; fc & 0x80; cnt++)
4def3b35 291 fc <<= 1;
dccce9ea 292 if (!cnt)
4def3b35
VS
293 {
294 // plain ASCII char
dccce9ea 295 if (buf)
4def3b35
VS
296 *buf++ = cc;
297 len++;
dccce9ea
VZ
298 }
299 else
4def3b35
VS
300 {
301 cnt--;
dccce9ea 302 if (!cnt)
4def3b35
VS
303 {
304 // invalid UTF-8 sequence
305 return (size_t)-1;
dccce9ea
VZ
306 }
307 else
4def3b35
VS
308 {
309 unsigned ocnt = cnt - 1;
310 wxUint32 res = cc & (0x3f >> cnt);
dccce9ea 311 while (cnt--)
4def3b35
VS
312 {
313 cc = *psz++;
dccce9ea 314 if ((cc & 0xC0) != 0x80)
4def3b35
VS
315 {
316 // invalid UTF-8 sequence
317 return (size_t)-1;
318 }
319 res = (res << 6) | (cc & 0x3f);
320 }
dccce9ea 321 if (res <= utf8_max[ocnt])
4def3b35
VS
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;
dccce9ea 330 if (buf)
4def3b35
VS
331 buf += pa;
332 len += pa;
1cd52418 333#else
dccce9ea 334 if (buf)
4def3b35
VS
335 *buf++ = res;
336 len++;
1cd52418 337#endif
4def3b35
VS
338 }
339 }
6001e347 340 }
dccce9ea 341 if (buf && (len < n))
4def3b35
VS
342 *buf = 0;
343 return len;
6001e347
RR
344}
345
346size_t wxMBConvUTF8::WC2MB(char *buf, const wchar_t *psz, size_t n) const
347{
4def3b35 348 size_t len = 0;
6001e347 349
dccce9ea 350 while (*psz && ((!buf) || (len < n)))
4def3b35
VS
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++) {}
dccce9ea 361 if (!cnt)
4def3b35
VS
362 {
363 // plain ASCII char
dccce9ea 364 if (buf)
4def3b35
VS
365 *buf++ = cc;
366 len++;
dccce9ea
VZ
367 }
368
369 else
4def3b35
VS
370 {
371 len += cnt + 1;
dccce9ea 372 if (buf)
4def3b35
VS
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
389WXDLLEXPORT_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
402static long CharsetToCodepage(const wxChar *name)
403{
dccce9ea 404 if (!name)
f1339c56 405 return GetACP();
dccce9ea 406
f1339c56 407 long CP=-1;
dccce9ea 408
f1339c56
RR
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;
dccce9ea 416
5ce0e4ac
VS
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;
dccce9ea 424
f1339c56 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 428 } while (1);
dccce9ea 429
f1339c56 430 return CP;
1cd52418
OK
431}
432#endif
433
6001e347
RR
434class wxCharacterSet
435{
1cd52418 436public:
f1339c56
RR
437 wxCharacterSet(const wxChar*name)
438 : cname(name) {}
439 virtual ~wxCharacterSet()
440 {}
dccce9ea 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; }
447public:
448 const wxChar*cname;
1cd52418
OK
449};
450
451class ID_CharSet : public wxCharacterSet
452{
453public:
4def3b35 454 ID_CharSet(const wxChar *name,wxMBConv *cnv)
f1339c56 455 : wxCharacterSet(name), work(cnv) {}
dccce9ea 456
4def3b35 457 size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
f1339c56 458 { return work ? work->MB2WC(buf,psz,n) : (size_t)-1; }
dccce9ea 459
4def3b35 460 size_t WC2MB(char *buf, const wchar_t *psz, size_t n)
f1339c56 461 { return work ? work->WC2MB(buf,psz,n) : (size_t)-1; }
dccce9ea 462
f1339c56
RR
463 bool usable()
464 { return work!=NULL; }
465public:
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
486class IC_CharSet : public wxCharacterSet
487{
488public:
dccce9ea 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 494 }
dccce9ea 495
3caec1bb
VS
496 ~IC_CharSet()
497 {
dccce9ea 498 if ( m2w != (iconv_t)-1 )
3caec1bb 499 iconv_close(m2w);
dccce9ea 500 if ( w2m != (iconv_t)-1 )
3caec1bb 501 iconv_close(w2m);
f1339c56 502 }
dccce9ea 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 }
dccce9ea 543
3caec1bb 544 if (ICONV_FAILED(cres, inbuf))
f1339c56 545 return (size_t)-1;
3caec1bb 546
f1339c56
RR
547 return res;
548 }
dccce9ea 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 600 return (size_t)-1;
dccce9ea 601
f1339c56
RR
602 return res;
603 }
dccce9ea 604
f1339c56 605 bool usable()
3caec1bb 606 { return (m2w != (iconv_t)-1) && (w2m != (iconv_t)-1); }
dccce9ea 607
f1339c56
RR
608public:
609 iconv_t m2w, w2m;
1cd52418
OK
610};
611#endif
612
613#ifdef __WIN32__
614class CP_CharSet : public wxCharacterSet
615{
616public:
dccce9ea 617 CP_CharSet(const wxChar*name)
f1339c56 618 : wxCharacterSet(name), CodePage(CharsetToCodepage(name)) {}
dccce9ea 619
4def3b35 620 size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
f1339c56 621 {
dccce9ea 622 size_t len =
4def3b35 623 MultiByteToWideChar(CodePage, 0, psz, -1, buf, buf ? n : 0);
f1339c56
RR
624 return len ? len : (size_t)-1;
625 }
dccce9ea 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 }
dccce9ea 633
f1339c56 634 bool usable()
4def3b35 635 { return CodePage != -1; }
f1339c56
RR
636
637public:
638 long CodePage;
1cd52418
OK
639};
640#endif
641
642class EC_CharSet : public wxCharacterSet
643{
6001e347 644public:
f1339c56
RR
645 // temporarily just use wxEncodingConverter stuff,
646 // so that it works while a better implementation is built
dccce9ea 647 EC_CharSet(const wxChar*name) : wxCharacterSet(name),
4def3b35 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 }
dccce9ea 655
4def3b35 656 size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
f1339c56
RR
657 {
658 size_t inbuf = strlen(psz);
dccce9ea 659 if (buf)
4def3b35 660 m2w.Convert(psz,buf);
f1339c56
RR
661 return inbuf;
662 }
dccce9ea 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);
dccce9ea 673
f1339c56
RR
674 return inbuf;
675 }
dccce9ea 676
f1339c56
RR
677 bool usable()
678 { return (enc!=wxFONTENCODING_SYSTEM) && (enc!=wxFONTENCODING_DEFAULT); }
679
680public:
681 wxFontEncoding enc;
682 wxEncodingConverter m2w, w2m;
f6bcfd97 683};
6001e347 684
f6bcfd97 685static 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 }
dccce9ea 701
f1339c56 702 if (cset && cset->usable()) return cset;
dccce9ea
VZ
703 if (cset)
704 {
705 delete cset;
706 cset = NULL;
707 }
708
1cd52418 709#ifdef __WIN32__
f1339c56 710 cset = new CP_CharSet(name); // may take NULL
dccce9ea
VZ
711 if (cset->usable())
712 return cset;
713
714 delete cset;
715#endif // __WIN32__
716
f1339c56 717 cset = new EC_CharSet(name);
dccce9ea
VZ
718 if (cset->usable())
719 return cset;
720
f1339c56 721 delete cset;
3caec1bb 722 wxLogError(_("Unknown encoding '%s'!"), name);
f1339c56 723 return NULL;
6001e347
RR
724}
725
6001e347
RR
726wxCSConv::wxCSConv(const wxChar *charset)
727{
dccce9ea 728 m_name = (wxChar *)NULL;
f1339c56 729 m_cset = (wxCharacterSet *) NULL;
f1339c56 730 SetName(charset);
6001e347
RR
731}
732
733wxCSConv::~wxCSConv()
734{
dccce9ea
VZ
735 free(m_name);
736 delete m_cset;
6001e347
RR
737}
738
739void wxCSConv::SetName(const wxChar *charset)
740{
f1339c56
RR
741 if (charset)
742 {
743 m_name = wxStrdup(charset);
744 m_deferred = TRUE;
745 }
6001e347
RR
746}
747
748void wxCSConv::LoadNow()
749{
f1339c56
RR
750 if (m_deferred)
751 {
dccce9ea 752 if ( !m_name )
f1339c56 753 {
dccce9ea
VZ
754 wxString name = wxLocale::GetSystemEncodingName();
755 if ( !name.empty() )
756 SetName(name);
f1339c56 757 }
dccce9ea 758
f1339c56
RR
759 m_cset = wxGetCharacterSet(m_name);
760 m_deferred = FALSE;
6001e347 761 }
6001e347
RR
762}
763
764size_t wxCSConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const
765{
f1339c56 766 ((wxCSConv *)this)->LoadNow(); // discard constness
dccce9ea 767
f1339c56
RR
768 if (m_cset)
769 return m_cset->MB2WC(buf, psz, n);
770
771 // latin-1 (direct)
4def3b35 772 size_t len = strlen(psz);
dccce9ea 773
f1339c56
RR
774 if (buf)
775 {
4def3b35 776 for (size_t c = 0; c <= len; c++)
f1339c56
RR
777 buf[c] = (unsigned char)(psz[c]);
778 }
dccce9ea 779
f1339c56 780 return len;
6001e347
RR
781}
782
783size_t wxCSConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
784{
f1339c56 785 ((wxCSConv *)this)->LoadNow(); // discard constness
dccce9ea 786
f1339c56
RR
787 if (m_cset)
788 return m_cset->WC2MB(buf, psz, n);
1cd52418 789
f1339c56 790 // latin-1 (direct)
d834f22c 791#if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
f1339c56 792 size_t len=std::wcslen(psz);
d834f22c 793#else
f1339c56 794 size_t len=::wcslen(psz);
d834f22c 795#endif
f1339c56
RR
796 if (buf)
797 {
4def3b35
VS
798 for (size_t c = 0; c <= len; c++)
799 buf[c] = (psz[c] > 0xff) ? '?' : psz[c];
f1339c56 800 }
dccce9ea 801
f1339c56 802 return len;
6001e347
RR
803}
804
1cd52418 805#ifdef HAVE_ICONV_H
dccce9ea 806
1cd52418
OK
807class IC_CharSetConverter
808{
809public:
4def3b35
VS
810 IC_CharSetConverter(IC_CharSet *from, IC_CharSet *to)
811 {
dccce9ea
VZ
812 cnv = iconv_open(wxConvLibc.cWX2MB(to->cname),
813 wxConvLibc.cWX2MB(from->cname));
4def3b35 814 }
dccce9ea 815
f1339c56 816 ~IC_CharSetConverter()
dccce9ea
VZ
817 {
818 if (cnv != (iconv_t)-1)
819 iconv_close(cnv);
4def3b35 820 }
dccce9ea 821
4def3b35 822 size_t Convert(char *buf, const char *psz, size_t n)
f1339c56
RR
823 {
824 size_t inbuf = strlen(psz);
825 size_t outbuf = n;
95c8801c 826#ifdef WX_ICONV_TAKES_CHAR
f1339c56 827 size_t res = iconv( cnv, (char**)&psz, &inbuf, &buf, &outbuf );
95c8801c
VS
828#else
829 size_t res = iconv( cnv, &psz, &inbuf, &buf, &outbuf );
830#endif
dccce9ea 831 if (res == (size_t)-1)
4def3b35
VS
832 return (size_t)-1;
833 return (n - outbuf);
f1339c56
RR
834 }
835
836public:
837 iconv_t cnv;
1cd52418 838};
dccce9ea
VZ
839
840#endif // HAVE_ICONV_H
1cd52418
OK
841
842class EC_CharSetConverter
843{
844public:
f1339c56
RR
845 EC_CharSetConverter(EC_CharSet*from,EC_CharSet*to)
846 { cnv.Init(from->enc,to->enc); }
dccce9ea 847
f1339c56
RR
848 size_t Convert(char*buf, const char*psz, size_t n)
849 {
850 size_t inbuf = strlen(psz);
851 if (buf) cnv.Convert(psz,buf);
852 return inbuf;
853 }
dccce9ea 854
f1339c56
RR
855public:
856 wxEncodingConverter cnv;
1cd52418
OK
857};
858
f6bcfd97
BP
859#else // !wxUSE_WCHAR_T
860
861// ----------------------------------------------------------------------------
862// stand-ins in absence of wchar_t
863// ----------------------------------------------------------------------------
864
865WXDLLEXPORT_DATA(wxMBConv) wxConvLibc, wxConvFile;
866
867#endif // wxUSE_WCHAR_T
6001e347
RR
868
869