Ignore:
Timestamp:
Feb 11, 2010, 11:19:06 PM (15 years ago)
Author:
Dmitry A. Kuminov
Message:

trunk: Merged in qt 4.6.1 sources.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/corelib/codecs/qtextcodec.cpp

    r359 r561  
    22**
    33** 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])
    56**
    67** This file is part of the QtCore module of the Qt Toolkit.
     
    2122** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    2223**
    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.
    2727**
    2828** GNU General Public License Usage
     
    3434** met: http://www.gnu.org/copyleft/gpl.html.
    3535**
    36 ** If you are unsure which license is appropriate for your use, please
    37 ** contact the sales department at qt-sales@nokia.com.
     36** If you
     37** @nokia.com.
    3838** $QT_END_LICENSE$
    3939**
     
    9191#include <ctype.h>
    9292#include <locale.h>
    93 #if defined (_XOPEN_UNIX) && !defined(Q_OS_QNX6) && !defined(Q_OS_OSF)
     93#if defined (_XOPEN_UNIX) && !defined(Q_OS_QNX) && !defined(Q_OS_OSF)
    9494#include <langinfo.h>
    9595#endif
     
    9898#  define QT_NO_SETLOCALE
    9999#endif
     100
     101
     102
    100103
    101104QT_BEGIN_NAMESPACE
     
    106109#endif
    107110
     111
     112
     113
     114
    108115
    109116static bool nameMatch(const QByteArray &name, const QByteArray &test)
     
    118125    // if the letters and numbers are the same, we have a match
    119126    while (*n != '\0') {
    120         if (isalnum((uchar)*n)) {
     127        if (*n)) {
    121128            for (;;) {
    122129                if (*h == '\0')
    123130                    return false;
    124                 if (isalnum((uchar)*h))
     131                if (*h))
    125132                    break;
    126133                ++h;
    127134            }
    128             if (tolower((uchar)*n) != tolower((uchar)*h))
     135            if (*h))
    129136                return false;
    130137            ++h;
     
    132139        ++n;
    133140    }
    134     while (*h && !isalnum((uchar)*h))
     141    while (*h && !*h))
    135142           ++h;
    136143    return (*h == '\0');
     
    172179
    173180static QList<QTextCodec*> *all = 0;
     181
    174182static bool destroying_is_ok = false;
     183
    175184
    176185static QTextCodec *localeMapper = 0;
     
    194203        return;
    195204
     205
    196206    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    }
    200213    delete all;
    201214    all = 0;
    202215    localeMapper = 0;
    203216
     217
    204218    destroying_is_ok = false;
     219
    205220}
    206221
     
    240255
    241256    const int wclen_auto = 4096;
    242     WCHAR wc_auto[wclen_auto];
     257    wc_auto[wclen_auto];
    243258    int wclen = wclen_auto;
    244     WCHAR *wc = wc_auto;
     259    *wc = wc_auto;
    245260    int len;
    246261    QString sp;
     
    282297                wclen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,
    283298                                    mb, mblen, 0, 0);
    284                 wc = new WCHAR[wclen];
     299                wc = new [wclen];
    285300                // and try again...
    286301            }
     
    348363    QString s;
    349364    while((next = CharNextExA(CP_ACP, mb, 0)) != mb) {
    350         WCHAR wc[2] ={0};
     365        wc[2] ={0};
    351366        int charlength = next - mb;
    352367        int len = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED|MB_ERR_INVALID_CHARS, mb, charlength, wc, 2);
     
    687702//     "vi", "vi_VN", 0 };
    688703
    689 static bool try_locale_list(const char * const locale[], const char * lang)
     704static bool try_locale_list(const char * const locale[], const lang)
    690705{
    691706    int i;
    692     for(i=0; locale[i] && *locale[i] && strcmp(locale[i], lang); i++)
     707    for(i=0; locale[i] && ; i++)
    693708        ;
    694709    return locale[i] != 0;
     
    748763
    749764#if !defined(Q_OS_WIN32) && !defined(Q_OS_WINCE) && !defined(Q_OS_OS2)
    750 static QTextCodec *checkForCodec(const char *name) {
     765static QTextCodec *checkForCodec(const name) {
    751766    QTextCodec *c = QTextCodec::codecForName(name);
    752767    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));
    757771        }
    758772    }
     
    776790#endif
    777791
    778 #if defined (_XOPEN_UNIX) && !defined(Q_OS_QNX6) && !defined(Q_OS_OSF)
     792#if defined (_XOPEN_UNIX) && !defined(Q_OS_QNX) && !defined(Q_OS_OSF)
    779793    if (!localeMapper) {
    780794        char *charset = nl_langinfo (CODESET);
     
    797811        // to fall back to environment variables.
    798812#if !defined(QT_NO_SETLOCALE)
    799         char * ctype = qstrdup(setlocale(LC_CTYPE, 0));
     813        c);
    800814#else
    801         char * ctype = qstrdup("");
     815        c;
    802816#endif
    803817
    804818        // Get the first nonempty value from $LC_ALL, $LC_CTYPE, and $LANG
    805819        // 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");
    810823        }
    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");
    814826        }
    815827
     
    824836
    825837        // 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();
    829841
    830842        // 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        }
    834848
    835849        // 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 && )
    837851            localeMapper = checkForCodec(ctype);
    838852
    839853        // 4. locale (ditto)
    840         if (!localeMapper && lang && *lang != 0)
     854        if (!localeMapper && )
    841855            localeMapper = checkForCodec(lang);
    842856
    843857        // 5. "@euro"
    844         if ((!localeMapper && ctype && strstr(ctype, "@euro")) || (lang && strstr(lang, "@euro")))
     858        if ((!localeMapper && ctype))
    845859            localeMapper = checkForCodec("ISO 8859-15");
    846860
    847861        // 6. guess locale from ctype unless ctype is "C"
    848862        // 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;
    852864
    853865        // Now do the guessing.
    854         if (lang && *lang && !localeMapper && try_by_name && *try_by_name) {
     866        if () {
    855867            if (try_locale_list(iso8859_15locales, lang))
    856868                localeMapper = QTextCodec::codecForName("ISO 8859-15");
     
    885897        }
    886898
    887         delete [] ctype;
    888         delete [] lang;
    889899    }
    890900
     
    906916        return;
    907917
     918
    908919    if (destroying_is_ok)
    909920        qWarning("QTextCodec: Creating new codec during codec cleanup");
     921
    910922    all = new QList<QTextCodec*>;
    911923    // create the cleanup object to cleanup all codecs on exit
     
    944956    (void)new QSjisCodec;
    945957    (void)new QEucKrCodec;
     958
    946959    (void)new QBig5Codec;
    947960    (void)new QBig5hkscsCodec;
     
    979992}
    980993
     994
     995
     996
     997
     998
     999
     1000
     1001
     1002
     1003
     1004
     1005
     1006
     1007
     1008
     1009
     1010
     1011
     1012
     1013
    9811014QTextCodec::ConverterState::~ConverterState()
    9821015{
     
    11311164
    11321165/*!
    1133     \enum QTextCodec::ConversionFlag
    1134 
    1135     \value DefaultConversion  No flag is set.
    1136     \value ConvertInvalidToNull  If this flag is set, each invalid input
    1137                                  character is output as a null character.
    1138     \value IgnoreHeader  Ignore any Unicode byte-order mark and don't generate any.
    1139 
    1140     \omitvalue FreeFunction
    1141 */
    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 /*!
    11561166    \nonreentrant
    11571167
     
    11761186QTextCodec::~QTextCodec()
    11771187{
     1188
    11781189    if (!destroying_is_ok)
    11791190        qWarning("QTextCodec::~QTextCodec: Called by application");
     1191
    11801192    if (all)
    11811193        all->removeAll(this);
     
    13081320    own mechanism for setting the locale.
    13091321
    1310     Setting this codec is not supported on DOS based Windows.
    1311 
    13121322    \sa codecForLocale()
    13131323*/
    13141324void QTextCodec::setCodecForLocale(QTextCodec *c)
    13151325{
    1316 #ifdef Q_WS_WIN
    1317     if (QSysInfo::WindowsVersion& QSysInfo::WV_DOS_based)
    1318         return;
    1319 #endif
    13201326    codecForLocaleSet = true;
    13211327    localeMapper = c;
     
    17701776    \since 4.4
    17711777
    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()
    17751786*/
    17761787QTextCodec *QTextCodec::codecForHtml(const QByteArray &ba, QTextCodec *defaultCodec)
     
    17801791    QTextCodec *c = 0;
    17811792
    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) {
    17911795        QByteArray header = ba.left(512).toLower();
    17921796        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                }
    17991805            }
    18001806        }
     
    18071813
    18081814/*!
    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.
    18121822*/
    18131823QTextCodec *QTextCodec::codecForHtml(const QByteArray &ba)
    18141824{
    18151825    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
    18161886}
    18171887
Note: See TracChangeset for help on using the changeset viewer.