]> git.saurik.com Git - wxWidgets.git/blob - src/common/strconv.cpp
Mutiple updates from SciTech for wxWindows including the following:
[wxWidgets.git] / src / common / strconv.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: strconv.cpp
3 // Purpose: Unicode conversion classes
4 // Author: Ove Kaaven, Robert Roebling, Vadim Zeitlin, Vaclav Slavik
5 // Modified by:
6 // Created: 29/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999 Ove Kaaven, Robert Roebling, Vadim Zeitlin, Vaclav Slavik
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
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
31 #ifdef __WXMSW__
32 #include "wx/msw/private.h"
33 #endif
34
35 #include <errno.h>
36 #include <ctype.h>
37 #include <string.h>
38 #include <stdlib.h>
39
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
50 WXDLLEXPORT_DATA(wxMBConv *) wxConvCurrent = &wxConvLibc;
51
52
53 // ============================================================================
54 // implementation
55 // ============================================================================
56
57 #if wxUSE_WCHAR_T
58
59 #ifdef __SALFORDC__
60 #include <clib.h>
61 #endif
62
63 #ifdef HAVE_ICONV_H
64 #include <iconv.h>
65 #endif
66
67 #ifdef __WXMSW__
68 #include <windows.h>
69 #endif
70
71 #define BSWAP_UCS4(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT32_SWAP_ALWAYS(str[_c]); }
72 #define BSWAP_UTF16(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT16_SWAP_ALWAYS(str[_c]); }
73
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
80 #if SIZEOF_WCHAR_T == 4
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
88 #elif SIZEOF_WCHAR_T == 2
89 #define WC_NAME "UTF16"
90 #define WC_BSWAP BSWAP_UTF16
91 #define WC_UTF16
92 #ifdef WORDS_BIGENDIAN
93 #define WC_NAME_BEST "UTF-16BE"
94 #else
95 #define WC_NAME_BEST "UTF-16LE"
96 #endif
97 #else // sizeof(wchar_t) != 2 nor 4
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"
100 #endif
101
102
103 #ifdef WC_UTF16
104
105 static size_t encode_utf16(wxUint32 input, wchar_t *output)
106 {
107 if (input<=0xffff)
108 {
109 if (output) *output++ = input;
110 return 1;
111 }
112 else if (input>=0x110000)
113 {
114 return (size_t)-1;
115 }
116 else
117 {
118 if (output)
119 {
120 *output++ = (input >> 10)+0xd7c0;
121 *output++ = (input&0x3ff)+0xdc00;
122 }
123 return 2;
124 }
125 }
126
127 static size_t decode_utf16(const wchar_t* input, wxUint32& output)
128 {
129 if ((*input<0xd800) || (*input>0xdfff))
130 {
131 output = *input;
132 return 1;
133 }
134 else if ((input[1]<0xdc00) || (input[1]>=0xdfff))
135 {
136 output = *input;
137 return (size_t)-1;
138 }
139 else
140 {
141 output = ((input[0] - 0xd7c0) << 10) + (input[1] - 0xdc00);
142 return 2;
143 }
144 }
145
146 #endif // WC_UTF16
147
148 // ----------------------------------------------------------------------------
149 // wxMBConv
150 // ----------------------------------------------------------------------------
151
152 WXDLLEXPORT_DATA(wxMBConv) wxConvLibc;
153
154 size_t wxMBConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const
155 {
156 return wxMB2WC(buf, psz, n);
157 }
158
159 size_t wxMBConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
160 {
161 return wxWC2MB(buf, psz, n);
162 }
163
164 const wxWCharBuffer wxMBConv::cMB2WC(const char *psz) const
165 {
166 if (psz)
167 {
168 size_t nLen = MB2WC((wchar_t *) NULL, psz, 0);
169 if (nLen == (size_t)-1)
170 return wxWCharBuffer((wchar_t *) NULL);
171 wxWCharBuffer buf(nLen);
172 MB2WC((wchar_t *)(const wchar_t *) buf, psz, nLen);
173 return buf;
174 }
175 else
176 return wxWCharBuffer((wchar_t *) NULL);
177 }
178
179 const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *psz) const
180 {
181 if (psz)
182 {
183 size_t nLen = WC2MB((char *) NULL, psz, 0);
184 if (nLen == (size_t)-1)
185 return wxCharBuffer((char *) NULL);
186 wxCharBuffer buf(nLen);
187 WC2MB((char *)(const char *) buf, psz, nLen);
188 return buf;
189 }
190 else
191 return wxCharBuffer((char *) NULL);
192 }
193
194 // ----------------------------------------------------------------------------
195 // standard file conversion
196 // ----------------------------------------------------------------------------
197
198 WXDLLEXPORT_DATA(wxMBConvFile) wxConvFile;
199
200 // just use the libc conversion for now
201 size_t wxMBConvFile::MB2WC(wchar_t *buf, const char *psz, size_t n) const
202 {
203 return wxMB2WC(buf, psz, n);
204 }
205
206 size_t wxMBConvFile::WC2MB(char *buf, const wchar_t *psz, size_t n) const
207 {
208 return wxWC2MB(buf, psz, n);
209 }
210
211 // ----------------------------------------------------------------------------
212 // standard gdk conversion
213 // ----------------------------------------------------------------------------
214
215 #ifdef __WXGTK12__
216
217 WXDLLEXPORT_DATA(wxMBConvGdk) wxConvGdk;
218
219 #include <gdk/gdk.h>
220
221 size_t wxMBConvGdk::MB2WC(wchar_t *buf, const char *psz, size_t n) const
222 {
223 if (buf)
224 {
225 return gdk_mbstowcs((GdkWChar *)buf, psz, n);
226 }
227 else
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 }
234 }
235
236 size_t wxMBConvGdk::WC2MB(char *buf, const wchar_t *psz, size_t n) const
237 {
238 char *mbstr = gdk_wcstombs((GdkWChar *)psz);
239 size_t len = mbstr ? strlen(mbstr) : 0;
240 if (buf)
241 {
242 if (len > n)
243 len = n;
244 memcpy(buf, psz, len);
245 if (len < n)
246 buf[len] = 0;
247 }
248 return len;
249 }
250
251 #endif // GTK > 1.0
252
253 // ----------------------------------------------------------------------------
254 // UTF-7
255 // ----------------------------------------------------------------------------
256
257 WXDLLEXPORT_DATA(wxMBConvUTF7) wxConvUTF7;
258
259 #if 0
260 static char utf7_setD[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
261 "abcdefghijklmnopqrstuvwxyz"
262 "0123456789'(),-./:?";
263 static char utf7_setO[]="!\"#$%&*;<=>@[]^_`{|}";
264 static char utf7_setB[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
265 "abcdefghijklmnopqrstuvwxyz"
266 "0123456789+/";
267 #endif
268
269 // TODO: write actual implementations of UTF-7 here
270 size_t wxMBConvUTF7::MB2WC(wchar_t * WXUNUSED(buf),
271 const char * WXUNUSED(psz),
272 size_t WXUNUSED(n)) const
273 {
274 return 0;
275 }
276
277 size_t wxMBConvUTF7::WC2MB(char * WXUNUSED(buf),
278 const wchar_t * WXUNUSED(psz),
279 size_t WXUNUSED(n)) const
280 {
281 return 0;
282 }
283
284 // ----------------------------------------------------------------------------
285 // UTF-8
286 // ----------------------------------------------------------------------------
287
288 WXDLLEXPORT_DATA(wxMBConvUTF8) wxConvUTF8;
289
290 static wxUint32 utf8_max[]=
291 { 0x7f, 0x7ff, 0xffff, 0x1fffff, 0x3ffffff, 0x7fffffff, 0xffffffff };
292
293 size_t wxMBConvUTF8::MB2WC(wchar_t *buf, const char *psz, size_t n) const
294 {
295 size_t len = 0;
296
297 while (*psz && ((!buf) || (len < n)))
298 {
299 unsigned char cc = *psz++, fc = cc;
300 unsigned cnt;
301 for (cnt = 0; fc & 0x80; cnt++)
302 fc <<= 1;
303 if (!cnt)
304 {
305 // plain ASCII char
306 if (buf)
307 *buf++ = cc;
308 len++;
309 }
310 else
311 {
312 cnt--;
313 if (!cnt)
314 {
315 // invalid UTF-8 sequence
316 return (size_t)-1;
317 }
318 else
319 {
320 unsigned ocnt = cnt - 1;
321 wxUint32 res = cc & (0x3f >> cnt);
322 while (cnt--)
323 {
324 cc = *psz++;
325 if ((cc & 0xC0) != 0x80)
326 {
327 // invalid UTF-8 sequence
328 return (size_t)-1;
329 }
330 res = (res << 6) | (cc & 0x3f);
331 }
332 if (res <= utf8_max[ocnt])
333 {
334 // illegal UTF-8 encoding
335 return (size_t)-1;
336 }
337 #ifdef WC_UTF16
338 size_t pa = encode_utf16(res, buf);
339 if (pa == (size_t)-1)
340 return (size_t)-1;
341 if (buf)
342 buf += pa;
343 len += pa;
344 #else
345 if (buf)
346 *buf++ = res;
347 len++;
348 #endif
349 }
350 }
351 }
352 if (buf && (len < n))
353 *buf = 0;
354 return len;
355 }
356
357 size_t wxMBConvUTF8::WC2MB(char *buf, const wchar_t *psz, size_t n) const
358 {
359 size_t len = 0;
360
361 while (*psz && ((!buf) || (len < n)))
362 {
363 wxUint32 cc;
364 #ifdef WC_UTF16
365 size_t pa = decode_utf16(psz, cc);
366 psz += (pa == (size_t)-1) ? 1 : pa;
367 #else
368 cc=(*psz++) & 0x7fffffff;
369 #endif
370 unsigned cnt;
371 for (cnt = 0; cc > utf8_max[cnt]; cnt++) {}
372 if (!cnt)
373 {
374 // plain ASCII char
375 if (buf)
376 *buf++ = cc;
377 len++;
378 }
379
380 else
381 {
382 len += cnt + 1;
383 if (buf)
384 {
385 *buf++ = (-128 >> cnt) | ((cc >> (cnt * 6)) & (0x3f >> cnt));
386 while (cnt--)
387 *buf++ = 0x80 | ((cc >> (cnt * 6)) & 0x3f);
388 }
389 }
390 }
391
392 if (buf && (len<n)) *buf = 0;
393 return len;
394 }
395
396 // ----------------------------------------------------------------------------
397 // specified character set
398 // ----------------------------------------------------------------------------
399
400 WXDLLEXPORT_DATA(wxCSConv) wxConvLocal((const wxChar *)NULL);
401
402 #include "wx/encconv.h"
403 #include "wx/fontmap.h"
404
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
410 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
411 #include "wx/msw/registry.h"
412 // this should work if M$ Internet Exploiter is installed
413 static long CharsetToCodepage(const wxChar *name)
414 {
415 if (!name)
416 return GetACP();
417
418 long CP=-1;
419
420 wxString cn(name);
421 do {
422 wxString path(wxT("MIME\\Database\\Charset\\"));
423 path += cn;
424 wxRegKey key(wxRegKey::HKCR, path);
425
426 if (!key.Exists()) break;
427
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;
435
436 // no encoding, see if it's an alias
437 if (!key.HasValue(wxT("AliasForCharset")) ||
438 !key.QueryValue(wxT("AliasForCharset"), cn)) break;
439 } while (1);
440
441 return CP;
442 }
443 #endif
444
445 class wxCharacterSet
446 {
447 public:
448 wxCharacterSet(const wxChar*name)
449 : cname(name) {}
450 virtual ~wxCharacterSet()
451 {}
452 virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
453 { return (size_t)-1; }
454 virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n)
455 { return (size_t)-1; }
456 virtual bool usable()
457 { return FALSE; }
458 public:
459 const wxChar*cname;
460 };
461
462 class ID_CharSet : public wxCharacterSet
463 {
464 public:
465 ID_CharSet(const wxChar *name,wxMBConv *cnv)
466 : wxCharacterSet(name), work(cnv) {}
467
468 size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
469 { return work ? work->MB2WC(buf,psz,n) : (size_t)-1; }
470
471 size_t WC2MB(char *buf, const wchar_t *psz, size_t n)
472 { return work ? work->WC2MB(buf,psz,n) : (size_t)-1; }
473
474 bool usable()
475 { return work!=NULL; }
476 public:
477 wxMBConv*work;
478 };
479
480
481 #ifdef HAVE_ICONV_H
482
483 bool g_wcNeedsSwap = FALSE;
484 static const char *g_wcCharset = NULL;
485
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
500 class IC_CharSet : public wxCharacterSet
501 {
502 public:
503 IC_CharSet(const wxChar *name)
504 : wxCharacterSet(name)
505 {
506 // check for charset that represents wchar_t:
507 if (g_wcCharset == NULL)
508 {
509 g_wcNeedsSwap = FALSE;
510
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()
575 {
576 if ( m2w != (iconv_t)-1 )
577 iconv_close(m2w);
578 if ( w2m != (iconv_t)-1 )
579 iconv_close(w2m);
580 }
581
582 size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
583 {
584 size_t inbuf = strlen(psz);
585 size_t outbuf = n * SIZEOF_WCHAR_T;
586 size_t res, cres;
587 // VS: Use these instead of psz, buf because iconv() modifies its arguments:
588 wchar_t *bufPtr = buf;
589 const char *pszPtr = psz;
590
591 if (buf)
592 {
593 // have destination buffer, convert there
594 #ifdef WX_ICONV_TAKES_CHAR
595 cres = iconv(m2w, (char**)&pszPtr, &inbuf, (char**)&bufPtr, &outbuf);
596 #else
597 cres = iconv(m2w, &pszPtr, &inbuf, (char**)&bufPtr, &outbuf);
598 #endif
599 res = n - (outbuf / SIZEOF_WCHAR_T);
600
601 if (g_wcNeedsSwap)
602 {
603 // convert to native endianness
604 WC_BSWAP(buf /* _not_ bufPtr */, res)
605 }
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 {
614 bufPtr = tbuf; outbuf = 8*SIZEOF_WCHAR_T;
615 #ifdef WX_ICONV_TAKES_CHAR
616 cres = iconv( m2w, (char**)&pszPtr, &inbuf, (char**)&bufPtr, &outbuf );
617 #else
618 cres = iconv( m2w, &pszPtr, &inbuf, (char**)&bufPtr, &outbuf );
619 #endif
620 res += 8-(outbuf/SIZEOF_WCHAR_T);
621 } while ((cres==(size_t)-1) && (errno==E2BIG));
622 }
623
624 if (ICONV_FAILED(cres, inbuf))
625 {
626 //VS: it is ok if iconv fails, hence trace only
627 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
628 return (size_t)-1;
629 }
630
631 return res;
632 }
633
634 size_t WC2MB(char *buf, const wchar_t *psz, size_t n)
635 {
636 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
637 size_t inbuf = std::wcslen(psz) * SIZEOF_WCHAR_T;
638 #else
639 size_t inbuf = ::wcslen(psz) * SIZEOF_WCHAR_T;
640 #endif
641 size_t outbuf = n;
642 size_t res, cres;
643
644 wchar_t *tmpbuf = 0;
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 }
657
658 if (buf)
659 {
660 // have destination buffer, convert there
661 #ifdef WX_ICONV_TAKES_CHAR
662 cres = iconv( w2m, (char**)&psz, &inbuf, &buf, &outbuf );
663 #else
664 cres = iconv( w2m, (const char**)&psz, &inbuf, &buf, &outbuf );
665 #endif
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;
676 #ifdef WX_ICONV_TAKES_CHAR
677 cres = iconv( w2m, (char**)&psz, &inbuf, &buf, &outbuf );
678 #else
679 cres = iconv( w2m, (const char**)&psz, &inbuf, &buf, &outbuf );
680 #endif
681 res += 16 - outbuf;
682 } while ((cres==(size_t)-1) && (errno==E2BIG));
683 }
684
685 if (g_wcNeedsSwap)
686 {
687 free(tmpbuf);
688 }
689
690 if (ICONV_FAILED(cres, inbuf))
691 {
692 //VS: it is ok if iconv fails, hence trace only
693 wxLogTrace(wxT("strconv"), wxT("iconv failed: %s"), wxSysErrorMsg(wxSysErrorCode()));
694 return (size_t)-1;
695 }
696
697 return res;
698 }
699
700 bool usable()
701 { return (m2w != (iconv_t)-1) && (w2m != (iconv_t)-1); }
702
703 protected:
704 iconv_t m2w, w2m;
705 };
706 #endif
707
708 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
709 class CP_CharSet : public wxCharacterSet
710 {
711 public:
712 CP_CharSet(const wxChar*name)
713 : wxCharacterSet(name), CodePage(CharsetToCodepage(name)) {}
714
715 size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
716 {
717 size_t len =
718 MultiByteToWideChar(CodePage, 0, psz, -1, buf, buf ? n : 0);
719 //VS: returns # of written chars for buf!=NULL and *size*
720 // needed buffer for buf==NULL
721 return len ? (buf ? len : len-1) : (size_t)-1;
722 }
723
724 size_t WC2MB(char *buf, const wchar_t *psz, size_t n)
725 {
726 size_t len = WideCharToMultiByte(CodePage, 0, psz, -1, buf,
727 buf ? n : 0, NULL, NULL);
728 //VS: returns # of written chars for buf!=NULL and *size*
729 // needed buffer for buf==NULL
730 return len ? (buf ? len : len-1) : (size_t)-1;
731 }
732
733 bool usable()
734 { return CodePage != -1; }
735
736 public:
737 long CodePage;
738 };
739 #endif // __WIN32__
740
741 #if wxUSE_FONTMAP
742
743 class EC_CharSet : public wxCharacterSet
744 {
745 public:
746 // temporarily just use wxEncodingConverter stuff,
747 // so that it works while a better implementation is built
748 EC_CharSet(const wxChar*name) : wxCharacterSet(name),
749 enc(wxFONTENCODING_SYSTEM)
750 {
751 if (name)
752 enc = wxTheFontMapper->CharsetToEncoding(name, FALSE);
753 m2w.Init(enc, wxFONTENCODING_UNICODE);
754 w2m.Init(wxFONTENCODING_UNICODE, enc);
755 }
756
757 size_t MB2WC(wchar_t *buf, const char *psz, size_t n)
758 {
759 size_t inbuf = strlen(psz);
760 if (buf)
761 m2w.Convert(psz,buf);
762 return inbuf;
763 }
764
765 size_t WC2MB(char *buf, const wchar_t *psz, size_t n)
766 {
767 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
768 size_t inbuf = std::wcslen(psz);
769 #else
770 size_t inbuf = ::wcslen(psz);
771 #endif
772 if (buf)
773 w2m.Convert(psz,buf);
774
775 return inbuf;
776 }
777
778 bool usable()
779 { return (enc!=wxFONTENCODING_SYSTEM) && (enc!=wxFONTENCODING_DEFAULT); }
780
781 public:
782 wxFontEncoding enc;
783 wxEncodingConverter m2w, w2m;
784 };
785
786 #endif // wxUSE_FONTMAP
787
788 static wxCharacterSet *wxGetCharacterSet(const wxChar *name)
789 {
790 wxCharacterSet *cset = NULL;
791 if (name)
792 {
793 if (wxStricmp(name, wxT("UTF8")) == 0 || wxStricmp(name, wxT("UTF-8")) == 0)
794 {
795 cset = new ID_CharSet(name, &wxConvUTF8);
796 }
797 else
798 {
799 #ifdef HAVE_ICONV_H
800 cset = new IC_CharSet(name); // may not take NULL
801 #endif
802 }
803 }
804
805 if (cset && cset->usable())
806 return cset;
807
808 if (cset)
809 {
810 delete cset;
811 cset = NULL;
812 }
813
814 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
815 cset = new CP_CharSet(name); // may take NULL
816 if (cset->usable())
817 return cset;
818
819 delete cset;
820 #endif // __WIN32__
821
822 #if wxUSE_FONTMAP
823 cset = new EC_CharSet(name);
824 if (cset->usable())
825 return cset;
826 #endif // wxUSE_FONTMAP
827
828 delete cset;
829 wxLogError(_("Unknown encoding '%s'!"), name);
830 return NULL;
831 }
832
833 wxCSConv::wxCSConv(const wxChar *charset)
834 {
835 m_name = (wxChar *)NULL;
836 m_cset = (wxCharacterSet *) NULL;
837 m_deferred = TRUE;
838
839 SetName(charset);
840 }
841
842 wxCSConv::~wxCSConv()
843 {
844 free(m_name);
845 delete m_cset;
846 }
847
848 void wxCSConv::SetName(const wxChar *charset)
849 {
850 if (charset)
851 {
852 m_name = wxStrdup(charset);
853 m_deferred = TRUE;
854 }
855 }
856
857 void wxCSConv::LoadNow()
858 {
859 if (m_deferred)
860 {
861 if ( !m_name )
862 {
863 wxString name = wxLocale::GetSystemEncodingName();
864 if ( !name.empty() )
865 SetName(name);
866 }
867
868 // wxGetCharacterSet() complains about NULL name
869 m_cset = m_name ? wxGetCharacterSet(m_name) : NULL;
870 m_deferred = FALSE;
871 }
872 }
873
874 size_t wxCSConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const
875 {
876 ((wxCSConv *)this)->LoadNow(); // discard constness
877
878 if (m_cset)
879 return m_cset->MB2WC(buf, psz, n);
880
881 // latin-1 (direct)
882 size_t len = strlen(psz);
883
884 if (buf)
885 {
886 for (size_t c = 0; c <= len; c++)
887 buf[c] = (unsigned char)(psz[c]);
888 }
889
890 return len;
891 }
892
893 size_t wxCSConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
894 {
895 ((wxCSConv *)this)->LoadNow(); // discard constness
896
897 if (m_cset)
898 return m_cset->WC2MB(buf, psz, n);
899
900 // latin-1 (direct)
901 #if defined(__BORLANDC__) && (__BORLANDC__ > 0x530)
902 size_t len=std::wcslen(psz);
903 #else
904 size_t len=::wcslen(psz);
905 #endif
906 if (buf)
907 {
908 for (size_t c = 0; c <= len; c++)
909 buf[c] = (psz[c] > 0xff) ? '?' : psz[c];
910 }
911
912 return len;
913 }
914
915 #ifdef HAVE_ICONV_H
916
917 class IC_CharSetConverter
918 {
919 public:
920 IC_CharSetConverter(IC_CharSet *from, IC_CharSet *to)
921 {
922 cnv = iconv_open(wxConvLibc.cWX2MB(to->cname),
923 wxConvLibc.cWX2MB(from->cname));
924 }
925
926 ~IC_CharSetConverter()
927 {
928 if (cnv != (iconv_t)-1)
929 iconv_close(cnv);
930 }
931
932 size_t Convert(char *buf, const char *psz, size_t n)
933 {
934 size_t inbuf = strlen(psz);
935 size_t outbuf = n;
936 #ifdef WX_ICONV_TAKES_CHAR
937 size_t res = iconv( cnv, (char**)&psz, &inbuf, &buf, &outbuf );
938 #else
939 size_t res = iconv( cnv, &psz, &inbuf, &buf, &outbuf );
940 #endif
941 if (res == (size_t)-1)
942 return (size_t)-1;
943 return (n - outbuf);
944 }
945
946 public:
947 iconv_t cnv;
948 };
949
950 #endif // HAVE_ICONV_H
951
952 class EC_CharSetConverter
953 {
954 public:
955 EC_CharSetConverter(EC_CharSet* from,EC_CharSet* to)
956 { cnv.Init(from->enc,to->enc); }
957
958 size_t Convert(char* buf, const char* psz, size_t n)
959 {
960 size_t inbuf = strlen(psz);
961 if (buf) cnv.Convert(psz,buf);
962 return inbuf;
963 }
964
965 public:
966 wxEncodingConverter cnv;
967 };
968
969 #else // !wxUSE_WCHAR_T
970
971 // ----------------------------------------------------------------------------
972 // stand-ins in absence of wchar_t
973 // ----------------------------------------------------------------------------
974
975 WXDLLEXPORT_DATA(wxMBConv) wxConvLibc, wxConvFile;
976
977 #endif // wxUSE_WCHAR_T
978
979