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