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