1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxEncodingConverter class for converting between different
5 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
17 #include "wx/encconv.h"
21 // conversion tables, generated by scripts in $(WXWIN)/misc/unictabl:
22 #if defined( __BORLANDC__ ) || defined(__DARWIN__)
23 #include "../common/unictabl.inc"
25 #include "unictabl.inc"
29 typedef wchar_t tchar
;
35 #include "wx/osx/core/cfstring.h"
36 #include <CoreFoundation/CFStringEncodingExt.h>
38 wxUint16 gMacEncodings
[wxFONTENCODING_MACMAX
-wxFONTENCODING_MACMIN
+1][128] ;
39 bool gMacEncodingsInited
[wxFONTENCODING_MACMAX
-wxFONTENCODING_MACMIN
+1] ;
43 #include "wx/msw/wince/missing.h" // for bsearch()
46 static const wxUint16
* GetEncTable(wxFontEncoding enc
)
49 if( enc
>= wxFONTENCODING_MACMIN
&& enc
<= wxFONTENCODING_MACMAX
)
51 int i
= enc
-wxFONTENCODING_MACMIN
;
52 if ( gMacEncodingsInited
[i
] == false )
55 CFStringEncoding cfencoding
= wxMacGetSystemEncFromFontEnc( enc
) ;
56 if( !CFStringIsEncodingAvailable( cfencoding
) )
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
)
65 wxCFStringRef
cfref( CFStringCreateWithCStringNoCopy( NULL
, s
, cfencoding
, kCFAllocatorNull
) );
66 CFStringGetCharacters( cfref
, firstchar
, (UniChar
*) &gMacEncodings
[i
][c
-128] );
68 gMacEncodingsInited
[i
]=true;
70 return gMacEncodings
[i
] ;
74 for (int i
= 0; encodings_list
[i
].table
!= NULL
; i
++)
76 if (encodings_list
[i
].encoding
== enc
)
77 return encodings_list
[i
].table
;
89 static int wxCMPFUNC_CONV
90 CompareCharsetItems(const void *i1
, const void *i2
)
92 return ( ((CharsetItem
*)i1
) -> u
- ((CharsetItem
*)i2
) -> u
);
96 static CharsetItem
* BuildReverseTable(const wxUint16
*tbl
)
98 CharsetItem
*rev
= new CharsetItem
[128];
100 for (int i
= 0; i
< 128; i
++)
101 rev
[i
].c
= wxUint8(128 + i
), rev
[i
].u
= tbl
[i
];
103 qsort(rev
, 128, sizeof(CharsetItem
), CompareCharsetItems
);
110 wxEncodingConverter::wxEncodingConverter()
113 m_UnicodeInput
= m_UnicodeOutput
= false;
119 bool wxEncodingConverter::Init(wxFontEncoding input_enc
, wxFontEncoding output_enc
, int method
)
122 const wxUint16
*in_tbl
;
123 const wxUint16
*out_tbl
= NULL
;
125 if (m_Table
) {delete[] m_Table
; m_Table
= NULL
;}
128 if (input_enc
== wxFONTENCODING_UNICODE
|| output_enc
== wxFONTENCODING_UNICODE
) return false;
131 if (input_enc
== output_enc
) {m_JustCopy
= true; return true;}
133 m_UnicodeOutput
= (output_enc
== wxFONTENCODING_UNICODE
);
136 if (input_enc
== wxFONTENCODING_UNICODE
)
138 if ((out_tbl
= GetEncTable(output_enc
)) == NULL
) return false;
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;
144 if (method
== wxCONVERT_SUBSTITUTE
)
146 for (i
= 0; i
< encoding_unicode_fallback_count
; i
++)
147 m_Table
[encoding_unicode_fallback
[i
].c
] = (tchar
) encoding_unicode_fallback
[i
].s
;
150 for (i
= 0; i
< 128; i
++)
151 m_Table
[out_tbl
[i
]] = (tchar
)(128 + i
);
153 m_UnicodeInput
= true;
155 else // input !Unicode
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;
161 m_UnicodeInput
= false;
163 m_Table
= new tchar
[256];
164 for (i
= 0; i
< 128; i
++) m_Table
[i
] = (tchar
)i
; // 7bit ASCII
166 if (output_enc
== wxFONTENCODING_UNICODE
)
168 for (i
= 0; i
< 128; i
++) m_Table
[128 + i
] = (tchar
)in_tbl
[i
];
171 else // output !Unicode
173 CharsetItem
*rev
= BuildReverseTable(out_tbl
);
177 for (i
= 0; i
< 128; 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
);
185 m_Table
[128 + i
] = (tchar
)item
-> c
;
188 m_Table
[128 + i
] = (wchar_t)(128 + i
);
190 m_Table
[128 + i
] = (char)(128 + i
);
202 #define REPLACEMENT_CHAR ((tchar)'?')
204 inline tchar
GetTableValue(const tchar
*table
, tchar value
, bool& repl
)
206 tchar r
= table
[value
];
207 if (r
== 0 && value
!= 0)
209 r
= REPLACEMENT_CHAR
;
216 bool wxEncodingConverter::Convert(const char* input
, char* output
) const
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*!"));
226 strcpy(output
, input
);
230 wxCHECK_MSG(m_Table
!= NULL
, false,
231 wxT("You must call wxEncodingConverter::Init() before actually converting!"));
233 bool replaced
= false;
235 for (i
= input
, o
= output
; *i
!= 0;)
236 *(o
++) = (char)(GetTableValue(m_Table
, (wxUint8
)*(i
++), replaced
));
245 bool wxEncodingConverter::Convert(const char* input
, wchar_t* output
) const
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*!"));
255 for (i
= input
, o
= output
; *i
!= 0;)
256 *(o
++) = (wchar_t)(*(i
++));
261 wxCHECK_MSG(m_Table
!= NULL
, false,
262 wxT("You must call wxEncodingConverter::Init() before actually converting!"));
264 bool replaced
= false;
266 for (i
= input
, o
= output
; *i
!= 0;)
267 *(o
++) = (wchar_t)(GetTableValue(m_Table
, (wxUint8
)*(i
++), replaced
));
275 bool wxEncodingConverter::Convert(const wchar_t* input
, char* output
) const
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*!"));
285 for (i
= input
, o
= output
; *i
!= 0;)
286 *(o
++) = (char)(*(i
++));
291 wxCHECK_MSG(m_Table
!= NULL
, false,
292 wxT("You must call wxEncodingConverter::Init() before actually converting!"));
294 bool replaced
= false;
296 for (i
= input
, o
= output
; *i
!= 0;)
297 *(o
++) = (char)(GetTableValue(m_Table
, (wxUint16
)*(i
++), replaced
));
305 bool wxEncodingConverter::Convert(const wchar_t* input
, wchar_t* output
) const
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*!"));
315 // wcscpy() is not guaranteed to exist
316 for (i
= input
, o
= output
; *i
!= 0;)
322 wxCHECK_MSG(m_Table
!= NULL
, false,
323 wxT("You must call wxEncodingConverter::Init() before actually converting!"));
325 bool replaced
= false;
327 for (i
= input
, o
= output
; *i
!= 0;)
328 *(o
++) = (wchar_t)(GetTableValue(m_Table
, (wxUint8
)*(i
++), replaced
));
334 #endif // wxUSE_WCHAR_T
337 wxString
wxEncodingConverter::Convert(const wxString
& input
) const
339 if (m_JustCopy
) return input
;
344 wxCHECK_MSG(m_Table
!= NULL
, s
,
345 wxT("You must call wxEncodingConverter::Init() before actually converting!"));
349 for (i
= input
.c_str(); *i
!= 0; i
++)
350 s
<< (wxChar
)(m_Table
[(wxUint16
)*i
]);
354 for (i
= input
.c_str(); *i
!= 0; i
++)
355 s
<< (wxChar
)(m_Table
[(wxUint8
)*i
]);
367 // Following tables describe classes of encoding equivalence.
370 #define STOP wxFONTENCODING_SYSTEM
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
378 static const wxFontEncoding
379 EquivalentEncodings
[][NUM_OF_PLATFORMS
][ENC_PER_PLATFORM
+1] = {
381 // *** Please put more common encodings as first! ***
385 /* unix */ {wxFONTENCODING_ISO8859_1
, wxFONTENCODING_ISO8859_15
, STOP
},
386 /* windows */ {wxFONTENCODING_CP1252
, STOP
},
388 /* mac */ {wxFONTENCODING_MACROMAN
, STOP
}
393 /* unix */ {wxFONTENCODING_ISO8859_2
, STOP
},
394 /* windows */ {wxFONTENCODING_CP1250
, STOP
},
396 /* mac */ {wxFONTENCODING_MACCENTRALEUR
, STOP
}
401 /* unix */ {wxFONTENCODING_ISO8859_13
, wxFONTENCODING_ISO8859_4
, STOP
},
402 /* windows */ {wxFONTENCODING_CP1257
, STOP
},
409 /* unix */ {wxFONTENCODING_ISO8859_8
, STOP
},
410 /* windows */ {wxFONTENCODING_CP1255
, STOP
},
412 /* mac */ {wxFONTENCODING_MACHEBREW
, STOP
}
417 /* unix */ {wxFONTENCODING_ISO8859_7
, STOP
},
418 /* windows */ {wxFONTENCODING_CP1253
, STOP
},
420 /* mac */ {wxFONTENCODING_MACGREEK
, STOP
}
425 /* unix */ {wxFONTENCODING_ISO8859_6
, STOP
},
426 /* windows */ {wxFONTENCODING_CP1256
, STOP
},
428 /* mac */ {wxFONTENCODING_MACARABIC
, STOP
}
433 /* unix */ {wxFONTENCODING_ISO8859_9
, STOP
},
434 /* windows */ {wxFONTENCODING_CP1254
, STOP
},
436 /* mac */ {wxFONTENCODING_MACTURKISH
, STOP
}
441 /* unix */ {wxFONTENCODING_KOI8
, wxFONTENCODING_KOI8_U
, wxFONTENCODING_ISO8859_5
, STOP
},
442 /* windows */ {wxFONTENCODING_CP1251
, STOP
},
444 /* mac */ {wxFONTENCODING_MACCYRILLIC
, STOP
}
447 {{STOP
},{STOP
},{STOP
},{STOP
}} /* Terminator */
448 /* no, _not_ Arnold! */
452 static bool FindEncoding(const wxFontEncodingArray
& arr
, wxFontEncoding f
)
454 for (wxFontEncodingArray::const_iterator it
= arr
.begin(), en
= arr
.end();
461 wxFontEncodingArray
wxEncodingConverter::GetPlatformEquivalents(wxFontEncoding enc
, int platform
)
463 if (platform
== wxPLATFORM_CURRENT
)
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
;
477 const wxFontEncoding
*f
;
478 wxFontEncodingArray arr
;
481 while (EquivalentEncodings
[clas
][0][0] != STOP
)
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
)
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;
501 wxFontEncodingArray
wxEncodingConverter::GetAllEquivalents(wxFontEncoding enc
)
504 const wxFontEncoding
*f
;
505 wxFontEncodingArray arr
;
507 arr
= GetPlatformEquivalents(enc
); // we want them to be first items in array
510 while (EquivalentEncodings
[clas
][0][0] != STOP
)
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
)
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;