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