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