codecs/qicucodec.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/****************************************************************************-
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/****************************************************************************
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 "qicucodec_p.h" -
43 -
44#ifndef QT_NO_TEXTCODEC -
45 -
46#include "qtextcodec_p.h" -
47#include "qutfcodec_p.h" -
48#include "qlatincodec_p.h" -
49#include "qtsciicodec_p.h" -
50#include "qisciicodec_p.h" -
51#include "private/qcoreglobaldata_p.h" -
52#include "qdebug.h" -
53 -
54#include "unicode/ucnv.h" -
55 -
56QT_BEGIN_NAMESPACE -
57 -
58static void qIcuCodecStateFree(QTextCodec::ConverterState *state) -
59{ -
60 ucnv_close(static_cast<UConverter *>(state->d)); -
61} -
62 -
63bool qTextCodecNameMatch(const char *n, const char *h) -
64{ -
65 return ucnv_compareNames(n, h) == 0; -
66} -
67 -
68/* The list below is generated from http://www.iana.org/assignments/character-sets/ -
69 using the snippet of code below: -
70 -
71#include <QtCore> -
72#include <unicode/ucnv.h> -
73 -
74int main(int argc, char **argv) -
75{ -
76 QCoreApplication app(argc, argv); -
77 -
78 QFile file("character-sets.txt"); -
79 file.open(QFile::ReadOnly); -
80 QByteArray name; -
81 int mib = -1; -
82 QByteArray nameList; -
83 int pos = 0; -
84 while (!file.atEnd()) { -
85 QByteArray s = file.readLine().trimmed(); -
86 if (s.isEmpty()) { -
87 if (mib != -1) { -
88 UErrorCode error = U_ZERO_ERROR; -
89 const char *standard_name = ucnv_getStandardName(name, "MIME", &error); -
90 if (U_FAILURE(error) || !standard_name) { -
91 error = U_ZERO_ERROR; -
92 standard_name = ucnv_getStandardName(name, "IANA", &error); -
93 } -
94 UConverter *conv = ucnv_open(standard_name, &error); -
95 if (!U_FAILURE(error) && conv && standard_name) { -
96 ucnv_close(conv); -
97 printf(" { %d, %d },\n", mib, pos); -
98 nameList += "\""; -
99 nameList += standard_name; -
100 nameList += "\\0\"\n"; -
101 pos += strlen(standard_name) + 1; -
102 } -
103 } -
104 name = QByteArray(); -
105 mib = -1; -
106 } -
107 if (s.startsWith("Name: ")) { -
108 name = s.mid(5).trimmed(); -
109 if (name.indexOf(' ') > 0) -
110 name = name.left(name.indexOf(' ')); -
111 } -
112 if (s.startsWith("MIBenum:")) -
113 mib = s.mid(8).trimmed().toInt(); -
114 if (s.startsWith("Alias:") && s.contains("MIME")) { -
115 name = s.mid(6).trimmed(); -
116 name = name.left(name.indexOf(' ')).trimmed(); -
117 } -
118 } -
119 qDebug() << nameList; -
120} -
121*/ -
122 -
123struct MibToName { -
124 short mib; -
125 short index; -
126}; -
127 -
128static MibToName mibToName[] = { -
129 { 3, 0 }, -
130 { 4, 9 }, -
131 { 5, 20 }, -
132 { 6, 31 }, -
133 { 7, 42 }, -
134 { 8, 53 }, -
135 { 9, 64 }, -
136 { 10, 75 }, -
137 { 11, 86 }, -
138 { 12, 97 }, -
139 { 13, 108 }, -
140 { 16, 120 }, -
141 { 17, 134 }, -
142 { 18, 144 }, -
143 { 30, 151 }, -
144 { 36, 160 }, -
145 { 37, 167 }, -
146 { 38, 179 }, -
147 { 39, 186 }, -
148 { 40, 198 }, -
149 { 57, 212 }, -
150 { 81, 223 }, -
151 { 82, 234 }, -
152 { 84, 245 }, -
153 { 85, 256 }, -
154 { 104, 267 }, -
155 { 105, 279 }, -
156 { 106, 295 }, -
157 { 109, 301 }, -
158 { 110, 313 }, -
159 { 111, 325 }, -
160 { 113, 337 }, -
161 { 114, 341 }, -
162 { 1000, 349 }, -
163 { 1001, 356 }, -
164 { 1011, 363 }, -
165 { 1012, 368 }, -
166 { 1013, 374 }, -
167 { 1014, 383 }, -
168 { 1015, 392 }, -
169 { 1016, 399 }, -
170 { 1017, 406 }, -
171 { 1018, 413 }, -
172 { 1019, 422 }, -
173 { 1020, 431 }, -
174 { 2004, 438 }, -
175 { 2005, 448 }, -
176 { 2009, 472 }, -
177 { 2013, 479 }, -
178 { 2016, 486 }, -
179 { 2024, 495 }, -
180 { 2025, 505 }, -
181 { 2026, 512 }, -
182 { 2027, 517 }, -
183 { 2028, 527 }, -
184 { 2030, 534 }, -
185 { 2033, 541 }, -
186 { 2034, 548 }, -
187 { 2035, 555 }, -
188 { 2037, 562 }, -
189 { 2038, 569 }, -
190 { 2039, 576 }, -
191 { 2040, 583 }, -
192 { 2041, 590 }, -
193 { 2043, 597 }, -
194 { 2011, 604 }, -
195 { 2044, 611 }, -
196 { 2045, 618 }, -
197 { 2010, 624 }, -
198 { 2046, 631 }, -
199 { 2047, 638 }, -
200 { 2048, 645 }, -
201 { 2049, 652 }, -
202 { 2050, 659 }, -
203 { 2051, 666 }, -
204 { 2052, 673 }, -
205 { 2053, 680 }, -
206 { 2054, 687 }, -
207 { 2055, 694 }, -
208 { 2056, 701 }, -
209 { 2062, 708 }, -
210 { 2063, 715 }, -
211 { 2084, 723 }, -
212 { 2085, 730 }, -
213 { 2086, 741 }, -
214 { 2087, 748 }, -
215 { 2088, 755 }, -
216 { 2089, 762 }, -
217 { 2091, 771 }, -
218 { 2092, 780 }, -
219 { 2093, 789 }, -
220 { 2094, 798 }, -
221 { 2095, 807 }, -
222 { 2096, 816 }, -
223 { 2097, 825 }, -
224 { 2098, 834 }, -
225 { 2099, 843 }, -
226 { 2100, 852 }, -
227 { 2101, 861 }, -
228 { 2102, 872 }, -
229 { 2250, 880 }, -
230 { 2251, 893 }, -
231 { 2252, 906 }, -
232 { 2253, 919 }, -
233 { 2254, 932 }, -
234 { 2255, 945 }, -
235 { 2256, 958 }, -
236 { 2257, 971 }, -
237 { 2258, 984 }, -
238 { 2259, 997 }, -
239}; -
240int mibToNameSize = sizeof(mibToName)/sizeof(MibToName); -
241 -
242static const char mibToNameTable[] = -
243 "US-ASCII\0" -
244 "ISO-8859-1\0" -
245 "ISO-8859-2\0" -
246 "ISO-8859-3\0" -
247 "ISO-8859-4\0" -
248 "ISO-8859-5\0" -
249 "ISO-8859-6\0" -
250 "ISO-8859-7\0" -
251 "ISO-8859-8\0" -
252 "ISO-8859-9\0" -
253 "ISO-8859-10\0" -
254 "ISO-2022-JP-1\0" -
255 "Shift_JIS\0" -
256 "EUC-JP\0" -
257 "US-ASCII\0" -
258 "EUC-KR\0" -
259 "ISO-2022-KR\0" -
260 "EUC-KR\0" -
261 "ISO-2022-JP\0" -
262 "ISO-2022-JP-2\0" -
263 "GB_2312-80\0" -
264 "ISO-8859-6\0" -
265 "ISO-8859-6\0" -
266 "ISO-8859-8\0" -
267 "ISO-8859-8\0" -
268 "ISO-2022-CN\0" -
269 "ISO-2022-CN-EXT\0" -
270 "UTF-8\0" -
271 "ISO-8859-13\0" -
272 "ISO-8859-14\0" -
273 "ISO-8859-15\0" -
274 "GBK\0" -
275 "GB18030\0" -
276 "UTF-16\0" -
277 "UTF-32\0" -
278 "SCSU\0" -
279 "UTF-7\0" -
280 "UTF-16BE\0" -
281 "UTF-16LE\0" -
282 "UTF-16\0" -
283 "CESU-8\0" -
284 "UTF-32\0" -
285 "UTF-32BE\0" -
286 "UTF-32LE\0" -
287 "BOCU-1\0" -
288 "hp-roman8\0" -
289 "Adobe-Standard-Encoding\0" -
290 "IBM850\0" -
291 "IBM862\0" -
292 "IBM-Thai\0" -
293 "Shift_JIS\0" -
294 "GB2312\0" -
295 "Big5\0" -
296 "macintosh\0" -
297 "IBM037\0" -
298 "IBM273\0" -
299 "IBM277\0" -
300 "IBM278\0" -
301 "IBM280\0" -
302 "IBM284\0" -
303 "IBM285\0" -
304 "IBM290\0" -
305 "IBM297\0" -
306 "IBM420\0" -
307 "IBM424\0" -
308 "IBM437\0" -
309 "IBM500\0" -
310 "cp851\0" -
311 "IBM852\0" -
312 "IBM855\0" -
313 "IBM857\0" -
314 "IBM860\0" -
315 "IBM861\0" -
316 "IBM863\0" -
317 "IBM864\0" -
318 "IBM865\0" -
319 "IBM868\0" -
320 "IBM869\0" -
321 "IBM870\0" -
322 "IBM871\0" -
323 "IBM918\0" -
324 "IBM1026\0" -
325 "KOI8-R\0" -
326 "HZ-GB-2312\0" -
327 "IBM866\0" -
328 "IBM775\0" -
329 "KOI8-U\0" -
330 "IBM00858\0" -
331 "IBM01140\0" -
332 "IBM01141\0" -
333 "IBM01142\0" -
334 "IBM01143\0" -
335 "IBM01144\0" -
336 "IBM01145\0" -
337 "IBM01146\0" -
338 "IBM01147\0" -
339 "IBM01148\0" -
340 "IBM01149\0" -
341 "Big5-HKSCS\0" -
342 "IBM1047\0" -
343 "windows-1250\0" -
344 "windows-1251\0" -
345 "windows-1252\0" -
346 "windows-1253\0" -
347 "windows-1254\0" -
348 "windows-1255\0" -
349 "windows-1256\0" -
350 "windows-1257\0" -
351 "windows-1258\0" -
352 "TIS-620\0"; -
353 -
354static QTextCodec *loadQtCodec(const char *name) -
355{ -
356 if (!strcmp(name, "UTF-8")) -
357 return new QUtf8Codec; -
358 if (!strcmp(name, "UTF-16")) -
359 return new QUtf16Codec; -
360 if (!strcmp(name, "ISO-8859-1")) -
361 return new QLatin1Codec; -
362 if (!strcmp(name, "UTF-16BE")) -
363 return new QUtf16BECodec; -
364 if (!strcmp(name, "UTF-16LE")) -
365 return new QUtf16LECodec; -
366 if (!strcmp(name, "UTF-32")) -
367 return new QUtf32Codec; -
368 if (!strcmp(name, "UTF-32BE")) -
369 return new QUtf32BECodec; -
370 if (!strcmp(name, "UTF-32LE")) -
371 return new QUtf32LECodec; -
372#ifndef QT_NO_CODECS -
373 if (!strcmp(name, "TSCII")) -
374 return new QTsciiCodec; -
375 if (!qstrnicmp(name, "iscii", 5)) -
376 return QIsciiCodec::create(name); -
377#endif -
378 -
379 return 0; -
380} -
381 -
382/// \threadsafe -
383QList<QByteArray> QIcuCodec::availableCodecs() -
384{ -
385 QList<QByteArray> codecs; -
386 int n = ucnv_countAvailable(); -
387 for (int i = 0; i < n; ++i) { -
388 const char *name = ucnv_getAvailableName(i); -
389 -
390 UErrorCode error = U_ZERO_ERROR; -
391 const char *standardName = ucnv_getStandardName(name, "MIME", &error); -
392 if (U_FAILURE(error) || !standardName) { -
393 error = U_ZERO_ERROR; -
394 standardName = ucnv_getStandardName(name, "IANA", &error); -
395 } -
396 if (U_FAILURE(error)) -
397 continue; -
398 -
399 error = U_ZERO_ERROR; -
400 int ac = ucnv_countAliases(standardName, &error); -
401 if (U_FAILURE(error)) -
402 continue; -
403 for (int j = 0; j < ac; ++j) { -
404 error = U_ZERO_ERROR; -
405 const char *alias = ucnv_getAlias(standardName, j, &error); -
406 if (!U_SUCCESS(error)) -
407 continue; -
408 codecs += alias; -
409 } -
410 } -
411 -
412 // handled by Qt and not in ICU: -
413 codecs += "TSCII"; -
414 -
415 return codecs; -
416} -
417 -
418/// \threadsafe -
419QList<int> QIcuCodec::availableMibs() -
420{ -
421 QList<int> mibs; -
422 for (int i = 0; i < mibToNameSize; ++i) -
423 mibs += mibToName[i].mib; -
424 -
425 // handled by Qt and not in ICU: -
426 mibs += 2107; // TSCII -
427 -
428 return mibs; -
429} -
430 -
431QTextCodec *QIcuCodec::defaultCodecUnlocked() -
432{ -
433 QCoreGlobalData *globalData = QCoreGlobalData::instance(); -
434 if (!globalData) -
435 return 0; -
436 QTextCodec *c = globalData->codecForLocale.loadAcquire(); -
437 if (c) -
438 return c; -
439 -
440#if defined(QT_LOCALE_IS_UTF8) -
441 const char *name = "UTF-8"; -
442#else -
443 const char *name = ucnv_getDefaultName(); -
444#endif -
445 c = codecForNameUnlocked(name); -
446 globalData->codecForLocale.storeRelease(c); -
447 return c; -
448} -
449 -
450 -
451QTextCodec *QIcuCodec::codecForNameUnlocked(const char *name) -
452{ -
453 // backwards compatibility with Qt 4.x -
454 if (!qstrcmp(name, "CP949"))
evaluated: !qstrcmp(name, "CP949")
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:11296
1-11296
455 name = "windows-949";
executed: name = "windows-949";
Execution Count:1
1
456 // this one is broken data in ICU 4.4, and can't be resolved even though it's an alias to tis-620-
/ these are broken data in ICU 4.4, and can't be resolved even though they are aliases to tis-620
457 if (!qstrcmp(name, "windows-874-2000")
evaluated: !qstrcmp(name, "windows-874-2000")
TRUEFALSE
yes
Evaluation Count:7
yes
Evaluation Count:11290
7-11290
458 || !qstrcmp(name, "windows-874")
evaluated: !qstrcmp(name, "windows-874")
TRUEFALSE
yes
Evaluation Count:7
yes
Evaluation Count:11283
7-11283
459 || !qstrcmp(name, "MS874")
evaluated: !qstrcmp(name, "MS874")
TRUEFALSE
yes
Evaluation Count:7
yes
Evaluation Count:11276
7-11276
460 || !qstrcmp(name, "x-windows-874"))
partially evaluated: !qstrcmp(name, "x-windows-874")
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:11276
0-11276
461 name = "TIS-620";
executed: name = "TIS-620";
Execution Count:21
21
462 -
463 UErrorCode error = U_ZERO_ERROR;
executed (the execution status of this line is deduced): UErrorCode error = U_ZERO_ERROR;
-
464 // MIME gives better default names -
465 const char *standardName = ucnv_getStandardName(name, "MIME", &error);
executed (the execution status of this line is deduced): const char *standardName = ucnv_getStandardName_44(name, "MIME", &error);
-
466 if (U_FAILURE(error) || !standardName) {
evaluated: U_FAILURE(error)
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:11296
evaluated: !standardName
TRUEFALSE
yes
Evaluation Count:3469
yes
Evaluation Count:7827
1-11296
467 error = U_ZERO_ERROR;
executed (the execution status of this line is deduced): error = U_ZERO_ERROR;
-
468 standardName = ucnv_getStandardName(name, "IANA", &error);
executed (the execution status of this line is deduced): standardName = ucnv_getStandardName_44(name, "IANA", &error);
-
469 }
executed: }
Execution Count:3470
3470
470 bool qt_only = false;
executed (the execution status of this line is deduced): bool qt_only = false;
-
471 if (U_FAILURE(error) || !standardName) {
evaluated: U_FAILURE(error)
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:11296
evaluated: !standardName
TRUEFALSE
yes
Evaluation Count:74
yes
Evaluation Count:11222
1-11296
472 standardName = name;
executed (the execution status of this line is deduced): standardName = name;
-
473 qt_only = true;
executed (the execution status of this line is deduced): qt_only = true;
-
474 } else {
executed: }
Execution Count:75
75
475 // correct some issues where the ICU data set contains duplicated entries. -
476 // Where this happens it's because one data set is a subset of another. We -
477 // always use the larger data set. -
478 -
479 if (qstrcmp(standardName, "GB2312") == 0 || qstrcmp(standardName, "GB_2312-80") == 0)
evaluated: qstrcmp(standardName, "GB2312") == 0
TRUEFALSE
yes
Evaluation Count:32
yes
Evaluation Count:11190
evaluated: qstrcmp(standardName, "GB_2312-80") == 0
TRUEFALSE
yes
Evaluation Count:25
yes
Evaluation Count:11165
25-11190
480 standardName = "GBK";
executed: standardName = "GBK";
Execution Count:57
57
481 else if (qstrcmp(standardName, "KSC_5601") == 0 || qstrcmp(standardName, "EUC-KR") == 0 || qstrcmp(standardName, "cp1363") == 0)
partially evaluated: qstrcmp(standardName, "KSC_5601") == 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:11165
evaluated: qstrcmp(standardName, "EUC-KR") == 0
TRUEFALSE
yes
Evaluation Count:99
yes
Evaluation Count:11066
evaluated: qstrcmp(standardName, "cp1363") == 0
TRUEFALSE
yes
Evaluation Count:157
yes
Evaluation Count:10909
0-11165
482 standardName = "windows-949";
executed: standardName = "windows-949";
Execution Count:256
256
483 } -
484 -
485 QCoreGlobalData *globalData = QCoreGlobalData::instance();
executed (the execution status of this line is deduced): QCoreGlobalData *globalData = QCoreGlobalData::instance();
-
486 QTextCodecCache *cache = &globalData->codecCache;
executed (the execution status of this line is deduced): QTextCodecCache *cache = &globalData->codecCache;
-
487 -
488 QTextCodec *codec;
executed (the execution status of this line is deduced): QTextCodec *codec;
-
489 if (cache) {
partially evaluated: cache
TRUEFALSE
yes
Evaluation Count:11297
no
Evaluation Count:0
0-11297
490 codec = cache->value(standardName);
executed (the execution status of this line is deduced): codec = cache->value(standardName);
-
491 if (codec)
evaluated: codec
TRUEFALSE
yes
Evaluation Count:10998
yes
Evaluation Count:299
299-10998
492 return codec;
executed: return codec;
Execution Count:10998
10998
493 }
executed: }
Execution Count:299
299
494 -
495 for (int i = 0; i < globalData->allCodecs.size(); ++i) {
evaluated: i < globalData->allCodecs.size()
TRUEFALSE
yes
Evaluation Count:7533
yes
Evaluation Count:266
266-7533
496 QTextCodec *cursor = globalData->allCodecs.at(i);
executed (the execution status of this line is deduced): QTextCodec *cursor = globalData->allCodecs.at(i);
-
497 if (qTextCodecNameMatch(cursor->name(), standardName)) {
evaluated: qTextCodecNameMatch(cursor->name(), standardName)
TRUEFALSE
yes
Evaluation Count:31
yes
Evaluation Count:7502
31-7502
498 if (cache)
partially evaluated: cache
TRUEFALSE
yes
Evaluation Count:31
no
Evaluation Count:0
0-31
499 cache->insert(standardName, cursor);
executed: cache->insert(standardName, cursor);
Execution Count:31
31
500 return cursor;
executed: return cursor;
Execution Count:31
31
501 } -
502 QList<QByteArray> aliases = cursor->aliases();
executed (the execution status of this line is deduced): QList<QByteArray> aliases = cursor->aliases();
-
503 for (int y = 0; y < aliases.size(); ++y)
evaluated: y < aliases.size()
TRUEFALSE
yes
Evaluation Count:39694
yes
Evaluation Count:7500
7500-39694
504 if (qTextCodecNameMatch(aliases.at(y), standardName)) {
evaluated: qTextCodecNameMatch(aliases.at(y), standardName)
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:39692
2-39692
505 if (cache)
partially evaluated: cache
TRUEFALSE
yes
Evaluation Count:2
no
Evaluation Count:0
0-2
506 cache->insert(standardName, cursor);
executed: cache->insert(standardName, cursor);
Execution Count:2
2
507 return cursor;
executed: return cursor;
Execution Count:2
2
508 } -
509 }
executed: }
Execution Count:7500
7500
510 -
511 QTextCodec *c = loadQtCodec(standardName);
executed (the execution status of this line is deduced): QTextCodec *c = loadQtCodec(standardName);
-
512 if (c)
evaluated: c
TRUEFALSE
yes
Evaluation Count:155
yes
Evaluation Count:111
111-155
513 return c;
executed: return c;
Execution Count:155
155
514 -
515 if (qt_only)
evaluated: qt_only
TRUEFALSE
yes
Evaluation Count:10
yes
Evaluation Count:101
10-101
516 return 0;
executed: return 0;
Execution Count:10
10
517 -
518 // check whether there is really a converter for the name available. -
519 UConverter *conv = ucnv_open(standardName, &error);
executed (the execution status of this line is deduced): UConverter *conv = ucnv_open_44(standardName, &error);
-
520 if (!conv) {
partially evaluated: !conv
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:101
0-101
521 qDebug() << "codecForName: ucnv_open failed" << standardName << u_errorName(error);
never executed (the execution status of this line is deduced): QMessageLogger("codecs/qicucodec.cpp", 521, __PRETTY_FUNCTION__).debug() << "codecForName: ucnv_open failed" << standardName << u_errorName_44(error);
-
522 return 0;
never executed: return 0;
0
523 } -
524 //qDebug() << "QIcuCodec: Standard name for " << name << "is" << standardName; -
525 ucnv_close(conv);
executed (the execution status of this line is deduced): ucnv_close_44(conv);
-
526 -
527 -
528 c = new QIcuCodec(standardName);
executed (the execution status of this line is deduced): c = new QIcuCodec(standardName);
-
529 if (cache)
partially evaluated: cache
TRUEFALSE
yes
Evaluation Count:101
no
Evaluation Count:0
0-101
530 cache->insert(standardName, c);
executed: cache->insert(standardName, c);
Execution Count:101
101
531 return c;
executed: return c;
Execution Count:101
101
532} -
533 -
534 -
535QTextCodec *QIcuCodec::codecForMibUnlocked(int mib) -
536{ -
537 for (int i = 0; i < mibToNameSize; ++i) { -
538 if (mibToName[i].mib == mib) -
539 return codecForNameUnlocked(mibToNameTable + mibToName[i].index); -
540 } -
541 -
542 if (mib == 2107) -
543 return codecForNameUnlocked("TSCII"); -
544 -
545 return 0; -
546} -
547 -
548 -
549QIcuCodec::QIcuCodec(const char *name) -
550 : m_name(name) -
551{ -
552} -
553 -
554QIcuCodec::~QIcuCodec() -
555{ -
556} -
557 -
558UConverter *QIcuCodec::getConverter(QTextCodec::ConverterState *state) const -
559{ -
560 UConverter *conv = 0; -
561 if (state) { -
562 if (!state->d) { -
563 // first time -
564 state->flags |= QTextCodec::FreeFunction; -
565 QTextCodecUnalignedPointer::encode(state->state_data, qIcuCodecStateFree); -
566 UErrorCode error = U_ZERO_ERROR; -
567 state->d = ucnv_open(m_name, &error); -
568 ucnv_setSubstChars(static_cast<UConverter *>(state->d), -
569 state->flags & QTextCodec::ConvertInvalidToNull ? "\0" : "?", 1, &error); -
570 if (U_FAILURE(error)) -
571 qDebug() << "getConverter(state) ucnv_open failed" << m_name << u_errorName(error); -
572 } -
573 conv = static_cast<UConverter *>(state->d); -
574 } -
575 if (!conv) { -
576 // stateless conversion -
577 UErrorCode error = U_ZERO_ERROR; -
578 conv = ucnv_open(m_name, &error); -
579 ucnv_setSubstChars(conv, "?", 1, &error); -
580 if (U_FAILURE(error)) -
581 qDebug() << "getConverter(no state) ucnv_open failed" << m_name << u_errorName(error); -
582 } -
583 return conv; -
584} -
585 -
586QString QIcuCodec::convertToUnicode(const char *chars, int length, QTextCodec::ConverterState *state) const -
587{ -
588 UConverter *conv = getConverter(state); -
589 -
590 QString string(length + 2, Qt::Uninitialized); -
591 -
592 const char *end = chars + length; -
593 int convertedChars = 0; -
594 while (1) { -
595 UChar *uc = (UChar *)string.data(); -
596 UChar *ucEnd = uc + string.length(); -
597 uc += convertedChars; -
598 UErrorCode error = U_ZERO_ERROR; -
599 ucnv_toUnicode(conv, -
600 &uc, ucEnd, -
601 &chars, end, -
602 0, false, &error); -
603 if (!U_SUCCESS(error) && error != U_BUFFER_OVERFLOW_ERROR) { -
604 qDebug() << "convertToUnicode failed:" << u_errorName(error); -
605 break; -
606 } -
607 -
608 convertedChars = uc - (UChar *)string.data(); -
609 if (chars >= end) -
610 break; -
611 string.resize(string.length()*2); -
612 } -
613 string.resize(convertedChars); -
614 -
615 if (!state) -
616 ucnv_close(conv); -
617 return string; -
618} -
619 -
620 -
621QByteArray QIcuCodec::convertFromUnicode(const QChar *unicode, int length, QTextCodec::ConverterState *state) const -
622{ -
623 UConverter *conv = getConverter(state); -
624 -
625 int requiredLength = UCNV_GET_MAX_BYTES_FOR_STRING(length, ucnv_getMaxCharSize(conv)); -
626 QByteArray string(requiredLength, Qt::Uninitialized); -
627 -
628 const UChar *uc = (const UChar *)unicode; -
629 const UChar *end = uc + length; -
630 int convertedChars = 0; -
631 while (1) { -
632 char *ch = (char *)string.data(); -
633 char *chEnd = ch + string.length(); -
634 ch += convertedChars; -
635 UErrorCode error = U_ZERO_ERROR; -
636 ucnv_fromUnicode(conv, -
637 &ch, chEnd, -
638 &uc, end, -
639 0, false, &error); -
640 if (!U_SUCCESS(error)) -
641 qDebug() << "convertFromUnicode failed:" << u_errorName(error); -
642 convertedChars = ch - string.data(); -
643 if (uc >= end) -
644 break; -
645 string.resize(string.length()*2); -
646 } -
647 string.resize(convertedChars); -
648 -
649 if (!state) -
650 ucnv_close(conv); -
651 -
652 return string; -
653} -
654 -
655 -
656QByteArray QIcuCodec::name() const -
657{ -
658 return m_name; -
659} -
660 -
661 -
662QList<QByteArray> QIcuCodec::aliases() const -
663{ -
664 UErrorCode error = U_ZERO_ERROR; -
665 -
666 int n = ucnv_countAliases(m_name, &error); -
667 -
668 QList<QByteArray> aliases; -
669 for (int i = 0; i < n; ++i) { -
670 const char *a = ucnv_getAlias(m_name, i, &error); -
671 // skip the canonical name -
672 if (!a || !qstrcmp(a, m_name)) -
673 continue; -
674 aliases += a; -
675 } -
676 -
677 return aliases; -
678} -
679 -
680 -
681int QIcuCodec::mibEnum() const -
682{ -
683 for (int i = 0; i < mibToNameSize; ++i) { -
684 if (qTextCodecNameMatch(m_name, (mibToNameTable + mibToName[i].index))) -
685 return mibToName[i].mib; -
686 } -
687 -
688 return 0; -
689} -
690 -
691QT_END_NAMESPACE -
692 -
693#endif // QT_NO_TEXTCODEC -
694 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial