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