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 static wxUint16
* LINKAGEMODE
GetEncTable(wxFontEncoding enc
)
35 for (int i
= 0; encodings_list
[i
].table
!= NULL
; i
++)
37 if (encodings_list
[i
].encoding
== enc
)
38 return encodings_list
[i
].table
;
50 static int LINKAGEMODE
CompareCharsetItems(const void *i1
, const void *i2
)
52 return ( ((CharsetItem
*)i1
) -> u
- ((CharsetItem
*)i2
) -> u
);
56 static CharsetItem
* LINKAGEMODE
BuildReverseTable(wxUint16
*tbl
)
58 CharsetItem
*rev
= new CharsetItem
[128];
60 for (int i
= 0; i
< 128; i
++)
61 rev
[i
].c
= 128 + i
, rev
[i
].u
= tbl
[i
];
63 qsort(rev
, 128, sizeof(CharsetItem
), CompareCharsetItems
);
70 wxEncodingConverter::wxEncodingConverter()
73 m_UnicodeInput
= m_UnicodeOutput
= FALSE
;
79 bool wxEncodingConverter::Init(wxFontEncoding input_enc
, wxFontEncoding output_enc
, int method
)
82 wxUint16
*in_tbl
= NULL
, *out_tbl
= NULL
;
84 if (m_Table
) {delete[] m_Table
; m_Table
= NULL
;}
87 if (input_enc
== wxFONTENCODING_UNICODE
|| output_enc
== wxFONTENCODING_UNICODE
) return FALSE
;
90 if (input_enc
== output_enc
) {m_JustCopy
= TRUE
; return TRUE
;}
92 m_UnicodeOutput
= (output_enc
== wxFONTENCODING_UNICODE
);
95 if (input_enc
== wxFONTENCODING_UNICODE
)
97 if ((out_tbl
= GetEncTable(output_enc
)) == NULL
) return FALSE
;
99 m_Table
= new wxChar
[65536];
100 for (i
= 0; i
< 128; i
++) m_Table
[i
] = (wxChar
)i
; // 7bit ASCII
101 for (i
= 128; i
< 65536; i
++) m_Table
[i
] = (wxChar
)'?';
102 // FIXME - this should be character that means `unicode to charset' impossible, not '?'
104 if (method
== wxCONVERT_SUBSTITUTE
)
106 for (i
= 0; i
< encoding_unicode_fallback_count
; i
++)
107 m_Table
[encoding_unicode_fallback
[i
].c
] = (wxChar
) encoding_unicode_fallback
[i
].s
;
110 for (i
= 0; i
< 128; i
++)
111 m_Table
[out_tbl
[i
]] = (wxChar
)(128 + i
);
113 m_UnicodeInput
= TRUE
;
119 if ((in_tbl
= GetEncTable(input_enc
)) == NULL
) return FALSE
;
120 if (output_enc
!= wxFONTENCODING_UNICODE
)
121 if ((out_tbl
= GetEncTable(output_enc
)) == NULL
) return FALSE
;
123 m_UnicodeInput
= FALSE
;
125 m_Table
= new wxChar
[256];
126 for (i
= 0; i
< 128; i
++) m_Table
[i
] = (wxChar
)i
; // 7bit ASCII
128 if (output_enc
== wxFONTENCODING_UNICODE
)
130 for (i
= 0; i
< 128; i
++) m_Table
[128 + i
] = (wxChar
)in_tbl
[i
]; // wxChar is 2byte now
135 CharsetItem
*rev
= BuildReverseTable(out_tbl
);
136 CharsetItem
*item
, key
;
138 for (i
= 0; i
< 128; i
++)
141 item
= (CharsetItem
*) bsearch(&key
, rev
, 128, sizeof(CharsetItem
), CompareCharsetItems
);
142 if (item
== NULL
&& method
== wxCONVERT_SUBSTITUTE
)
143 item
= (CharsetItem
*) bsearch(&key
, encoding_unicode_fallback
,
144 encoding_unicode_fallback_count
, sizeof(CharsetItem
), CompareCharsetItems
);
146 m_Table
[128 + i
] = (wxChar
)item
-> c
;
148 m_Table
[128 + i
] = 128 + i
; // don't know => don't touch
159 void wxEncodingConverter::Convert(const wxChar
* input
, wxChar
* output
)
163 wxStrcpy(output
, input
);
167 wxASSERT_MSG(m_Table
!= NULL
, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
173 for (i
= input
, o
= output
; *i
!= 0; i
++, o
++)
174 *o
= (wxChar
)(m_Table
[(wxUint16
)*i
]);
176 for (i
= input
, o
= output
; *i
!= 0; i
++, o
++)
177 *o
= (wxChar
)(m_Table
[(wxUint8
)*i
]);
182 #if wxUSE_UNICODE // otherwise wxChar === char
184 void wxEncodingConverter::Convert(const char* input
, wxChar
* output
)
186 wxASSERT_MSG(!m_UnicodeInput
, wxT("You cannot convert from unicode if input is const char*!"));
193 for (i
= input
, o
= output
; *i
!= 0;)
194 *(o
++) = (wxChar
)(*(i
++));
199 wxASSERT_MSG(m_Table
!= NULL
, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
201 for (i
= input
, o
= output
; *i
!= 0;)
202 *(o
++) = (wxChar
)(m_Table
[(wxUint8
)*(i
++)]);
208 void wxEncodingConverter::Convert(const wxChar
* input
, char* output
)
210 wxASSERT_MSG(!m_UnicodeOutput
, wxT("You cannot convert to unicode if output is const char*!"));
217 for (i
= input
, o
= output
; *i
!= 0;)
218 *(o
++) = (char)(*(i
++));
223 wxASSERT_MSG(m_Table
!= NULL
, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
226 for (i
= input
, o
= output
; *i
!= 0; i
++, o
++)
227 *o
= (char)(m_Table
[(wxUint16
)*i
]);
229 for (i
= input
, o
= output
; *i
!= 0; i
++, o
++)
230 *o
= (char)(m_Table
[(wxUint8
)*i
]);
236 void wxEncodingConverter::Convert(const char* input
, char* output
)
238 wxASSERT_MSG(!m_UnicodeOutput
, wxT("You cannot convert to unicode if output is const char*!"));
239 wxASSERT_MSG(!m_UnicodeInput
, wxT("You cannot convert from unicode if input is const char*!"));
246 strcpy(output
, input
);
250 wxASSERT_MSG(m_Table
!= NULL
, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
252 for (i
= input
, o
= output
; *i
!= 0;)
253 *(o
++) = (char)(m_Table
[(wxUint8
)*(i
++)]);
257 #endif // wxUSE_UNICODE
260 wxString
wxEncodingConverter::Convert(const wxString
& input
)
262 if (m_JustCopy
) return input
;
267 wxASSERT_MSG(m_Table
!= NULL
, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
270 for (i
= input
.c_str(); *i
!= 0; i
++)
271 s
<< (wxChar
)(m_Table
[(wxUint16
)*i
]);
273 for (i
= input
.c_str(); *i
!= 0; i
++)
274 s
<< (wxChar
)(m_Table
[(wxUint8
)*i
]);
284 // Following tables describe classes of encoding equivalence.
287 #define STOP wxFONTENCODING_SYSTEM
289 #define NUM_OF_PLATFORMS 4 /*must conform to enum wxPLATFORM_XXXX !!!*/
290 #define ENC_PER_PLATFORM 5
291 // max no. of encodings for one language used on one platform
292 // Anybody thinks 5 is not enough? ;-)
294 static wxFontEncoding
295 EquivalentEncodings
[][NUM_OF_PLATFORMS
][ENC_PER_PLATFORM
+1] = {
297 // *** Please put more common encodings as first! ***
301 /* unix */ {wxFONTENCODING_ISO8859_1
, wxFONTENCODING_ISO8859_15
, STOP
},
302 /* windows */ {wxFONTENCODING_CP1252
, STOP
},
309 /* unix */ {wxFONTENCODING_ISO8859_2
, STOP
},
310 /* windows */ {wxFONTENCODING_CP1250
, STOP
},
317 /* unix */ {wxFONTENCODING_ISO8859_13
, STOP
},
318 /* windows */ {wxFONTENCODING_CP1257
, STOP
},
325 /* unix */ {wxFONTENCODING_ISO8859_8
, STOP
},
326 /* windows */ {wxFONTENCODING_CP1255
, STOP
},
333 /* unix */ {wxFONTENCODING_ISO8859_7
, STOP
},
334 /* windows */ {wxFONTENCODING_CP1253
, STOP
},
341 /* unix */ {wxFONTENCODING_ISO8859_6
, STOP
},
342 /* windows */ {wxFONTENCODING_CP1256
, STOP
},
349 /* unix */ {wxFONTENCODING_ISO8859_9
, STOP
},
350 /* windows */ {wxFONTENCODING_CP1254
, STOP
},
357 /* unix */ {wxFONTENCODING_ISO8859_13
, wxFONTENCODING_ISO8859_4
,
358 wxFONTENCODING_ISO8859_15
, wxFONTENCODING_ISO8859_1
, STOP
},
359 /* windows */ {wxFONTENCODING_CP1257
, wxFONTENCODING_CP1252
, STOP
},
364 // Russia and other KOI-8 users:
366 /* unix */ {wxFONTENCODING_KOI8
, wxFONTENCODING_ISO8859_5
, STOP
},
367 /* windows */ {wxFONTENCODING_CP1251
, STOP
},
372 {{STOP
},{STOP
},{STOP
},{STOP
}} /* Terminator */
373 /* no, _not_ Arnold! */
379 wxFontEncodingArray
wxEncodingConverter::GetPlatformEquivalents(wxFontEncoding enc
, int platform
)
381 if (platform
== wxPLATFORM_CURRENT
)
383 #if defined(__WXMSW__)
384 platform
= wxPLATFORM_WINDOWS
;
385 #elif defined(__WXGTK__) || defined(__WXMOTIF__)
386 platform
= wxPLATFORM_UNIX
;
387 #elif defined(__WXOS2__)
388 platform
= wxPLATFORM_OS2
;
389 #elif defined(__WXMAC__)
390 platform
= wxPLATFORM_MAC
;
396 wxFontEncodingArray arr
;
399 while (EquivalentEncodings
[clas
][0][0] != STOP
)
401 for (i
= 0; i
< NUM_OF_PLATFORMS
; i
++)
402 for (e
= 0; EquivalentEncodings
[clas
][i
][e
] != STOP
; e
++)
403 if (EquivalentEncodings
[clas
][i
][e
] == enc
)
405 for (f
= EquivalentEncodings
[clas
][platform
]; *f
!= STOP
; f
++)
406 if (*f
== enc
) arr
.Add(enc
);
407 for (f
= EquivalentEncodings
[clas
][platform
]; *f
!= STOP
; f
++)
408 if (arr
.Index(*f
) == wxNOT_FOUND
) arr
.Add(*f
);
409 i
= NUM_OF_PLATFORMS
/*hack*/; break;
419 wxFontEncodingArray
wxEncodingConverter::GetAllEquivalents(wxFontEncoding enc
)
423 wxFontEncodingArray arr
;
425 arr
= GetPlatformEquivalents(enc
); // we want them to be first items in array
428 while (EquivalentEncodings
[clas
][0][0] != STOP
)
430 for (i
= 0; i
< NUM_OF_PLATFORMS
; i
++)
431 for (e
= 0; EquivalentEncodings
[clas
][i
][e
] != STOP
; e
++)
432 if (EquivalentEncodings
[clas
][i
][e
] == enc
)
434 for (j
= 0; j
< NUM_OF_PLATFORMS
; j
++)
435 for (f
= EquivalentEncodings
[clas
][j
]; *f
!= STOP
; f
++)
436 if (arr
.Index(*f
) == wxNOT_FOUND
) arr
.Add(*f
);
437 i
= NUM_OF_PLATFORMS
/*hack*/; break;