1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxEncodingConverter class for converting between different
5 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "encconv.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
21 #include "wx/encconv.h"
25 // conversion tables, generated by scripts in $(WXWIN)/misc/unictabl:
27 #include "../common/unictabl.inc"
29 #include "unictabl.inc"
33 typedef wchar_t tchar
;
38 static wxUint16
* LINKAGEMODE
GetEncTable(wxFontEncoding enc
)
40 for (int i
= 0; encodings_list
[i
].table
!= NULL
; i
++)
42 if (encodings_list
[i
].encoding
== enc
)
43 return encodings_list
[i
].table
;
55 static int LINKAGEMODE
CompareCharsetItems(const void *i1
, const void *i2
)
57 return ( ((CharsetItem
*)i1
) -> u
- ((CharsetItem
*)i2
) -> u
);
61 static CharsetItem
* LINKAGEMODE
BuildReverseTable(wxUint16
*tbl
)
63 CharsetItem
*rev
= new CharsetItem
[128];
65 for (int i
= 0; i
< 128; i
++)
66 rev
[i
].c
= 128 + i
, rev
[i
].u
= tbl
[i
];
68 qsort(rev
, 128, sizeof(CharsetItem
), CompareCharsetItems
);
75 wxEncodingConverter::wxEncodingConverter()
78 m_UnicodeInput
= m_UnicodeOutput
= FALSE
;
84 bool wxEncodingConverter::Init(wxFontEncoding input_enc
, wxFontEncoding output_enc
, int method
)
87 wxUint16
*in_tbl
= NULL
, *out_tbl
= NULL
;
89 if (m_Table
) {delete[] m_Table
; m_Table
= NULL
;}
92 if (input_enc
== wxFONTENCODING_UNICODE
|| output_enc
== wxFONTENCODING_UNICODE
) return FALSE
;
95 if (input_enc
== output_enc
) {m_JustCopy
= TRUE
; return TRUE
;}
97 m_UnicodeOutput
= (output_enc
== wxFONTENCODING_UNICODE
);
100 if (input_enc
== wxFONTENCODING_UNICODE
)
102 if ((out_tbl
= GetEncTable(output_enc
)) == NULL
) return FALSE
;
104 m_Table
= new tchar
[65536];
105 for (i
= 0; i
< 128; i
++) m_Table
[i
] = (tchar
)i
; // 7bit ASCII
106 for (i
= 128; i
< 65536; i
++) m_Table
[i
] = (tchar
)'?';
107 // FIXME - this should be character that means `unicode to charset' impossible, not '?'
109 if (method
== wxCONVERT_SUBSTITUTE
)
111 for (i
= 0; i
< encoding_unicode_fallback_count
; i
++)
112 m_Table
[encoding_unicode_fallback
[i
].c
] = (tchar
) encoding_unicode_fallback
[i
].s
;
115 for (i
= 0; i
< 128; i
++)
116 m_Table
[out_tbl
[i
]] = (tchar
)(128 + i
);
118 m_UnicodeInput
= TRUE
;
124 if ((in_tbl
= GetEncTable(input_enc
)) == NULL
) return FALSE
;
125 if (output_enc
!= wxFONTENCODING_UNICODE
)
126 if ((out_tbl
= GetEncTable(output_enc
)) == NULL
) return FALSE
;
128 m_UnicodeInput
= FALSE
;
130 m_Table
= new tchar
[256];
131 for (i
= 0; i
< 128; i
++) m_Table
[i
] = (tchar
)i
; // 7bit ASCII
133 if (output_enc
== wxFONTENCODING_UNICODE
)
135 for (i
= 0; i
< 128; i
++) m_Table
[128 + i
] = (tchar
)in_tbl
[i
];
140 CharsetItem
*rev
= BuildReverseTable(out_tbl
);
141 CharsetItem
*item
, key
;
143 for (i
= 0; i
< 128; i
++)
146 item
= (CharsetItem
*) bsearch(&key
, rev
, 128, sizeof(CharsetItem
), CompareCharsetItems
);
147 if (item
== NULL
&& method
== wxCONVERT_SUBSTITUTE
)
148 item
= (CharsetItem
*) bsearch(&key
, encoding_unicode_fallback
,
149 encoding_unicode_fallback_count
, sizeof(CharsetItem
), CompareCharsetItems
);
151 m_Table
[128 + i
] = (tchar
)item
-> c
;
153 m_Table
[128 + i
] = 128 + i
; // don't know => don't touch
164 void wxEncodingConverter::Convert(const char* input
, char* output
)
166 wxASSERT_MSG(!m_UnicodeOutput
, wxT("You cannot convert to unicode if output is const char*!"));
167 wxASSERT_MSG(!m_UnicodeInput
, wxT("You cannot convert from unicode if input is const char*!"));
174 strcpy(output
, input
);
178 wxASSERT_MSG(m_Table
!= NULL
, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
180 for (i
= input
, o
= output
; *i
!= 0;)
181 *(o
++) = (char)(m_Table
[(wxUint8
)*(i
++)]);
188 void wxEncodingConverter::Convert(const char* input
, wchar_t* output
)
190 wxASSERT_MSG(m_UnicodeOutput
, wxT("You cannot convert to 8-bit if output is const wchar_t*!"));
191 wxASSERT_MSG(!m_UnicodeInput
, wxT("You cannot convert from unicode if input is const char*!"));
198 for (i
= input
, o
= output
; *i
!= 0;)
199 *(o
++) = (wchar_t)(*(i
++));
204 wxASSERT_MSG(m_Table
!= NULL
, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
206 for (i
= input
, o
= output
; *i
!= 0;)
207 *(o
++) = (wchar_t)(m_Table
[(wxUint8
)*(i
++)]);
213 void wxEncodingConverter::Convert(const wchar_t* input
, char* output
)
215 wxASSERT_MSG(!m_UnicodeOutput
, wxT("You cannot convert to unicode if output is const char*!"));
216 wxASSERT_MSG(m_UnicodeInput
, wxT("You cannot convert from 8-bit if input is const wchar_t*!"));
223 for (i
= input
, o
= output
; *i
!= 0;)
224 *(o
++) = (char)(*(i
++));
229 wxASSERT_MSG(m_Table
!= NULL
, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
231 for (i
= input
, o
= output
; *i
!= 0;)
232 *(o
++) = (char)(m_Table
[(wxUint16
)*(i
++)]);
238 void wxEncodingConverter::Convert(const wchar_t* input
, wchar_t* output
)
240 wxASSERT_MSG(m_UnicodeOutput
, wxT("You cannot convert to 8-bit if output is const wchar_t*!"));
241 wxASSERT_MSG(m_UnicodeInput
, wxT("You cannot convert from 8-bit if input is const wchar_t*!"));
248 // wcscpy() is not guaranteed to exist
249 for (i
= input
, o
= output
; *i
!= 0;)
255 wxASSERT_MSG(m_Table
!= NULL
, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
257 for (i
= input
, o
= output
; *i
!= 0;)
258 *(o
++) = (wchar_t)(m_Table
[(wxUint8
)*(i
++)]);
262 #endif // wxUSE_WCHAR_T
265 wxString
wxEncodingConverter::Convert(const wxString
& input
)
267 if (m_JustCopy
) return input
;
272 wxASSERT_MSG(m_Table
!= NULL
, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
275 for (i
= input
.c_str(); *i
!= 0; i
++)
276 s
<< (wxChar
)(m_Table
[(wxUint16
)*i
]);
278 for (i
= input
.c_str(); *i
!= 0; i
++)
279 s
<< (wxChar
)(m_Table
[(wxUint8
)*i
]);
289 // Following tables describe classes of encoding equivalence.
292 #define STOP wxFONTENCODING_SYSTEM
294 #define NUM_OF_PLATFORMS 4 /*must conform to enum wxPLATFORM_XXXX !!!*/
295 #define ENC_PER_PLATFORM 5
296 // max no. of encodings for one language used on one platform
297 // Anybody thinks 5 is not enough? ;-)
299 static wxFontEncoding
300 EquivalentEncodings
[][NUM_OF_PLATFORMS
][ENC_PER_PLATFORM
+1] = {
302 // *** Please put more common encodings as first! ***
306 /* unix */ {wxFONTENCODING_ISO8859_1
, wxFONTENCODING_ISO8859_15
, STOP
},
307 /* windows */ {wxFONTENCODING_CP1252
, STOP
},
314 /* unix */ {wxFONTENCODING_ISO8859_2
, STOP
},
315 /* windows */ {wxFONTENCODING_CP1250
, STOP
},
322 /* unix */ {wxFONTENCODING_ISO8859_13
, STOP
},
323 /* windows */ {wxFONTENCODING_CP1257
, STOP
},
330 /* unix */ {wxFONTENCODING_ISO8859_8
, STOP
},
331 /* windows */ {wxFONTENCODING_CP1255
, STOP
},
338 /* unix */ {wxFONTENCODING_ISO8859_7
, STOP
},
339 /* windows */ {wxFONTENCODING_CP1253
, STOP
},
346 /* unix */ {wxFONTENCODING_ISO8859_6
, STOP
},
347 /* windows */ {wxFONTENCODING_CP1256
, STOP
},
354 /* unix */ {wxFONTENCODING_ISO8859_9
, STOP
},
355 /* windows */ {wxFONTENCODING_CP1254
, STOP
},
362 /* unix */ {wxFONTENCODING_ISO8859_13
, wxFONTENCODING_ISO8859_4
,
363 wxFONTENCODING_ISO8859_15
, wxFONTENCODING_ISO8859_1
, STOP
},
364 /* windows */ {wxFONTENCODING_CP1257
, wxFONTENCODING_CP1252
, STOP
},
369 // Russia and other KOI-8 users:
371 /* unix */ {wxFONTENCODING_KOI8
, wxFONTENCODING_ISO8859_5
, STOP
},
372 /* windows */ {wxFONTENCODING_CP1251
, STOP
},
377 {{STOP
},{STOP
},{STOP
},{STOP
}} /* Terminator */
378 /* no, _not_ Arnold! */
384 wxFontEncodingArray
wxEncodingConverter::GetPlatformEquivalents(wxFontEncoding enc
, int platform
)
386 if (platform
== wxPLATFORM_CURRENT
)
388 #if defined(__WXMSW__)
389 platform
= wxPLATFORM_WINDOWS
;
390 #elif defined(__WXGTK__) || defined(__WXMOTIF__)
391 platform
= wxPLATFORM_UNIX
;
392 #elif defined(__WXOS2__)
393 platform
= wxPLATFORM_OS2
;
394 #elif defined(__WXMAC__)
395 platform
= wxPLATFORM_MAC
;
401 wxFontEncodingArray arr
;
404 while (EquivalentEncodings
[clas
][0][0] != STOP
)
406 for (i
= 0; i
< NUM_OF_PLATFORMS
; i
++)
407 for (e
= 0; EquivalentEncodings
[clas
][i
][e
] != STOP
; e
++)
408 if (EquivalentEncodings
[clas
][i
][e
] == enc
)
410 for (f
= EquivalentEncodings
[clas
][platform
]; *f
!= STOP
; f
++)
411 if (*f
== enc
) arr
.Add(enc
);
412 for (f
= EquivalentEncodings
[clas
][platform
]; *f
!= STOP
; f
++)
413 if (arr
.Index(*f
) == wxNOT_FOUND
) arr
.Add(*f
);
414 i
= NUM_OF_PLATFORMS
/*hack*/; break;
424 wxFontEncodingArray
wxEncodingConverter::GetAllEquivalents(wxFontEncoding enc
)
428 wxFontEncodingArray arr
;
430 arr
= GetPlatformEquivalents(enc
); // we want them to be first items in array
433 while (EquivalentEncodings
[clas
][0][0] != STOP
)
435 for (i
= 0; i
< NUM_OF_PLATFORMS
; i
++)
436 for (e
= 0; EquivalentEncodings
[clas
][i
][e
] != STOP
; e
++)
437 if (EquivalentEncodings
[clas
][i
][e
] == enc
)
439 for (j
= 0; j
< NUM_OF_PLATFORMS
; j
++)
440 for (f
= EquivalentEncodings
[clas
][j
]; *f
!= STOP
; f
++)
441 if (arr
.Index(*f
) == wxNOT_FOUND
) arr
.Add(*f
);
442 i
= NUM_OF_PLATFORMS
/*hack*/; break;