]> git.saurik.com Git - wxWidgets.git/blame - src/common/strconv.cpp
tell that there was an error...
[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{
24f588af 191 return wxMB2WC(buf, psz, n);
6001e347
RR
192}
193
194size_t wxMBConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
195{
24f588af 196 return wxWC2MB(buf, psz, n);
6001e347
RR
197}
198
199const wxWCharBuffer wxMBConv::cMB2WC(const char *psz) const
200{
f6bcfd97 201 if (psz)
6001e347 202 {
24f588af 203 size_t nLen = MB2WC((wchar_t *) NULL, psz, 0); // return value excludes /0
f6bcfd97
BP
204 if (nLen == (size_t)-1)
205 return wxWCharBuffer((wchar_t *) NULL);
24f588af
RR
206 wxWCharBuffer buf(nLen); // this allocates nLen1+
207 MB2WC((wchar_t *)(const wchar_t *) buf, psz, nLen+1);
6001e347 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 217 {
24f588af 218 size_t nLen = WC2MB((char *) NULL, psz, 0); // return value excludes /0
f6bcfd97
BP
219 if (nLen == (size_t)-1)
220 return wxCharBuffer((char *) NULL);
24f588af
RR
221 wxCharBuffer buf(nLen); // this allocates nLen+1
222 WC2MB((char *)(const char *) buf, psz, nLen+1);
6001e347 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;
24f588af 428
4def3b35 429 return len;
6001e347
RR
430}
431
36acb880
VZ
432// ============================================================================
433// wxCharacterSet and derived classes
434// ============================================================================
435
436// ----------------------------------------------------------------------------
437// wxCharacterSet is the ABC for the classes below
438// ----------------------------------------------------------------------------
439
6001e347
RR
440class wxCharacterSet
441{
1cd52418 442public:
4f61e22c
VS
443 wxCharacterSet(const wxChar*name) : cname(name) {}
444 virtual ~wxCharacterSet() {}
445 virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) = 0;
446 virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) = 0;
447 virtual bool usable() const = 0;
f1339c56
RR
448public:
449 const wxChar*cname;
1cd52418
OK
450};
451
36acb880
VZ
452// ----------------------------------------------------------------------------
453// ID_CharSet: implementation of wxCharacterSet using an existing wxMBConv
454// ----------------------------------------------------------------------------
455
1cd52418
OK
456class ID_CharSet : public wxCharacterSet
457{
458public:
36acb880 459 ID_CharSet(const wxChar *name, wxMBConv *cnv)
f1339c56 460 : wxCharacterSet(name), work(cnv) {}
dccce9ea 461
4def3b35 462 size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
f1339c56 463 { return work ? work->MB2WC(buf,psz,n) : (size_t)-1; }
dccce9ea 464
4def3b35 465 size_t WC2MB(char *buf, const wchar_t *psz, size_t n)
f1339c56 466 { return work ? work->WC2MB(buf,psz,n) : (size_t)-1; }
dccce9ea 467
4f61e22c 468 bool usable() const
f1339c56
RR
469 { return work!=NULL; }
470public:
471 wxMBConv*work;
1cd52418
OK
472};
473
3caec1bb 474
36acb880
VZ
475// ============================================================================
476// The classes doing conversion using the iconv_xxx() functions
477// ============================================================================
3caec1bb 478
b040e242 479#ifdef HAVE_ICONV
3a0d76bc 480
3caec1bb
VS
481// VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with E2BIG
482// if output buffer is _exactly_ as big as needed. Such case is (unless there's
483// yet another bug in glibc) the only case when iconv() returns with (size_t)-1
484// (which means error) and says there are 0 bytes left in the input buffer --
485// when _real_ error occurs, bytes-left-in-input buffer is non-zero. Hence,
486// this alternative test for iconv() failure.
487// [This bug does not appear in glibc 2.2.]
488#if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 1
489#define ICONV_FAILED(cres, bufLeft) ((cres == (size_t)-1) && \
490 (errno != E2BIG || bufLeft != 0))
491#else
492#define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1)
493#endif
494
b040e242 495#define ICONV_CHAR_CAST(x) ((ICONV_CONST char **)(x))
36acb880
VZ
496
497// ----------------------------------------------------------------------------
498// IC_CharSet: encapsulates an iconv character set
499// ----------------------------------------------------------------------------
500
1cd52418
OK
501class IC_CharSet : public wxCharacterSet
502{
503public:
36acb880
VZ
504 IC_CharSet(const wxChar *name);
505 virtual ~IC_CharSet();
506
507 virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n);
508 virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n);
509
510 bool usable() const
511 { return (m2w != (iconv_t)-1) && (w2m != (iconv_t)-1); }
512
513protected:
514 // the iconv handlers used to translate from multibyte to wide char and in
515 // the other direction
516 iconv_t m2w,
517 w2m;
518
519private:
520 // the name (for iconv_open()) of a wide char charset - if none is
521 // available on this machine, it will remain NULL
522 static const char *ms_wcCharsetName;
523
524 // true if the wide char encoding we use (i.e. ms_wcCharsetName) has
525 // different endian-ness than the native one
405d8f46 526 static bool ms_wcNeedsSwap;
36acb880
VZ
527};
528
529const char *IC_CharSet::ms_wcCharsetName = NULL;
405d8f46 530bool IC_CharSet::ms_wcNeedsSwap = FALSE;
36acb880
VZ
531
532IC_CharSet::IC_CharSet(const wxChar *name)
533 : wxCharacterSet(name)
534{
535 // check for charset that represents wchar_t:
536 if (ms_wcCharsetName == NULL)
f1339c56 537 {
36acb880 538 ms_wcNeedsSwap = FALSE;
dccce9ea 539
36acb880
VZ
540 // try charset with explicit bytesex info (e.g. "UCS-4LE"):
541 ms_wcCharsetName = WC_NAME_BEST;
542 m2w = iconv_open(ms_wcCharsetName, wxConvLibc.cWX2MB(name));
3a0d76bc 543
36acb880
VZ
544 if (m2w == (iconv_t)-1)
545 {
546 // try charset w/o bytesex info (e.g. "UCS4")
547 // and check for bytesex ourselves:
548 ms_wcCharsetName = WC_NAME;
549 m2w = iconv_open(ms_wcCharsetName, wxConvLibc.cWX2MB(name));
550
551 // last bet, try if it knows WCHAR_T pseudo-charset
3a0d76bc
VS
552 if (m2w == (iconv_t)-1)
553 {
36acb880
VZ
554 ms_wcCharsetName = "WCHAR_T";
555 m2w = iconv_open(ms_wcCharsetName, wxConvLibc.cWX2MB(name));
556 }
3a0d76bc 557
36acb880
VZ
558 if (m2w != (iconv_t)-1)
559 {
560 char buf[2], *bufPtr;
561 wchar_t wbuf[2], *wbufPtr;
562 size_t insz, outsz;
563 size_t res;
564
565 buf[0] = 'A';
566 buf[1] = 0;
567 wbuf[0] = 0;
568 insz = 2;
569 outsz = SIZEOF_WCHAR_T * 2;
570 wbufPtr = wbuf;
571 bufPtr = buf;
572
573 res = iconv(m2w, ICONV_CHAR_CAST(&bufPtr), &insz,
574 (char**)&wbufPtr, &outsz);
575
576 if (ICONV_FAILED(res, insz))
3a0d76bc 577 {
36acb880
VZ
578 ms_wcCharsetName = NULL;
579 wxLogLastError(wxT("iconv"));
580 wxLogError(_("Convertion to charset '%s' doesn't work."), name);
3a0d76bc
VS
581 }
582 else
583 {
36acb880 584 ms_wcNeedsSwap = wbuf[0] != (wchar_t)buf[0];
3a0d76bc
VS
585 }
586 }
36acb880
VZ
587 else
588 {
589 ms_wcCharsetName = NULL;
373658eb 590
957686c8
VS
591 // VS: we must not output an error here, since wxWindows will safely
592 // fall back to using wxEncodingConverter.
593 wxLogTrace(wxT("strconv"), wxT("Impossible to convert to/from charset '%s' with iconv, falling back to wxEncodingConverter."), name);
594 //wxLogError(
36acb880 595 }
3a0d76bc 596 }
36acb880 597 wxLogTrace(wxT("strconv"), wxT("wchar_t charset is '%s', needs swap: %i"), ms_wcCharsetName, ms_wcNeedsSwap);
3a0d76bc 598 }
36acb880 599 else // we already have ms_wcCharsetName
3caec1bb 600 {
36acb880 601 m2w = iconv_open(ms_wcCharsetName, wxConvLibc.cWX2MB(name));
f1339c56 602 }
dccce9ea 603
36acb880
VZ
604 // NB: don't ever pass NULL to iconv_open(), it may crash!
605 if ( ms_wcCharsetName )
f1339c56 606 {
36acb880
VZ
607 w2m = iconv_open(wxConvLibc.cWX2MB(name), ms_wcCharsetName);
608 }
405d8f46
VZ
609 else
610 {
611 w2m = (iconv_t)-1;
612 }
36acb880 613}
3caec1bb 614
36acb880
VZ
615IC_CharSet::~IC_CharSet()
616{
617 if ( m2w != (iconv_t)-1 )
618 iconv_close(m2w);
619 if ( w2m != (iconv_t)-1 )
620 iconv_close(w2m);
621}
3a0d76bc 622
36acb880
VZ
623size_t IC_CharSet::MB2WC(wchar_t *buf, const char *psz, size_t n)
624{
625 size_t inbuf = strlen(psz);
626 size_t outbuf = n * SIZEOF_WCHAR_T;
627 size_t res, cres;
628 // VS: Use these instead of psz, buf because iconv() modifies its arguments:
629 wchar_t *bufPtr = buf;
630 const char *pszPtr = psz;
631
632 if (buf)
633 {
634 // have destination buffer, convert there
635 cres = iconv(m2w,
636 ICONV_CHAR_CAST(&pszPtr), &inbuf,
637 (char**)&bufPtr, &outbuf);
638 res = n - (outbuf / SIZEOF_WCHAR_T);
dccce9ea 639
36acb880 640 if (ms_wcNeedsSwap)
3a0d76bc 641 {
36acb880
VZ
642 // convert to native endianness
643 WC_BSWAP(buf /* _not_ bufPtr */, res)
3a0d76bc 644 }
36acb880
VZ
645 }
646 else
647 {
648 // no destination buffer... convert using temp buffer
649 // to calculate destination buffer requirement
650 wchar_t tbuf[8];
651 res = 0;
652 do {
653 bufPtr = tbuf;
654 outbuf = 8*SIZEOF_WCHAR_T;
655
656 cres = iconv(m2w,
657 ICONV_CHAR_CAST(&pszPtr), &inbuf,
658 (char**)&bufPtr, &outbuf );
659
660 res += 8-(outbuf/SIZEOF_WCHAR_T);
661 } while ((cres==(size_t)-1) && (errno==E2BIG));
f1339c56 662 }
dccce9ea 663
36acb880 664 if (ICONV_FAILED(cres, inbuf))
f1339c56 665 {
36acb880
VZ
666 //VS: it is ok if iconv fails, hence trace only
667 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
668 return (size_t)-1;
669 }
670
671 return res;
672}
673
674size_t IC_CharSet::WC2MB(char *buf, const wchar_t *psz, size_t n)
675{
1cd52418 676#if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
36acb880 677 size_t inbuf = std::wcslen(psz) * SIZEOF_WCHAR_T;
1cd52418 678#else
36acb880 679 size_t inbuf = ::wcslen(psz) * SIZEOF_WCHAR_T;
1cd52418 680#endif
36acb880
VZ
681 size_t outbuf = n;
682 size_t res, cres;
3a0d76bc 683
36acb880 684 wchar_t *tmpbuf = 0;
3caec1bb 685
36acb880
VZ
686 if (ms_wcNeedsSwap)
687 {
688 // need to copy to temp buffer to switch endianness
689 // this absolutely doesn't rock!
690 // (no, doing WC_BSWAP twice on the original buffer won't help, as it
691 // could be in read-only memory, or be accessed in some other thread)
692 tmpbuf=(wchar_t*)malloc((inbuf+1)*SIZEOF_WCHAR_T);
693 memcpy(tmpbuf,psz,(inbuf+1)*SIZEOF_WCHAR_T);
694 WC_BSWAP(tmpbuf, inbuf)
695 psz=tmpbuf;
696 }
3a0d76bc 697
36acb880
VZ
698 if (buf)
699 {
700 // have destination buffer, convert there
701 cres = iconv( w2m, ICONV_CHAR_CAST(&psz), &inbuf, &buf, &outbuf );
3a0d76bc 702
36acb880
VZ
703 res = n-outbuf;
704 }
705 else
706 {
707 // no destination buffer... convert using temp buffer
708 // to calculate destination buffer requirement
709 char tbuf[16];
710 res = 0;
711 do {
712 buf = tbuf; outbuf = 16;
713
714 cres = iconv( w2m, ICONV_CHAR_CAST(&psz), &inbuf, &buf, &outbuf );
dccce9ea 715
36acb880
VZ
716 res += 16 - outbuf;
717 } while ((cres==(size_t)-1) && (errno==E2BIG));
f1339c56 718 }
dccce9ea 719
36acb880
VZ
720 if (ms_wcNeedsSwap)
721 {
722 free(tmpbuf);
723 }
dccce9ea 724
36acb880
VZ
725 if (ICONV_FAILED(cres, inbuf))
726 {
727 //VS: it is ok if iconv fails, hence trace only
728 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
729 return (size_t)-1;
730 }
731
732 return res;
733}
734
b040e242 735#endif // HAVE_ICONV
36acb880
VZ
736
737// ============================================================================
738// Win32 conversion classes
739// ============================================================================
1cd52418 740
04ef50df 741#if defined(__WIN32__) && !defined(__WXMICROWIN__)
373658eb
VZ
742
743extern long wxCharsetToCodepage(const wxChar *charset); // from utils.cpp
744
1cd52418
OK
745class CP_CharSet : public wxCharacterSet
746{
747public:
b1d66b54
VZ
748 CP_CharSet(const wxChar* name)
749 : wxCharacterSet(name)
750 {
751 m_CodePage = wxCharsetToCodepage(name);
752 }
dccce9ea 753
4def3b35 754 size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
f1339c56 755 {
dccce9ea 756 size_t len =
b1d66b54 757 MultiByteToWideChar(m_CodePage, 0, psz, -1, buf, buf ? n : 0);
1e6feb95 758 //VS: returns # of written chars for buf!=NULL and *size*
35d764b0
VS
759 // needed buffer for buf==NULL
760 return len ? (buf ? len : len-1) : (size_t)-1;
f1339c56 761 }
dccce9ea 762
4def3b35 763 size_t WC2MB(char *buf, const wchar_t *psz, size_t n)
f1339c56 764 {
b1d66b54 765 size_t len = WideCharToMultiByte(m_CodePage, 0, psz, -1, buf,
4def3b35 766 buf ? n : 0, NULL, NULL);
1e6feb95 767 //VS: returns # of written chars for buf!=NULL and *size*
35d764b0
VS
768 // needed buffer for buf==NULL
769 return len ? (buf ? len : len-1) : (size_t)-1;
f1339c56 770 }
dccce9ea 771
4f61e22c 772 bool usable() const
b1d66b54 773 { return m_CodePage != -1; }
f1339c56
RR
774
775public:
b1d66b54 776 long m_CodePage;
1cd52418 777};
1e6feb95
VZ
778#endif // __WIN32__
779
36acb880
VZ
780// ============================================================================
781// wxEncodingConverter based conversion classes
782// ============================================================================
783
1e6feb95 784#if wxUSE_FONTMAP
1cd52418
OK
785
786class EC_CharSet : public wxCharacterSet
787{
6001e347 788public:
f1339c56
RR
789 // temporarily just use wxEncodingConverter stuff,
790 // so that it works while a better implementation is built
b1d66b54
VZ
791 EC_CharSet(const wxChar* name) : wxCharacterSet(name),
792 enc(wxFONTENCODING_SYSTEM)
f1339c56
RR
793 {
794 if (name)
142b3bc2 795 enc = wxFontMapper::Get()->CharsetToEncoding(name, FALSE);
cafbf6fb
VZ
796
797 m_ok = m2w.Init(enc, wxFONTENCODING_UNICODE) &&
798 w2m.Init(wxFONTENCODING_UNICODE, enc);
f1339c56 799 }
dccce9ea 800
574c939e 801 size_t MB2WC(wchar_t *buf, const char *psz, size_t WXUNUSED(n))
f1339c56
RR
802 {
803 size_t inbuf = strlen(psz);
dccce9ea 804 if (buf)
4def3b35 805 m2w.Convert(psz,buf);
f1339c56
RR
806 return inbuf;
807 }
dccce9ea 808
574c939e 809 size_t WC2MB(char *buf, const wchar_t *psz, size_t WXUNUSED(n))
f1339c56 810 {
de85a884
VZ
811#if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
812 || ( defined(__MWERKS__) && defined(__WXMSW__) )
f1339c56 813 size_t inbuf = std::wcslen(psz);
1cd52418 814#else
f1339c56 815 size_t inbuf = ::wcslen(psz);
1cd52418 816#endif
f1339c56
RR
817 if (buf)
818 w2m.Convert(psz,buf);
dccce9ea 819
f1339c56
RR
820 return inbuf;
821 }
dccce9ea 822
cafbf6fb 823 bool usable() const { return m_ok; }
f1339c56
RR
824
825public:
826 wxFontEncoding enc;
827 wxEncodingConverter m2w, w2m;
cafbf6fb
VZ
828
829 // were we initialized successfully?
830 bool m_ok;
f6bcfd97 831};
6001e347 832
1e6feb95
VZ
833#endif // wxUSE_FONTMAP
834
36acb880
VZ
835// ----------------------------------------------------------------------------
836// the function creating the wxCharacterSet for the specified charset on the
837// current system, trying all possibilities
838// ----------------------------------------------------------------------------
839
f6bcfd97 840static wxCharacterSet *wxGetCharacterSet(const wxChar *name)
6001e347 841{
cafbf6fb
VZ
842 // check for the special case of ASCII charset
843#if wxUSE_FONTMAP
142b3bc2 844 if ( wxFontMapper::Get()->CharsetToEncoding(name) == wxFONTENCODING_DEFAULT )
cafbf6fb
VZ
845#else // wxUSE_FONTMAP
846 if ( !name )
847#endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
f1339c56 848 {
cafbf6fb
VZ
849 // don't convert at all
850 return NULL;
1cd52418 851 }
dccce9ea 852
cafbf6fb
VZ
853 // the test above must have taken care of this case
854 wxCHECK_MSG( name, NULL, _T("NULL name must be wxFONTENCODING_DEFAULT") );
1e6feb95 855
cafbf6fb
VZ
856 wxCharacterSet *cset;
857
858 if ( wxStricmp(name, wxT("UTF8")) == 0 || wxStricmp(name, wxT("UTF-8")) == 0)
859 {
860 cset = new ID_CharSet(name, &wxConvUTF8);
861 }
862 else
dccce9ea 863 {
cafbf6fb
VZ
864#ifdef HAVE_ICONV
865 cset = new IC_CharSet(name);
866#else // !HAVE_ICONV
dccce9ea 867 cset = NULL;
cafbf6fb 868#endif // HAVE_ICONV/!HAVE_ICONV
dccce9ea
VZ
869 }
870
734eda8a
VZ
871 // it can only be NULL in this case
872#ifndef HAVE_ICONV
873 if ( cset )
874#endif // !HAVE_ICONV
875 {
876 if ( cset->usable() )
877 return cset;
cafbf6fb 878
734eda8a
VZ
879 delete cset;
880 cset = NULL;
881 }
cafbf6fb 882
04ef50df 883#if defined(__WIN32__) && !defined(__WXMICROWIN__)
cafbf6fb
VZ
884 cset = new CP_CharSet(name);
885 if ( cset->usable() )
dccce9ea
VZ
886 return cset;
887
888 delete cset;
cafbf6fb 889 cset = NULL;
dccce9ea
VZ
890#endif // __WIN32__
891
1e6feb95 892#if wxUSE_FONTMAP
f1339c56 893 cset = new EC_CharSet(name);
cafbf6fb 894 if ( cset->usable() )
dccce9ea
VZ
895 return cset;
896
f1339c56 897 delete cset;
cafbf6fb
VZ
898 cset = NULL;
899#endif // wxUSE_FONTMAP
900
901 wxLogError(_("Cannot convert from encoding '%s'!"), name);
902
f1339c56 903 return NULL;
6001e347
RR
904}
905
36acb880
VZ
906// ============================================================================
907// wxCSConv implementation
908// ============================================================================
909
6001e347
RR
910wxCSConv::wxCSConv(const wxChar *charset)
911{
dccce9ea 912 m_name = (wxChar *)NULL;
f1339c56 913 m_cset = (wxCharacterSet *) NULL;
82713003
VZ
914 m_deferred = TRUE;
915
f1339c56 916 SetName(charset);
6001e347
RR
917}
918
919wxCSConv::~wxCSConv()
920{
65e50848
JS
921 Clear();
922}
923
54380f29
GD
924wxCSConv::wxCSConv(const wxCSConv& conv)
925 : wxMBConv()
926{
927 Clear();
928 SetName(conv.m_name);
929}
930
931wxCSConv& wxCSConv::operator=(const wxCSConv& conv)
932{
933 Clear();
934 SetName(conv.m_name);
935 return *this;
936}
937
65e50848
JS
938void wxCSConv::Clear()
939{
940 if (m_name)
941 free(m_name);
942 if (m_cset)
943 delete m_cset;
944 m_name = NULL;
945 m_cset = NULL;
6001e347
RR
946}
947
948void wxCSConv::SetName(const wxChar *charset)
949{
f1339c56
RR
950 if (charset)
951 {
952 m_name = wxStrdup(charset);
953 m_deferred = TRUE;
954 }
6001e347
RR
955}
956
957void wxCSConv::LoadNow()
958{
f1339c56
RR
959 if (m_deferred)
960 {
dccce9ea 961 if ( !m_name )
f1339c56 962 {
dccce9ea
VZ
963 wxString name = wxLocale::GetSystemEncodingName();
964 if ( !name.empty() )
965 SetName(name);
f1339c56 966 }
dccce9ea 967
a45a98fb
VZ
968 // wxGetCharacterSet() complains about NULL name
969 m_cset = m_name ? wxGetCharacterSet(m_name) : NULL;
f1339c56 970 m_deferred = FALSE;
6001e347 971 }
6001e347
RR
972}
973
974size_t wxCSConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const
975{
f1339c56 976 ((wxCSConv *)this)->LoadNow(); // discard constness
dccce9ea 977
f1339c56
RR
978 if (m_cset)
979 return m_cset->MB2WC(buf, psz, n);
980
981 // latin-1 (direct)
4def3b35 982 size_t len = strlen(psz);
dccce9ea 983
f1339c56
RR
984 if (buf)
985 {
4def3b35 986 for (size_t c = 0; c <= len; c++)
f1339c56
RR
987 buf[c] = (unsigned char)(psz[c]);
988 }
dccce9ea 989
f1339c56 990 return len;
6001e347
RR
991}
992
993size_t wxCSConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
994{
f1339c56 995 ((wxCSConv *)this)->LoadNow(); // discard constness
dccce9ea 996
f1339c56
RR
997 if (m_cset)
998 return m_cset->WC2MB(buf, psz, n);
1cd52418 999
f1339c56 1000 // latin-1 (direct)
de85a884
VZ
1001#if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
1002 || ( defined(__MWERKS__) && defined(__WXMSW__) )
f1339c56 1003 size_t len=std::wcslen(psz);
d834f22c 1004#else
f1339c56 1005 size_t len=::wcslen(psz);
d834f22c 1006#endif
f1339c56
RR
1007 if (buf)
1008 {
4def3b35
VS
1009 for (size_t c = 0; c <= len; c++)
1010 buf[c] = (psz[c] > 0xff) ? '?' : psz[c];
f1339c56 1011 }
dccce9ea 1012
f1339c56 1013 return len;
6001e347
RR
1014}
1015
f6bcfd97 1016#endif // wxUSE_WCHAR_T
6001e347
RR
1017
1018