]> git.saurik.com Git - wxWidgets.git/blame - src/common/strconv.cpp
Printing update for Pango (GTK2 and X11-Unicode).
[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
VZ
786
787extern long wxCharsetToCodepage(const wxChar *charset); // from utils.cpp
788
1cd52418
OK
789class CP_CharSet : public wxCharacterSet
790{
791public:
b1d66b54
VZ
792 CP_CharSet(const wxChar* name)
793 : wxCharacterSet(name)
794 {
795 m_CodePage = wxCharsetToCodepage(name);
796 }
dccce9ea 797
4def3b35 798 size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
f1339c56 799 {
2b5f62a0
VZ
800 const size_t len = ::MultiByteToWideChar
801 (
802 m_CodePage, // code page
803 0, // flags (none)
804 psz, // input string
805 -1, // its length (NUL-terminated)
b4da152e 806 buf, // output string
2b5f62a0
VZ
807 buf ? n : 0 // size of output buffer
808 );
809
810 // note that it returns # of written chars for buf != NULL and *size*
811 // of the needed buffer for buf == NULL
812 return len ? (buf ? len : len - 1) : (size_t)-1;
f1339c56 813 }
dccce9ea 814
4def3b35 815 size_t WC2MB(char *buf, const wchar_t *psz, size_t n)
f1339c56 816 {
2b5f62a0
VZ
817 const size_t len = ::WideCharToMultiByte
818 (
819 m_CodePage, // code page
820 0, // flags (none)
b4da152e 821 psz, // input string
2b5f62a0
VZ
822 -1, // it is (wide) NUL-terminated
823 buf, // output buffer
824 buf ? n : 0, // and its size
825 NULL, // default "replacement" char
826 NULL // [out] was it used?
827 );
828
829 // see the comment above!
830 return len ? (buf ? len : len - 1) : (size_t)-1;
f1339c56 831 }
dccce9ea 832
4f61e22c 833 bool usable() const
b1d66b54 834 { return m_CodePage != -1; }
f1339c56
RR
835
836public:
b1d66b54 837 long m_CodePage;
1cd52418 838};
317246df 839#endif // defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
1e6feb95 840
36acb880
VZ
841// ============================================================================
842// wxEncodingConverter based conversion classes
843// ============================================================================
844
1e6feb95 845#if wxUSE_FONTMAP
1cd52418
OK
846
847class EC_CharSet : public wxCharacterSet
848{
6001e347 849public:
f1339c56
RR
850 // temporarily just use wxEncodingConverter stuff,
851 // so that it works while a better implementation is built
b1d66b54
VZ
852 EC_CharSet(const wxChar* name) : wxCharacterSet(name),
853 enc(wxFONTENCODING_SYSTEM)
f1339c56
RR
854 {
855 if (name)
142b3bc2 856 enc = wxFontMapper::Get()->CharsetToEncoding(name, FALSE);
cafbf6fb
VZ
857
858 m_ok = m2w.Init(enc, wxFONTENCODING_UNICODE) &&
859 w2m.Init(wxFONTENCODING_UNICODE, enc);
f1339c56 860 }
dccce9ea 861
574c939e 862 size_t MB2WC(wchar_t *buf, const char *psz, size_t WXUNUSED(n))
f1339c56
RR
863 {
864 size_t inbuf = strlen(psz);
dccce9ea 865 if (buf)
4def3b35 866 m2w.Convert(psz,buf);
f1339c56
RR
867 return inbuf;
868 }
dccce9ea 869
574c939e 870 size_t WC2MB(char *buf, const wchar_t *psz, size_t WXUNUSED(n))
f1339c56 871 {
f8d791e0 872 const size_t inbuf = wxWcslen(psz);
f1339c56
RR
873 if (buf)
874 w2m.Convert(psz,buf);
dccce9ea 875
f1339c56
RR
876 return inbuf;
877 }
dccce9ea 878
cafbf6fb 879 bool usable() const { return m_ok; }
f1339c56
RR
880
881public:
882 wxFontEncoding enc;
883 wxEncodingConverter m2w, w2m;
cafbf6fb
VZ
884
885 // were we initialized successfully?
886 bool m_ok;
f6bcfd97 887};
6001e347 888
1e6feb95
VZ
889#endif // wxUSE_FONTMAP
890
36acb880
VZ
891// ----------------------------------------------------------------------------
892// the function creating the wxCharacterSet for the specified charset on the
893// current system, trying all possibilities
894// ----------------------------------------------------------------------------
895
f6bcfd97 896static wxCharacterSet *wxGetCharacterSet(const wxChar *name)
6001e347 897{
cafbf6fb
VZ
898 // check for the special case of ASCII charset
899#if wxUSE_FONTMAP
142b3bc2 900 if ( wxFontMapper::Get()->CharsetToEncoding(name) == wxFONTENCODING_DEFAULT )
cafbf6fb
VZ
901#else // wxUSE_FONTMAP
902 if ( !name )
903#endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
f1339c56 904 {
cafbf6fb
VZ
905 // don't convert at all
906 return NULL;
1cd52418 907 }
dccce9ea 908
cafbf6fb
VZ
909 // the test above must have taken care of this case
910 wxCHECK_MSG( name, NULL, _T("NULL name must be wxFONTENCODING_DEFAULT") );
1e6feb95 911
cafbf6fb
VZ
912 wxCharacterSet *cset;
913
914 if ( wxStricmp(name, wxT("UTF8")) == 0 || wxStricmp(name, wxT("UTF-8")) == 0)
915 {
916 cset = new ID_CharSet(name, &wxConvUTF8);
917 }
918 else
dccce9ea 919 {
cafbf6fb
VZ
920#ifdef HAVE_ICONV
921 cset = new IC_CharSet(name);
922#else // !HAVE_ICONV
dccce9ea 923 cset = NULL;
cafbf6fb 924#endif // HAVE_ICONV/!HAVE_ICONV
dccce9ea
VZ
925 }
926
734eda8a
VZ
927 // it can only be NULL in this case
928#ifndef HAVE_ICONV
929 if ( cset )
930#endif // !HAVE_ICONV
931 {
932 if ( cset->usable() )
933 return cset;
cafbf6fb 934
734eda8a
VZ
935 delete cset;
936 cset = NULL;
937 }
cafbf6fb 938
317246df 939#if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
e4c2bd84 940 cset = new CP_CharSet(name);
cafbf6fb 941 if ( cset->usable() )
dccce9ea
VZ
942 return cset;
943
944 delete cset;
cafbf6fb 945 cset = NULL;
317246df 946#endif // defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXUNIVERSAL__)
dccce9ea 947
e4c2bd84
VS
948#if wxUSE_FONTMAP
949 cset = new EC_CharSet(name);
cafbf6fb 950 if ( cset->usable() )
dccce9ea
VZ
951 return cset;
952
f1339c56 953 delete cset;
cafbf6fb 954 cset = NULL;
e4c2bd84 955#endif // wxUSE_FONTMAP
cafbf6fb
VZ
956
957 wxLogError(_("Cannot convert from encoding '%s'!"), name);
958
f1339c56 959 return NULL;
6001e347
RR
960}
961
36acb880
VZ
962// ============================================================================
963// wxCSConv implementation
964// ============================================================================
965
6001e347
RR
966wxCSConv::wxCSConv(const wxChar *charset)
967{
dccce9ea 968 m_name = (wxChar *)NULL;
f1339c56 969 m_cset = (wxCharacterSet *) NULL;
82713003
VZ
970 m_deferred = TRUE;
971
f1339c56 972 SetName(charset);
6001e347
RR
973}
974
975wxCSConv::~wxCSConv()
976{
65e50848
JS
977 Clear();
978}
979
54380f29
GD
980wxCSConv::wxCSConv(const wxCSConv& conv)
981 : wxMBConv()
982{
983 Clear();
984 SetName(conv.m_name);
985}
986
987wxCSConv& wxCSConv::operator=(const wxCSConv& conv)
988{
989 Clear();
990 SetName(conv.m_name);
991 return *this;
992}
993
65e50848
JS
994void wxCSConv::Clear()
995{
996 if (m_name)
997 free(m_name);
998 if (m_cset)
999 delete m_cset;
1000 m_name = NULL;
1001 m_cset = NULL;
6001e347
RR
1002}
1003
1004void wxCSConv::SetName(const wxChar *charset)
1005{
f1339c56
RR
1006 if (charset)
1007 {
1008 m_name = wxStrdup(charset);
1009 m_deferred = TRUE;
1010 }
6001e347
RR
1011}
1012
1013void wxCSConv::LoadNow()
1014{
adb45366 1015 if ( m_deferred )
f1339c56 1016 {
adb45366
VZ
1017 // it would probably be better to make GetSystemEncodingName() always
1018 // available (i.e. even when wxUSE_INTL == 0)?
1019#if wxUSE_INTL
dccce9ea 1020 if ( !m_name )
f1339c56 1021 {
dccce9ea
VZ
1022 wxString name = wxLocale::GetSystemEncodingName();
1023 if ( !name.empty() )
b1ac3b56 1024 {
dccce9ea 1025 SetName(name);
b1ac3b56 1026 }
f1339c56 1027 }
adb45366 1028#endif // wxUSE_INTL
dccce9ea 1029
a45a98fb
VZ
1030 // wxGetCharacterSet() complains about NULL name
1031 m_cset = m_name ? wxGetCharacterSet(m_name) : NULL;
f1339c56 1032 m_deferred = FALSE;
6001e347 1033 }
6001e347
RR
1034}
1035
1036size_t wxCSConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const
1037{
f1339c56 1038 ((wxCSConv *)this)->LoadNow(); // discard constness
dccce9ea 1039
f1339c56
RR
1040 if (m_cset)
1041 return m_cset->MB2WC(buf, psz, n);
1042
1043 // latin-1 (direct)
4def3b35 1044 size_t len = strlen(psz);
dccce9ea 1045
f1339c56
RR
1046 if (buf)
1047 {
4def3b35 1048 for (size_t c = 0; c <= len; c++)
f1339c56
RR
1049 buf[c] = (unsigned char)(psz[c]);
1050 }
dccce9ea 1051
f1339c56 1052 return len;
6001e347
RR
1053}
1054
1055size_t wxCSConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
1056{
f1339c56 1057 ((wxCSConv *)this)->LoadNow(); // discard constness
dccce9ea 1058
f1339c56
RR
1059 if (m_cset)
1060 return m_cset->WC2MB(buf, psz, n);
1cd52418 1061
f1339c56 1062 // latin-1 (direct)
f8d791e0 1063 const size_t len = wxWcslen(psz);
f1339c56
RR
1064 if (buf)
1065 {
4def3b35
VS
1066 for (size_t c = 0; c <= len; c++)
1067 buf[c] = (psz[c] > 0xff) ? '?' : psz[c];
f1339c56 1068 }
dccce9ea 1069
f1339c56 1070 return len;
6001e347
RR
1071}
1072
f6bcfd97 1073#endif // wxUSE_WCHAR_T
6001e347
RR
1074
1075