]> git.saurik.com Git - wxWidgets.git/blame - src/common/encconv.cpp
Common default datetime formats.
[wxWidgets.git] / src / common / encconv.cpp
CommitLineData
c958260b
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: encconv.cpp
3// Purpose: wxEncodingConverter class for converting between different
4// font encodings
5// Author: Vaclav Slavik
6// Copyright: (c) 1999 Vaclav Slavik
65571936 7// Licence: wxWindows licence
c958260b
VS
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2 10#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c958260b
VS
11#pragma implementation "encconv.h"
12#endif
13
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
17#ifdef __BORLANDC__
18 #pragma hdrstop
19#endif
20
21#include "wx/encconv.h"
22
23#include <stdlib.h>
24
94fc5183 25// conversion tables, generated by scripts in $(WXWIN)/misc/unictabl:
8f9c25cc 26#if defined( __BORLANDC__ ) || defined(__DARWIN__)
0f8d6461 27 #include "../common/unictabl.inc"
94fc5183 28#else
0f8d6461 29 #include "unictabl.inc"
94fc5183 30#endif
c958260b 31
f6bcfd97 32#if wxUSE_WCHAR_T
0f8d6461 33 typedef wchar_t tchar;
f6bcfd97 34#else
0f8d6461 35 typedef char tchar;
1c193821
JS
36#endif
37
788a28b4 38#ifdef __WXMAC__
0f8d6461
VZ
39 #include <ATSUnicode.h>
40 #include <TextCommon.h>
41 #include <TextEncodingConverter.h>
788a28b4 42
0f8d6461
VZ
43 #include "wx/fontutil.h"
44 #include "wx/mac/private.h" // includes mac headers
788a28b4 45
0f8d6461
VZ
46 wxUint16 gMacEncodings[wxFONTENCODING_MACMAX-wxFONTENCODING_MACMIN+1][128] ;
47 bool gMacEncodingsInited[wxFONTENCODING_MACMAX-wxFONTENCODING_MACMIN+1] ;
48#endif
788a28b4 49
0f8d6461
VZ
50#ifdef __WXWINCE__
51 #include "wx/msw/wince/missing.h" // for bsearch()
788a28b4
SC
52#endif
53
0f8d6461 54static wxUint16* GetEncTable(wxFontEncoding enc)
c958260b 55{
788a28b4 56#ifdef __WXMAC__
3af5821c 57 if( enc >= wxFONTENCODING_MACMIN && enc <= wxFONTENCODING_MACMAX )
788a28b4 58 {
3af5821c
SC
59 int i = enc-wxFONTENCODING_MACMIN ;
60 if ( gMacEncodingsInited[i] == false )
788a28b4 61 {
3af5821c
SC
62 TECObjectRef converter ;
63 TextEncodingBase code = wxMacGetSystemEncFromFontEnc( enc ) ;
1a18887b
WS
64 TextEncodingBase unicode = CreateTextEncoding(kTextEncodingUnicodeDefault,0,kUnicode16BitFormat) ;
65 OSStatus status = TECCreateConverter(&converter,code,unicode);
66 char s[2] ;
67 s[1] = 0 ;
68 ByteCount byteInLen, byteOutLen ;
69 for( unsigned char c = 255 ; c >= 128 ; --c )
70 {
71 s[0] = c ;
3af5821c
SC
72 status = TECConvertText(converter, (ConstTextPtr) &s , 1, &byteInLen,
73 (TextPtr) &gMacEncodings[i][c-128] , 2, &byteOutLen);
1a18887b 74 }
3af5821c 75 status = TECDisposeConverter(converter);
1a18887b 76 gMacEncodingsInited[i]=true;
788a28b4 77 }
3af5821c 78 return gMacEncodings[i] ;
788a28b4
SC
79 }
80#endif
81
c958260b
VS
82 for (int i = 0; encodings_list[i].table != NULL; i++)
83 {
03424b1b 84 if (encodings_list[i].encoding == enc)
c958260b
VS
85 return encodings_list[i].table;
86 }
87 return NULL;
88}
89
90typedef struct {
91 wxUint16 u;
92 wxUint8 c;
93} CharsetItem;
94
0f8d6461
VZ
95extern "C" int wxCMPFUNC_CONV
96CompareCharsetItems(const void *i1, const void *i2)
c958260b
VS
97{
98 return ( ((CharsetItem*)i1) -> u - ((CharsetItem*)i2) -> u );
99}
100
101
0f8d6461 102static CharsetItem* BuildReverseTable(wxUint16 *tbl)
c958260b
VS
103{
104 CharsetItem *rev = new CharsetItem[128];
03424b1b 105
c958260b 106 for (int i = 0; i < 128; i++)
0203c8cd 107 rev[i].c = wxUint8(128 + i), rev[i].u = tbl[i];
c958260b
VS
108
109 qsort(rev, 128, sizeof(CharsetItem), CompareCharsetItems);
03424b1b 110
c958260b
VS
111 return rev;
112}
113
114
115
116wxEncodingConverter::wxEncodingConverter()
117{
118 m_Table = NULL;
1a18887b
WS
119 m_UnicodeInput = m_UnicodeOutput = false;
120 m_JustCopy = false;
c958260b
VS
121}
122
123
124
125bool wxEncodingConverter::Init(wxFontEncoding input_enc, wxFontEncoding output_enc, int method)
126{
127 unsigned i;
999836aa 128 wxUint16 *in_tbl, *out_tbl = NULL;
c958260b
VS
129
130 if (m_Table) {delete[] m_Table; m_Table = NULL;}
131
f6bcfd97 132#if !wxUSE_WCHAR_T
1a18887b 133 if (input_enc == wxFONTENCODING_UNICODE || output_enc == wxFONTENCODING_UNICODE) return false;
c958260b
VS
134#endif
135
1a18887b 136 if (input_enc == output_enc) {m_JustCopy = true; return true;}
03424b1b 137
47e55c2f 138 m_UnicodeOutput = (output_enc == wxFONTENCODING_UNICODE);
1a18887b 139 m_JustCopy = false;
03424b1b 140
c958260b
VS
141 if (input_enc == wxFONTENCODING_UNICODE)
142 {
1a18887b 143 if ((out_tbl = GetEncTable(output_enc)) == NULL) return false;
c958260b 144
f6bcfd97
BP
145 m_Table = new tchar[65536];
146 for (i = 0; i < 128; i++) m_Table[i] = (tchar)i; // 7bit ASCII
02c92ad9 147 for (i = 128; i < 65536; i++) m_Table[i] = (tchar)0;
c958260b
VS
148
149 if (method == wxCONVERT_SUBSTITUTE)
150 {
151 for (i = 0; i < encoding_unicode_fallback_count; i++)
f6bcfd97 152 m_Table[encoding_unicode_fallback[i].c] = (tchar) encoding_unicode_fallback[i].s;
c958260b
VS
153 }
154
155 for (i = 0; i < 128; i++)
f6bcfd97 156 m_Table[out_tbl[i]] = (tchar)(128 + i);
c958260b 157
1a18887b 158 m_UnicodeInput = true;
c958260b 159 }
b8c253ec 160 else // input !Unicode
c958260b 161 {
1a18887b 162 if ((in_tbl = GetEncTable(input_enc)) == NULL) return false;
c958260b 163 if (output_enc != wxFONTENCODING_UNICODE)
1a18887b 164 if ((out_tbl = GetEncTable(output_enc)) == NULL) return false;
c958260b 165
1a18887b 166 m_UnicodeInput = false;
03424b1b 167
f6bcfd97
BP
168 m_Table = new tchar[256];
169 for (i = 0; i < 128; i++) m_Table[i] = (tchar)i; // 7bit ASCII
03424b1b 170
c958260b
VS
171 if (output_enc == wxFONTENCODING_UNICODE)
172 {
f6bcfd97 173 for (i = 0; i < 128; i++) m_Table[128 + i] = (tchar)in_tbl[i];
1a18887b 174 return true;
c958260b 175 }
b8c253ec 176 else // output !Unicode
c958260b
VS
177 {
178 CharsetItem *rev = BuildReverseTable(out_tbl);
33ac7e6f
KB
179 CharsetItem *item;
180 CharsetItem key;
03424b1b
VZ
181
182 for (i = 0; i < 128; i++)
c958260b
VS
183 {
184 key.u = in_tbl[i];
185 item = (CharsetItem*) bsearch(&key, rev, 128, sizeof(CharsetItem), CompareCharsetItems);
186 if (item == NULL && method == wxCONVERT_SUBSTITUTE)
03424b1b 187 item = (CharsetItem*) bsearch(&key, encoding_unicode_fallback,
c958260b
VS
188 encoding_unicode_fallback_count, sizeof(CharsetItem), CompareCharsetItems);
189 if (item)
f6bcfd97 190 m_Table[128 + i] = (tchar)item -> c;
c958260b 191 else
33ac7e6f
KB
192#if wxUSE_WCHAR_T
193 m_Table[128 + i] = (wchar_t)(128 + i);
194#else
195 m_Table[128 + i] = (char)(128 + i);
307fd956 196#endif
c958260b 197 }
03424b1b 198
c958260b 199 delete[] rev;
c958260b
VS
200 }
201 }
b8c253ec 202
1a18887b 203 return true;
c958260b
VS
204}
205
206
682f3d02 207#define REPLACEMENT_CHAR ((tchar)'?')
47e55c2f 208
02c92ad9
VS
209inline tchar GetTableValue(const tchar *table, tchar value, bool& repl)
210{
211 tchar r = table[value];
212 if (r == 0 && value != 0)
213 {
214 r = REPLACEMENT_CHAR;
215 repl = true;
216 }
217 return r;
218}
219
220
221bool wxEncodingConverter::Convert(const char* input, char* output) const
c958260b 222{
f6bcfd97
BP
223 wxASSERT_MSG(!m_UnicodeOutput, wxT("You cannot convert to unicode if output is const char*!"));
224 wxASSERT_MSG(!m_UnicodeInput, wxT("You cannot convert from unicode if input is const char*!"));
225
226 const char *i;
227 char *o;
228
c958260b
VS
229 if (m_JustCopy)
230 {
f6bcfd97 231 strcpy(output, input);
02c92ad9 232 return true;
c958260b 233 }
03424b1b 234
02c92ad9
VS
235 wxCHECK_MSG(m_Table != NULL, false,
236 wxT("You must call wxEncodingConverter::Init() before actually converting!"));
237
238 bool replaced = false;
03424b1b 239
f6bcfd97 240 for (i = input, o = output; *i != 0;)
02c92ad9 241 *(o++) = (char)(GetTableValue(m_Table, (wxUint8)*(i++), replaced));
c958260b 242 *o = 0;
02c92ad9
VS
243
244 return !replaced;
c958260b
VS
245}
246
247
f6bcfd97 248#if wxUSE_WCHAR_T
47e55c2f 249
02c92ad9 250bool wxEncodingConverter::Convert(const char* input, wchar_t* output) const
47e55c2f 251{
f6bcfd97 252 wxASSERT_MSG(m_UnicodeOutput, wxT("You cannot convert to 8-bit if output is const wchar_t*!"));
47e55c2f
VS
253 wxASSERT_MSG(!m_UnicodeInput, wxT("You cannot convert from unicode if input is const char*!"));
254
255 const char *i;
f6bcfd97 256 wchar_t *o;
47e55c2f
VS
257
258 if (m_JustCopy)
259 {
260 for (i = input, o = output; *i != 0;)
f6bcfd97 261 *(o++) = (wchar_t)(*(i++));
47e55c2f 262 *o = 0;
02c92ad9 263 return true;
47e55c2f 264 }
03424b1b 265
02c92ad9
VS
266 wxCHECK_MSG(m_Table != NULL, false,
267 wxT("You must call wxEncodingConverter::Init() before actually converting!"));
268
269 bool replaced = false;
03424b1b 270
47e55c2f 271 for (i = input, o = output; *i != 0;)
02c92ad9 272 *(o++) = (wchar_t)(GetTableValue(m_Table, (wxUint8)*(i++), replaced));
47e55c2f 273 *o = 0;
02c92ad9
VS
274
275 return !replaced;
47e55c2f
VS
276}
277
278
279
02c92ad9 280bool wxEncodingConverter::Convert(const wchar_t* input, char* output) const
47e55c2f
VS
281{
282 wxASSERT_MSG(!m_UnicodeOutput, wxT("You cannot convert to unicode if output is const char*!"));
f6bcfd97 283 wxASSERT_MSG(m_UnicodeInput, wxT("You cannot convert from 8-bit if input is const wchar_t*!"));
47e55c2f 284
f6bcfd97 285 const wchar_t *i;
47e55c2f
VS
286 char *o;
287
288 if (m_JustCopy)
289 {
290 for (i = input, o = output; *i != 0;)
291 *(o++) = (char)(*(i++));
292 *o = 0;
02c92ad9 293 return true;
47e55c2f 294 }
03424b1b 295
02c92ad9
VS
296 wxCHECK_MSG(m_Table != NULL, false,
297 wxT("You must call wxEncodingConverter::Init() before actually converting!"));
298
299 bool replaced = false;
03424b1b 300
f6bcfd97 301 for (i = input, o = output; *i != 0;)
02c92ad9 302 *(o++) = (char)(GetTableValue(m_Table, (wxUint16)*(i++), replaced));
47e55c2f 303 *o = 0;
02c92ad9
VS
304
305 return !replaced;
47e55c2f
VS
306}
307
308
309
02c92ad9 310bool wxEncodingConverter::Convert(const wchar_t* input, wchar_t* output) const
47e55c2f 311{
f6bcfd97
BP
312 wxASSERT_MSG(m_UnicodeOutput, wxT("You cannot convert to 8-bit if output is const wchar_t*!"));
313 wxASSERT_MSG(m_UnicodeInput, wxT("You cannot convert from 8-bit if input is const wchar_t*!"));
47e55c2f 314
f6bcfd97
BP
315 const wchar_t *i;
316 wchar_t *o;
47e55c2f
VS
317
318 if (m_JustCopy)
319 {
f6bcfd97
BP
320 // wcscpy() is not guaranteed to exist
321 for (i = input, o = output; *i != 0;)
322 *(o++) = (*(i++));
323 *o = 0;
02c92ad9 324 return true;
47e55c2f 325 }
03424b1b 326
02c92ad9
VS
327 wxCHECK_MSG(m_Table != NULL, false,
328 wxT("You must call wxEncodingConverter::Init() before actually converting!"));
03424b1b 329
02c92ad9
VS
330 bool replaced = false;
331
47e55c2f 332 for (i = input, o = output; *i != 0;)
02c92ad9 333 *(o++) = (wchar_t)(GetTableValue(m_Table, (wxUint8)*(i++), replaced));
47e55c2f 334 *o = 0;
02c92ad9
VS
335
336 return !replaced;
47e55c2f
VS
337}
338
f6bcfd97 339#endif // wxUSE_WCHAR_T
47e55c2f 340
c958260b 341
57c5293e 342wxString wxEncodingConverter::Convert(const wxString& input) const
c958260b
VS
343{
344 if (m_JustCopy) return input;
03424b1b 345
c958260b
VS
346 wxString s;
347 const wxChar *i;
03424b1b 348
4ccae30a
VZ
349 wxCHECK_MSG(m_Table != NULL, s,
350 wxT("You must call wxEncodingConverter::Init() before actually converting!"));
03424b1b 351
c958260b 352 if (m_UnicodeInput)
307fd956 353 {
c958260b
VS
354 for (i = input.c_str(); *i != 0; i++)
355 s << (wxChar)(m_Table[(wxUint16)*i]);
307fd956 356 }
c958260b 357 else
307fd956 358 {
c958260b
VS
359 for (i = input.c_str(); *i != 0; i++)
360 s << (wxChar)(m_Table[(wxUint8)*i]);
307fd956
VZ
361 }
362
c958260b
VS
363 return s;
364}
365
366
367
368
47e55c2f
VS
369
370
371
c958260b 372// Following tables describe classes of encoding equivalence.
03424b1b 373//
c958260b
VS
374
375#define STOP wxFONTENCODING_SYSTEM
376
377#define NUM_OF_PLATFORMS 4 /*must conform to enum wxPLATFORM_XXXX !!!*/
617eb021 378#define ENC_PER_PLATFORM 5
c958260b 379 // max no. of encodings for one language used on one platform
617eb021 380 // Anybody thinks 5 is not enough? ;-)
c958260b 381
03424b1b 382static wxFontEncoding
c958260b
VS
383 EquivalentEncodings[][NUM_OF_PLATFORMS][ENC_PER_PLATFORM+1] = {
384
47e55c2f
VS
385 // *** Please put more common encodings as first! ***
386
03424b1b 387 // Western European
c958260b
VS
388 {
389 /* unix */ {wxFONTENCODING_ISO8859_1, wxFONTENCODING_ISO8859_15, STOP},
390 /* windows */ {wxFONTENCODING_CP1252, STOP},
391 /* os2 */ {STOP},
788a28b4 392 /* mac */ {wxFONTENCODING_MACROMAN, STOP}
c958260b
VS
393 },
394
47e55c2f 395 // Central European
c958260b
VS
396 {
397 /* unix */ {wxFONTENCODING_ISO8859_2, STOP},
398 /* windows */ {wxFONTENCODING_CP1250, STOP},
399 /* os2 */ {STOP},
788a28b4 400 /* mac */ {wxFONTENCODING_MACCENTRALEUR, STOP}
c958260b 401 },
03424b1b 402
47e55c2f
VS
403 // Baltic
404 {
03424b1b 405 /* unix */ {wxFONTENCODING_ISO8859_13, wxFONTENCODING_ISO8859_4, STOP},
47e55c2f
VS
406 /* windows */ {wxFONTENCODING_CP1257, STOP},
407 /* os2 */ {STOP},
03424b1b 408 /* mac */ {STOP}
47e55c2f
VS
409 },
410
411 // Hebrew
412 {
413 /* unix */ {wxFONTENCODING_ISO8859_8, STOP},
414 /* windows */ {wxFONTENCODING_CP1255, STOP},
415 /* os2 */ {STOP},
788a28b4 416 /* mac */ {wxFONTENCODING_MACHEBREW, STOP}
47e55c2f
VS
417 },
418
419 // Greek
420 {
421 /* unix */ {wxFONTENCODING_ISO8859_7, STOP},
422 /* windows */ {wxFONTENCODING_CP1253, STOP},
423 /* os2 */ {STOP},
788a28b4 424 /* mac */ {wxFONTENCODING_MACGREEK, STOP}
47e55c2f
VS
425 },
426
427 // Arabic
428 {
429 /* unix */ {wxFONTENCODING_ISO8859_6, STOP},
430 /* windows */ {wxFONTENCODING_CP1256, STOP},
431 /* os2 */ {STOP},
788a28b4 432 /* mac */ {wxFONTENCODING_MACARABIC, STOP}
47e55c2f
VS
433 },
434
435 // Turkish
436 {
437 /* unix */ {wxFONTENCODING_ISO8859_9, STOP},
438 /* windows */ {wxFONTENCODING_CP1254, STOP},
439 /* os2 */ {STOP},
788a28b4 440 /* mac */ {wxFONTENCODING_MACTURKISH, STOP}
47e55c2f
VS
441 },
442
443 // Cyrillic
3b61656e 444 {
15ad38c3 445 /* unix */ {wxFONTENCODING_KOI8, wxFONTENCODING_KOI8_U, wxFONTENCODING_ISO8859_5, STOP},
47e55c2f
VS
446 /* windows */ {wxFONTENCODING_CP1251, STOP},
447 /* os2 */ {STOP},
788a28b4 448 /* mac */ {wxFONTENCODING_MACCYRILLIC, STOP}
47e55c2f 449 },
c958260b
VS
450
451 {{STOP},{STOP},{STOP},{STOP}} /* Terminator */
452 /* no, _not_ Arnold! */
453};
454
455
df5168c4
MB
456static bool FindEncoding(const wxFontEncodingArray& arr, wxFontEncoding f)
457{
458 for (wxFontEncodingArray::const_iterator it = arr.begin(), en = arr.end();
459 it != en; ++it)
460 if (*it == f)
461 return true;
462 return false;
463}
c958260b
VS
464
465wxFontEncodingArray wxEncodingConverter::GetPlatformEquivalents(wxFontEncoding enc, int platform)
466{
467 if (platform == wxPLATFORM_CURRENT)
468 {
469#if defined(__WXMSW__)
470 platform = wxPLATFORM_WINDOWS;
471#elif defined(__WXGTK__) || defined(__WXMOTIF__)
472 platform = wxPLATFORM_UNIX;
473#elif defined(__WXOS2__)
474 platform = wxPLATFORM_OS2;
475#elif defined(__WXMAC__)
476 platform = wxPLATFORM_MAC;
477#endif
478 }
03424b1b 479
c958260b
VS
480 int i, clas, e ;
481 wxFontEncoding *f;
482 wxFontEncodingArray arr;
483
484 clas = 0;
485 while (EquivalentEncodings[clas][0][0] != STOP)
486 {
487 for (i = 0; i < NUM_OF_PLATFORMS; i++)
488 for (e = 0; EquivalentEncodings[clas][i][e] != STOP; e++)
489 if (EquivalentEncodings[clas][i][e] == enc)
490 {
47e55c2f 491 for (f = EquivalentEncodings[clas][platform]; *f != STOP; f++)
df5168c4 492 if (*f == enc) arr.push_back(enc);
47e55c2f 493 for (f = EquivalentEncodings[clas][platform]; *f != STOP; f++)
df5168c4 494 if (!FindEncoding(arr, *f)) arr.push_back(*f);
03424b1b 495 i = NUM_OF_PLATFORMS/*hack*/; break;
c958260b
VS
496 }
497 clas++;
498 }
03424b1b 499
c958260b
VS
500 return arr;
501}
502
503
504
505wxFontEncodingArray wxEncodingConverter::GetAllEquivalents(wxFontEncoding enc)
506{
507 int i, clas, e, j ;
508 wxFontEncoding *f;
509 wxFontEncodingArray arr;
03424b1b 510
47e55c2f 511 arr = GetPlatformEquivalents(enc); // we want them to be first items in array
c958260b
VS
512
513 clas = 0;
514 while (EquivalentEncodings[clas][0][0] != STOP)
515 {
516 for (i = 0; i < NUM_OF_PLATFORMS; i++)
517 for (e = 0; EquivalentEncodings[clas][i][e] != STOP; e++)
518 if (EquivalentEncodings[clas][i][e] == enc)
519 {
520 for (j = 0; j < NUM_OF_PLATFORMS; j++)
03424b1b 521 for (f = EquivalentEncodings[clas][j]; *f != STOP; f++)
df5168c4 522 if (!FindEncoding(arr, *f)) arr.push_back(*f);
03424b1b 523 i = NUM_OF_PLATFORMS/*hack*/; break;
c958260b
VS
524 }
525 clas++;
526 }
03424b1b 527
c958260b
VS
528 return arr;
529}
1e6feb95 530