OK, fixing wxEncodingConverter was a little bit harder than I expected

because of some dodgy overloading implementation in here, but this ought
to do it, I think... (or is it hope?)

I wonder if my tree still keeps on having unicode enabled even when I
reconfigure with --disable-unicode, and that's why I didn't detect the
wxEncodingConverter problem myself? Wonder if that is still the case?


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_2_BRANCH@6962 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Ove Kaaven
2000-03-28 00:34:38 +00:00
parent 387cdaf2f4
commit 8a2d60784d
2 changed files with 106 additions and 114 deletions

View File

@@ -52,7 +52,7 @@ class WXDLLEXPORT wxEncodingConverter : public wxObject
~wxEncodingConverter() { if (m_Table) delete[] m_Table; } ~wxEncodingConverter() { if (m_Table) delete[] m_Table; }
// Initialize convertion. Both output or input encoding may // Initialize convertion. Both output or input encoding may
// be wxFONTENCODING_UNICODE, but only if wxUSE_UNICODE is set to 1. // be wxFONTENCODING_UNICODE, but only if wxUSE_WCHAR_T is set to 1.
// //
// All subsequent calls to Convert() will interpret it's argument // All subsequent calls to Convert() will interpret it's argument
// as a string in input_enc encoding and will output string in // as a string in input_enc encoding and will output string in
@@ -82,15 +82,15 @@ class WXDLLEXPORT wxEncodingConverter : public wxObject
// Convert input string according to settings passed to Init. // Convert input string according to settings passed to Init.
// Note that you must call Init before using Convert! // Note that you must call Init before using Convert!
void Convert(const wxChar* input, wxChar* output);
void Convert(wxChar* str) { Convert(str, str); }
wxString Convert(const wxString& input);
#if wxUSE_UNICODE // otherwise wxChar === char
void Convert(const char* input, wxChar* output);
void Convert(const wxChar* input, char* output);
void Convert(const char* input, char* output); void Convert(const char* input, char* output);
void Convert(char* str) { Convert(str, str); } void Convert(char* str) { Convert(str, str); }
wxString Convert(const wxString& input);
#if wxUSE_WCHAR_T
void Convert(const char* input, wchar_t* output);
void Convert(const wchar_t* input, char* output);
void Convert(const wchar_t* input, wchar_t* output);
void Convert(wchar_t* str) { Convert(str, str); }
#endif #endif
// Return equivalent(s) for given font that are used // Return equivalent(s) for given font that are used
// under given platform. wxPLATFORM_CURRENT means the plaform // under given platform. wxPLATFORM_CURRENT means the plaform
@@ -129,7 +129,11 @@ class WXDLLEXPORT wxEncodingConverter : public wxObject
private: private:
wxChar *m_Table; #if wxUSE_WCHAR_T
wchar_t *m_Table;
#else
char *m_Table;
#endif
bool m_UnicodeInput, m_UnicodeOutput; bool m_UnicodeInput, m_UnicodeOutput;
bool m_JustCopy; bool m_JustCopy;
@@ -137,20 +141,3 @@ class WXDLLEXPORT wxEncodingConverter : public wxObject
#endif // __ENCCONV_H__ #endif // __ENCCONV_H__

View File

@@ -29,6 +29,11 @@
#include "unictabl.inc" #include "unictabl.inc"
#endif #endif
#if wxUSE_WCHAR_T
typedef wchar_t tchar;
#else
typedef char tchar;
#endif
static wxUint16* LINKAGEMODE GetEncTable(wxFontEncoding enc) static wxUint16* LINKAGEMODE GetEncTable(wxFontEncoding enc)
{ {
@@ -83,7 +88,7 @@ bool wxEncodingConverter::Init(wxFontEncoding input_enc, wxFontEncoding output_e
if (m_Table) {delete[] m_Table; m_Table = NULL;} if (m_Table) {delete[] m_Table; m_Table = NULL;}
#if !wxUSE_UNICODE #if !wxUSE_WCHAR_T
if (input_enc == wxFONTENCODING_UNICODE || output_enc == wxFONTENCODING_UNICODE) return FALSE; if (input_enc == wxFONTENCODING_UNICODE || output_enc == wxFONTENCODING_UNICODE) return FALSE;
#endif #endif
@@ -96,19 +101,19 @@ bool wxEncodingConverter::Init(wxFontEncoding input_enc, wxFontEncoding output_e
{ {
if ((out_tbl = GetEncTable(output_enc)) == NULL) return FALSE; if ((out_tbl = GetEncTable(output_enc)) == NULL) return FALSE;
m_Table = new wxChar[65536]; m_Table = new tchar[65536];
for (i = 0; i < 128; i++) m_Table[i] = (wxChar)i; // 7bit ASCII for (i = 0; i < 128; i++) m_Table[i] = (tchar)i; // 7bit ASCII
for (i = 128; i < 65536; i++) m_Table[i] = (wxChar)'?'; for (i = 128; i < 65536; i++) m_Table[i] = (tchar)'?';
// FIXME - this should be character that means `unicode to charset' impossible, not '?' // FIXME - this should be character that means `unicode to charset' impossible, not '?'
if (method == wxCONVERT_SUBSTITUTE) if (method == wxCONVERT_SUBSTITUTE)
{ {
for (i = 0; i < encoding_unicode_fallback_count; i++) for (i = 0; i < encoding_unicode_fallback_count; i++)
m_Table[encoding_unicode_fallback[i].c] = (wxChar) encoding_unicode_fallback[i].s; m_Table[encoding_unicode_fallback[i].c] = (tchar) encoding_unicode_fallback[i].s;
} }
for (i = 0; i < 128; i++) for (i = 0; i < 128; i++)
m_Table[out_tbl[i]] = (wxChar)(128 + i); m_Table[out_tbl[i]] = (tchar)(128 + i);
m_UnicodeInput = TRUE; m_UnicodeInput = TRUE;
return TRUE; return TRUE;
@@ -122,12 +127,12 @@ bool wxEncodingConverter::Init(wxFontEncoding input_enc, wxFontEncoding output_e
m_UnicodeInput = FALSE; m_UnicodeInput = FALSE;
m_Table = new wxChar[256]; m_Table = new tchar[256];
for (i = 0; i < 128; i++) m_Table[i] = (wxChar)i; // 7bit ASCII for (i = 0; i < 128; i++) m_Table[i] = (tchar)i; // 7bit ASCII
if (output_enc == wxFONTENCODING_UNICODE) if (output_enc == wxFONTENCODING_UNICODE)
{ {
for (i = 0; i < 128; i++) m_Table[128 + i] = (wxChar)in_tbl[i]; // wxChar is 2byte now for (i = 0; i < 128; i++) m_Table[128 + i] = (tchar)in_tbl[i];
return TRUE; return TRUE;
} }
else else
@@ -143,7 +148,7 @@ bool wxEncodingConverter::Init(wxFontEncoding input_enc, wxFontEncoding output_e
item = (CharsetItem*) bsearch(&key, encoding_unicode_fallback, item = (CharsetItem*) bsearch(&key, encoding_unicode_fallback,
encoding_unicode_fallback_count, sizeof(CharsetItem), CompareCharsetItems); encoding_unicode_fallback_count, sizeof(CharsetItem), CompareCharsetItems);
if (item) if (item)
m_Table[128 + i] = (wxChar)item -> c; m_Table[128 + i] = (tchar)item -> c;
else else
m_Table[128 + i] = 128 + i; // don't know => don't touch m_Table[128 + i] = 128 + i; // don't know => don't touch
} }
@@ -156,83 +161,6 @@ bool wxEncodingConverter::Init(wxFontEncoding input_enc, wxFontEncoding output_e
void wxEncodingConverter::Convert(const wxChar* input, wxChar* output)
{
if (m_JustCopy)
{
wxStrcpy(output, input);
return;
}
wxASSERT_MSG(m_Table != NULL, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
const wxChar *i;
wxChar *o;
if (m_UnicodeInput)
for (i = input, o = output; *i != 0; i++, o++)
*o = (wxChar)(m_Table[(wxUint16)*i]);
else
for (i = input, o = output; *i != 0; i++, o++)
*o = (wxChar)(m_Table[(wxUint8)*i]);
*o = 0;
}
#if wxUSE_UNICODE // otherwise wxChar === char
void wxEncodingConverter::Convert(const char* input, wxChar* output)
{
wxASSERT_MSG(!m_UnicodeInput, wxT("You cannot convert from unicode if input is const char*!"));
const char *i;
wxChar *o;
if (m_JustCopy)
{
for (i = input, o = output; *i != 0;)
*(o++) = (wxChar)(*(i++));
*o = 0;
return;
}
wxASSERT_MSG(m_Table != NULL, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
for (i = input, o = output; *i != 0;)
*(o++) = (wxChar)(m_Table[(wxUint8)*(i++)]);
*o = 0;
}
void wxEncodingConverter::Convert(const wxChar* input, char* output)
{
wxASSERT_MSG(!m_UnicodeOutput, wxT("You cannot convert to unicode if output is const char*!"));
const wxChar *i;
char *o;
if (m_JustCopy)
{
for (i = input, o = output; *i != 0;)
*(o++) = (char)(*(i++));
*o = 0;
return;
}
wxASSERT_MSG(m_Table != NULL, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
if (m_UnicodeInput)
for (i = input, o = output; *i != 0; i++, o++)
*o = (char)(m_Table[(wxUint16)*i]);
else
for (i = input, o = output; *i != 0; i++, o++)
*o = (char)(m_Table[(wxUint8)*i]);
*o = 0;
}
void wxEncodingConverter::Convert(const char* input, char* output) void wxEncodingConverter::Convert(const char* input, char* output)
{ {
wxASSERT_MSG(!m_UnicodeOutput, wxT("You cannot convert to unicode if output is const char*!")); wxASSERT_MSG(!m_UnicodeOutput, wxT("You cannot convert to unicode if output is const char*!"));
@@ -254,7 +182,84 @@ void wxEncodingConverter::Convert(const char* input, char* output)
*o = 0; *o = 0;
} }
#endif // wxUSE_UNICODE
#if wxUSE_WCHAR_T
void wxEncodingConverter::Convert(const char* input, wchar_t* output)
{
wxASSERT_MSG(m_UnicodeOutput, wxT("You cannot convert to 8-bit if output is const wchar_t*!"));
wxASSERT_MSG(!m_UnicodeInput, wxT("You cannot convert from unicode if input is const char*!"));
const char *i;
wchar_t *o;
if (m_JustCopy)
{
for (i = input, o = output; *i != 0;)
*(o++) = (wchar_t)(*(i++));
*o = 0;
return;
}
wxASSERT_MSG(m_Table != NULL, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
for (i = input, o = output; *i != 0;)
*(o++) = (wchar_t)(m_Table[(wxUint8)*(i++)]);
*o = 0;
}
void wxEncodingConverter::Convert(const wchar_t* input, char* output)
{
wxASSERT_MSG(!m_UnicodeOutput, wxT("You cannot convert to unicode if output is const char*!"));
wxASSERT_MSG(m_UnicodeInput, wxT("You cannot convert from 8-bit if input is const wchar_t*!"));
const wchar_t *i;
char *o;
if (m_JustCopy)
{
for (i = input, o = output; *i != 0;)
*(o++) = (char)(*(i++));
*o = 0;
return;
}
wxASSERT_MSG(m_Table != NULL, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
for (i = input, o = output; *i != 0;)
*(o++) = (char)(m_Table[(wxUint16)*(i++)]);
*o = 0;
}
void wxEncodingConverter::Convert(const wchar_t* input, wchar_t* output)
{
wxASSERT_MSG(m_UnicodeOutput, wxT("You cannot convert to 8-bit if output is const wchar_t*!"));
wxASSERT_MSG(m_UnicodeInput, wxT("You cannot convert from 8-bit if input is const wchar_t*!"));
const wchar_t *i;
wchar_t *o;
if (m_JustCopy)
{
// wcscpy() is not guaranteed to exist
for (i = input, o = output; *i != 0;)
*(o++) = (*(i++));
*o = 0;
return;
}
wxASSERT_MSG(m_Table != NULL, wxT("You must call wxEncodingConverter::Init() before actually converting!"));
for (i = input, o = output; *i != 0;)
*(o++) = (wchar_t)(m_Table[(wxUint8)*(i++)]);
*o = 0;
}
#endif // wxUSE_WCHAR_T
wxString wxEncodingConverter::Convert(const wxString& input) wxString wxEncodingConverter::Convert(const wxString& input)