]> git.saurik.com Git - wxWidgets.git/blame - src/common/strconv.cpp
Make easier to compile with MSVC5.
[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__)
1cd52418
OK
411#include "wx/msw/registry.h"
412// this should work if M$ Internet Exploiter is installed
413static long CharsetToCodepage(const wxChar *name)
414{
dccce9ea 415 if (!name)
f1339c56 416 return GetACP();
dccce9ea 417
f1339c56 418 long CP=-1;
dccce9ea 419
f1339c56
RR
420 wxString cn(name);
421 do {
5ce0e4ac 422 wxString path(wxT("MIME\\Database\\Charset\\"));
f1339c56 423 path += cn;
5ce0e4ac
VS
424 wxRegKey key(wxRegKey::HKCR, path);
425
9c904e25 426 if (!key.Exists()) break;
dccce9ea 427
5ce0e4ac
VS
428 // two cases: either there's an AliasForCharset string,
429 // or there are Codepage and InternetEncoding dwords.
430 // The InternetEncoding gives us the actual encoding,
431 // the Codepage just says which Windows character set to
432 // use when displaying the data.
433 if (key.HasValue(wxT("InternetEncoding")) &&
434 key.QueryValue(wxT("InternetEncoding"), &CP)) break;
dccce9ea 435
f1339c56 436 // no encoding, see if it's an alias
5ce0e4ac
VS
437 if (!key.HasValue(wxT("AliasForCharset")) ||
438 !key.QueryValue(wxT("AliasForCharset"), cn)) break;
f1339c56 439 } while (1);
dccce9ea 440
f1339c56 441 return CP;
1cd52418
OK
442}
443#endif
444
6001e347
RR
445class wxCharacterSet
446{
1cd52418 447public:
f1339c56
RR
448 wxCharacterSet(const wxChar*name)
449 : cname(name) {}
450 virtual ~wxCharacterSet()
451 {}
dccce9ea 452 virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
f1339c56 453 { return (size_t)-1; }
4def3b35 454 virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n)
f1339c56
RR
455 { return (size_t)-1; }
456 virtual bool usable()
457 { return FALSE; }
458public:
459 const wxChar*cname;
1cd52418
OK
460};
461
462class ID_CharSet : public wxCharacterSet
463{
464public:
4def3b35 465 ID_CharSet(const wxChar *name,wxMBConv *cnv)
f1339c56 466 : wxCharacterSet(name), work(cnv) {}
dccce9ea 467
4def3b35 468 size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
f1339c56 469 { return work ? work->MB2WC(buf,psz,n) : (size_t)-1; }
dccce9ea 470
4def3b35 471 size_t WC2MB(char *buf, const wchar_t *psz, size_t n)
f1339c56 472 { return work ? work->WC2MB(buf,psz,n) : (size_t)-1; }
dccce9ea 473
f1339c56
RR
474 bool usable()
475 { return work!=NULL; }
476public:
477 wxMBConv*work;
1cd52418
OK
478};
479
3caec1bb 480
1cd52418 481#ifdef HAVE_ICONV_H
3caec1bb 482
3a0d76bc
VS
483bool g_wcNeedsSwap = FALSE;
484static const char *g_wcCharset = NULL;
485
3caec1bb
VS
486// VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with E2BIG
487// if output buffer is _exactly_ as big as needed. Such case is (unless there's
488// yet another bug in glibc) the only case when iconv() returns with (size_t)-1
489// (which means error) and says there are 0 bytes left in the input buffer --
490// when _real_ error occurs, bytes-left-in-input buffer is non-zero. Hence,
491// this alternative test for iconv() failure.
492// [This bug does not appear in glibc 2.2.]
493#if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 1
494#define ICONV_FAILED(cres, bufLeft) ((cres == (size_t)-1) && \
495 (errno != E2BIG || bufLeft != 0))
496#else
497#define ICONV_FAILED(cres, bufLeft) (cres == (size_t)-1)
498#endif
499
1cd52418
OK
500class IC_CharSet : public wxCharacterSet
501{
502public:
dccce9ea 503 IC_CharSet(const wxChar *name)
3caec1bb 504 : wxCharacterSet(name)
f1339c56 505 {
3a0d76bc
VS
506 // check for charset that represents wchar_t:
507 if (g_wcCharset == NULL)
508 {
509 g_wcNeedsSwap = FALSE;
dccce9ea 510
3a0d76bc
VS
511 // try charset with explicit bytesex info (e.g. "UCS-4LE"):
512 g_wcCharset = WC_NAME_BEST;
513 m2w = iconv_open(g_wcCharset, wxConvLibc.cWX2MB(name));
514
515 if (m2w == (iconv_t)-1)
516 {
517 // try charset w/o bytesex info (e.g. "UCS4")
518 // and check for bytesex ourselves:
519 g_wcCharset = WC_NAME;
520 m2w = iconv_open(g_wcCharset, wxConvLibc.cWX2MB(name));
521
522 // last bet, try if it knows WCHAR_T pseudo-charset
523 if (m2w == (iconv_t)-1)
524 {
525 g_wcCharset = "WCHAR_T";
526 m2w = iconv_open(g_wcCharset, wxConvLibc.cWX2MB(name));
527 }
528
529 if (m2w != (iconv_t)-1)
530 {
531 char buf[2], *bufPtr;
532 wchar_t wbuf[2], *wbufPtr;
533 size_t insz, outsz;
534 size_t res;
535
536 buf[0] = 'A';
537 buf[1] = 0;
538 wbuf[0] = 0;
539 insz = 2;
540 outsz = SIZEOF_WCHAR_T * 2;
541 wbufPtr = wbuf;
542 bufPtr = buf;
543
544 #ifdef WX_ICONV_TAKES_CHAR
545 res = iconv(m2w, (char**)&bufPtr, &insz, (char**)&wbufPtr, &outsz);
546 #else
547 res = iconv(m2w, (const char**)&bufPtr, &insz, (char**)&wbufPtr, &outsz);
548 #endif
549 if (ICONV_FAILED(res, insz))
550 {
551 g_wcCharset = NULL;
552 wxLogLastError(wxT("iconv"));
553 wxLogError(_("Convertion to charset '%s' doesn't work."), name);
554 }
555 else
556 {
557 g_wcNeedsSwap = (wbuf[0] != (wchar_t)buf[0]);
558 }
559 }
560 else
561 {
562 g_wcCharset = NULL;
563 wxLogError(_("Don't know how to convert to/from charset '%s'."), name);
564 }
565 }
566 wxLogTrace(wxT("strconv"), wxT("wchar_t charset is '%s', needs swap: %i"), g_wcCharset, g_wcNeedsSwap);
567 }
568 else
569 m2w = iconv_open(g_wcCharset, wxConvLibc.cWX2MB(name));
570
571 w2m = iconv_open(wxConvLibc.cWX2MB(name), g_wcCharset);
572 }
573
574 ~IC_CharSet()
3caec1bb 575 {
dccce9ea 576 if ( m2w != (iconv_t)-1 )
3caec1bb 577 iconv_close(m2w);
dccce9ea 578 if ( w2m != (iconv_t)-1 )
3caec1bb 579 iconv_close(w2m);
f1339c56 580 }
dccce9ea 581
3caec1bb 582 size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
f1339c56 583 {
f1339c56 584 size_t inbuf = strlen(psz);
3caec1bb 585 size_t outbuf = n * SIZEOF_WCHAR_T;
f1339c56 586 size_t res, cres;
3caec1bb
VS
587 // VS: Use these instead of psz, buf because iconv() modifies its arguments:
588 wchar_t *bufPtr = buf;
589 const char *pszPtr = psz;
590
f1339c56
RR
591 if (buf)
592 {
593 // have destination buffer, convert there
95c8801c 594#ifdef WX_ICONV_TAKES_CHAR
3caec1bb 595 cres = iconv(m2w, (char**)&pszPtr, &inbuf, (char**)&bufPtr, &outbuf);
95c8801c 596#else
3caec1bb 597 cres = iconv(m2w, &pszPtr, &inbuf, (char**)&bufPtr, &outbuf);
95c8801c 598#endif
3caec1bb 599 res = n - (outbuf / SIZEOF_WCHAR_T);
3a0d76bc
VS
600
601 if (g_wcNeedsSwap)
602 {
603 // convert to native endianness
604 WC_BSWAP(buf /* _not_ bufPtr */, res)
605 }
f1339c56
RR
606 }
607 else
608 {
609 // no destination buffer... convert using temp buffer
610 // to calculate destination buffer requirement
611 wchar_t tbuf[8];
612 res = 0;
613 do {
3caec1bb 614 bufPtr = tbuf; outbuf = 8*SIZEOF_WCHAR_T;
95c8801c 615#ifdef WX_ICONV_TAKES_CHAR
3caec1bb 616 cres = iconv( m2w, (char**)&pszPtr, &inbuf, (char**)&bufPtr, &outbuf );
95c8801c 617#else
3caec1bb 618 cres = iconv( m2w, &pszPtr, &inbuf, (char**)&bufPtr, &outbuf );
95c8801c 619#endif
f1339c56
RR
620 res += 8-(outbuf/SIZEOF_WCHAR_T);
621 } while ((cres==(size_t)-1) && (errno==E2BIG));
622 }
dccce9ea 623
3caec1bb 624 if (ICONV_FAILED(cres, inbuf))
3a0d76bc
VS
625 {
626 //VS: it is ok if iconv fails, hence trace only
627 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
f1339c56 628 return (size_t)-1;
3a0d76bc 629 }
3caec1bb 630
f1339c56
RR
631 return res;
632 }
dccce9ea 633
4def3b35 634 size_t WC2MB(char *buf, const wchar_t *psz, size_t n)
f1339c56 635 {
1cd52418 636#if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
3caec1bb 637 size_t inbuf = std::wcslen(psz) * SIZEOF_WCHAR_T;
1cd52418 638#else
3caec1bb 639 size_t inbuf = ::wcslen(psz) * SIZEOF_WCHAR_T;
1cd52418 640#endif
f1339c56
RR
641 size_t outbuf = n;
642 size_t res, cres;
3a0d76bc
VS
643
644 wchar_t *tmpbuf;
645
646 if (g_wcNeedsSwap)
647 {
648 // need to copy to temp buffer to switch endianness
649 // this absolutely doesn't rock!
650 // (no, doing WC_BSWAP twice on the original buffer won't help, as it
651 // could be in read-only memory, or be accessed in some other thread)
652 tmpbuf=(wchar_t*)malloc((inbuf+1)*SIZEOF_WCHAR_T);
653 memcpy(tmpbuf,psz,(inbuf+1)*SIZEOF_WCHAR_T);
654 WC_BSWAP(tmpbuf, inbuf)
655 psz=tmpbuf;
656 }
3caec1bb 657
f1339c56
RR
658 if (buf)
659 {
660 // have destination buffer, convert there
95c8801c 661#ifdef WX_ICONV_TAKES_CHAR
f1339c56 662 cres = iconv( w2m, (char**)&psz, &inbuf, &buf, &outbuf );
95c8801c
VS
663#else
664 cres = iconv( w2m, (const char**)&psz, &inbuf, &buf, &outbuf );
665#endif
f1339c56
RR
666 res = n-outbuf;
667 }
668 else
669 {
670 // no destination buffer... convert using temp buffer
671 // to calculate destination buffer requirement
672 char tbuf[16];
673 res = 0;
674 do {
675 buf = tbuf; outbuf = 16;
95c8801c 676#ifdef WX_ICONV_TAKES_CHAR
f1339c56 677 cres = iconv( w2m, (char**)&psz, &inbuf, &buf, &outbuf );
95c8801c
VS
678#else
679 cres = iconv( w2m, (const char**)&psz, &inbuf, &buf, &outbuf );
680#endif
f1339c56
RR
681 res += 16 - outbuf;
682 } while ((cres==(size_t)-1) && (errno==E2BIG));
683 }
3a0d76bc
VS
684
685 if (g_wcNeedsSwap)
686 {
687 free(tmpbuf);
688 }
689
3caec1bb 690 if (ICONV_FAILED(cres, inbuf))
3a0d76bc
VS
691 {
692 //VS: it is ok if iconv fails, hence trace only
693 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
f1339c56 694 return (size_t)-1;
3a0d76bc 695 }
dccce9ea 696
f1339c56
RR
697 return res;
698 }
dccce9ea 699
f1339c56 700 bool usable()
3caec1bb 701 { return (m2w != (iconv_t)-1) && (w2m != (iconv_t)-1); }
dccce9ea 702
3a0d76bc 703protected:
f1339c56 704 iconv_t m2w, w2m;
1cd52418
OK
705};
706#endif
707
04ef50df 708#if defined(__WIN32__) && !defined(__WXMICROWIN__)
1cd52418
OK
709class CP_CharSet : public wxCharacterSet
710{
711public:
dccce9ea 712 CP_CharSet(const wxChar*name)
f1339c56 713 : wxCharacterSet(name), CodePage(CharsetToCodepage(name)) {}
dccce9ea 714
4def3b35 715 size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
f1339c56 716 {
dccce9ea 717 size_t len =
4def3b35 718 MultiByteToWideChar(CodePage, 0, psz, -1, buf, buf ? n : 0);
1e6feb95 719 //VS: returns # of written chars for buf!=NULL and *size*
35d764b0
VS
720 // needed buffer for buf==NULL
721 return len ? (buf ? len : len-1) : (size_t)-1;
f1339c56 722 }
dccce9ea 723
4def3b35 724 size_t WC2MB(char *buf, const wchar_t *psz, size_t n)
f1339c56 725 {
4def3b35
VS
726 size_t len = WideCharToMultiByte(CodePage, 0, psz, -1, buf,
727 buf ? n : 0, NULL, NULL);
1e6feb95 728 //VS: returns # of written chars for buf!=NULL and *size*
35d764b0
VS
729 // needed buffer for buf==NULL
730 return len ? (buf ? len : len-1) : (size_t)-1;
f1339c56 731 }
dccce9ea 732
f1339c56 733 bool usable()
4def3b35 734 { return CodePage != -1; }
f1339c56
RR
735
736public:
737 long CodePage;
1cd52418 738};
1e6feb95
VZ
739#endif // __WIN32__
740
741#if wxUSE_FONTMAP
1cd52418
OK
742
743class EC_CharSet : public wxCharacterSet
744{
6001e347 745public:
f1339c56
RR
746 // temporarily just use wxEncodingConverter stuff,
747 // so that it works while a better implementation is built
dccce9ea 748 EC_CharSet(const wxChar*name) : wxCharacterSet(name),
4def3b35 749 enc(wxFONTENCODING_SYSTEM)
f1339c56
RR
750 {
751 if (name)
752 enc = wxTheFontMapper->CharsetToEncoding(name, FALSE);
753 m2w.Init(enc, wxFONTENCODING_UNICODE);
754 w2m.Init(wxFONTENCODING_UNICODE, enc);
755 }
dccce9ea 756
4def3b35 757 size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
f1339c56
RR
758 {
759 size_t inbuf = strlen(psz);
dccce9ea 760 if (buf)
4def3b35 761 m2w.Convert(psz,buf);
f1339c56
RR
762 return inbuf;
763 }
dccce9ea 764
4def3b35 765 size_t WC2MB(char *buf, const wchar_t *psz, size_t n)
f1339c56 766 {
1cd52418 767#if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
f1339c56 768 size_t inbuf = std::wcslen(psz);
1cd52418 769#else
f1339c56 770 size_t inbuf = ::wcslen(psz);
1cd52418 771#endif
f1339c56
RR
772 if (buf)
773 w2m.Convert(psz,buf);
dccce9ea 774
f1339c56
RR
775 return inbuf;
776 }
dccce9ea 777
f1339c56
RR
778 bool usable()
779 { return (enc!=wxFONTENCODING_SYSTEM) && (enc!=wxFONTENCODING_DEFAULT); }
780
781public:
782 wxFontEncoding enc;
783 wxEncodingConverter m2w, w2m;
f6bcfd97 784};
6001e347 785
1e6feb95
VZ
786#endif // wxUSE_FONTMAP
787
f6bcfd97 788static wxCharacterSet *wxGetCharacterSet(const wxChar *name)
6001e347 789{
f1339c56
RR
790 wxCharacterSet *cset = NULL;
791 if (name)
792 {
4def3b35 793 if (wxStricmp(name, wxT("UTF8")) == 0 || wxStricmp(name, wxT("UTF-8")) == 0)
f1339c56
RR
794 {
795 cset = new ID_CharSet(name, &wxConvUTF8);
796 }
797 else
798 {
1cd52418 799#ifdef HAVE_ICONV_H
f1339c56 800 cset = new IC_CharSet(name); // may not take NULL
1cd52418 801#endif
f1339c56 802 }
1cd52418 803 }
dccce9ea 804
1e6feb95
VZ
805 if (cset && cset->usable())
806 return cset;
807
dccce9ea
VZ
808 if (cset)
809 {
810 delete cset;
811 cset = NULL;
812 }
813
04ef50df 814#if defined(__WIN32__) && !defined(__WXMICROWIN__)
f1339c56 815 cset = new CP_CharSet(name); // may take NULL
dccce9ea
VZ
816 if (cset->usable())
817 return cset;
818
819 delete cset;
820#endif // __WIN32__
821
1e6feb95 822#if wxUSE_FONTMAP
f1339c56 823 cset = new EC_CharSet(name);
dccce9ea
VZ
824 if (cset->usable())
825 return cset;
1e6feb95 826#endif // wxUSE_FONTMAP
dccce9ea 827
f1339c56 828 delete cset;
3caec1bb 829 wxLogError(_("Unknown encoding '%s'!"), name);
f1339c56 830 return NULL;
6001e347
RR
831}
832
6001e347
RR
833wxCSConv::wxCSConv(const wxChar *charset)
834{
dccce9ea 835 m_name = (wxChar *)NULL;
f1339c56 836 m_cset = (wxCharacterSet *) NULL;
82713003
VZ
837 m_deferred = TRUE;
838
f1339c56 839 SetName(charset);
6001e347
RR
840}
841
842wxCSConv::~wxCSConv()
843{
dccce9ea
VZ
844 free(m_name);
845 delete m_cset;
6001e347
RR
846}
847
848void wxCSConv::SetName(const wxChar *charset)
849{
f1339c56
RR
850 if (charset)
851 {
852 m_name = wxStrdup(charset);
853 m_deferred = TRUE;
854 }
6001e347
RR
855}
856
857void wxCSConv::LoadNow()
858{
f1339c56
RR
859 if (m_deferred)
860 {
dccce9ea 861 if ( !m_name )
f1339c56 862 {
dccce9ea
VZ
863 wxString name = wxLocale::GetSystemEncodingName();
864 if ( !name.empty() )
865 SetName(name);
f1339c56 866 }
dccce9ea 867
a45a98fb
VZ
868 // wxGetCharacterSet() complains about NULL name
869 m_cset = m_name ? wxGetCharacterSet(m_name) : NULL;
f1339c56 870 m_deferred = FALSE;
6001e347 871 }
6001e347
RR
872}
873
874size_t wxCSConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const
875{
f1339c56 876 ((wxCSConv *)this)->LoadNow(); // discard constness
dccce9ea 877
f1339c56
RR
878 if (m_cset)
879 return m_cset->MB2WC(buf, psz, n);
880
881 // latin-1 (direct)
4def3b35 882 size_t len = strlen(psz);
dccce9ea 883
f1339c56
RR
884 if (buf)
885 {
4def3b35 886 for (size_t c = 0; c <= len; c++)
f1339c56
RR
887 buf[c] = (unsigned char)(psz[c]);
888 }
dccce9ea 889
f1339c56 890 return len;
6001e347
RR
891}
892
893size_t wxCSConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
894{
f1339c56 895 ((wxCSConv *)this)->LoadNow(); // discard constness
dccce9ea 896
f1339c56
RR
897 if (m_cset)
898 return m_cset->WC2MB(buf, psz, n);
1cd52418 899
f1339c56 900 // latin-1 (direct)
d834f22c 901#if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
f1339c56 902 size_t len=std::wcslen(psz);
d834f22c 903#else
f1339c56 904 size_t len=::wcslen(psz);
d834f22c 905#endif
f1339c56
RR
906 if (buf)
907 {
4def3b35
VS
908 for (size_t c = 0; c <= len; c++)
909 buf[c] = (psz[c] > 0xff) ? '?' : psz[c];
f1339c56 910 }
dccce9ea 911
f1339c56 912 return len;
6001e347
RR
913}
914
1cd52418 915#ifdef HAVE_ICONV_H
dccce9ea 916
1cd52418
OK
917class IC_CharSetConverter
918{
919public:
4def3b35
VS
920 IC_CharSetConverter(IC_CharSet *from, IC_CharSet *to)
921 {
dccce9ea
VZ
922 cnv = iconv_open(wxConvLibc.cWX2MB(to->cname),
923 wxConvLibc.cWX2MB(from->cname));
4def3b35 924 }
dccce9ea 925
f1339c56 926 ~IC_CharSetConverter()
dccce9ea
VZ
927 {
928 if (cnv != (iconv_t)-1)
929 iconv_close(cnv);
4def3b35 930 }
dccce9ea 931
4def3b35 932 size_t Convert(char *buf, const char *psz, size_t n)
f1339c56
RR
933 {
934 size_t inbuf = strlen(psz);
935 size_t outbuf = n;
95c8801c 936#ifdef WX_ICONV_TAKES_CHAR
f1339c56 937 size_t res = iconv( cnv, (char**)&psz, &inbuf, &buf, &outbuf );
95c8801c
VS
938#else
939 size_t res = iconv( cnv, &psz, &inbuf, &buf, &outbuf );
940#endif
dccce9ea 941 if (res == (size_t)-1)
4def3b35
VS
942 return (size_t)-1;
943 return (n - outbuf);
f1339c56
RR
944 }
945
946public:
947 iconv_t cnv;
1cd52418 948};
dccce9ea
VZ
949
950#endif // HAVE_ICONV_H
1cd52418
OK
951
952class EC_CharSetConverter
953{
954public:
1e6feb95 955 EC_CharSetConverter(EC_CharSet* from,EC_CharSet* to)
f1339c56 956 { cnv.Init(from->enc,to->enc); }
dccce9ea 957
1e6feb95 958 size_t Convert(char* buf, const char* psz, size_t n)
f1339c56
RR
959 {
960 size_t inbuf = strlen(psz);
961 if (buf) cnv.Convert(psz,buf);
962 return inbuf;
963 }
dccce9ea 964
f1339c56
RR
965public:
966 wxEncodingConverter cnv;
1cd52418
OK
967};
968
f6bcfd97
BP
969#else // !wxUSE_WCHAR_T
970
971// ----------------------------------------------------------------------------
972// stand-ins in absence of wchar_t
973// ----------------------------------------------------------------------------
974
975WXDLLEXPORT_DATA(wxMBConv) wxConvLibc, wxConvFile;
976
977#endif // wxUSE_WCHAR_T
6001e347
RR
978
979