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