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