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