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