Changeset 561 for trunk/src/corelib/codecs/qtextcodec.cpp
- Timestamp:
- Feb 11, 2010, 11:19:06 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property svn:mergeinfo
set to (toggle deleted branches)
/branches/vendor/nokia/qt/4.6.1 merged eligible /branches/vendor/nokia/qt/current merged eligible /branches/vendor/trolltech/qt/current 3-149
-
Property svn:mergeinfo
set to (toggle deleted branches)
-
trunk/src/corelib/codecs/qtextcodec.cpp
r359 r561 2 2 ** 3 3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). 4 ** Contact: Qt Software Information ([email protected]) 4 ** All rights reserved. 5 ** Contact: Nokia Corporation ([email protected]) 5 6 ** 6 7 ** This file is part of the QtCore module of the Qt Toolkit. … … 21 22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 22 23 ** 23 ** In addition, as a special exception, Nokia gives you certain 24 ** additional rights. These rights are described in the Nokia Qt LGPL 25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this 26 ** package. 24 ** In addition, as a special exception, Nokia gives you certain additional 25 ** rights. These rights are described in the Nokia Qt LGPL Exception 26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. 27 27 ** 28 28 ** GNU General Public License Usage … … 34 34 ** met: http://www.gnu.org/copyleft/gpl.html. 35 35 ** 36 ** If you are unsure which license is appropriate for your use, please37 ** contact the sales department at qt-sales@nokia.com.36 ** If you 37 ** @nokia.com. 38 38 ** $QT_END_LICENSE$ 39 39 ** … … 91 91 #include <ctype.h> 92 92 #include <locale.h> 93 #if defined (_XOPEN_UNIX) && !defined(Q_OS_QNX 6) && !defined(Q_OS_OSF)93 #if defined (_XOPEN_UNIX) && !defined(Q_OS_QNX) && !defined(Q_OS_OSF) 94 94 #include <langinfo.h> 95 95 #endif … … 98 98 # define QT_NO_SETLOCALE 99 99 #endif 100 101 102 100 103 101 104 QT_BEGIN_NAMESPACE … … 106 109 #endif 107 110 111 112 113 114 108 115 109 116 static bool nameMatch(const QByteArray &name, const QByteArray &test) … … 118 125 // if the letters and numbers are the same, we have a match 119 126 while (*n != '\0') { 120 if ( isalnum((uchar)*n)) {127 if (*n)) { 121 128 for (;;) { 122 129 if (*h == '\0') 123 130 return false; 124 if ( isalnum((uchar)*h))131 if (*h)) 125 132 break; 126 133 ++h; 127 134 } 128 if ( tolower((uchar)*n) != tolower((uchar)*h))135 if (*h)) 129 136 return false; 130 137 ++h; … … 132 139 ++n; 133 140 } 134 while (*h && ! isalnum((uchar)*h))141 while (*h && !*h)) 135 142 ++h; 136 143 return (*h == '\0'); … … 172 179 173 180 static QList<QTextCodec*> *all = 0; 181 174 182 static bool destroying_is_ok = false; 183 175 184 176 185 static QTextCodec *localeMapper = 0; … … 194 203 return; 195 204 205 196 206 destroying_is_ok = true; 197 198 while (all->size()) 199 delete all->takeFirst(); 207 #endif 208 209 for (QList<QTextCodec *>::const_iterator it = all->constBegin() 210 ; it != all->constEnd(); ++it) { 211 delete *it; 212 } 200 213 delete all; 201 214 all = 0; 202 215 localeMapper = 0; 203 216 217 204 218 destroying_is_ok = false; 219 205 220 } 206 221 … … 240 255 241 256 const int wclen_auto = 4096; 242 WCHARwc_auto[wclen_auto];257 wc_auto[wclen_auto]; 243 258 int wclen = wclen_auto; 244 WCHAR*wc = wc_auto;259 *wc = wc_auto; 245 260 int len; 246 261 QString sp; … … 282 297 wclen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, 283 298 mb, mblen, 0, 0); 284 wc = new WCHAR[wclen];299 wc = new [wclen]; 285 300 // and try again... 286 301 } … … 348 363 QString s; 349 364 while((next = CharNextExA(CP_ACP, mb, 0)) != mb) { 350 WCHARwc[2] ={0};365 wc[2] ={0}; 351 366 int charlength = next - mb; 352 367 int len = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED|MB_ERR_INVALID_CHARS, mb, charlength, wc, 2); … … 687 702 // "vi", "vi_VN", 0 }; 688 703 689 static bool try_locale_list(const char * const locale[], const char *lang)704 static bool try_locale_list(const char * const locale[], const lang) 690 705 { 691 706 int i; 692 for(i=0; locale[i] && *locale[i] && strcmp(locale[i], lang); i++)707 for(i=0; locale[i] && ; i++) 693 708 ; 694 709 return locale[i] != 0; … … 748 763 749 764 #if !defined(Q_OS_WIN32) && !defined(Q_OS_WINCE) && !defined(Q_OS_OS2) 750 static QTextCodec *checkForCodec(const char *name) {765 static QTextCodec *checkForCodec(const name) { 751 766 QTextCodec *c = QTextCodec::codecForName(name); 752 767 if (!c) { 753 const char *at = strchr(name, '@'); 754 if (at) { 755 QByteArray n(name, at - name); 756 c = QTextCodec::codecForName(n.data()); 768 const int index = name.indexOf('@'); 769 if (index != -1) { 770 c = QTextCodec::codecForName(name.left(index)); 757 771 } 758 772 } … … 776 790 #endif 777 791 778 #if defined (_XOPEN_UNIX) && !defined(Q_OS_QNX 6) && !defined(Q_OS_OSF)792 #if defined (_XOPEN_UNIX) && !defined(Q_OS_QNX) && !defined(Q_OS_OSF) 779 793 if (!localeMapper) { 780 794 char *charset = nl_langinfo (CODESET); … … 797 811 // to fall back to environment variables. 798 812 #if !defined(QT_NO_SETLOCALE) 799 c har * ctype = qstrdup(setlocale(LC_CTYPE, 0));813 c); 800 814 #else 801 c har * ctype = qstrdup("");815 c; 802 816 #endif 803 817 804 818 // Get the first nonempty value from $LC_ALL, $LC_CTYPE, and $LANG 805 819 // environment variables. 806 char * lang = qstrdup(qgetenv("LC_ALL").constData()); 807 if (!lang || lang[0] == 0 || strcmp(lang, "C") == 0) { 808 if (lang) delete [] lang; 809 lang = qstrdup(qgetenv("LC_CTYPE").constData()); 820 QByteArray lang = qgetenv("LC_ALL"); 821 if (lang.isEmpty() || lang == "C") { 822 lang = qgetenv("LC_CTYPE"); 810 823 } 811 if (!lang || lang[0] == 0 || strcmp(lang, "C") == 0) { 812 if (lang) delete [] lang; 813 lang = qstrdup(qgetenv("LANG").constData()); 824 if (lang.isEmpty() || lang == "C") { 825 lang = qgetenv("LANG"); 814 826 } 815 827 … … 824 836 825 837 // 1. CODESET from ctype if it contains a .CODESET part (e.g. en_US.ISO8859-15) 826 char * codeset = ctype ? strchr(ctype, '.') : 0;827 if ( codeset && *codeset == '.')828 localeMapper = checkForCodec( codeset + 1);838 ; 839 if () 840 localeMapper = checkForCodec(); 829 841 830 842 // 2. CODESET from lang if it contains a .CODESET part 831 codeset = lang ? strchr(lang, '.') : 0; 832 if (!localeMapper && codeset && *codeset == '.') 833 localeMapper = checkForCodec(codeset + 1); 843 if (!localeMapper) { 844 indexOfDot = lang.indexOf('.'); 845 if (indexOfDot != -1) 846 localeMapper = checkForCodec( lang.mid(indexOfDot + 1) ); 847 } 834 848 835 849 // 3. ctype (maybe the locale is named "ISO-8859-1" or something) 836 if (!localeMapper && ctype && *ctype != 0 && strcmp (ctype, "C") != 0)850 if (!localeMapper && ) 837 851 localeMapper = checkForCodec(ctype); 838 852 839 853 // 4. locale (ditto) 840 if (!localeMapper && lang && *lang != 0)854 if (!localeMapper && ) 841 855 localeMapper = checkForCodec(lang); 842 856 843 857 // 5. "@euro" 844 if ((!localeMapper && ctype && strstr(ctype, "@euro")) || (lang && strstr(lang, "@euro")))858 if ((!localeMapper && ctype)) 845 859 localeMapper = checkForCodec("ISO 8859-15"); 846 860 847 861 // 6. guess locale from ctype unless ctype is "C" 848 862 // 7. guess locale from lang 849 char * try_by_name = ctype; 850 if (ctype && *ctype != 0 && strcmp (ctype, "C") != 0) 851 try_by_name = lang; 863 const QByteArray &try_by_name = (!ctype.isEmpty() && ctype != "C") ? lang : ctype; 852 864 853 865 // Now do the guessing. 854 if ( lang && *lang && !localeMapper && try_by_name && *try_by_name) {866 if () { 855 867 if (try_locale_list(iso8859_15locales, lang)) 856 868 localeMapper = QTextCodec::codecForName("ISO 8859-15"); … … 885 897 } 886 898 887 delete [] ctype;888 delete [] lang;889 899 } 890 900 … … 906 916 return; 907 917 918 908 919 if (destroying_is_ok) 909 920 qWarning("QTextCodec: Creating new codec during codec cleanup"); 921 910 922 all = new QList<QTextCodec*>; 911 923 // create the cleanup object to cleanup all codecs on exit … … 944 956 (void)new QSjisCodec; 945 957 (void)new QEucKrCodec; 958 946 959 (void)new QBig5Codec; 947 960 (void)new QBig5hkscsCodec; … … 979 992 } 980 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 981 1014 QTextCodec::ConverterState::~ConverterState() 982 1015 { … … 1131 1164 1132 1165 /*! 1133 \enum QTextCodec::ConversionFlag1134 1135 \value DefaultConversion No flag is set.1136 \value ConvertInvalidToNull If this flag is set, each invalid input1137 character is output as a null character.1138 \value IgnoreHeader Ignore any Unicode byte-order mark and don't generate any.1139 1140 \omitvalue FreeFunction1141 */1142 1143 /*!1144 \fn QTextCodec::ConverterState::ConverterState(ConversionFlags flags)1145 1146 Constructs a ConverterState object initialized with the given \a flags.1147 */1148 1149 /*!1150 \fn QTextCodec::ConverterState::~ConverterState()1151 1152 Destroys the ConverterState object.1153 */1154 1155 /*!1156 1166 \nonreentrant 1157 1167 … … 1176 1186 QTextCodec::~QTextCodec() 1177 1187 { 1188 1178 1189 if (!destroying_is_ok) 1179 1190 qWarning("QTextCodec::~QTextCodec: Called by application"); 1191 1180 1192 if (all) 1181 1193 all->removeAll(this); … … 1308 1320 own mechanism for setting the locale. 1309 1321 1310 Setting this codec is not supported on DOS based Windows.1311 1312 1322 \sa codecForLocale() 1313 1323 */ 1314 1324 void QTextCodec::setCodecForLocale(QTextCodec *c) 1315 1325 { 1316 #ifdef Q_WS_WIN1317 if (QSysInfo::WindowsVersion& QSysInfo::WV_DOS_based)1318 return;1319 #endif1320 1326 codecForLocaleSet = true; 1321 1327 localeMapper = c; … … 1770 1776 \since 4.4 1771 1777 1772 Tries to detect the encoding of the provided snippet of HTML in the given byte array, \a ba, 1773 and returns a QTextCodec instance that is capable of decoding the html to unicode. 1774 If the codec cannot be detected from the content provided, \a defaultCodec is returned. 1778 Tries to detect the encoding of the provided snippet of HTML in 1779 the given byte array, \a ba, by checking the BOM (Byte Order Mark) 1780 and the content-type meta header and returns a QTextCodec instance 1781 that is capable of decoding the html to unicode. If the codec 1782 cannot be detected from the content provided, \a defaultCodec is 1783 returned. 1784 1785 \sa codecForUtfText() 1775 1786 */ 1776 1787 QTextCodec *QTextCodec::codecForHtml(const QByteArray &ba, QTextCodec *defaultCodec) … … 1780 1791 QTextCodec *c = 0; 1781 1792 1782 if (ba.size() > 1 && (((uchar)ba[0] == 0xfe && (uchar)ba[1] == 0xff) 1783 || ((uchar)ba[0] == 0xff && (uchar)ba[1] == 0xfe))) { 1784 c = QTextCodec::codecForMib(1015); // utf16 1785 } else if (ba.size() > 2 1786 && (uchar)ba[0] == 0xef 1787 && (uchar)ba[1] == 0xbb 1788 && (uchar)ba[2] == 0xbf) { 1789 c = QTextCodec::codecForMib(106); // utf-8 1790 } else { 1793 c = QTextCodec::codecForUtfText(ba, c); 1794 if (!c) { 1791 1795 QByteArray header = ba.left(512).toLower(); 1792 1796 if ((pos = header.indexOf("http-equiv=")) != -1) { 1793 pos = header.indexOf("charset=", pos) + int(strlen("charset=")); 1794 if (pos != -1) { 1795 int pos2 = header.indexOf('\"', pos+1); 1796 QByteArray cs = header.mid(pos, pos2-pos); 1797 // qDebug("found charset: %s", cs.data()); 1798 c = QTextCodec::codecForName(cs); 1797 if ((pos = header.lastIndexOf("meta ", pos)) != -1) { 1798 pos = header.indexOf("charset=", pos) + int(strlen("charset=")); 1799 if (pos != -1) { 1800 int pos2 = header.indexOf('\"', pos+1); 1801 QByteArray cs = header.mid(pos, pos2-pos); 1802 // qDebug("found charset: %s", cs.data()); 1803 c = QTextCodec::codecForName(cs); 1804 } 1799 1805 } 1800 1806 } … … 1807 1813 1808 1814 /*! 1809 \overload 1810 1811 If the codec cannot be detected, this overload returns a Latin-1 QTextCodec. 1815 \overload 1816 1817 Tries to detect the encoding of the provided snippet of HTML in 1818 the given byte array, \a ba, by checking the BOM (Byte Order Mark) 1819 and the content-type meta header and returns a QTextCodec instance 1820 that is capable of decoding the html to unicode. If the codec cannot 1821 be detected, this overload returns a Latin-1 QTextCodec. 1812 1822 */ 1813 1823 QTextCodec *QTextCodec::codecForHtml(const QByteArray &ba) 1814 1824 { 1815 1825 return codecForHtml(ba, QTextCodec::codecForMib(/*Latin 1*/ 4)); 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1816 1886 } 1817 1887
Note:
See TracChangeset
for help on using the changeset viewer.