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