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