]> git.saurik.com Git - wxWidgets.git/blame - src/common/strconv.cpp
added GetBorder(flags)
[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
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 52#if wxUSE_WCHAR_T
fd242375
VS
53 WXDLLIMPEXP_DATA_BASE(wxMBConv) wxConvLibc;
54 WXDLLIMPEXP_DATA_BASE(wxCSConv) wxConvLocal((const wxChar *)NULL);
55 WXDLLIMPEXP_DATA_BASE(wxCSConv) wxConvISO8859_1(_T("iso-8859-1"));
373658eb
VZ
56#else
57 // stand-ins in absence of wchar_t
fd242375
VS
58 WXDLLIMPEXP_DATA_BASE(wxMBConv) wxConvLibc,
59 wxConvFile,
60 wxConvISO8859_1,
61 wxConvLocal,
62 wxConvUTF8;
373658eb 63#endif // wxUSE_WCHAR_T
7af284fd 64
fd242375 65WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvCurrent = &wxConvLibc;
7af284fd 66
65e50848
JS
67class wxStrConvModule: public wxModule
68{
69public:
70 wxStrConvModule() : wxModule() { }
71 virtual bool OnInit() { return TRUE; }
72 virtual void OnExit()
73 {
7f1698c3 74#if wxUSE_WCHAR_T
ca11abde 75 wxConvLocal.Clear();
2b5f62a0 76 wxConvISO8859_1.Clear();
7f1698c3 77#endif
65e50848
JS
78 }
79
80 DECLARE_DYNAMIC_CLASS(wxStrConvModule)
81};
82
83IMPLEMENT_DYNAMIC_CLASS(wxStrConvModule, wxModule)
84
85
373658eb
VZ
86// ----------------------------------------------------------------------------
87// headers
88// ----------------------------------------------------------------------------
7af284fd
VS
89
90#if wxUSE_WCHAR_T
91
6001e347 92#ifdef __SALFORDC__
373658eb 93 #include <clib.h>
6001e347
RR
94#endif
95
b040e242 96#ifdef HAVE_ICONV
373658eb 97 #include <iconv.h>
1cd52418 98#endif
1cd52418 99
373658eb
VZ
100#include "wx/encconv.h"
101#include "wx/fontmap.h"
102
103// ----------------------------------------------------------------------------
104// macros
105// ----------------------------------------------------------------------------
3e61dfb0 106
1cd52418 107#define BSWAP_UCS4(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT32_SWAP_ALWAYS(str[_c]); }
3a0d76bc 108#define BSWAP_UTF16(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT16_SWAP_ALWAYS(str[_c]); }
1cd52418 109
a3f2769e
VZ
110// under Unix SIZEOF_WCHAR_T is defined by configure, but under other platforms
111// it might be not defined - assume the most common value
112#ifndef SIZEOF_WCHAR_T
113 #define SIZEOF_WCHAR_T 2
114#endif // !defined(SIZEOF_WCHAR_T)
115
1cd52418 116#if SIZEOF_WCHAR_T == 4
3a0d76bc
VS
117 #define WC_NAME "UCS4"
118 #define WC_BSWAP BSWAP_UCS4
119 #ifdef WORDS_BIGENDIAN
120 #define WC_NAME_BEST "UCS-4BE"
121 #else
122 #define WC_NAME_BEST "UCS-4LE"
123 #endif
1cd52418 124#elif SIZEOF_WCHAR_T == 2
3a0d76bc
VS
125 #define WC_NAME "UTF16"
126 #define WC_BSWAP BSWAP_UTF16
a3f2769e 127 #define WC_UTF16
3a0d76bc
VS
128 #ifdef WORDS_BIGENDIAN
129 #define WC_NAME_BEST "UTF-16BE"
130 #else
131 #define WC_NAME_BEST "UTF-16LE"
132 #endif
bab1e722 133#else // sizeof(wchar_t) != 2 nor 4
a3f2769e
VZ
134 // I don't know what to do about this
135 #error "Weird sizeof(wchar_t): please report your platform details to wx-users mailing list"
1cd52418
OK
136#endif
137
373658eb
VZ
138// ============================================================================
139// implementation
140// ============================================================================
141
142// ----------------------------------------------------------------------------
143// UTF-16 en/decoding
144// ----------------------------------------------------------------------------
6001e347 145
b0a6bb75
VZ
146#ifdef WC_UTF16
147
eccf1b2c 148static size_t encode_utf16(wxUint32 input, wchar_t *output)
1cd52418 149{
dccce9ea 150 if (input<=0xffff)
4def3b35 151 {
574c939e 152 if (output) *output++ = (wchar_t) input;
4def3b35 153 return 1;
dccce9ea
VZ
154 }
155 else if (input>=0x110000)
4def3b35
VS
156 {
157 return (size_t)-1;
dccce9ea
VZ
158 }
159 else
4def3b35 160 {
dccce9ea 161 if (output)
4def3b35 162 {
574c939e
KB
163 *output++ = (wchar_t) ((input >> 10)+0xd7c0);
164 *output++ = (wchar_t) ((input&0x3ff)+0xdc00);
4def3b35
VS
165 }
166 return 2;
1cd52418 167 }
1cd52418
OK
168}
169
eccf1b2c 170static size_t decode_utf16(const wchar_t* input, wxUint32& output)
1cd52418 171{
dccce9ea 172 if ((*input<0xd800) || (*input>0xdfff))
4def3b35
VS
173 {
174 output = *input;
175 return 1;
dccce9ea
VZ
176 }
177 else if ((input[1]<0xdc00) || (input[1]>=0xdfff))
4def3b35
VS
178 {
179 output = *input;
180 return (size_t)-1;
dccce9ea
VZ
181 }
182 else
4def3b35
VS
183 {
184 output = ((input[0] - 0xd7c0) << 10) + (input[1] - 0xdc00);
185 return 2;
186 }
1cd52418
OK
187}
188
b0a6bb75
VZ
189#endif // WC_UTF16
190
f6bcfd97 191// ----------------------------------------------------------------------------
6001e347 192// wxMBConv
f6bcfd97 193// ----------------------------------------------------------------------------
6001e347 194
b1ac3b56
RR
195#define IGNORE_LIBC 0
196
2b5f62a0
VZ
197wxMBConv::~wxMBConv()
198{
199 // nothing to do here
200}
201
6001e347
RR
202size_t wxMBConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const
203{
b1ac3b56
RR
204#if IGNORE_LIBC
205 if (buf)
206 {
207 for (size_t i = 0; i < strlen( psz )+1; i++)
208 buf[i] = (wchar_t) psz[i];
b1ac3b56
RR
209 return strlen( psz );
210 }
211 else
212 {
213 return strlen( psz );
214 }
215#else
24f588af 216 return wxMB2WC(buf, psz, n);
b1ac3b56 217#endif
6001e347
RR
218}
219
220size_t wxMBConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
221{
b1ac3b56
RR
222#if IGNORE_LIBC
223 if (buf)
224 {
225 for (size_t i = 0; i < wxStrlen( psz )+1; i++)
226 buf[i] = (char) psz[i];
b1ac3b56
RR
227 return wxStrlen( psz );
228 }
229 else
230 {
231 return wxStrlen( psz );
232 }
233#else
24f588af 234 return wxWC2MB(buf, psz, n);
b1ac3b56 235#endif
6001e347
RR
236}
237
238const wxWCharBuffer wxMBConv::cMB2WC(const char *psz) const
239{
2b5f62a0 240 if ( psz )
6001e347 241 {
2b5f62a0
VZ
242 // calculate the length of the buffer needed first
243 size_t nLen = MB2WC(NULL, psz, 0);
244 if ( nLen != (size_t)-1 )
245 {
246 // now do the actual conversion
247 wxWCharBuffer buf(nLen);
248 MB2WC(buf.data(), psz, nLen + 1); // with the trailing NUL
249
250 return buf;
251 }
f6bcfd97 252 }
2b5f62a0
VZ
253
254 wxWCharBuffer buf((wchar_t *)NULL);
255
256 return buf;
6001e347
RR
257}
258
e5cceba0 259const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *pwz) const
6001e347 260{
2b5f62a0
VZ
261 if ( pwz )
262 {
263 size_t nLen = WC2MB(NULL, pwz, 0);
264 if ( nLen != (size_t)-1 )
265 {
266 wxCharBuffer buf(nLen);
267 WC2MB(buf.data(), pwz, nLen + 1);
268
269 return buf;
270 }
271 }
272
273 wxCharBuffer buf((char *)NULL);
e5cceba0 274
e5cceba0 275 return buf;
6001e347
RR
276}
277
6001e347
RR
278// ----------------------------------------------------------------------------
279// UTF-7
280// ----------------------------------------------------------------------------
281
fd242375 282WXDLLIMPEXP_DATA_BASE(wxMBConvUTF7) wxConvUTF7;
6001e347
RR
283
284#if 0
285static char utf7_setD[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
286 "abcdefghijklmnopqrstuvwxyz"
287 "0123456789'(),-./:?";
288static char utf7_setO[]="!\"#$%&*;<=>@[]^_`{|}";
289static char utf7_setB[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
290 "abcdefghijklmnopqrstuvwxyz"
291 "0123456789+/";
292#endif
293
294// TODO: write actual implementations of UTF-7 here
295size_t wxMBConvUTF7::MB2WC(wchar_t * WXUNUSED(buf),
296 const char * WXUNUSED(psz),
297 size_t WXUNUSED(n)) const
298{
299 return 0;
300}
301
302size_t wxMBConvUTF7::WC2MB(char * WXUNUSED(buf),
303 const wchar_t * WXUNUSED(psz),
304 size_t WXUNUSED(n)) const
305{
306 return 0;
307}
308
f6bcfd97 309// ----------------------------------------------------------------------------
6001e347 310// UTF-8
f6bcfd97 311// ----------------------------------------------------------------------------
6001e347 312
fd242375 313WXDLLIMPEXP_DATA_BASE(wxMBConvUTF8) wxConvUTF8;
6001e347 314
dccce9ea 315static wxUint32 utf8_max[]=
4def3b35 316 { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff, 0xffffffff };
6001e347
RR
317
318size_t wxMBConvUTF8::MB2WC(wchar_t *buf, const char *psz, size_t n) const
319{
4def3b35
VS
320 size_t len = 0;
321
dccce9ea 322 while (*psz && ((!buf) || (len < n)))
4def3b35
VS
323 {
324 unsigned char cc = *psz++, fc = cc;
325 unsigned cnt;
dccce9ea 326 for (cnt = 0; fc & 0x80; cnt++)
4def3b35 327 fc <<= 1;
dccce9ea 328 if (!cnt)
4def3b35
VS
329 {
330 // plain ASCII char
dccce9ea 331 if (buf)
4def3b35
VS
332 *buf++ = cc;
333 len++;
dccce9ea
VZ
334 }
335 else
4def3b35
VS
336 {
337 cnt--;
dccce9ea 338 if (!cnt)
4def3b35
VS
339 {
340 // invalid UTF-8 sequence
341 return (size_t)-1;
dccce9ea
VZ
342 }
343 else
4def3b35
VS
344 {
345 unsigned ocnt = cnt - 1;
346 wxUint32 res = cc & (0x3f >> cnt);
dccce9ea 347 while (cnt--)
4def3b35
VS
348 {
349 cc = *psz++;
dccce9ea 350 if ((cc & 0xC0) != 0x80)
4def3b35
VS
351 {
352 // invalid UTF-8 sequence
353 return (size_t)-1;
354 }
355 res = (res << 6) | (cc & 0x3f);
356 }
dccce9ea 357 if (res <= utf8_max[ocnt])
4def3b35
VS
358 {
359 // illegal UTF-8 encoding
360 return (size_t)-1;
361 }
1cd52418 362#ifdef WC_UTF16
4def3b35
VS
363 size_t pa = encode_utf16(res, buf);
364 if (pa == (size_t)-1)
365 return (size_t)-1;
dccce9ea 366 if (buf)
4def3b35
VS
367 buf += pa;
368 len += pa;
373658eb 369#else // !WC_UTF16
dccce9ea 370 if (buf)
4def3b35
VS
371 *buf++ = res;
372 len++;
373658eb 373#endif // WC_UTF16/!WC_UTF16
4def3b35
VS
374 }
375 }
6001e347 376 }
dccce9ea 377 if (buf && (len < n))
4def3b35
VS
378 *buf = 0;
379 return len;
6001e347
RR
380}
381
382size_t wxMBConvUTF8::WC2MB(char *buf, const wchar_t *psz, size_t n) const
383{
4def3b35 384 size_t len = 0;
6001e347 385
dccce9ea 386 while (*psz && ((!buf) || (len < n)))
4def3b35
VS
387 {
388 wxUint32 cc;
1cd52418 389#ifdef WC_UTF16
eccf1b2c 390 size_t pa = decode_utf16(psz, cc);
4def3b35 391 psz += (pa == (size_t)-1) ? 1 : pa;
1cd52418 392#else
4def3b35
VS
393 cc=(*psz++) & 0x7fffffff;
394#endif
395 unsigned cnt;
396 for (cnt = 0; cc > utf8_max[cnt]; cnt++) {}
dccce9ea 397 if (!cnt)
4def3b35
VS
398 {
399 // plain ASCII char
dccce9ea 400 if (buf)
574c939e 401 *buf++ = (char) cc;
4def3b35 402 len++;
dccce9ea
VZ
403 }
404
405 else
4def3b35
VS
406 {
407 len += cnt + 1;
dccce9ea 408 if (buf)
4def3b35 409 {
574c939e 410 *buf++ = (char) ((-128 >> cnt) | ((cc >> (cnt * 6)) & (0x3f >> cnt)));
4def3b35 411 while (cnt--)
574c939e 412 *buf++ = (char) (0x80 | ((cc >> (cnt * 6)) & 0x3f));
4def3b35
VS
413 }
414 }
6001e347 415 }
4def3b35
VS
416
417 if (buf && (len<n)) *buf = 0;
adb45366 418
4def3b35 419 return len;
6001e347
RR
420}
421
36acb880
VZ
422// ============================================================================
423// wxCharacterSet and derived classes
424// ============================================================================
425
426// ----------------------------------------------------------------------------
427// wxCharacterSet is the ABC for the classes below
428// ----------------------------------------------------------------------------
429
6001e347
RR
430class wxCharacterSet
431{
1cd52418 432public:
4f61e22c
VS
433 wxCharacterSet(const wxChar*name) : cname(name) {}
434 virtual ~wxCharacterSet() {}
435 virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) = 0;
436 virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) = 0;
437 virtual bool usable() const = 0;
f1339c56
RR
438public:
439 const wxChar*cname;
1cd52418
OK
440};
441
36acb880
VZ
442// ----------------------------------------------------------------------------
443// ID_CharSet: implementation of wxCharacterSet using an existing wxMBConv
444// ----------------------------------------------------------------------------
445
1cd52418
OK
446class ID_CharSet : public wxCharacterSet
447{
448public:
36acb880 449 ID_CharSet(const wxChar *name, wxMBConv *cnv)
f1339c56 450 : wxCharacterSet(name), work(cnv) {}
dccce9ea 451
4def3b35 452 size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
f1339c56 453 { return work ? work->MB2WC(buf,psz,n) : (size_t)-1; }
dccce9ea 454
4def3b35 455 size_t WC2MB(char *buf, const wchar_t *psz, size_t n)
f1339c56 456 { return work ? work->WC2MB(buf,psz,n) : (size_t)-1; }
dccce9ea 457
4f61e22c 458 bool usable() const
f1339c56
RR
459 { return work!=NULL; }
460public:
461 wxMBConv*work;
1cd52418
OK
462};
463
3caec1bb 464
36acb880
VZ
465// ============================================================================
466// The classes doing conversion using the iconv_xxx() functions
467// ============================================================================
3caec1bb 468
b040e242 469#ifdef HAVE_ICONV
3a0d76bc 470
3caec1bb
VS
471// VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with E2BIG
472// if output buffer is _exactly_ as big as needed. Such case is (unless there's
473// yet another bug in glibc) the only case when iconv() returns with (size_t)-1
474// (which means error) and says there are 0 bytes left in the input buffer --
475// when _real_ error occurs, bytes-left-in-input buffer is non-zero. Hence,
476// this alternative test for iconv() failure.
477// [This bug does not appear in glibc 2.2.]
478#if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 1
479#define ICONV_FAILED(cres, bufLeft) ((cres == (size_t)-1) && \
480 (errno != E2BIG || bufLeft != 0))
481#else
482#define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1)
483#endif
484
ab217dba 485#define ICONV_CHAR_CAST(x) ((ICONV_CONST char **)(x))
36acb880
VZ
486
487// ----------------------------------------------------------------------------
488// IC_CharSet: encapsulates an iconv character set
489// ----------------------------------------------------------------------------
490
1cd52418
OK
491class IC_CharSet : public wxCharacterSet
492{
493public:
36acb880
VZ
494 IC_CharSet(const wxChar *name);
495 virtual ~IC_CharSet();
496
497 virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n);
498 virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n);
499
500 bool usable() const
501 { return (m2w != (iconv_t)-1) && (w2m != (iconv_t)-1); }
502
503protected:
504 // the iconv handlers used to translate from multibyte to wide char and in
505 // the other direction
506 iconv_t m2w,
507 w2m;
508
509private:
510 // the name (for iconv_open()) of a wide char charset - if none is
511 // available on this machine, it will remain NULL
512 static const char *ms_wcCharsetName;
513
514 // true if the wide char encoding we use (i.e. ms_wcCharsetName) has
515 // different endian-ness than the native one
405d8f46 516 static bool ms_wcNeedsSwap;
36acb880
VZ
517};
518
519const char *IC_CharSet::ms_wcCharsetName = NULL;
405d8f46 520bool IC_CharSet::ms_wcNeedsSwap = FALSE;
36acb880
VZ
521
522IC_CharSet::IC_CharSet(const wxChar *name)
523 : wxCharacterSet(name)
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
VZ
745
746extern long wxCharsetToCodepage(const wxChar *charset); // from utils.cpp
747
1cd52418
OK
748class CP_CharSet : public wxCharacterSet
749{
750public:
b1d66b54
VZ
751 CP_CharSet(const wxChar* name)
752 : wxCharacterSet(name)
753 {
754 m_CodePage = wxCharsetToCodepage(name);
755 }
dccce9ea 756
4def3b35 757 size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
f1339c56 758 {
2b5f62a0
VZ
759 const size_t len = ::MultiByteToWideChar
760 (
761 m_CodePage, // code page
762 0, // flags (none)
763 psz, // input string
764 -1, // its length (NUL-terminated)
b4da152e 765 buf, // output string
2b5f62a0
VZ
766 buf ? n : 0 // size of output buffer
767 );
768
769 // note that it returns # of written chars for buf != NULL and *size*
770 // of the needed buffer for buf == NULL
771 return len ? (buf ? len : len - 1) : (size_t)-1;
f1339c56 772 }
dccce9ea 773
4def3b35 774 size_t WC2MB(char *buf, const wchar_t *psz, size_t n)
f1339c56 775 {
2b5f62a0
VZ
776 const size_t len = ::WideCharToMultiByte
777 (
778 m_CodePage, // code page
779 0, // flags (none)
b4da152e 780 psz, // input string
2b5f62a0
VZ
781 -1, // it is (wide) NUL-terminated
782 buf, // output buffer
783 buf ? n : 0, // and its size
784 NULL, // default "replacement" char
785 NULL // [out] was it used?
786 );
787
788 // see the comment above!
789 return len ? (buf ? len : len - 1) : (size_t)-1;
f1339c56 790 }
dccce9ea 791
4f61e22c 792 bool usable() const
b1d66b54 793 { return m_CodePage != -1; }
f1339c56
RR
794
795public:
b1d66b54 796 long m_CodePage;
1cd52418 797};
317246df 798#endif // defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
1e6feb95 799
36acb880
VZ
800// ============================================================================
801// wxEncodingConverter based conversion classes
802// ============================================================================
803
1e6feb95 804#if wxUSE_FONTMAP
1cd52418
OK
805
806class EC_CharSet : public wxCharacterSet
807{
6001e347 808public:
f1339c56
RR
809 // temporarily just use wxEncodingConverter stuff,
810 // so that it works while a better implementation is built
b1d66b54
VZ
811 EC_CharSet(const wxChar* name) : wxCharacterSet(name),
812 enc(wxFONTENCODING_SYSTEM)
f1339c56
RR
813 {
814 if (name)
142b3bc2 815 enc = wxFontMapper::Get()->CharsetToEncoding(name, FALSE);
cafbf6fb
VZ
816
817 m_ok = m2w.Init(enc, wxFONTENCODING_UNICODE) &&
818 w2m.Init(wxFONTENCODING_UNICODE, enc);
f1339c56 819 }
dccce9ea 820
574c939e 821 size_t MB2WC(wchar_t *buf, const char *psz, size_t WXUNUSED(n))
f1339c56
RR
822 {
823 size_t inbuf = strlen(psz);
dccce9ea 824 if (buf)
4def3b35 825 m2w.Convert(psz,buf);
f1339c56
RR
826 return inbuf;
827 }
dccce9ea 828
574c939e 829 size_t WC2MB(char *buf, const wchar_t *psz, size_t WXUNUSED(n))
f1339c56 830 {
f8d791e0 831 const size_t inbuf = wxWcslen(psz);
f1339c56
RR
832 if (buf)
833 w2m.Convert(psz,buf);
dccce9ea 834
f1339c56
RR
835 return inbuf;
836 }
dccce9ea 837
cafbf6fb 838 bool usable() const { return m_ok; }
f1339c56
RR
839
840public:
841 wxFontEncoding enc;
842 wxEncodingConverter m2w, w2m;
cafbf6fb
VZ
843
844 // were we initialized successfully?
845 bool m_ok;
f6bcfd97 846};
6001e347 847
1e6feb95
VZ
848#endif // wxUSE_FONTMAP
849
36acb880
VZ
850// ----------------------------------------------------------------------------
851// the function creating the wxCharacterSet for the specified charset on the
852// current system, trying all possibilities
853// ----------------------------------------------------------------------------
854
f6bcfd97 855static wxCharacterSet *wxGetCharacterSet(const wxChar *name)
6001e347 856{
cafbf6fb
VZ
857 // check for the special case of ASCII charset
858#if wxUSE_FONTMAP
142b3bc2 859 if ( wxFontMapper::Get()->CharsetToEncoding(name) == wxFONTENCODING_DEFAULT )
cafbf6fb
VZ
860#else // wxUSE_FONTMAP
861 if ( !name )
862#endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
f1339c56 863 {
cafbf6fb
VZ
864 // don't convert at all
865 return NULL;
1cd52418 866 }
dccce9ea 867
cafbf6fb
VZ
868 // the test above must have taken care of this case
869 wxCHECK_MSG( name, NULL, _T("NULL name must be wxFONTENCODING_DEFAULT") );
1e6feb95 870
cafbf6fb
VZ
871 wxCharacterSet *cset;
872
873 if ( wxStricmp(name, wxT("UTF8")) == 0 || wxStricmp(name, wxT("UTF-8")) == 0)
874 {
875 cset = new ID_CharSet(name, &wxConvUTF8);
876 }
877 else
dccce9ea 878 {
cafbf6fb
VZ
879#ifdef HAVE_ICONV
880 cset = new IC_CharSet(name);
881#else // !HAVE_ICONV
dccce9ea 882 cset = NULL;
cafbf6fb 883#endif // HAVE_ICONV/!HAVE_ICONV
dccce9ea
VZ
884 }
885
734eda8a
VZ
886 // it can only be NULL in this case
887#ifndef HAVE_ICONV
888 if ( cset )
889#endif // !HAVE_ICONV
890 {
891 if ( cset->usable() )
892 return cset;
cafbf6fb 893
734eda8a
VZ
894 delete cset;
895 cset = NULL;
896 }
cafbf6fb 897
317246df 898#if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
e4c2bd84 899 cset = new CP_CharSet(name);
cafbf6fb 900 if ( cset->usable() )
dccce9ea
VZ
901 return cset;
902
903 delete cset;
cafbf6fb 904 cset = NULL;
317246df 905#endif // defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
dccce9ea 906
e4c2bd84
VS
907#if wxUSE_FONTMAP
908 cset = new EC_CharSet(name);
cafbf6fb 909 if ( cset->usable() )
dccce9ea
VZ
910 return cset;
911
f1339c56 912 delete cset;
cafbf6fb 913 cset = NULL;
e4c2bd84 914#endif // wxUSE_FONTMAP
cafbf6fb
VZ
915
916 wxLogError(_("Cannot convert from encoding '%s'!"), name);
917
f1339c56 918 return NULL;
6001e347
RR
919}
920
36acb880
VZ
921// ============================================================================
922// wxCSConv implementation
923// ============================================================================
924
6001e347
RR
925wxCSConv::wxCSConv(const wxChar *charset)
926{
dccce9ea 927 m_name = (wxChar *)NULL;
f1339c56 928 m_cset = (wxCharacterSet *) NULL;
82713003
VZ
929 m_deferred = TRUE;
930
f1339c56 931 SetName(charset);
6001e347
RR
932}
933
934wxCSConv::~wxCSConv()
935{
65e50848
JS
936 Clear();
937}
938
54380f29
GD
939wxCSConv::wxCSConv(const wxCSConv& conv)
940 : wxMBConv()
941{
942 Clear();
943 SetName(conv.m_name);
944}
945
946wxCSConv& wxCSConv::operator=(const wxCSConv& conv)
947{
948 Clear();
949 SetName(conv.m_name);
950 return *this;
951}
952
65e50848
JS
953void wxCSConv::Clear()
954{
955 if (m_name)
956 free(m_name);
957 if (m_cset)
958 delete m_cset;
959 m_name = NULL;
960 m_cset = NULL;
6001e347
RR
961}
962
963void wxCSConv::SetName(const wxChar *charset)
964{
f1339c56
RR
965 if (charset)
966 {
967 m_name = wxStrdup(charset);
968 m_deferred = TRUE;
969 }
6001e347
RR
970}
971
972void wxCSConv::LoadNow()
973{
adb45366 974 if ( m_deferred )
f1339c56 975 {
adb45366
VZ
976 // it would probably be better to make GetSystemEncodingName() always
977 // available (i.e. even when wxUSE_INTL == 0)?
978#if wxUSE_INTL
dccce9ea 979 if ( !m_name )
f1339c56 980 {
dccce9ea
VZ
981 wxString name = wxLocale::GetSystemEncodingName();
982 if ( !name.empty() )
b1ac3b56 983 {
dccce9ea 984 SetName(name);
b1ac3b56 985 }
f1339c56 986 }
adb45366 987#endif // wxUSE_INTL
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)
f8d791e0 1022 const size_t len = wxWcslen(psz);
f1339c56
RR
1023 if (buf)
1024 {
4def3b35
VS
1025 for (size_t c = 0; c <= len; c++)
1026 buf[c] = (psz[c] > 0xff) ? '?' : psz[c];
f1339c56 1027 }
dccce9ea 1028
f1339c56 1029 return len;
6001e347
RR
1030}
1031
f6bcfd97 1032#endif // wxUSE_WCHAR_T
6001e347
RR
1033
1034