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