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