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