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