- wxConfigBase *config = GetConfig();
-
- // do we have an encoding for this charset?
- long value = config->Read(charset, -1l);
- if ( value != -1 )
- {
- if ( value == wxFONTENCODING_UNKNOWN )
- {
- // don't try to find it, in particular don't ask the user
- return wxFONTENCODING_SYSTEM;
- }
-
- if ( value >= 0 && value <= wxFONTENCODING_MAX )
- {
- encoding = (wxFontEncoding)value;
- }
- else
- {
- wxLogDebug(wxT("corrupted config data: invalid encoding %ld for charset '%s' ignored"),
- value, charset.c_str());
- }
- }
-
- if ( encoding == wxFONTENCODING_SYSTEM )
- {
- // may be we have an alias?
- config->SetPath(FONTMAPPER_CHARSET_ALIAS_PATH);
-
- wxString alias = config->Read(charset);
- if ( !!alias )
- {
- // yes, we do - use it instead
- cs = alias;
- }
- }
-
- RestorePath(pathOld);
- }
-#endif // wxUSE_CONFIG
-
- // if didn't find it there, try to recognize it ourselves
- if ( encoding == wxFONTENCODING_SYSTEM )
- {
- // trim any spaces
- cs.Trim(TRUE);
- cs.Trim(FALSE);
-
- // discard the optional quotes
- if ( !cs.empty() )
- {
- if ( cs[0u] == _T('"') && cs.Last() == _T('"') )
- {
- cs = wxString(cs.c_str(), cs.length() - 1);
- }
- }
-
- cs.MakeUpper();
-
- if ( cs.empty() || cs == _T("US-ASCII") )
- {
- encoding = wxFONTENCODING_DEFAULT;
- }
- else if ( cs == wxT("UTF-7") )
- {
- encoding = wxFONTENCODING_UTF7;
- }
- else if ( cs == wxT("UTF-8") )
- {
- encoding = wxFONTENCODING_UTF8;
- }
- else if ( cs == wxT("GB2312") )
- {
- encoding = wxFONTENCODING_GB2312;
- }
- else if ( cs == wxT("BIG5") )
- {
- encoding = wxFONTENCODING_BIG5;
- }
- else if ( cs == wxT("SJIS") ||
- cs == wxT("SHIFT_JIS") ||
- cs == wxT("SHIFT-JIS") )
- {
- encoding = wxFONTENCODING_SHIFT_JIS;
- }
- else if ( cs == wxT("EUC-JP") ||
- cs == wxT("EUC_JP") )
- {
- encoding = wxFONTENCODING_EUC_JP;
- }
- else if ( cs == wxT("KOI8-R") ||
- cs == wxT("KOI8-U") ||
- cs == wxT("KOI8-RU") )
- {
- // although koi8-ru is not strictly speaking the same as koi8-r,
- // they are similar enough to make mapping it to koi8 better than
- // not reckognizing it at all
- encoding = wxFONTENCODING_KOI8;
- }
- else if ( cs.Left(3) == wxT("ISO") )
- {
- // the dash is optional (or, to be exact, it is not, but
- // several brokenmails "forget" it)
- const wxChar *p = cs.c_str() + 3;
- if ( *p == wxT('-') )
- p++;
-
- // printf( "iso %s\n", (const char*) cs.ToAscii() );
-
- unsigned int value;
- if ( wxSscanf(p, wxT("8859-%u"), &value) == 1 )
- {
- // printf( "value %d\n", (int)value );
-
- // make it 0 based and check that it is strictly positive in
- // the process (no such thing as iso8859-0 encoding)
- if ( (value-- > 0) &&
- (value < wxFONTENCODING_ISO8859_MAX -
- wxFONTENCODING_ISO8859_1) )
- {
- // it's a valid ISO8859 encoding
- value += wxFONTENCODING_ISO8859_1;
- encoding = (wxFontEncoding)value;
- }
- }
- }
- else if ( cs.Left(4) == wxT("8859") )
- {
- const wxChar *p = cs.c_str();
-
- unsigned int value;
- if ( wxSscanf(p, wxT("8859-%u"), &value) == 1 )
- {
- // printf( "value %d\n", (int)value );
-
- // make it 0 based and check that it is strictly positive in
- // the process (no such thing as iso8859-0 encoding)
- if ( (value-- > 0) &&
- (value < wxFONTENCODING_ISO8859_MAX -
- wxFONTENCODING_ISO8859_1) )
- {
- // it's a valid ISO8859 encoding
- value += wxFONTENCODING_ISO8859_1;
- encoding = (wxFontEncoding)value;
- }
- }
- }
- else // check for Windows charsets
- {
- size_t len;
- if ( cs.Left(7) == wxT("WINDOWS") )
- {
- len = 7;
- }
- else if ( cs.Left(2) == wxT("CP") )
- {
- len = 2;
- }
- else // not a Windows encoding
- {
- len = 0;
- }
-
- if ( len )
- {
- const wxChar *p = cs.c_str() + len;
- if ( *p == wxT('-') )
- p++;
-
- int value;
- if ( wxSscanf(p, wxT("%u"), &value) == 1 )
- {
- if ( value >= 1250 )
- {
- value -= 1250;
- if ( value < wxFONTENCODING_CP12_MAX -
- wxFONTENCODING_CP1250 )
- {
- // a valid Windows code page
- value += wxFONTENCODING_CP1250;
- encoding = (wxFontEncoding)value;
- }
- }
-
- switch ( value )
- {
- case 932:
- encoding = wxFONTENCODING_CP932;
- break;
-
- case 936:
- encoding = wxFONTENCODING_CP936;
- break;
-
- case 949:
- encoding = wxFONTENCODING_CP949;
- break;
-
- case 950:
- encoding = wxFONTENCODING_CP950;
- break;
- }
- }
- }
- }
- //else: unknown