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