| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/codecs/qtextcodec.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||||||||
| 2 | ** | - | ||||||||||||||||||
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||
| 4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
| 5 | ** | - | ||||||||||||||||||
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||||||||
| 7 | ** | - | ||||||||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
| 9 | ** Commercial License Usage | - | ||||||||||||||||||
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||
| 11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||
| 14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
| 15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||||||||
| 16 | ** | - | ||||||||||||||||||
| 17 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||
| 19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||||||||
| 20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
| 21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
| 22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
| 23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
| 24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
| 25 | ** | - | ||||||||||||||||||
| 26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
| 27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
| 28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
| 29 | ** | - | ||||||||||||||||||
| 30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
| 31 | ** | - | ||||||||||||||||||
| 32 | ****************************************************************************/ | - | ||||||||||||||||||
| 33 | - | |||||||||||||||||||
| 34 | #include "qplatformdefs.h" | - | ||||||||||||||||||
| 35 | #include "qtextcodec.h" | - | ||||||||||||||||||
| 36 | #include "qtextcodec_p.h" | - | ||||||||||||||||||
| 37 | - | |||||||||||||||||||
| 38 | #ifndef QT_NO_TEXTCODEC | - | ||||||||||||||||||
| 39 | - | |||||||||||||||||||
| 40 | #include "qlist.h" | - | ||||||||||||||||||
| 41 | #include "qfile.h" | - | ||||||||||||||||||
| 42 | #include "qstringlist.h" | - | ||||||||||||||||||
| 43 | #include "qvarlengtharray.h" | - | ||||||||||||||||||
| 44 | #if !defined(QT_BOOTSTRAPPED) | - | ||||||||||||||||||
| 45 | #include <private/qcoreapplication_p.h> | - | ||||||||||||||||||
| 46 | #endif | - | ||||||||||||||||||
| 47 | #include "private/qcoreglobaldata_p.h" | - | ||||||||||||||||||
| 48 | - | |||||||||||||||||||
| 49 | #include "qutfcodec_p.h" | - | ||||||||||||||||||
| 50 | #include "qlatincodec_p.h" | - | ||||||||||||||||||
| 51 | - | |||||||||||||||||||
| 52 | #if !defined(QT_BOOTSTRAPPED) | - | ||||||||||||||||||
| 53 | # include "qtsciicodec_p.h" | - | ||||||||||||||||||
| 54 | # include "qisciicodec_p.h" | - | ||||||||||||||||||
| 55 | #if defined(QT_USE_ICU) | - | ||||||||||||||||||
| 56 | #include "qicucodec_p.h" | - | ||||||||||||||||||
| 57 | #else | - | ||||||||||||||||||
| 58 | #if !defined(QT_NO_ICONV) | - | ||||||||||||||||||
| 59 | # include "qiconvcodec_p.h" | - | ||||||||||||||||||
| 60 | #endif | - | ||||||||||||||||||
| 61 | #ifdef Q_OS_WIN | - | ||||||||||||||||||
| 62 | # include "qwindowscodec_p.h" | - | ||||||||||||||||||
| 63 | #endif | - | ||||||||||||||||||
| 64 | # include "qsimplecodec_p.h" | - | ||||||||||||||||||
| 65 | #if !defined(QT_NO_BIG_CODECS) | - | ||||||||||||||||||
| 66 | # ifndef Q_OS_INTEGRITY | - | ||||||||||||||||||
| 67 | # include "qgb18030codec_p.h" | - | ||||||||||||||||||
| 68 | # include "qeucjpcodec_p.h" | - | ||||||||||||||||||
| 69 | # include "qjiscodec_p.h" | - | ||||||||||||||||||
| 70 | # include "qsjiscodec_p.h" | - | ||||||||||||||||||
| 71 | # include "qeuckrcodec_p.h" | - | ||||||||||||||||||
| 72 | # include "qbig5codec_p.h" | - | ||||||||||||||||||
| 73 | # endif // !Q_OS_INTEGRITY | - | ||||||||||||||||||
| 74 | #endif // !QT_NO_BIG_CODECS | - | ||||||||||||||||||
| 75 | - | |||||||||||||||||||
| 76 | #endif // QT_USE_ICU | - | ||||||||||||||||||
| 77 | #endif // QT_BOOTSTRAPPED | - | ||||||||||||||||||
| 78 | - | |||||||||||||||||||
| 79 | #include "qmutex.h" | - | ||||||||||||||||||
| 80 | - | |||||||||||||||||||
| 81 | #include <stdlib.h> | - | ||||||||||||||||||
| 82 | #include <ctype.h> | - | ||||||||||||||||||
| 83 | #include <locale.h> | - | ||||||||||||||||||
| 84 | #if defined (_XOPEN_UNIX) && !defined(Q_OS_QNX) && !defined(Q_OS_OSF) && !defined(Q_OS_ANDROID) | - | ||||||||||||||||||
| 85 | # include <langinfo.h> | - | ||||||||||||||||||
| 86 | #endif | - | ||||||||||||||||||
| 87 | - | |||||||||||||||||||
| 88 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
| 89 | - | |||||||||||||||||||
| 90 | typedef QList<QTextCodec*>::ConstIterator TextCodecListConstIt; | - | ||||||||||||||||||
| 91 | typedef QList<QByteArray>::ConstIterator ByteArrayListConstIt; | - | ||||||||||||||||||
| 92 | - | |||||||||||||||||||
| 93 | Q_GLOBAL_STATIC_WITH_ARGS(QMutex, textCodecsMutex, (QMutex::Recursive)); executed 1434 times by 541 tests: end of blockExecuted by:
executed 1434 times by 541 tests: guard.store(QtGlobalStatic::Destroyed);Executed by:
executed 35418 times by 43 tests: return &holder.value;Executed by:
| 0-35418 | ||||||||||||||||||
| 94 | QMutex *qTextCodecsMutex() { return textCodecsMutex(); } never executed: return textCodecsMutex(); | 0 | ||||||||||||||||||
| 95 | - | |||||||||||||||||||
| 96 | #if !defined(QT_USE_ICU) | - | ||||||||||||||||||
| 97 | static char qtolower(char c) | - | ||||||||||||||||||
| 98 | { if (c >= 'A' && c <= 'Z') return c + 0x20; return c; } | - | ||||||||||||||||||
| 99 | static bool qisalnum(char c) | - | ||||||||||||||||||
| 100 | { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); } | - | ||||||||||||||||||
| 101 | - | |||||||||||||||||||
| 102 | bool qTextCodecNameMatch(const char *n, const char *h) | - | ||||||||||||||||||
| 103 | { | - | ||||||||||||||||||
| 104 | if (qstricmp(n, h) == 0) | - | ||||||||||||||||||
| 105 | return true; | - | ||||||||||||||||||
| 106 | - | |||||||||||||||||||
| 107 | // if the letters and numbers are the same, we have a match | - | ||||||||||||||||||
| 108 | while (*n != '\0') { | - | ||||||||||||||||||
| 109 | if (qisalnum(*n)) { | - | ||||||||||||||||||
| 110 | for (;;) { | - | ||||||||||||||||||
| 111 | if (*h == '\0') | - | ||||||||||||||||||
| 112 | return false; | - | ||||||||||||||||||
| 113 | if (qisalnum(*h)) | - | ||||||||||||||||||
| 114 | break; | - | ||||||||||||||||||
| 115 | ++h; | - | ||||||||||||||||||
| 116 | } | - | ||||||||||||||||||
| 117 | if (qtolower(*n) != qtolower(*h)) | - | ||||||||||||||||||
| 118 | return false; | - | ||||||||||||||||||
| 119 | ++h; | - | ||||||||||||||||||
| 120 | } | - | ||||||||||||||||||
| 121 | ++n; | - | ||||||||||||||||||
| 122 | } | - | ||||||||||||||||||
| 123 | while (*h && !qisalnum(*h)) | - | ||||||||||||||||||
| 124 | ++h; | - | ||||||||||||||||||
| 125 | return (*h == '\0'); | - | ||||||||||||||||||
| 126 | } | - | ||||||||||||||||||
| 127 | - | |||||||||||||||||||
| 128 | - | |||||||||||||||||||
| 129 | #if !defined(Q_OS_WIN32) && !defined(Q_OS_WINCE) && !defined(QT_LOCALE_IS_UTF8) | - | ||||||||||||||||||
| 130 | static QTextCodec *checkForCodec(const QByteArray &name) { | - | ||||||||||||||||||
| 131 | QTextCodec *c = QTextCodec::codecForName(name); | - | ||||||||||||||||||
| 132 | if (!c) { | - | ||||||||||||||||||
| 133 | const int index = name.indexOf('@'); | - | ||||||||||||||||||
| 134 | if (index != -1) { | - | ||||||||||||||||||
| 135 | c = QTextCodec::codecForName(name.left(index)); | - | ||||||||||||||||||
| 136 | } | - | ||||||||||||||||||
| 137 | } | - | ||||||||||||||||||
| 138 | return c; | - | ||||||||||||||||||
| 139 | } | - | ||||||||||||||||||
| 140 | #endif | - | ||||||||||||||||||
| 141 | - | |||||||||||||||||||
| 142 | static void setup(); | - | ||||||||||||||||||
| 143 | - | |||||||||||||||||||
| 144 | // \threadsafe | - | ||||||||||||||||||
| 145 | // this returns the codec the method sets up as locale codec to | - | ||||||||||||||||||
| 146 | // avoid a race condition in codecForLocale() when | - | ||||||||||||||||||
| 147 | // setCodecForLocale(0) is called at the same time. | - | ||||||||||||||||||
| 148 | static QTextCodec *setupLocaleMapper() | - | ||||||||||||||||||
| 149 | { | - | ||||||||||||||||||
| 150 | QCoreGlobalData *globalData = QCoreGlobalData::instance(); | - | ||||||||||||||||||
| 151 | - | |||||||||||||||||||
| 152 | QTextCodec *locale = 0; | - | ||||||||||||||||||
| 153 | - | |||||||||||||||||||
| 154 | { | - | ||||||||||||||||||
| 155 | QMutexLocker locker(textCodecsMutex()); | - | ||||||||||||||||||
| 156 | if (globalData->allCodecs.isEmpty()) | - | ||||||||||||||||||
| 157 | setup(); | - | ||||||||||||||||||
| 158 | } | - | ||||||||||||||||||
| 159 | - | |||||||||||||||||||
| 160 | #if !defined(QT_BOOTSTRAPPED) | - | ||||||||||||||||||
| 161 | QCoreApplicationPrivate::initLocale(); | - | ||||||||||||||||||
| 162 | #endif | - | ||||||||||||||||||
| 163 | - | |||||||||||||||||||
| 164 | #if defined(QT_LOCALE_IS_UTF8) | - | ||||||||||||||||||
| 165 | locale = QTextCodec::codecForName("UTF-8"); | - | ||||||||||||||||||
| 166 | #elif defined(Q_OS_WIN) || defined(Q_OS_WINCE) | - | ||||||||||||||||||
| 167 | locale = QTextCodec::codecForName("System"); | - | ||||||||||||||||||
| 168 | #else | - | ||||||||||||||||||
| 169 | - | |||||||||||||||||||
| 170 | // First try getting the codecs name from nl_langinfo and see | - | ||||||||||||||||||
| 171 | // if we have a builtin codec for it. | - | ||||||||||||||||||
| 172 | // Only fall back to using iconv if we can't find a builtin codec | - | ||||||||||||||||||
| 173 | // This is because the builtin utf8 codec is around 5 times faster | - | ||||||||||||||||||
| 174 | // then the using QIconvCodec | - | ||||||||||||||||||
| 175 | - | |||||||||||||||||||
| 176 | #if defined (_XOPEN_UNIX) && !defined(Q_OS_OSF) | - | ||||||||||||||||||
| 177 | char *charset = nl_langinfo(CODESET); | - | ||||||||||||||||||
| 178 | if (charset) | - | ||||||||||||||||||
| 179 | locale = QTextCodec::codecForName(charset); | - | ||||||||||||||||||
| 180 | #endif | - | ||||||||||||||||||
| 181 | #if !defined(QT_NO_ICONV) && !defined(QT_BOOTSTRAPPED) | - | ||||||||||||||||||
| 182 | if (!locale) { | - | ||||||||||||||||||
| 183 | // no builtin codec for the locale found, let's try using iconv | - | ||||||||||||||||||
| 184 | (void) new QIconvCodec(); | - | ||||||||||||||||||
| 185 | locale = QTextCodec::codecForName("System"); | - | ||||||||||||||||||
| 186 | } | - | ||||||||||||||||||
| 187 | #endif | - | ||||||||||||||||||
| 188 | - | |||||||||||||||||||
| 189 | if (!locale) { | - | ||||||||||||||||||
| 190 | // Very poorly defined and followed standards causes lots of | - | ||||||||||||||||||
| 191 | // code to try to get all the cases... This logic is | - | ||||||||||||||||||
| 192 | // duplicated in QIconvCodec, so if you change it here, change | - | ||||||||||||||||||
| 193 | // it there too. | - | ||||||||||||||||||
| 194 | - | |||||||||||||||||||
| 195 | // Try to determine locale codeset from locale name assigned to | - | ||||||||||||||||||
| 196 | // LC_CTYPE category. | - | ||||||||||||||||||
| 197 | - | |||||||||||||||||||
| 198 | // First part is getting that locale name. First try setlocale() which | - | ||||||||||||||||||
| 199 | // definitely knows it, but since we cannot fully trust it, get ready | - | ||||||||||||||||||
| 200 | // to fall back to environment variables. | - | ||||||||||||||||||
| 201 | const QByteArray ctype = setlocale(LC_CTYPE, 0); | - | ||||||||||||||||||
| 202 | - | |||||||||||||||||||
| 203 | // Get the first nonempty value from $LC_ALL, $LC_CTYPE, and $LANG | - | ||||||||||||||||||
| 204 | // environment variables. | - | ||||||||||||||||||
| 205 | QByteArray lang = qgetenv("LC_ALL"); | - | ||||||||||||||||||
| 206 | if (lang.isEmpty() || lang == "C") { | - | ||||||||||||||||||
| 207 | lang = qgetenv("LC_CTYPE"); | - | ||||||||||||||||||
| 208 | } | - | ||||||||||||||||||
| 209 | if (lang.isEmpty() || lang == "C") { | - | ||||||||||||||||||
| 210 | lang = qgetenv("LANG"); | - | ||||||||||||||||||
| 211 | } | - | ||||||||||||||||||
| 212 | - | |||||||||||||||||||
| 213 | // Now try these in order: | - | ||||||||||||||||||
| 214 | // 1. CODESET from ctype if it contains a .CODESET part (e.g. en_US.ISO8859-15) | - | ||||||||||||||||||
| 215 | // 2. CODESET from lang if it contains a .CODESET part | - | ||||||||||||||||||
| 216 | // 3. ctype (maybe the locale is named "ISO-8859-1" or something) | - | ||||||||||||||||||
| 217 | // 4. locale (ditto) | - | ||||||||||||||||||
| 218 | // 5. check for "@euro" | - | ||||||||||||||||||
| 219 | // 6. guess locale from ctype unless ctype is "C" | - | ||||||||||||||||||
| 220 | // 7. guess locale from lang | - | ||||||||||||||||||
| 221 | - | |||||||||||||||||||
| 222 | // 1. CODESET from ctype if it contains a .CODESET part (e.g. en_US.ISO8859-15) | - | ||||||||||||||||||
| 223 | int indexOfDot = ctype.indexOf('.'); | - | ||||||||||||||||||
| 224 | if (indexOfDot != -1) | - | ||||||||||||||||||
| 225 | locale = checkForCodec( ctype.mid(indexOfDot + 1) ); | - | ||||||||||||||||||
| 226 | - | |||||||||||||||||||
| 227 | // 2. CODESET from lang if it contains a .CODESET part | - | ||||||||||||||||||
| 228 | if (!locale) { | - | ||||||||||||||||||
| 229 | indexOfDot = lang.indexOf('.'); | - | ||||||||||||||||||
| 230 | if (indexOfDot != -1) | - | ||||||||||||||||||
| 231 | locale = checkForCodec( lang.mid(indexOfDot + 1) ); | - | ||||||||||||||||||
| 232 | } | - | ||||||||||||||||||
| 233 | - | |||||||||||||||||||
| 234 | // 3. ctype (maybe the locale is named "ISO-8859-1" or something) | - | ||||||||||||||||||
| 235 | if (!locale && !ctype.isEmpty() && ctype != "C") | - | ||||||||||||||||||
| 236 | locale = checkForCodec(ctype); | - | ||||||||||||||||||
| 237 | - | |||||||||||||||||||
| 238 | // 4. locale (ditto) | - | ||||||||||||||||||
| 239 | if (!locale && !lang.isEmpty()) | - | ||||||||||||||||||
| 240 | locale = checkForCodec(lang); | - | ||||||||||||||||||
| 241 | - | |||||||||||||||||||
| 242 | // 5. "@euro" | - | ||||||||||||||||||
| 243 | if ((!locale && ctype.contains("@euro")) || lang.contains("@euro")) | - | ||||||||||||||||||
| 244 | locale = checkForCodec("ISO 8859-15"); | - | ||||||||||||||||||
| 245 | } | - | ||||||||||||||||||
| 246 | - | |||||||||||||||||||
| 247 | #endif | - | ||||||||||||||||||
| 248 | // If everything failed, we default to 8859-1 | - | ||||||||||||||||||
| 249 | if (!locale) | - | ||||||||||||||||||
| 250 | locale = QTextCodec::codecForName("ISO 8859-1"); | - | ||||||||||||||||||
| 251 | globalData->codecForLocale.storeRelease(locale); | - | ||||||||||||||||||
| 252 | return locale; | - | ||||||||||||||||||
| 253 | } | - | ||||||||||||||||||
| 254 | - | |||||||||||||||||||
| 255 | - | |||||||||||||||||||
| 256 | // textCodecsMutex need to be locked to enter this function | - | ||||||||||||||||||
| 257 | static void setup() | - | ||||||||||||||||||
| 258 | { | - | ||||||||||||||||||
| 259 | static bool initialized = false; | - | ||||||||||||||||||
| 260 | if (initialized) | - | ||||||||||||||||||
| 261 | return; | - | ||||||||||||||||||
| 262 | initialized = true; | - | ||||||||||||||||||
| 263 | - | |||||||||||||||||||
| 264 | #if !defined(QT_NO_CODECS) && !defined(QT_BOOTSTRAPPED) | - | ||||||||||||||||||
| 265 | (void)new QTsciiCodec; | - | ||||||||||||||||||
| 266 | for (int i = 0; i < 9; ++i) | - | ||||||||||||||||||
| 267 | (void)new QIsciiCodec(i); | - | ||||||||||||||||||
| 268 | for (int i = 0; i < QSimpleTextCodec::numSimpleCodecs; ++i) | - | ||||||||||||||||||
| 269 | (void)new QSimpleTextCodec(i); | - | ||||||||||||||||||
| 270 | - | |||||||||||||||||||
| 271 | # if !defined(QT_NO_BIG_CODECS) && !defined(Q_OS_INTEGRITY) | - | ||||||||||||||||||
| 272 | (void)new QGb18030Codec; | - | ||||||||||||||||||
| 273 | (void)new QGbkCodec; | - | ||||||||||||||||||
| 274 | (void)new QGb2312Codec; | - | ||||||||||||||||||
| 275 | (void)new QEucJpCodec; | - | ||||||||||||||||||
| 276 | (void)new QJisCodec; | - | ||||||||||||||||||
| 277 | (void)new QSjisCodec; | - | ||||||||||||||||||
| 278 | (void)new QEucKrCodec; | - | ||||||||||||||||||
| 279 | (void)new QCP949Codec; | - | ||||||||||||||||||
| 280 | (void)new QBig5Codec; | - | ||||||||||||||||||
| 281 | (void)new QBig5hkscsCodec; | - | ||||||||||||||||||
| 282 | # endif // !QT_NO_BIG_CODECS && !Q_OS_INTEGRITY | - | ||||||||||||||||||
| 283 | #if !defined(QT_NO_ICONV) | - | ||||||||||||||||||
| 284 | (void) new QIconvCodec; | - | ||||||||||||||||||
| 285 | #endif | - | ||||||||||||||||||
| 286 | #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE) | - | ||||||||||||||||||
| 287 | (void) new QWindowsLocalCodec; | - | ||||||||||||||||||
| 288 | #endif // Q_OS_WIN32 | - | ||||||||||||||||||
| 289 | #endif // !QT_NO_CODECS && !QT_BOOTSTRAPPED | - | ||||||||||||||||||
| 290 | - | |||||||||||||||||||
| 291 | (void)new QUtf16Codec; | - | ||||||||||||||||||
| 292 | (void)new QUtf16BECodec; | - | ||||||||||||||||||
| 293 | (void)new QUtf16LECodec; | - | ||||||||||||||||||
| 294 | (void)new QUtf32Codec; | - | ||||||||||||||||||
| 295 | (void)new QUtf32BECodec; | - | ||||||||||||||||||
| 296 | (void)new QUtf32LECodec; | - | ||||||||||||||||||
| 297 | (void)new QLatin15Codec; | - | ||||||||||||||||||
| 298 | (void)new QLatin1Codec; | - | ||||||||||||||||||
| 299 | (void)new QUtf8Codec; | - | ||||||||||||||||||
| 300 | } | - | ||||||||||||||||||
| 301 | #else | - | ||||||||||||||||||
| 302 | static void setup() {} | - | ||||||||||||||||||
| 303 | #endif // QT_USE_ICU | - | ||||||||||||||||||
| 304 | - | |||||||||||||||||||
| 305 | /*! | - | ||||||||||||||||||
| 306 | \enum QTextCodec::ConversionFlag | - | ||||||||||||||||||
| 307 | - | |||||||||||||||||||
| 308 | \value DefaultConversion No flag is set. | - | ||||||||||||||||||
| 309 | \value ConvertInvalidToNull If this flag is set, each invalid input | - | ||||||||||||||||||
| 310 | character is output as a null character. | - | ||||||||||||||||||
| 311 | \value IgnoreHeader Ignore any Unicode byte-order mark and don't generate any. | - | ||||||||||||||||||
| 312 | - | |||||||||||||||||||
| 313 | \omitvalue FreeFunction | - | ||||||||||||||||||
| 314 | */ | - | ||||||||||||||||||
| 315 | - | |||||||||||||||||||
| 316 | /*! | - | ||||||||||||||||||
| 317 | \fn QTextCodec::ConverterState::ConverterState(ConversionFlags flags) | - | ||||||||||||||||||
| 318 | - | |||||||||||||||||||
| 319 | Constructs a ConverterState object initialized with the given \a flags. | - | ||||||||||||||||||
| 320 | */ | - | ||||||||||||||||||
| 321 | - | |||||||||||||||||||
| 322 | /*! | - | ||||||||||||||||||
| 323 | Destroys the ConverterState object. | - | ||||||||||||||||||
| 324 | */ | - | ||||||||||||||||||
| 325 | QTextCodec::ConverterState::~ConverterState() | - | ||||||||||||||||||
| 326 | { | - | ||||||||||||||||||
| 327 | if (flags & FreeFunction)
| 14-759217 | ||||||||||||||||||
| 328 | (QTextCodecUnalignedPointer::decode(state_data))(this); executed 14 times by 3 tests: (QTextCodecUnalignedPointer::decode(state_data))(this);Executed by:
| 14 | ||||||||||||||||||
| 329 | else if (d)
| 0-759217 | ||||||||||||||||||
| 330 | free(d); never executed: free(d); | 0 | ||||||||||||||||||
| 331 | } executed 759231 times by 111 tests: end of blockExecuted by:
| 759231 | ||||||||||||||||||
| 332 | - | |||||||||||||||||||
| 333 | /*! | - | ||||||||||||||||||
| 334 | \class QTextCodec | - | ||||||||||||||||||
| 335 | \inmodule QtCore | - | ||||||||||||||||||
| 336 | \brief The QTextCodec class provides conversions between text encodings. | - | ||||||||||||||||||
| 337 | \reentrant | - | ||||||||||||||||||
| 338 | \ingroup i18n | - | ||||||||||||||||||
| 339 | - | |||||||||||||||||||
| 340 | Qt uses Unicode to store, draw and manipulate strings. In many | - | ||||||||||||||||||
| 341 | situations you may wish to deal with data that uses a different | - | ||||||||||||||||||
| 342 | encoding. For example, most Japanese documents are still stored | - | ||||||||||||||||||
| 343 | in Shift-JIS or ISO 2022-JP, while Russian users often have their | - | ||||||||||||||||||
| 344 | documents in KOI8-R or Windows-1251. | - | ||||||||||||||||||
| 345 | - | |||||||||||||||||||
| 346 | Qt provides a set of QTextCodec classes to help with converting | - | ||||||||||||||||||
| 347 | non-Unicode formats to and from Unicode. You can also create your | - | ||||||||||||||||||
| 348 | own codec classes. | - | ||||||||||||||||||
| 349 | - | |||||||||||||||||||
| 350 | The supported encodings are: | - | ||||||||||||||||||
| 351 | - | |||||||||||||||||||
| 352 | \list | - | ||||||||||||||||||
| 353 | \li \l{Big5 Text Codec}{Big5} | - | ||||||||||||||||||
| 354 | \li \l{Big5-HKSCS Text Codec}{Big5-HKSCS} | - | ||||||||||||||||||
| 355 | \li CP949 | - | ||||||||||||||||||
| 356 | \li \l{EUC-JP Text Codec}{EUC-JP} | - | ||||||||||||||||||
| 357 | \li \l{EUC-KR Text Codec}{EUC-KR} | - | ||||||||||||||||||
| 358 | \li \l{GBK Text Codec}{GB18030} | - | ||||||||||||||||||
| 359 | \li HP-ROMAN8 | - | ||||||||||||||||||
| 360 | \li IBM 850 | - | ||||||||||||||||||
| 361 | \li IBM 866 | - | ||||||||||||||||||
| 362 | \li IBM 874 | - | ||||||||||||||||||
| 363 | \li \l{ISO 2022-JP (JIS) Text Codec}{ISO 2022-JP} | - | ||||||||||||||||||
| 364 | \li ISO 8859-1 to 10 | - | ||||||||||||||||||
| 365 | \li ISO 8859-13 to 16 | - | ||||||||||||||||||
| 366 | \li Iscii-Bng, Dev, Gjr, Knd, Mlm, Ori, Pnj, Tlg, and Tml | - | ||||||||||||||||||
| 367 | \li KOI8-R | - | ||||||||||||||||||
| 368 | \li KOI8-U | - | ||||||||||||||||||
| 369 | \li Macintosh | - | ||||||||||||||||||
| 370 | \li \l{Shift-JIS Text Codec}{Shift-JIS} | - | ||||||||||||||||||
| 371 | \li TIS-620 | - | ||||||||||||||||||
| 372 | \li \l{TSCII Text Codec}{TSCII} | - | ||||||||||||||||||
| 373 | \li UTF-8 | - | ||||||||||||||||||
| 374 | \li UTF-16 | - | ||||||||||||||||||
| 375 | \li UTF-16BE | - | ||||||||||||||||||
| 376 | \li UTF-16LE | - | ||||||||||||||||||
| 377 | \li UTF-32 | - | ||||||||||||||||||
| 378 | \li UTF-32BE | - | ||||||||||||||||||
| 379 | \li UTF-32LE | - | ||||||||||||||||||
| 380 | \li Windows-1250 to 1258 | - | ||||||||||||||||||
| 381 | \endlist | - | ||||||||||||||||||
| 382 | - | |||||||||||||||||||
| 383 | If Qt is compiled with ICU support enabled, most codecs supported by | - | ||||||||||||||||||
| 384 | ICU will also be available to the application. | - | ||||||||||||||||||
| 385 | - | |||||||||||||||||||
| 386 | \l {QTextCodec}s can be used as follows to convert some locally encoded | - | ||||||||||||||||||
| 387 | string to Unicode. Suppose you have some string encoded in Russian | - | ||||||||||||||||||
| 388 | KOI8-R encoding, and want to convert it to Unicode. The simple way | - | ||||||||||||||||||
| 389 | to do it is like this: | - | ||||||||||||||||||
| 390 | - | |||||||||||||||||||
| 391 | \snippet code/src_corelib_codecs_qtextcodec.cpp 0 | - | ||||||||||||||||||
| 392 | - | |||||||||||||||||||
| 393 | After this, \c string holds the text converted to Unicode. | - | ||||||||||||||||||
| 394 | Converting a string from Unicode to the local encoding is just as | - | ||||||||||||||||||
| 395 | easy: | - | ||||||||||||||||||
| 396 | - | |||||||||||||||||||
| 397 | \snippet code/src_corelib_codecs_qtextcodec.cpp 1 | - | ||||||||||||||||||
| 398 | - | |||||||||||||||||||
| 399 | To read or write files in various encodings, use QTextStream and | - | ||||||||||||||||||
| 400 | its \l{QTextStream::setCodec()}{setCodec()} function. See the | - | ||||||||||||||||||
| 401 | \l{tools/codecs}{Codecs} example for an application of QTextCodec | - | ||||||||||||||||||
| 402 | to file I/O. | - | ||||||||||||||||||
| 403 | - | |||||||||||||||||||
| 404 | Some care must be taken when trying to convert the data in chunks, | - | ||||||||||||||||||
| 405 | for example, when receiving it over a network. In such cases it is | - | ||||||||||||||||||
| 406 | possible that a multi-byte character will be split over two | - | ||||||||||||||||||
| 407 | chunks. At best this might result in the loss of a character and | - | ||||||||||||||||||
| 408 | at worst cause the entire conversion to fail. | - | ||||||||||||||||||
| 409 | - | |||||||||||||||||||
| 410 | The approach to use in these situations is to create a QTextDecoder | - | ||||||||||||||||||
| 411 | object for the codec and use this QTextDecoder for the whole | - | ||||||||||||||||||
| 412 | decoding process, as shown below: | - | ||||||||||||||||||
| 413 | - | |||||||||||||||||||
| 414 | \snippet code/src_corelib_codecs_qtextcodec.cpp 2 | - | ||||||||||||||||||
| 415 | - | |||||||||||||||||||
| 416 | The QTextDecoder object maintains state between chunks and therefore | - | ||||||||||||||||||
| 417 | works correctly even if a multi-byte character is split between | - | ||||||||||||||||||
| 418 | chunks. | - | ||||||||||||||||||
| 419 | - | |||||||||||||||||||
| 420 | \section1 Creating Your Own Codec Class | - | ||||||||||||||||||
| 421 | - | |||||||||||||||||||
| 422 | Support for new text encodings can be added to Qt by creating | - | ||||||||||||||||||
| 423 | QTextCodec subclasses. | - | ||||||||||||||||||
| 424 | - | |||||||||||||||||||
| 425 | The pure virtual functions describe the encoder to the system and | - | ||||||||||||||||||
| 426 | the coder is used as required in the different text file formats | - | ||||||||||||||||||
| 427 | supported by QTextStream, and under X11, for the locale-specific | - | ||||||||||||||||||
| 428 | character input and output. | - | ||||||||||||||||||
| 429 | - | |||||||||||||||||||
| 430 | To add support for another encoding to Qt, make a subclass of | - | ||||||||||||||||||
| 431 | QTextCodec and implement the functions listed in the table below. | - | ||||||||||||||||||
| 432 | - | |||||||||||||||||||
| 433 | \table | - | ||||||||||||||||||
| 434 | \header \li Function \li Description | - | ||||||||||||||||||
| 435 | - | |||||||||||||||||||
| 436 | \row \li name() | - | ||||||||||||||||||
| 437 | \li Returns the official name for the encoding. If the | - | ||||||||||||||||||
| 438 | encoding is listed in the | - | ||||||||||||||||||
| 439 | \l{IANA character-sets encoding file}, the name | - | ||||||||||||||||||
| 440 | should be the preferred MIME name for the encoding. | - | ||||||||||||||||||
| 441 | - | |||||||||||||||||||
| 442 | \row \li aliases() | - | ||||||||||||||||||
| 443 | \li Returns a list of alternative names for the encoding. | - | ||||||||||||||||||
| 444 | QTextCodec provides a default implementation that returns | - | ||||||||||||||||||
| 445 | an empty list. For example, "ISO-8859-1" has "latin1", | - | ||||||||||||||||||
| 446 | "CP819", "IBM819", and "iso-ir-100" as aliases. | - | ||||||||||||||||||
| 447 | - | |||||||||||||||||||
| 448 | \row \li \l{QTextCodec::mibEnum()}{mibEnum()} | - | ||||||||||||||||||
| 449 | \li Return the MIB enum for the encoding if it is listed in | - | ||||||||||||||||||
| 450 | the \l{IANA character-sets encoding file}. | - | ||||||||||||||||||
| 451 | - | |||||||||||||||||||
| 452 | \row \li convertToUnicode() | - | ||||||||||||||||||
| 453 | \li Converts an 8-bit character string to Unicode. | - | ||||||||||||||||||
| 454 | - | |||||||||||||||||||
| 455 | \row \li convertFromUnicode() | - | ||||||||||||||||||
| 456 | \li Converts a Unicode string to an 8-bit character string. | - | ||||||||||||||||||
| 457 | \endtable | - | ||||||||||||||||||
| 458 | - | |||||||||||||||||||
| 459 | \sa QTextStream, QTextDecoder, QTextEncoder, {Text Codecs Example} | - | ||||||||||||||||||
| 460 | */ | - | ||||||||||||||||||
| 461 | - | |||||||||||||||||||
| 462 | /*! | - | ||||||||||||||||||
| 463 | Constructs a QTextCodec, and gives it the highest precedence. The | - | ||||||||||||||||||
| 464 | QTextCodec should always be constructed on the heap (i.e. with \c | - | ||||||||||||||||||
| 465 | new). Qt takes ownership and will delete it when the application | - | ||||||||||||||||||
| 466 | terminates. | - | ||||||||||||||||||
| 467 | */ | - | ||||||||||||||||||
| 468 | QTextCodec::QTextCodec() | - | ||||||||||||||||||
| 469 | { | - | ||||||||||||||||||
| 470 | QMutexLocker locker(textCodecsMutex()); | - | ||||||||||||||||||
| 471 | - | |||||||||||||||||||
| 472 | QCoreGlobalData *globalInstance = QCoreGlobalData::instance(); | - | ||||||||||||||||||
| 473 | if (globalInstance->allCodecs.isEmpty())
| 140-980 | ||||||||||||||||||
| 474 | setup(); executed 980 times by 14 tests: setup();Executed by:
| 980 | ||||||||||||||||||
| 475 | - | |||||||||||||||||||
| 476 | globalInstance->allCodecs.prepend(this); | - | ||||||||||||||||||
| 477 | } executed 1120 times by 24 tests: end of blockExecuted by:
| 1120 | ||||||||||||||||||
| 478 | - | |||||||||||||||||||
| 479 | - | |||||||||||||||||||
| 480 | /*! | - | ||||||||||||||||||
| 481 | \nonreentrant | - | ||||||||||||||||||
| 482 | - | |||||||||||||||||||
| 483 | Destroys the QTextCodec. Note that you should not delete codecs | - | ||||||||||||||||||
| 484 | yourself: once created they become Qt's responsibility. | - | ||||||||||||||||||
| 485 | */ | - | ||||||||||||||||||
| 486 | QTextCodec::~QTextCodec() | - | ||||||||||||||||||
| 487 | { | - | ||||||||||||||||||
| 488 | } | - | ||||||||||||||||||
| 489 | - | |||||||||||||||||||
| 490 | /*! | - | ||||||||||||||||||
| 491 | \fn QTextCodec *QTextCodec::codecForName(const char *name) | - | ||||||||||||||||||
| 492 | - | |||||||||||||||||||
| 493 | Searches all installed QTextCodec objects and returns the one | - | ||||||||||||||||||
| 494 | which best matches \a name; the match is case-insensitive. Returns | - | ||||||||||||||||||
| 495 | 0 if no codec matching the name \a name could be found. | - | ||||||||||||||||||
| 496 | */ | - | ||||||||||||||||||
| 497 | - | |||||||||||||||||||
| 498 | /*! | - | ||||||||||||||||||
| 499 | \threadsafe | - | ||||||||||||||||||
| 500 | Searches all installed QTextCodec objects and returns the one | - | ||||||||||||||||||
| 501 | which best matches \a name; the match is case-insensitive. Returns | - | ||||||||||||||||||
| 502 | 0 if no codec matching the name \a name could be found. | - | ||||||||||||||||||
| 503 | */ | - | ||||||||||||||||||
| 504 | QTextCodec *QTextCodec::codecForName(const QByteArray &name) | - | ||||||||||||||||||
| 505 | { | - | ||||||||||||||||||
| 506 | if (name.isEmpty())
| 2-18898 | ||||||||||||||||||
| 507 | return 0; executed 2 times by 1 test: return 0;Executed by:
| 2 | ||||||||||||||||||
| 508 | - | |||||||||||||||||||
| 509 | QMutexLocker locker(textCodecsMutex()); | - | ||||||||||||||||||
| 510 | - | |||||||||||||||||||
| 511 | QCoreGlobalData *globalData = QCoreGlobalData::instance(); | - | ||||||||||||||||||
| 512 | if (!globalData)
| 1-18897 | ||||||||||||||||||
| 513 | return 0; executed 1 time by 1 test: return 0;Executed by:
| 1 | ||||||||||||||||||
| 514 | setup(); | - | ||||||||||||||||||
| 515 | - | |||||||||||||||||||
| 516 | #ifndef QT_USE_ICU | - | ||||||||||||||||||
| 517 | QTextCodecCache *cache = &globalData->codecCache; | - | ||||||||||||||||||
| 518 | QTextCodec *codec; | - | ||||||||||||||||||
| 519 | if (cache) { | - | ||||||||||||||||||
| 520 | codec = cache->value(name); | - | ||||||||||||||||||
| 521 | if (codec) | - | ||||||||||||||||||
| 522 | return codec; | - | ||||||||||||||||||
| 523 | } | - | ||||||||||||||||||
| 524 | - | |||||||||||||||||||
| 525 | for (TextCodecListConstIt it = globalData->allCodecs.constBegin(), cend = globalData->allCodecs.constEnd(); it != cend; ++it) { | - | ||||||||||||||||||
| 526 | QTextCodec *cursor = *it; | - | ||||||||||||||||||
| 527 | if (qTextCodecNameMatch(cursor->name(), name)) { | - | ||||||||||||||||||
| 528 | if (cache) | - | ||||||||||||||||||
| 529 | cache->insert(name, cursor); | - | ||||||||||||||||||
| 530 | return cursor; | - | ||||||||||||||||||
| 531 | } | - | ||||||||||||||||||
| 532 | QList<QByteArray> aliases = cursor->aliases(); | - | ||||||||||||||||||
| 533 | for (ByteArrayListConstIt ait = aliases.constBegin(), acend = aliases.constEnd(); ait != acend; ++ait) { | - | ||||||||||||||||||
| 534 | if (qTextCodecNameMatch(*ait, name)) { | - | ||||||||||||||||||
| 535 | if (cache) | - | ||||||||||||||||||
| 536 | cache->insert(name, cursor); | - | ||||||||||||||||||
| 537 | return cursor; | - | ||||||||||||||||||
| 538 | } | - | ||||||||||||||||||
| 539 | } | - | ||||||||||||||||||
| 540 | } | - | ||||||||||||||||||
| 541 | - | |||||||||||||||||||
| 542 | return 0; | - | ||||||||||||||||||
| 543 | #else | - | ||||||||||||||||||
| 544 | return QIcuCodec::codecForNameUnlocked(name); executed 18897 times by 18 tests: return QIcuCodec::codecForNameUnlocked(name);Executed by:
| 18897 | ||||||||||||||||||
| 545 | #endif | - | ||||||||||||||||||
| 546 | } | - | ||||||||||||||||||
| 547 | - | |||||||||||||||||||
| 548 | - | |||||||||||||||||||
| 549 | /*! | - | ||||||||||||||||||
| 550 | \threadsafe | - | ||||||||||||||||||
| 551 | Returns the QTextCodec which matches the | - | ||||||||||||||||||
| 552 | \l{QTextCodec::mibEnum()}{MIBenum} \a mib. | - | ||||||||||||||||||
| 553 | */ | - | ||||||||||||||||||
| 554 | QTextCodec* QTextCodec::codecForMib(int mib) | - | ||||||||||||||||||
| 555 | { | - | ||||||||||||||||||
| 556 | QMutexLocker locker(textCodecsMutex()); | - | ||||||||||||||||||
| 557 | - | |||||||||||||||||||
| 558 | QCoreGlobalData *globalData = QCoreGlobalData::instance(); | - | ||||||||||||||||||
| 559 | if (!globalData)
| 0-13435 | ||||||||||||||||||
| 560 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
| 561 | if (globalData->allCodecs.isEmpty())
| 0-13435 | ||||||||||||||||||
| 562 | setup(); never executed: setup(); | 0 | ||||||||||||||||||
| 563 | - | |||||||||||||||||||
| 564 | QByteArray key = "MIB: " + QByteArray::number(mib); | - | ||||||||||||||||||
| 565 | - | |||||||||||||||||||
| 566 | QTextCodecCache *cache = &globalData->codecCache; | - | ||||||||||||||||||
| 567 | QTextCodec *codec; | - | ||||||||||||||||||
| 568 | if (cache) {
| 0-13435 | ||||||||||||||||||
| 569 | codec = cache->value(key); | - | ||||||||||||||||||
| 570 | if (codec)
| 270-13165 | ||||||||||||||||||
| 571 | return codec; executed 13165 times by 14 tests: return codec;Executed by:
| 13165 | ||||||||||||||||||
| 572 | } executed 270 times by 22 tests: end of blockExecuted by:
| 270 | ||||||||||||||||||
| 573 | - | |||||||||||||||||||
| 574 | for (TextCodecListConstIt it = globalData->allCodecs.constBegin(), cend = globalData->allCodecs.constEnd(); it != cend; ++it) {
| 23-6646 | ||||||||||||||||||
| 575 | QTextCodec *cursor = *it; | - | ||||||||||||||||||
| 576 | if (cursor->mibEnum() == mib) {
| 247-6399 | ||||||||||||||||||
| 577 | if (cache)
| 0-247 | ||||||||||||||||||
| 578 | cache->insert(key, cursor); executed 247 times by 22 tests: cache->insert(key, cursor);Executed by:
| 247 | ||||||||||||||||||
| 579 | return cursor; executed 247 times by 22 tests: return cursor;Executed by:
| 247 | ||||||||||||||||||
| 580 | } | - | ||||||||||||||||||
| 581 | } executed 6399 times by 5 tests: end of blockExecuted by:
| 6399 | ||||||||||||||||||
| 582 | - | |||||||||||||||||||
| 583 | #ifdef QT_USE_ICU | - | ||||||||||||||||||
| 584 | return QIcuCodec::codecForMibUnlocked(mib); executed 23 times by 5 tests: return QIcuCodec::codecForMibUnlocked(mib);Executed by:
| 23 | ||||||||||||||||||
| 585 | #else | - | ||||||||||||||||||
| 586 | return 0; | - | ||||||||||||||||||
| 587 | #endif | - | ||||||||||||||||||
| 588 | } | - | ||||||||||||||||||
| 589 | - | |||||||||||||||||||
| 590 | /*! | - | ||||||||||||||||||
| 591 | \threadsafe | - | ||||||||||||||||||
| 592 | Returns the list of all available codecs, by name. Call | - | ||||||||||||||||||
| 593 | QTextCodec::codecForName() to obtain the QTextCodec for the name. | - | ||||||||||||||||||
| 594 | - | |||||||||||||||||||
| 595 | The list may contain many mentions of the same codec | - | ||||||||||||||||||
| 596 | if the codec has aliases. | - | ||||||||||||||||||
| 597 | - | |||||||||||||||||||
| 598 | \sa availableMibs(), name(), aliases() | - | ||||||||||||||||||
| 599 | */ | - | ||||||||||||||||||
| 600 | QList<QByteArray> QTextCodec::availableCodecs() | - | ||||||||||||||||||
| 601 | { | - | ||||||||||||||||||
| 602 | QMutexLocker locker(textCodecsMutex()); | - | ||||||||||||||||||
| 603 | - | |||||||||||||||||||
| 604 | QCoreGlobalData *globalData = QCoreGlobalData::instance(); | - | ||||||||||||||||||
| 605 | if (globalData->allCodecs.isEmpty())
| 0-4 | ||||||||||||||||||
| 606 | setup(); never executed: setup(); | 0 | ||||||||||||||||||
| 607 | - | |||||||||||||||||||
| 608 | QList<QByteArray> codecs; | - | ||||||||||||||||||
| 609 | - | |||||||||||||||||||
| 610 | for (TextCodecListConstIt it = globalData->allCodecs.constBegin(), cend = globalData->allCodecs.constEnd(); it != cend; ++it) {
| 4-344 | ||||||||||||||||||
| 611 | codecs += (*it)->name(); | - | ||||||||||||||||||
| 612 | codecs += (*it)->aliases(); | - | ||||||||||||||||||
| 613 | } executed 344 times by 1 test: end of blockExecuted by:
| 344 | ||||||||||||||||||
| 614 | - | |||||||||||||||||||
| 615 | #ifdef QT_USE_ICU | - | ||||||||||||||||||
| 616 | codecs += QIcuCodec::availableCodecs(); | - | ||||||||||||||||||
| 617 | #endif | - | ||||||||||||||||||
| 618 | - | |||||||||||||||||||
| 619 | return codecs; executed 4 times by 1 test: return codecs;Executed by:
| 4 | ||||||||||||||||||
| 620 | } | - | ||||||||||||||||||
| 621 | - | |||||||||||||||||||
| 622 | /*! | - | ||||||||||||||||||
| 623 | \threadsafe | - | ||||||||||||||||||
| 624 | Returns the list of MIBs for all available codecs. Call | - | ||||||||||||||||||
| 625 | QTextCodec::codecForMib() to obtain the QTextCodec for the MIB. | - | ||||||||||||||||||
| 626 | - | |||||||||||||||||||
| 627 | \sa availableCodecs(), mibEnum() | - | ||||||||||||||||||
| 628 | */ | - | ||||||||||||||||||
| 629 | QList<int> QTextCodec::availableMibs() | - | ||||||||||||||||||
| 630 | { | - | ||||||||||||||||||
| 631 | #ifdef QT_USE_ICU | - | ||||||||||||||||||
| 632 | return QIcuCodec::availableMibs(); executed 2 times by 1 test: return QIcuCodec::availableMibs();Executed by:
| 2 | ||||||||||||||||||
| 633 | #else | - | ||||||||||||||||||
| 634 | QMutexLocker locker(textCodecsMutex()); | - | ||||||||||||||||||
| 635 | - | |||||||||||||||||||
| 636 | QCoreGlobalData *globalData = QCoreGlobalData::instance(); | - | ||||||||||||||||||
| 637 | if (globalData->allCodecs.isEmpty()) | - | ||||||||||||||||||
| 638 | setup(); | - | ||||||||||||||||||
| 639 | - | |||||||||||||||||||
| 640 | QList<int> codecs; | - | ||||||||||||||||||
| 641 | - | |||||||||||||||||||
| 642 | for (TextCodecListConstIt it = globalData->allCodecs.constBegin(), cend = globalData->allCodecs.constEnd(); it != cend; ++it) | - | ||||||||||||||||||
| 643 | codecs += (*it)->mibEnum(); | - | ||||||||||||||||||
| 644 | - | |||||||||||||||||||
| 645 | return codecs; | - | ||||||||||||||||||
| 646 | #endif | - | ||||||||||||||||||
| 647 | } | - | ||||||||||||||||||
| 648 | - | |||||||||||||||||||
| 649 | /*! | - | ||||||||||||||||||
| 650 | \nonreentrant | - | ||||||||||||||||||
| 651 | - | |||||||||||||||||||
| 652 | Set the codec to \a c; this will be returned by | - | ||||||||||||||||||
| 653 | codecForLocale(). If \a c is a null pointer, the codec is reset to | - | ||||||||||||||||||
| 654 | the default. | - | ||||||||||||||||||
| 655 | - | |||||||||||||||||||
| 656 | This might be needed for some applications that want to use their | - | ||||||||||||||||||
| 657 | own mechanism for setting the locale. | - | ||||||||||||||||||
| 658 | - | |||||||||||||||||||
| 659 | \sa codecForLocale() | - | ||||||||||||||||||
| 660 | */ | - | ||||||||||||||||||
| 661 | void QTextCodec::setCodecForLocale(QTextCodec *c) | - | ||||||||||||||||||
| 662 | { | - | ||||||||||||||||||
| 663 | QCoreGlobalData::instance()->codecForLocale.storeRelease(c); | - | ||||||||||||||||||
| 664 | } executed 6 times by 2 tests: end of blockExecuted by:
| 6 | ||||||||||||||||||
| 665 | - | |||||||||||||||||||
| 666 | /*! | - | ||||||||||||||||||
| 667 | \threadsafe | - | ||||||||||||||||||
| 668 | Returns a pointer to the codec most suitable for this locale. | - | ||||||||||||||||||
| 669 | - | |||||||||||||||||||
| 670 | On Windows, the codec will be based on a system locale. On Unix | - | ||||||||||||||||||
| 671 | systems, the codec will might fall back to using the \e iconv | - | ||||||||||||||||||
| 672 | library if no builtin codec for the locale can be found. | - | ||||||||||||||||||
| 673 | - | |||||||||||||||||||
| 674 | Note that in these cases the codec's name will be "System". | - | ||||||||||||||||||
| 675 | */ | - | ||||||||||||||||||
| 676 | - | |||||||||||||||||||
| 677 | QTextCodec* QTextCodec::codecForLocale() | - | ||||||||||||||||||
| 678 | { | - | ||||||||||||||||||
| 679 | QCoreGlobalData *globalData = QCoreGlobalData::instance(); | - | ||||||||||||||||||
| 680 | if (!globalData)
| 7-920047 | ||||||||||||||||||
| 681 | return 0; executed 7 times by 2 tests: return 0;Executed by:
| 7 | ||||||||||||||||||
| 682 | - | |||||||||||||||||||
| 683 | QTextCodec *codec = globalData->codecForLocale.loadAcquire(); | - | ||||||||||||||||||
| 684 | if (!codec) {
| 981-919066 | ||||||||||||||||||
| 685 | #ifdef QT_USE_ICU | - | ||||||||||||||||||
| 686 | textCodecsMutex()->lock(); | - | ||||||||||||||||||
| 687 | codec = QIcuCodec::defaultCodecUnlocked(); | - | ||||||||||||||||||
| 688 | textCodecsMutex()->unlock(); | - | ||||||||||||||||||
| 689 | #else | - | ||||||||||||||||||
| 690 | // setupLocaleMapper locks as necessary | - | ||||||||||||||||||
| 691 | codec = setupLocaleMapper(); | - | ||||||||||||||||||
| 692 | #endif | - | ||||||||||||||||||
| 693 | } executed 981 times by 15 tests: end of blockExecuted by:
| 981 | ||||||||||||||||||
| 694 | - | |||||||||||||||||||
| 695 | return codec; executed 920047 times by 416 tests: return codec;Executed by:
| 920047 | ||||||||||||||||||
| 696 | } | - | ||||||||||||||||||
| 697 | - | |||||||||||||||||||
| 698 | - | |||||||||||||||||||
| 699 | /*! | - | ||||||||||||||||||
| 700 | \fn QByteArray QTextCodec::name() const | - | ||||||||||||||||||
| 701 | - | |||||||||||||||||||
| 702 | QTextCodec subclasses must reimplement this function. It returns | - | ||||||||||||||||||
| 703 | the name of the encoding supported by the subclass. | - | ||||||||||||||||||
| 704 | - | |||||||||||||||||||
| 705 | If the codec is registered as a character set in the | - | ||||||||||||||||||
| 706 | \l{IANA character-sets encoding file} this method should | - | ||||||||||||||||||
| 707 | return the preferred mime name for the codec if defined, | - | ||||||||||||||||||
| 708 | otherwise its name. | - | ||||||||||||||||||
| 709 | */ | - | ||||||||||||||||||
| 710 | - | |||||||||||||||||||
| 711 | /*! | - | ||||||||||||||||||
| 712 | \fn int QTextCodec::mibEnum() const | - | ||||||||||||||||||
| 713 | - | |||||||||||||||||||
| 714 | Subclasses of QTextCodec must reimplement this function. It | - | ||||||||||||||||||
| 715 | returns the \l{QTextCodec::mibEnum()}{MIBenum} (see \l{IANA character-sets encoding file} | - | ||||||||||||||||||
| 716 | for more information). It is important that each QTextCodec | - | ||||||||||||||||||
| 717 | subclass returns the correct unique value for this function. | - | ||||||||||||||||||
| 718 | */ | - | ||||||||||||||||||
| 719 | - | |||||||||||||||||||
| 720 | /*! | - | ||||||||||||||||||
| 721 | Subclasses can return a number of aliases for the codec in question. | - | ||||||||||||||||||
| 722 | - | |||||||||||||||||||
| 723 | Standard aliases for codecs can be found in the | - | ||||||||||||||||||
| 724 | \l{IANA character-sets encoding file}. | - | ||||||||||||||||||
| 725 | */ | - | ||||||||||||||||||
| 726 | QList<QByteArray> QTextCodec::aliases() const | - | ||||||||||||||||||
| 727 | { | - | ||||||||||||||||||
| 728 | return QList<QByteArray>(); executed 400 times by 8 tests: return QList<QByteArray>();Executed by:
| 400 | ||||||||||||||||||
| 729 | } | - | ||||||||||||||||||
| 730 | - | |||||||||||||||||||
| 731 | /*! | - | ||||||||||||||||||
| 732 | \fn QString QTextCodec::convertToUnicode(const char *chars, int len, | - | ||||||||||||||||||
| 733 | ConverterState *state) const | - | ||||||||||||||||||
| 734 | - | |||||||||||||||||||
| 735 | QTextCodec subclasses must reimplement this function. | - | ||||||||||||||||||
| 736 | - | |||||||||||||||||||
| 737 | Converts the first \a len characters of \a chars from the | - | ||||||||||||||||||
| 738 | encoding of the subclass to Unicode, and returns the result in a | - | ||||||||||||||||||
| 739 | QString. | - | ||||||||||||||||||
| 740 | - | |||||||||||||||||||
| 741 | \a state can be 0, in which case the conversion is stateless and | - | ||||||||||||||||||
| 742 | default conversion rules should be used. If state is not 0, the | - | ||||||||||||||||||
| 743 | codec should save the state after the conversion in \a state, and | - | ||||||||||||||||||
| 744 | adjust the \c remainingChars and \c invalidChars members of the struct. | - | ||||||||||||||||||
| 745 | */ | - | ||||||||||||||||||
| 746 | - | |||||||||||||||||||
| 747 | /*! | - | ||||||||||||||||||
| 748 | \fn QByteArray QTextCodec::convertFromUnicode(const QChar *input, int number, | - | ||||||||||||||||||
| 749 | ConverterState *state) const | - | ||||||||||||||||||
| 750 | - | |||||||||||||||||||
| 751 | QTextCodec subclasses must reimplement this function. | - | ||||||||||||||||||
| 752 | - | |||||||||||||||||||
| 753 | Converts the first \a number of characters from the \a input array | - | ||||||||||||||||||
| 754 | from Unicode to the encoding of the subclass, and returns the result | - | ||||||||||||||||||
| 755 | in a QByteArray. | - | ||||||||||||||||||
| 756 | - | |||||||||||||||||||
| 757 | \a state can be 0 in which case the conversion is stateless and | - | ||||||||||||||||||
| 758 | default conversion rules should be used. If state is not 0, the | - | ||||||||||||||||||
| 759 | codec should save the state after the conversion in \a state, and | - | ||||||||||||||||||
| 760 | adjust the \c remainingChars and \c invalidChars members of the struct. | - | ||||||||||||||||||
| 761 | */ | - | ||||||||||||||||||
| 762 | - | |||||||||||||||||||
| 763 | /*! | - | ||||||||||||||||||
| 764 | Creates a QTextDecoder with a specified \a flags to decode chunks | - | ||||||||||||||||||
| 765 | of \c{char *} data to create chunks of Unicode data. | - | ||||||||||||||||||
| 766 | - | |||||||||||||||||||
| 767 | The caller is responsible for deleting the returned object. | - | ||||||||||||||||||
| 768 | - | |||||||||||||||||||
| 769 | \since 4.7 | - | ||||||||||||||||||
| 770 | */ | - | ||||||||||||||||||
| 771 | QTextDecoder* QTextCodec::makeDecoder(QTextCodec::ConversionFlags flags) const | - | ||||||||||||||||||
| 772 | { | - | ||||||||||||||||||
| 773 | return new QTextDecoder(this, flags); executed 5465 times by 19 tests: return new QTextDecoder(this, flags);Executed by:
| 5465 | ||||||||||||||||||
| 774 | } | - | ||||||||||||||||||
| 775 | - | |||||||||||||||||||
| 776 | /*! | - | ||||||||||||||||||
| 777 | Creates a QTextEncoder with a specified \a flags to encode chunks | - | ||||||||||||||||||
| 778 | of Unicode data as \c{char *} data. | - | ||||||||||||||||||
| 779 | - | |||||||||||||||||||
| 780 | The caller is responsible for deleting the returned object. | - | ||||||||||||||||||
| 781 | - | |||||||||||||||||||
| 782 | \since 4.7 | - | ||||||||||||||||||
| 783 | */ | - | ||||||||||||||||||
| 784 | QTextEncoder* QTextCodec::makeEncoder(QTextCodec::ConversionFlags flags) const | - | ||||||||||||||||||
| 785 | { | - | ||||||||||||||||||
| 786 | return new QTextEncoder(this, flags); executed 1012 times by 4 tests: return new QTextEncoder(this, flags);Executed by:
| 1012 | ||||||||||||||||||
| 787 | } | - | ||||||||||||||||||
| 788 | - | |||||||||||||||||||
| 789 | /*! | - | ||||||||||||||||||
| 790 | \fn QByteArray QTextCodec::fromUnicode(const QChar *input, int number, | - | ||||||||||||||||||
| 791 | ConverterState *state) const | - | ||||||||||||||||||
| 792 | - | |||||||||||||||||||
| 793 | Converts the first \a number of characters from the \a input array | - | ||||||||||||||||||
| 794 | from Unicode to the encoding of this codec, and returns the result | - | ||||||||||||||||||
| 795 | in a QByteArray. | - | ||||||||||||||||||
| 796 | - | |||||||||||||||||||
| 797 | The \a state of the convertor used is updated. | - | ||||||||||||||||||
| 798 | */ | - | ||||||||||||||||||
| 799 | - | |||||||||||||||||||
| 800 | /*! | - | ||||||||||||||||||
| 801 | Converts \a str from Unicode to the encoding of this codec, and | - | ||||||||||||||||||
| 802 | returns the result in a QByteArray. | - | ||||||||||||||||||
| 803 | */ | - | ||||||||||||||||||
| 804 | QByteArray QTextCodec::fromUnicode(const QString& str) const | - | ||||||||||||||||||
| 805 | { | - | ||||||||||||||||||
| 806 | return convertFromUnicode(str.constData(), str.length(), 0); executed 10251 times by 139 tests: return convertFromUnicode(str.constData(), str.length(), 0);Executed by:
| 10251 | ||||||||||||||||||
| 807 | } | - | ||||||||||||||||||
| 808 | - | |||||||||||||||||||
| 809 | /*! | - | ||||||||||||||||||
| 810 | \fn QString QTextCodec::toUnicode(const char *input, int size, | - | ||||||||||||||||||
| 811 | ConverterState *state) const | - | ||||||||||||||||||
| 812 | - | |||||||||||||||||||
| 813 | Converts the first \a size characters from the \a input from the | - | ||||||||||||||||||
| 814 | encoding of this codec to Unicode, and returns the result in a | - | ||||||||||||||||||
| 815 | QString. | - | ||||||||||||||||||
| 816 | - | |||||||||||||||||||
| 817 | The \a state of the convertor used is updated. | - | ||||||||||||||||||
| 818 | */ | - | ||||||||||||||||||
| 819 | - | |||||||||||||||||||
| 820 | /*! | - | ||||||||||||||||||
| 821 | Converts \a a from the encoding of this codec to Unicode, and | - | ||||||||||||||||||
| 822 | returns the result in a QString. | - | ||||||||||||||||||
| 823 | */ | - | ||||||||||||||||||
| 824 | QString QTextCodec::toUnicode(const QByteArray& a) const | - | ||||||||||||||||||
| 825 | { | - | ||||||||||||||||||
| 826 | return convertToUnicode(a.constData(), a.length(), 0); executed 1006 times by 5 tests: return convertToUnicode(a.constData(), a.length(), 0);Executed by:
| 1006 | ||||||||||||||||||
| 827 | } | - | ||||||||||||||||||
| 828 | - | |||||||||||||||||||
| 829 | /*! | - | ||||||||||||||||||
| 830 | Returns \c true if the Unicode character \a ch can be fully encoded | - | ||||||||||||||||||
| 831 | with this codec; otherwise returns \c false. | - | ||||||||||||||||||
| 832 | */ | - | ||||||||||||||||||
| 833 | bool QTextCodec::canEncode(QChar ch) const | - | ||||||||||||||||||
| 834 | { | - | ||||||||||||||||||
| 835 | ConverterState state; | - | ||||||||||||||||||
| 836 | state.flags = ConvertInvalidToNull; | - | ||||||||||||||||||
| 837 | convertFromUnicode(&ch, 1, &state); | - | ||||||||||||||||||
| 838 | return (state.invalidChars == 0); executed 3899 times by 3 tests: return (state.invalidChars == 0);Executed by:
| 3899 | ||||||||||||||||||
| 839 | } | - | ||||||||||||||||||
| 840 | - | |||||||||||||||||||
| 841 | /*! | - | ||||||||||||||||||
| 842 | \overload | - | ||||||||||||||||||
| 843 | - | |||||||||||||||||||
| 844 | \a s contains the string being tested for encode-ability. | - | ||||||||||||||||||
| 845 | */ | - | ||||||||||||||||||
| 846 | bool QTextCodec::canEncode(const QString& s) const | - | ||||||||||||||||||
| 847 | { | - | ||||||||||||||||||
| 848 | ConverterState state; | - | ||||||||||||||||||
| 849 | state.flags = ConvertInvalidToNull; | - | ||||||||||||||||||
| 850 | convertFromUnicode(s.constData(), s.length(), &state); | - | ||||||||||||||||||
| 851 | return (state.invalidChars == 0); executed 9 times by 1 test: return (state.invalidChars == 0);Executed by:
| 9 | ||||||||||||||||||
| 852 | } | - | ||||||||||||||||||
| 853 | - | |||||||||||||||||||
| 854 | /*! | - | ||||||||||||||||||
| 855 | \overload | - | ||||||||||||||||||
| 856 | - | |||||||||||||||||||
| 857 | \a chars contains the source characters. | - | ||||||||||||||||||
| 858 | */ | - | ||||||||||||||||||
| 859 | QString QTextCodec::toUnicode(const char *chars) const | - | ||||||||||||||||||
| 860 | { | - | ||||||||||||||||||
| 861 | int len = qstrlen(chars); | - | ||||||||||||||||||
| 862 | return convertToUnicode(chars, len, 0); executed 2 times by 2 tests: return convertToUnicode(chars, len, 0);Executed by:
| 2 | ||||||||||||||||||
| 863 | } | - | ||||||||||||||||||
| 864 | - | |||||||||||||||||||
| 865 | - | |||||||||||||||||||
| 866 | /*! | - | ||||||||||||||||||
| 867 | \class QTextEncoder | - | ||||||||||||||||||
| 868 | \inmodule QtCore | - | ||||||||||||||||||
| 869 | \brief The QTextEncoder class provides a state-based encoder. | - | ||||||||||||||||||
| 870 | \reentrant | - | ||||||||||||||||||
| 871 | \ingroup i18n | - | ||||||||||||||||||
| 872 | - | |||||||||||||||||||
| 873 | A text encoder converts text from Unicode into an encoded text format | - | ||||||||||||||||||
| 874 | using a specific codec. | - | ||||||||||||||||||
| 875 | - | |||||||||||||||||||
| 876 | The encoder converts Unicode into another format, remembering any | - | ||||||||||||||||||
| 877 | state that is required between calls. | - | ||||||||||||||||||
| 878 | - | |||||||||||||||||||
| 879 | \sa QTextCodec::makeEncoder(), QTextDecoder | - | ||||||||||||||||||
| 880 | */ | - | ||||||||||||||||||
| 881 | - | |||||||||||||||||||
| 882 | /*! | - | ||||||||||||||||||
| 883 | \fn QTextEncoder::QTextEncoder(const QTextCodec *codec) | - | ||||||||||||||||||
| 884 | - | |||||||||||||||||||
| 885 | Constructs a text encoder for the given \a codec. | - | ||||||||||||||||||
| 886 | */ | - | ||||||||||||||||||
| 887 | - | |||||||||||||||||||
| 888 | /*! | - | ||||||||||||||||||
| 889 | Constructs a text encoder for the given \a codec and conversion \a flags. | - | ||||||||||||||||||
| 890 | - | |||||||||||||||||||
| 891 | \since 4.7 | - | ||||||||||||||||||
| 892 | */ | - | ||||||||||||||||||
| 893 | QTextEncoder::QTextEncoder(const QTextCodec *codec, QTextCodec::ConversionFlags flags) | - | ||||||||||||||||||
| 894 | : c(codec), state() | - | ||||||||||||||||||
| 895 | { | - | ||||||||||||||||||
| 896 | state.flags = flags; | - | ||||||||||||||||||
| 897 | } executed 1012 times by 4 tests: end of blockExecuted by:
| 1012 | ||||||||||||||||||
| 898 | - | |||||||||||||||||||
| 899 | /*! | - | ||||||||||||||||||
| 900 | Destroys the encoder. | - | ||||||||||||||||||
| 901 | */ | - | ||||||||||||||||||
| 902 | QTextEncoder::~QTextEncoder() | - | ||||||||||||||||||
| 903 | { | - | ||||||||||||||||||
| 904 | } | - | ||||||||||||||||||
| 905 | - | |||||||||||||||||||
| 906 | /*! | - | ||||||||||||||||||
| 907 | \internal | - | ||||||||||||||||||
| 908 | \since 4.5 | - | ||||||||||||||||||
| 909 | Determines whether the eecoder encountered a failure while decoding the input. If | - | ||||||||||||||||||
| 910 | an error was encountered, the produced result is undefined, and gets converted as according | - | ||||||||||||||||||
| 911 | to the conversion flags. | - | ||||||||||||||||||
| 912 | */ | - | ||||||||||||||||||
| 913 | bool QTextEncoder::hasFailure() const | - | ||||||||||||||||||
| 914 | { | - | ||||||||||||||||||
| 915 | return state.invalidChars != 0; executed 494 times by 1 test: return state.invalidChars != 0;Executed by:
| 494 | ||||||||||||||||||
| 916 | } | - | ||||||||||||||||||
| 917 | - | |||||||||||||||||||
| 918 | /*! | - | ||||||||||||||||||
| 919 | Converts the Unicode string \a str into an encoded QByteArray. | - | ||||||||||||||||||
| 920 | */ | - | ||||||||||||||||||
| 921 | QByteArray QTextEncoder::fromUnicode(const QString& str) | - | ||||||||||||||||||
| 922 | { | - | ||||||||||||||||||
| 923 | QByteArray result = c->fromUnicode(str.constData(), str.length(), &state); | - | ||||||||||||||||||
| 924 | return result; executed 5379 times by 4 tests: return result;Executed by:
| 5379 | ||||||||||||||||||
| 925 | } | - | ||||||||||||||||||
| 926 | - | |||||||||||||||||||
| 927 | /*! | - | ||||||||||||||||||
| 928 | \overload | - | ||||||||||||||||||
| 929 | - | |||||||||||||||||||
| 930 | Converts \a len characters (not bytes) from \a uc, and returns the | - | ||||||||||||||||||
| 931 | result in a QByteArray. | - | ||||||||||||||||||
| 932 | */ | - | ||||||||||||||||||
| 933 | QByteArray QTextEncoder::fromUnicode(const QChar *uc, int len) | - | ||||||||||||||||||
| 934 | { | - | ||||||||||||||||||
| 935 | QByteArray result = c->fromUnicode(uc, len, &state); | - | ||||||||||||||||||
| 936 | return result; executed 4925 times by 4 tests: return result;Executed by:
| 4925 | ||||||||||||||||||
| 937 | } | - | ||||||||||||||||||
| 938 | - | |||||||||||||||||||
| 939 | /*! | - | ||||||||||||||||||
| 940 | \class QTextDecoder | - | ||||||||||||||||||
| 941 | \inmodule QtCore | - | ||||||||||||||||||
| 942 | \brief The QTextDecoder class provides a state-based decoder. | - | ||||||||||||||||||
| 943 | \reentrant | - | ||||||||||||||||||
| 944 | \ingroup i18n | - | ||||||||||||||||||
| 945 | - | |||||||||||||||||||
| 946 | A text decoder converts text from an encoded text format into Unicode | - | ||||||||||||||||||
| 947 | using a specific codec. | - | ||||||||||||||||||
| 948 | - | |||||||||||||||||||
| 949 | The decoder converts text in this format into Unicode, remembering any | - | ||||||||||||||||||
| 950 | state that is required between calls. | - | ||||||||||||||||||
| 951 | - | |||||||||||||||||||
| 952 | \sa QTextCodec::makeDecoder(), QTextEncoder | - | ||||||||||||||||||
| 953 | */ | - | ||||||||||||||||||
| 954 | - | |||||||||||||||||||
| 955 | /*! | - | ||||||||||||||||||
| 956 | \fn QTextDecoder::QTextDecoder(const QTextCodec *codec) | - | ||||||||||||||||||
| 957 | - | |||||||||||||||||||
| 958 | Constructs a text decoder for the given \a codec. | - | ||||||||||||||||||
| 959 | */ | - | ||||||||||||||||||
| 960 | - | |||||||||||||||||||
| 961 | /*! | - | ||||||||||||||||||
| 962 | Constructs a text decoder for the given \a codec and conversion \a flags. | - | ||||||||||||||||||
| 963 | - | |||||||||||||||||||
| 964 | \since 4.7 | - | ||||||||||||||||||
| 965 | */ | - | ||||||||||||||||||
| 966 | - | |||||||||||||||||||
| 967 | QTextDecoder::QTextDecoder(const QTextCodec *codec, QTextCodec::ConversionFlags flags) | - | ||||||||||||||||||
| 968 | : c(codec), state() | - | ||||||||||||||||||
| 969 | { | - | ||||||||||||||||||
| 970 | state.flags = flags; | - | ||||||||||||||||||
| 971 | } executed 5465 times by 19 tests: end of blockExecuted by:
| 5465 | ||||||||||||||||||
| 972 | - | |||||||||||||||||||
| 973 | /*! | - | ||||||||||||||||||
| 974 | Destroys the decoder. | - | ||||||||||||||||||
| 975 | */ | - | ||||||||||||||||||
| 976 | QTextDecoder::~QTextDecoder() | - | ||||||||||||||||||
| 977 | { | - | ||||||||||||||||||
| 978 | } | - | ||||||||||||||||||
| 979 | - | |||||||||||||||||||
| 980 | /*! | - | ||||||||||||||||||
| 981 | \fn QString QTextDecoder::toUnicode(const char *chars, int len) | - | ||||||||||||||||||
| 982 | - | |||||||||||||||||||
| 983 | Converts the first \a len bytes in \a chars to Unicode, returning | - | ||||||||||||||||||
| 984 | the result. | - | ||||||||||||||||||
| 985 | - | |||||||||||||||||||
| 986 | If not all characters are used (e.g. if only part of a multi-byte | - | ||||||||||||||||||
| 987 | encoding is at the end of the characters), the decoder remembers | - | ||||||||||||||||||
| 988 | enough state to continue with the next call to this function. | - | ||||||||||||||||||
| 989 | */ | - | ||||||||||||||||||
| 990 | QString QTextDecoder::toUnicode(const char *chars, int len) | - | ||||||||||||||||||
| 991 | { | - | ||||||||||||||||||
| 992 | return c->toUnicode(chars, len, &state); executed 46108 times by 4 tests: return c->toUnicode(chars, len, &state);Executed by:
| 46108 | ||||||||||||||||||
| 993 | } | - | ||||||||||||||||||
| 994 | - | |||||||||||||||||||
| 995 | // in qstring.cpp: | - | ||||||||||||||||||
| 996 | void qt_from_latin1(ushort *dst, const char *str, size_t size) Q_DECL_NOTHROW; | - | ||||||||||||||||||
| 997 | - | |||||||||||||||||||
| 998 | /*! \overload | - | ||||||||||||||||||
| 999 | - | |||||||||||||||||||
| 1000 | The converted string is returned in \a target. | - | ||||||||||||||||||
| 1001 | */ | - | ||||||||||||||||||
| 1002 | void QTextDecoder::toUnicode(QString *target, const char *chars, int len) | - | ||||||||||||||||||
| 1003 | { | - | ||||||||||||||||||
| 1004 | Q_ASSERT(target); | - | ||||||||||||||||||
| 1005 | switch (c->mibEnum()) { | - | ||||||||||||||||||
| 1006 | case 106: // utf8 executed 60357 times by 16 tests: case 106:Executed by:
| 60357 | ||||||||||||||||||
| 1007 | static_cast<const QUtf8Codec*>(c)->convertToUnicode(target, chars, len, &state); | - | ||||||||||||||||||
| 1008 | break; executed 60357 times by 16 tests: break;Executed by:
| 60357 | ||||||||||||||||||
| 1009 | case 4: // latin1 executed 3 times by 1 test: case 4:Executed by:
| 3 | ||||||||||||||||||
| 1010 | target->resize(len); | - | ||||||||||||||||||
| 1011 | qt_from_latin1((ushort*)target->data(), chars, len); | - | ||||||||||||||||||
| 1012 | break; executed 3 times by 1 test: break;Executed by:
| 3 | ||||||||||||||||||
| 1013 | default: executed 427 times by 1 test: default:Executed by:
| 427 | ||||||||||||||||||
| 1014 | *target = c->toUnicode(chars, len, &state); | - | ||||||||||||||||||
| 1015 | } executed 427 times by 1 test: end of blockExecuted by:
| 427 | ||||||||||||||||||
| 1016 | } | - | ||||||||||||||||||
| 1017 | - | |||||||||||||||||||
| 1018 | - | |||||||||||||||||||
| 1019 | /*! | - | ||||||||||||||||||
| 1020 | \overload | - | ||||||||||||||||||
| 1021 | - | |||||||||||||||||||
| 1022 | Converts the bytes in the byte array specified by \a ba to Unicode | - | ||||||||||||||||||
| 1023 | and returns the result. | - | ||||||||||||||||||
| 1024 | */ | - | ||||||||||||||||||
| 1025 | QString QTextDecoder::toUnicode(const QByteArray &ba) | - | ||||||||||||||||||
| 1026 | { | - | ||||||||||||||||||
| 1027 | return c->toUnicode(ba.constData(), ba.length(), &state); executed 301 times by 2 tests: return c->toUnicode(ba.constData(), ba.length(), &state);Executed by:
| 301 | ||||||||||||||||||
| 1028 | } | - | ||||||||||||||||||
| 1029 | - | |||||||||||||||||||
| 1030 | /*! | - | ||||||||||||||||||
| 1031 | \since 4.4 | - | ||||||||||||||||||
| 1032 | - | |||||||||||||||||||
| 1033 | Tries to detect the encoding of the provided snippet of HTML in | - | ||||||||||||||||||
| 1034 | the given byte array, \a ba, by checking the BOM (Byte Order Mark) | - | ||||||||||||||||||
| 1035 | and the content-type meta header and returns a QTextCodec instance | - | ||||||||||||||||||
| 1036 | that is capable of decoding the html to unicode. If the codec | - | ||||||||||||||||||
| 1037 | cannot be detected from the content provided, \a defaultCodec is | - | ||||||||||||||||||
| 1038 | returned. | - | ||||||||||||||||||
| 1039 | - | |||||||||||||||||||
| 1040 | \sa codecForUtfText() | - | ||||||||||||||||||
| 1041 | */ | - | ||||||||||||||||||
| 1042 | QTextCodec *QTextCodec::codecForHtml(const QByteArray &ba, QTextCodec *defaultCodec) | - | ||||||||||||||||||
| 1043 | { | - | ||||||||||||||||||
| 1044 | // determine charset | - | ||||||||||||||||||
| 1045 | QTextCodec *c = QTextCodec::codecForUtfText(ba, 0); | - | ||||||||||||||||||
| 1046 | if (!c) {
| 2-72 | ||||||||||||||||||
| 1047 | QByteArray header = ba.left(1024).toLower(); | - | ||||||||||||||||||
| 1048 | int pos = header.indexOf("meta "); | - | ||||||||||||||||||
| 1049 | if (pos != -1) {
| 24-48 | ||||||||||||||||||
| 1050 | pos = header.indexOf("charset=", pos); | - | ||||||||||||||||||
| 1051 | if (pos != -1) {
| 0-24 | ||||||||||||||||||
| 1052 | pos += qstrlen("charset="); | - | ||||||||||||||||||
| 1053 | - | |||||||||||||||||||
| 1054 | int pos2 = pos; | - | ||||||||||||||||||
| 1055 | // The attribute can be closed with either """, "'", ">" or "/", | - | ||||||||||||||||||
| 1056 | // none of which are valid charset characters. | - | ||||||||||||||||||
| 1057 | while (++pos2 < header.size()) {
| 3-169 | ||||||||||||||||||
| 1058 | char ch = header.at(pos2); | - | ||||||||||||||||||
| 1059 | if (ch == '\"' || ch == '\'' || ch == '>') {
| 2-155 | ||||||||||||||||||
| 1060 | QByteArray name = header.mid(pos, pos2 - pos); | - | ||||||||||||||||||
| 1061 | if (name == "unicode") // QTBUG-41998, ICU will return UTF-16.
| 1-20 | ||||||||||||||||||
| 1062 | name = QByteArrayLiteral("UTF-8"); executed 1 time by 1 test: name = ([]() -> QByteArray { enum { Size = sizeof("UTF-8") - 1 }; static const QStaticByteArrayData<Size> qbytearray_literal = { { { { -1 } }, Size, 0, 0, sizeof(QByteArrayData) }, "UTF-8" }; QByteArrayDataPtr holder = { qbytearray_literal.data_ptr() }; const QByteArray ba(holder); return ba; }());Executed by:
executed 1 time by 1 test: return ba;Executed by:
| 1 | ||||||||||||||||||
| 1063 | c = QTextCodec::codecForName(name); | - | ||||||||||||||||||
| 1064 | return c ? c : defaultCodec; executed 21 times by 3 tests: return c ? c : defaultCodec;Executed by:
| 4-21 | ||||||||||||||||||
| 1065 | } | - | ||||||||||||||||||
| 1066 | } executed 148 times by 3 tests: end of blockExecuted by:
| 148 | ||||||||||||||||||
| 1067 | } executed 3 times by 1 test: end of blockExecuted by:
| 3 | ||||||||||||||||||
| 1068 | } executed 3 times by 1 test: end of blockExecuted by:
| 3 | ||||||||||||||||||
| 1069 | } executed 51 times by 3 tests: end of blockExecuted by:
| 51 | ||||||||||||||||||
| 1070 | if (!c)
| 2-51 | ||||||||||||||||||
| 1071 | c = defaultCodec; executed 51 times by 3 tests: c = defaultCodec;Executed by:
| 51 | ||||||||||||||||||
| 1072 | - | |||||||||||||||||||
| 1073 | return c; executed 53 times by 3 tests: return c;Executed by:
| 53 | ||||||||||||||||||
| 1074 | } | - | ||||||||||||||||||
| 1075 | - | |||||||||||||||||||
| 1076 | /*! | - | ||||||||||||||||||
| 1077 | \overload | - | ||||||||||||||||||
| 1078 | - | |||||||||||||||||||
| 1079 | Tries to detect the encoding of the provided snippet of HTML in | - | ||||||||||||||||||
| 1080 | the given byte array, \a ba, by checking the BOM (Byte Order Mark) | - | ||||||||||||||||||
| 1081 | and the content-type meta header and returns a QTextCodec instance | - | ||||||||||||||||||
| 1082 | that is capable of decoding the html to unicode. If the codec cannot | - | ||||||||||||||||||
| 1083 | be detected, this overload returns a Latin-1 QTextCodec. | - | ||||||||||||||||||
| 1084 | */ | - | ||||||||||||||||||
| 1085 | QTextCodec *QTextCodec::codecForHtml(const QByteArray &ba) | - | ||||||||||||||||||
| 1086 | { | - | ||||||||||||||||||
| 1087 | return codecForHtml(ba, QTextCodec::codecForName("ISO-8859-1")); executed 62 times by 3 tests: return codecForHtml(ba, QTextCodec::codecForName("ISO-8859-1"));Executed by:
| 62 | ||||||||||||||||||
| 1088 | } | - | ||||||||||||||||||
| 1089 | - | |||||||||||||||||||
| 1090 | /*! | - | ||||||||||||||||||
| 1091 | \since 4.6 | - | ||||||||||||||||||
| 1092 | - | |||||||||||||||||||
| 1093 | Tries to detect the encoding of the provided snippet \a ba by | - | ||||||||||||||||||
| 1094 | using the BOM (Byte Order Mark) and returns a QTextCodec instance | - | ||||||||||||||||||
| 1095 | that is capable of decoding the text to unicode. If the codec | - | ||||||||||||||||||
| 1096 | cannot be detected from the content provided, \a defaultCodec is | - | ||||||||||||||||||
| 1097 | returned. | - | ||||||||||||||||||
| 1098 | - | |||||||||||||||||||
| 1099 | \sa codecForHtml() | - | ||||||||||||||||||
| 1100 | */ | - | ||||||||||||||||||
| 1101 | QTextCodec *QTextCodec::codecForUtfText(const QByteArray &ba, QTextCodec *defaultCodec) | - | ||||||||||||||||||
| 1102 | { | - | ||||||||||||||||||
| 1103 | const int arraySize = ba.size(); | - | ||||||||||||||||||
| 1104 | - | |||||||||||||||||||
| 1105 | if (arraySize > 3) {
| 1977-2652 | ||||||||||||||||||
| 1106 | if ((uchar)ba[0] == 0x00
| 12-2640 | ||||||||||||||||||
| 1107 | && (uchar)ba[1] == 0x00
| 1-11 | ||||||||||||||||||
| 1108 | && (uchar)ba[2] == 0xFE
| 1-10 | ||||||||||||||||||
| 1109 | && (uchar)ba[3] == 0xFF)
| 0-10 | ||||||||||||||||||
| 1110 | return QTextCodec::codecForMib(1018); // utf-32 be executed 10 times by 2 tests: return QTextCodec::codecForMib(1018);Executed by:
| 10 | ||||||||||||||||||
| 1111 | else if ((uchar)ba[0] == 0xFF
| 70-2572 | ||||||||||||||||||
| 1112 | && (uchar)ba[1] == 0xFE
| 0-70 | ||||||||||||||||||
| 1113 | && (uchar)ba[2] == 0x00
| 10-60 | ||||||||||||||||||
| 1114 | && (uchar)ba[3] == 0x00)
| 0-10 | ||||||||||||||||||
| 1115 | return QTextCodec::codecForMib(1019); // utf-32 le executed 10 times by 2 tests: return QTextCodec::codecForMib(1019);Executed by:
| 10 | ||||||||||||||||||
| 1116 | } executed 2632 times by 22 tests: end of blockExecuted by:
| 2632 | ||||||||||||||||||
| 1117 | - | |||||||||||||||||||
| 1118 | if (arraySize < 2)
| 1822-2787 | ||||||||||||||||||
| 1119 | return defaultCodec; executed 1822 times by 1 test: return defaultCodec;Executed by:
| 1822 | ||||||||||||||||||
| 1120 | if ((uchar)ba[0] == 0xfe && (uchar)ba[1] == 0xff)
| 0-2705 | ||||||||||||||||||
| 1121 | return QTextCodec::codecForMib(1013); // utf16 be executed 82 times by 2 tests: return QTextCodec::codecForMib(1013);Executed by:
| 82 | ||||||||||||||||||
| 1122 | else if ((uchar)ba[0] == 0xff && (uchar)ba[1] == 0xfe)
| 0-2630 | ||||||||||||||||||
| 1123 | return QTextCodec::codecForMib(1014); // utf16 le executed 75 times by 2 tests: return QTextCodec::codecForMib(1014);Executed by:
| 75 | ||||||||||||||||||
| 1124 | - | |||||||||||||||||||
| 1125 | if (arraySize < 3)
| 54-2576 | ||||||||||||||||||
| 1126 | return defaultCodec; executed 54 times by 2 tests: return defaultCodec;Executed by:
| 54 | ||||||||||||||||||
| 1127 | if ((uchar)ba[0] == 0xef
| 11-2565 | ||||||||||||||||||
| 1128 | && (uchar)ba[1] == 0xbb
| 0-11 | ||||||||||||||||||
| 1129 | && (uchar)ba[2] == 0xbf)
| 0-11 | ||||||||||||||||||
| 1130 | return QTextCodec::codecForMib(106); // utf-8 executed 11 times by 2 tests: return QTextCodec::codecForMib(106);Executed by:
| 11 | ||||||||||||||||||
| 1131 | - | |||||||||||||||||||
| 1132 | return defaultCodec; executed 2565 times by 22 tests: return defaultCodec;Executed by:
| 2565 | ||||||||||||||||||
| 1133 | } | - | ||||||||||||||||||
| 1134 | - | |||||||||||||||||||
| 1135 | /*! | - | ||||||||||||||||||
| 1136 | \overload | - | ||||||||||||||||||
| 1137 | - | |||||||||||||||||||
| 1138 | Tries to detect the encoding of the provided snippet \a ba by | - | ||||||||||||||||||
| 1139 | using the BOM (Byte Order Mark) and returns a QTextCodec instance | - | ||||||||||||||||||
| 1140 | that is capable of decoding the text to unicode. If the codec | - | ||||||||||||||||||
| 1141 | cannot be detected, this overload returns a Latin-1 QTextCodec. | - | ||||||||||||||||||
| 1142 | - | |||||||||||||||||||
| 1143 | \sa codecForHtml() | - | ||||||||||||||||||
| 1144 | */ | - | ||||||||||||||||||
| 1145 | QTextCodec *QTextCodec::codecForUtfText(const QByteArray &ba) | - | ||||||||||||||||||
| 1146 | { | - | ||||||||||||||||||
| 1147 | return codecForUtfText(ba, QTextCodec::codecForMib(/*Latin 1*/ 4)); never executed: return codecForUtfText(ba, QTextCodec::codecForMib( 4)); | 0 | ||||||||||||||||||
| 1148 | } | - | ||||||||||||||||||
| 1149 | - | |||||||||||||||||||
| 1150 | /*! | - | ||||||||||||||||||
| 1151 | \fn QTextCodec * QTextCodec::codecForTr () | - | ||||||||||||||||||
| 1152 | \obsolete | - | ||||||||||||||||||
| 1153 | - | |||||||||||||||||||
| 1154 | Returns the codec used by QObject::tr() on its argument. If this | - | ||||||||||||||||||
| 1155 | function returns 0 (the default), tr() assumes Latin-1. | - | ||||||||||||||||||
| 1156 | */ | - | ||||||||||||||||||
| 1157 | - | |||||||||||||||||||
| 1158 | /*! | - | ||||||||||||||||||
| 1159 | \internal | - | ||||||||||||||||||
| 1160 | \since 4.3 | - | ||||||||||||||||||
| 1161 | Determines whether the decoder encountered a failure while decoding the | - | ||||||||||||||||||
| 1162 | input. If an error was encountered, the produced result is undefined, and | - | ||||||||||||||||||
| 1163 | gets converted as according to the conversion flags. | - | ||||||||||||||||||
| 1164 | */ | - | ||||||||||||||||||
| 1165 | bool QTextDecoder::hasFailure() const | - | ||||||||||||||||||
| 1166 | { | - | ||||||||||||||||||
| 1167 | return state.invalidChars != 0; executed 59028 times by 16 tests: return state.invalidChars != 0;Executed by:
| 59028 | ||||||||||||||||||
| 1168 | } | - | ||||||||||||||||||
| 1169 | - | |||||||||||||||||||
| 1170 | QT_END_NAMESPACE | - | ||||||||||||||||||
| 1171 | - | |||||||||||||||||||
| 1172 | #endif // QT_NO_TEXTCODEC | - | ||||||||||||||||||
| Source code | Switch to Preprocessed file |