📄 qtextcodec.cpp
字号:
inserted, or NULL if there is no such QTextCodec. Thus, codecForIndex(0) returns the most recently created QTextCodec.*/QTextCodec* QTextCodec::codecForIndex(int i){ setup(); return (uint)i >= all->count() ? 0 : all->at(i);}/*! Returns the QTextCodec which matches the \link QTextCodec::mibEnum() MIBenum\endlink \a mib.*/QTextCodec* QTextCodec::codecForMib(int mib){ setup(); QListIterator<QTextCodec> i(*all); QTextCodec* result; for ( ; (result=i); ++i ) { if ( result->mibEnum()==mib ) break; } return result;}#ifdef _OS_WIN32_class QWindowsLocalCodec: public QTextCodec{public: QWindowsLocalCodec(); ~QWindowsLocalCodec(); QString toUnicode(const char* chars, int len) const; QCString fromUnicode(const QString& uc, int& lenInOut ) const; const char* name() const; int mibEnum() const; int heuristicContentMatch(const char* chars, int len) const;};QWindowsLocalCodec::QWindowsLocalCodec(){}QWindowsLocalCodec::~QWindowsLocalCodec(){}QString QWindowsLocalCodec::toUnicode(const char* chars, int len) const{ if ( len == 1 && chars ) { // Optimization; avoids allocation char c[2]; c[0] = *chars; c[1] = 0; return qt_winMB2QString( c, 2 ); } if ( len < 0 ) return qt_winMB2QString( chars ); QCString s(chars,len+1); return qt_winMB2QString(s);}QCString QWindowsLocalCodec::fromUnicode(const QString& uc, int& lenInOut ) const{ QCString r = qt_winQString2MB( uc, lenInOut ); lenInOut = r.length(); return r;}const char* QWindowsLocalCodec::name() const{ return "System";}int QWindowsLocalCodec::mibEnum() const{ return 0;}int QWindowsLocalCodec::heuristicContentMatch(const char* chars, int len) const{ // ### Not a bad default implementation? QString t = toUnicode(chars,len); int l = t.length(); QCString mb = fromUnicode(t,l); int i=0; while ( i < len ) if ( chars[i] == mb[i] ) i++; return i;}#else/* locale names mostly copied from XFree86 */static const char * const iso8859_2locales[] = { "croatian", "cs", "cs_CS", "cs_CZ","cz", "cz_CZ", "czech", "hr", "hr_HR", "hu", "hu_HU", "hungarian", "pl", "pl_PL", "polish", "ro", "ro_RO", "rumanian", "serbocroatian", "sh", "sh_SP", "sh_YU", "sk", "sk_SK", "sl", "sl_CS", "sl_SI", "slovak", "slovene", "sr_SP", 0 };static const char * const iso8859_3locales[] = { "eo", 0 };static const char * const iso8859_4locales[] = { "ee", "ee_EE", "lt", "lt_LT", "lv", "lv_LV", 0 };static const char * const iso8859_5locales[] = { "bg", "bg_BG", "bulgarian", "mk", "mk_MK", "sp", "sp_YU", 0 };static const char * const iso8859_6locales[] = { "ar_AA", "ar_SA", "arabic", 0 };static const char * const iso8859_7locales[] = { "el", "el_GR", "greek", 0 };static const char * const iso8859_8locales[] = { "hebrew", "he", "he_IL", "iw", "iw_IL", 0 };static const char * const iso8859_9locales[] = { "tr", "tr_TR", "turkish", 0 };static const char * const iso8859_15locales[] = { "fr", "fi", "french", "finnish", "et", "et_EE", 0 };static const char * const koi8_ulocales[] = { "uk", "uk_UA", "ru_UA", "ukrainian", 0 };static const char * const tis_620locales[] = { "th", "th_TH", "thai", 0 };static bool try_locale_list( const char * const locale[], const char * lang ){ int i; for( i=0; locale[i] && qstrcmp(locale[i], lang); i++ ) { } return locale[i] != 0;}// For the probably_koi8_locales we have to look. the standard says// these are 8859-5, but almsot all Russion users uses KOI8-R and// incorrectly set $LANG to ru_RU. We'll check tolower() to see what// tolower() thinks ru_RU means.// If you read the history, it seems that many Russians blame ISO and// Peristroika for the confusion.//// The real bug is that some programs break if the user specifies// ru_RU.KOI8-R.static const char * const probably_koi8_rlocales[] = { "ru", "ru_SU", "ru_RU", "russian", 0 };// this means ANY of these locale aliases. if they're aliases for// different locales, the code breaks.static QTextCodec * ru_RU_codec = 0;static QTextCodec * ru_RU_hack( const char * i ) { if ( ! ru_RU_codec ) { QCString origlocale = setlocale( LC_CTYPE, i ); // unicode koi8r latin5 name // 0x044E 0xC0 0xEE CYRILLIC SMALL LETTER YU // 0x042E 0xE0 0xCE CYRILLIC CAPITAL LETTER YU int latin5 = tolower( 0xCE ); int koi8r = tolower( 0xE0 ); if ( koi8r == 0xC0 && latin5 != 0xEE ) { ru_RU_codec = QTextCodec::codecForName( "KOI8-R" ); } else if ( koi8r != 0xC0 && latin5 == 0xEE ) { ru_RU_codec = QTextCodec::codecForName( "ISO 8859-5" ); } else { // something else again... let's assume... *throws dice* ru_RU_codec = QTextCodec::codecForName( "KOI8-R" ); qWarning( "QTextCodec: using KOI8-R, probe failed (%02x %02x %s)", koi8r, latin5, i ); } setlocale( LC_CTYPE, origlocale.data() ); } return ru_RU_codec;}#endifstatic QTextCodec * localeMapper = 0;void qt_set_locale_codec( QTextCodec *codec ){ localeMapper = codec;}/*! Returns a pointer to the codec most suitable for this locale. */QTextCodec* QTextCodec::codecForLocale(){ if ( localeMapper ) return localeMapper; setup();#ifdef _OS_WIN32_ localeMapper = new QWindowsLocalCodec;#else // Very poorly defined and followed standards causes lots of code // to try to get all the cases... char * lang = qstrdup( getenv("LANG") ); char * p = lang ? strchr( lang, '.' ) : 0; if ( !p || *p != '.' ) { // Some versions of setlocale return encoding, others not. char *ctype = qstrdup( setlocale( LC_CTYPE, 0 ) ); // Some Linux distributions have broken locales which will return // "C" for LC_CTYPE if ( qstrcmp( ctype, "C" ) == 0 ) { delete [] ctype; } else { if ( lang ) delete [] lang; lang = ctype; p = lang ? strchr( lang, '.' ) : 0; } } if( p && *p == '.' ) { // if there is an encoding and we don't know it, we return 0 // User knows what they are doing. Codecs will believe them. localeMapper = codecForName( lang ); if ( !localeMapper ) { // Use or codec disagree. localeMapper = codecForName( p+1 ); } } if ( !localeMapper || !(p && *p == '.') ) { // if there is none, we default to 8859-1 // We could perhaps default to 8859-15. if ( try_locale_list( iso8859_2locales, lang ) ) localeMapper = codecForName( "ISO 8859-2" ); else if ( try_locale_list( iso8859_3locales, lang ) ) localeMapper = codecForName( "ISO 8859-3" ); else if ( try_locale_list( iso8859_4locales, lang ) ) localeMapper = codecForName( "ISO 8859-4" ); else if ( try_locale_list( iso8859_5locales, lang ) ) localeMapper = codecForName( "ISO 8859-5" ); else if ( try_locale_list( iso8859_6locales, lang ) ) localeMapper = codecForName( "ISO 8859-6-I" ); else if ( try_locale_list( iso8859_7locales, lang ) ) localeMapper = codecForName( "ISO 8859-7" ); else if ( try_locale_list( iso8859_8locales, lang ) ) localeMapper = codecForName( "ISO 8859-8-I" ); else if ( try_locale_list( iso8859_9locales, lang ) ) localeMapper = codecForName( "ISO 8859-9" ); else if ( try_locale_list( iso8859_15locales, lang ) ) localeMapper = codecForName( "ISO 8859-15" ); else if ( try_locale_list( tis_620locales, lang ) ) localeMapper = codecForName( "ISO 8859-11" ); else if ( try_locale_list( koi8_ulocales, lang ) ) localeMapper = codecForName( "KOI8-U" ); else if ( try_locale_list( probably_koi8_rlocales, lang ) ) localeMapper = ru_RU_hack( lang ); else if (!lang || !(localeMapper = codecForName(lang) )) localeMapper = codecForName( "ISO 8859-1" ); } delete[] lang;#endif return localeMapper;}/*! Searches all installed QTextCodec objects, returning the one which best matches given name. Returns NULL if no codec has a match closeness above \a accuracy. \sa heuristicNameMatch()*/QTextCodec* QTextCodec::codecForName(const char* hint, int accuracy){ setup(); QListIterator<QTextCodec> i(*all); QTextCodec* result = 0; int best=accuracy; for ( QTextCodec* cursor; (cursor=i); ++i ) { int s = cursor->heuristicNameMatch(hint); if ( s > best ) { best = s; result = cursor; } } return result;}/*! Searches all installed QTextCodec objects, returning the one which most recognizes the given content. May return 0. Note that this is often a poor choice, since character encodings often use most of the available character sequences, and so only by linguistic analysis could a true match be made. \sa heuristicContentMatch()*/QTextCodec* QTextCodec::codecForContent(const char* chars, int len){ setup(); QListIterator<QTextCodec> i(*all); QTextCodec* result = 0; int best=0; for ( QTextCodec* cursor; (cursor=i); ++i ) { int s = cursor->heuristicContentMatch(chars,len); if ( s > best ) { best = s; result = cursor; } } return result;}/*! \fn const char* QTextCodec::name() const Subclasses of QTextCodec must reimplement this function. It returns the name of the encoding supported by the subclass. When choosing a name for an encoding, consider these points: <ul> <li>On X11, heuristicNameMatch( const char * hint ) is used to test if a the QTextCodec can convert between Unicode and the encoding of a font
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -