Qt 4.8
qtextcodec.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 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 "qvarlengtharray.h"
51 #ifndef QT_NO_LIBRARY
52 # include "qcoreapplication.h"
53 # include "qtextcodecplugin.h"
54 # include "private/qfactoryloader_p.h"
55 #endif
56 #include "qstringlist.h"
57 
58 #ifdef Q_OS_UNIX
59 # include "qiconvcodec_p.h"
60 #endif
61 
62 #include "qutfcodec_p.h"
63 #include "qsimplecodec_p.h"
64 #include "qlatincodec_p.h"
65 #ifndef QT_NO_CODECS
66 # include "qtsciicodec_p.h"
67 # include "qisciicodec_p.h"
68 #if !defined(Q_OS_SYMBIAN) && !defined(Q_OS_INTEGRITY)
69 # if defined(QT_NO_ICONV) && !defined(QT_BOOTSTRAPPED) && !defined(QT_CODEC_PLUGINS)
70 // no iconv(3) support, must build all codecs into the library
71 # include "../../plugins/codecs/cn/qgb18030codec.h"
72 # include "../../plugins/codecs/jp/qeucjpcodec.h"
73 # include "../../plugins/codecs/jp/qjiscodec.h"
74 # include "../../plugins/codecs/jp/qsjiscodec.h"
75 # include "../../plugins/codecs/kr/qeuckrcodec.h"
76 # include "../../plugins/codecs/tw/qbig5codec.h"
77 # endif // QT_NO_ICONV && !QT_BOOTSTRAPPED && !QT_CODEC_PLUGINS
78 # if defined(Q_WS_X11) && !defined(QT_BOOTSTRAPPED)
79 # include "qfontlaocodec_p.h"
80 # include "../../plugins/codecs/jp/qfontjpcodec.h"
81 # endif
82 #endif // QT_NO_SYMBIAN
83 #endif // QT_NO_CODECS
84 #include "qlocale.h"
85 #include "qmutex.h"
86 #include "qhash.h"
87 
88 #include <stdlib.h>
89 #include <ctype.h>
90 #include <locale.h>
91 #if defined (_XOPEN_UNIX) && !defined(Q_OS_QNX) && !defined(Q_OS_OSF)
92 #include <langinfo.h>
93 #endif
94 
95 #if defined(Q_OS_WINCE)
96 # define QT_NO_SETLOCALE
97 #endif
98 
99 #ifdef Q_OS_SYMBIAN
100 #include "qtextcodec_symbian.cpp"
101 #endif
102 
103 
104 // enabling this is not exception safe!
105 // #define Q_DEBUG_TEXTCODEC
106 
108 
109 #if !defined(QT_NO_LIBRARY) && !defined(QT_NO_TEXTCODECPLUGIN)
112 #endif
113 
114 //Cache for QTextCodec::codecForName and codecForMib.
116 Q_GLOBAL_STATIC(QTextCodecCache, qTextCodecCache)
117 
118 
119 static char qtolower(register char c)
120 { if (c >= 'A' && c <= 'Z') return c + 0x20; return c; }
121 static bool qisalnum(register char c)
122 { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); }
123 
124 static bool nameMatch(const QByteArray &name, const QByteArray &test)
125 {
126  // if they're the same, return a perfect score
127  if (qstricmp(name, test) == 0)
128  return true;
129 
130  const char *n = name.constData();
131  const char *h = test.constData();
132 
133  // if the letters and numbers are the same, we have a match
134  while (*n != '\0') {
135  if (qisalnum(*n)) {
136  for (;;) {
137  if (*h == '\0')
138  return false;
139  if (qisalnum(*h))
140  break;
141  ++h;
142  }
143  if (qtolower(*n) != qtolower(*h))
144  return false;
145  ++h;
146  }
147  ++n;
148  }
149  while (*h && !qisalnum(*h))
150  ++h;
151  return (*h == '\0');
152 }
153 
154 
156 {
157 #if !defined(QT_NO_LIBRARY) && !defined(QT_NO_TEXTCODECPLUGIN)
158  QFactoryLoader *l = loader();
159  QStringList keys = l->keys();
160  for (int i = 0; i < keys.size(); ++i) {
161  if (nameMatch(name, keys.at(i).toLatin1())) {
162  QString realName = keys.at(i);
163  if (QTextCodecFactoryInterface *factory
164  = qobject_cast<QTextCodecFactoryInterface*>(l->instance(realName))) {
165  return factory->create(realName);
166  }
167  }
168  }
169 #else
170  Q_UNUSED(name);
171 #endif
172  return 0;
173 }
174 
176 {
177 #ifndef QT_NO_TEXTCODECPLUGIN
178  QString name = QLatin1String("MIB: ") + QString::number(mib);
179  if (QTextCodecFactoryInterface *factory
180  = qobject_cast<QTextCodecFactoryInterface*>(loader()->instance(name)))
181  return factory->create(name);
182 #else
183  Q_UNUSED(mib);
184 #endif
185  return 0;
186 }
187 
189 #ifdef Q_DEBUG_TEXTCODEC
190 static bool destroying_is_ok = false;
191 #endif
192 
195 
196 
198 {
199 public:
201 };
202 
203 /*
204  Deletes all the created codecs. This destructor is called just
205  before exiting to delete any QTextCodec objects that may be lying
206  around.
207 */
209 {
210  if (!all)
211  return;
212 
213 #ifdef Q_DEBUG_TEXTCODEC
214  destroying_is_ok = true;
215 #endif
216 
217  QList<QTextCodec *> *myAll = all;
218  all = 0; // Otherwise the d'tor destroys the iterator
220  ; it != myAll->constEnd(); ++it) {
221  delete *it;
222  }
223  delete myAll;
224  localeMapper = 0;
225 
226 #ifdef Q_DEBUG_TEXTCODEC
227  destroying_is_ok = false;
228 #endif
229 }
230 
231 Q_GLOBAL_STATIC(QTextCodecCleanup, createQTextCodecCleanup)
232 
233 bool QTextCodec::validCodecs()
234 {
235 #ifdef Q_OS_SYMBIAN
236  // If we don't have a trap handler, we're outside of the main() function,
237  // ie. in global constructors or destructors. Don't use codecs in this
238  // case as it would lead to crashes because we don't have a cleanup stack on Symbian
239  return (User::TrapHandler() != NULL);
240 #else
241  return true;
242 #endif
243 }
244 
245 
246 #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
248 {
249 public:
252 
253  QString convertToUnicode(const char *, int, ConverterState *) const;
254  QByteArray convertFromUnicode(const QChar *, int, ConverterState *) const;
255  QString convertToUnicodeCharByChar(const char *chars, int length, ConverterState *state) const;
256 
257  QByteArray name() const;
258  int mibEnum() const;
259 
260 };
261 
263 {
264 }
265 
267 {
268 }
269 
270 QString QWindowsLocalCodec::convertToUnicode(const char *chars, int length, ConverterState *state) const
271 {
272  const char *mb = chars;
273  int mblen = length;
274 
275  if (!mb || !mblen)
276  return QString();
277 
279  int len;
280  QString sp;
281  bool prepend = false;
282  char state_data = 0;
283  int remainingChars = 0;
284 
285  //save the current state information
286  if (state) {
287  state_data = (char)state->state_data[0];
288  remainingChars = state->remainingChars;
289  }
290 
291  //convert the pending charcter (if available)
292  if (state && remainingChars) {
293  char prev[3] = {0};
294  prev[0] = state_data;
295  prev[1] = mb[0];
296  remainingChars = 0;
297  len = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,
298  prev, 2, wc.data(), wc.size());
299  if (len) {
300  prepend = true;
301  sp.append(QChar(wc[0]));
302  mb++;
303  mblen--;
304  wc[0] = 0;
305  }
306  }
307 
308  while (!(len=MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED|MB_ERR_INVALID_CHARS,
309  mb, mblen, wc.data(), wc.size()))) {
310  int r = GetLastError();
311  if (r == ERROR_INSUFFICIENT_BUFFER) {
312  const int wclen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,
313  mb, mblen, 0, 0);
314  wc.resize(wclen);
315  } else if (r == ERROR_NO_UNICODE_TRANSLATION) {
316  //find the last non NULL character
317  while (mblen > 1 && !(mb[mblen-1]))
318  mblen--;
319  //check whether, we hit an invalid character in the middle
320  if ((mblen <= 1) || (remainingChars && state_data))
321  return convertToUnicodeCharByChar(chars, length, state);
322  //Remove the last character and try again...
323  state_data = mb[mblen-1];
324  remainingChars = 1;
325  mblen--;
326  } else {
327  // Fail.
328  qWarning("MultiByteToWideChar: Cannot convert multibyte text");
329  break;
330  }
331  }
332 
333  if (len <= 0)
334  return QString();
335 
336  if (wc[len-1] == 0) // len - 1: we don't want terminator
337  --len;
338 
339  //save the new state information
340  if (state) {
341  state->state_data[0] = (char)state_data;
342  state->remainingChars = remainingChars;
343  }
344  QString s((QChar*)wc.data(), len);
345  if (prepend) {
346  return sp+s;
347  }
348  return s;
349 }
350 
351 QString QWindowsLocalCodec::convertToUnicodeCharByChar(const char *chars, int length, ConverterState *state) const
352 {
353  if (!chars || !length)
354  return QString();
355 
356  int copyLocation = 0;
357  int extra = 2;
358  if (state && state->remainingChars) {
359  copyLocation = state->remainingChars;
360  extra += copyLocation;
361  }
362  int newLength = length + extra;
363  char *mbcs = new char[newLength];
364  //ensure that we have a NULL terminated string
365  mbcs[newLength-1] = 0;
366  mbcs[newLength-2] = 0;
367  memcpy(&(mbcs[copyLocation]), chars, length);
368  if (copyLocation) {
369  //copy the last character from the state
370  mbcs[0] = (char)state->state_data[0];
371  state->remainingChars = 0;
372  }
373  const char *mb = mbcs;
374 #ifndef Q_OS_WINCE
375  const char *next = 0;
376  QString s;
377  while((next = CharNextExA(CP_ACP, mb, 0)) != mb) {
378  wchar_t wc[2] ={0};
379  int charlength = next - mb;
380  int len = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED|MB_ERR_INVALID_CHARS, mb, charlength, wc, 2);
381  if (len>0) {
382  s.append(QChar(wc[0]));
383  } else {
384  int r = GetLastError();
385  //check if the character being dropped is the last character
386  if (r == ERROR_NO_UNICODE_TRANSLATION && mb == (mbcs+newLength -3) && state) {
387  state->remainingChars = 1;
388  state->state_data[0] = (char)*mb;
389  }
390  }
391  mb = next;
392  }
393 #else
394  QString s;
395  int size = mbstowcs(NULL, mb, length);
396  if (size < 0) {
397  Q_ASSERT("Error in CE TextCodec");
398  return QString();
399  }
400  wchar_t* ws = new wchar_t[size + 2];
401  ws[size +1] = 0;
402  ws[size] = 0;
403  size = mbstowcs(ws, mb, length);
404  for (int i=0; i< size; i++)
405  s.append(QChar(ws[i]));
406  delete [] ws;
407 #endif
408  delete [] mbcs;
409  return s;
410 }
411 
413 {
414  if (!ch)
415  return QByteArray();
416  if (uclen == 0)
417  return QByteArray("");
418  BOOL used_def;
419  QByteArray mb(4096, 0);
420  int len;
421  while (!(len=WideCharToMultiByte(CP_ACP, 0, (const wchar_t*)ch, uclen,
422  mb.data(), mb.size()-1, 0, &used_def)))
423  {
424  int r = GetLastError();
425  if (r == ERROR_INSUFFICIENT_BUFFER) {
426  mb.resize(1+WideCharToMultiByte(CP_ACP, 0,
427  (const wchar_t*)ch, uclen,
428  0, 0, 0, &used_def));
429  // and try again...
430  } else {
431 #ifndef QT_NO_DEBUG
432  // Fail.
433  qWarning("WideCharToMultiByte: Cannot convert multibyte text (error %d): %s (UTF-8)",
434  r, QString(ch, uclen).toLocal8Bit().data());
435 #endif
436  break;
437  }
438  }
439  mb.resize(len);
440  return mb;
441 }
442 
443 
445 {
446  return "System";
447 }
448 
450 {
451  return 0;
452 }
453 
454 #else
455 
456 /* locale names mostly copied from XFree86 */
457 static const char * const iso8859_2locales[] = {
458  "croatian", "cs", "cs_CS", "cs_CZ","cz", "cz_CZ", "czech", "hr",
459  "hr_HR", "hu", "hu_HU", "hungarian", "pl", "pl_PL", "polish", "ro",
460  "ro_RO", "rumanian", "serbocroatian", "sh", "sh_SP", "sh_YU", "sk",
461  "sk_SK", "sl", "sl_CS", "sl_SI", "slovak", "slovene", "sr_SP", 0 };
462 
463 static const char * const iso8859_3locales[] = {
464  "eo", 0 };
465 
466 static const char * const iso8859_4locales[] = {
467  "ee", "ee_EE", 0 };
468 
469 static const char * const iso8859_5locales[] = {
470  "mk", "mk_MK", "sp", "sp_YU", 0 };
471 
472 static const char * const cp_1251locales[] = {
473  "be", "be_BY", "bg", "bg_BG", "bulgarian", 0 };
474 
475 static const char * const pt_154locales[] = {
476  "ba_RU", "ky", "ky_KG", "kk", "kk_KZ", 0 };
477 
478 static const char * const iso8859_6locales[] = {
479  "ar_AA", "ar_SA", "arabic", 0 };
480 
481 static const char * const iso8859_7locales[] = {
482  "el", "el_GR", "greek", 0 };
483 
484 static const char * const iso8859_8locales[] = {
485  "hebrew", "he", "he_IL", "iw", "iw_IL", 0 };
486 
487 static const char * const iso8859_9locales[] = {
488  "tr", "tr_TR", "turkish", 0 };
489 
490 static const char * const iso8859_13locales[] = {
491  "lt", "lt_LT", "lv", "lv_LV", 0 };
492 
493 static const char * const iso8859_15locales[] = {
494  "et", "et_EE",
495  // Euro countries
496  "br_FR", "ca_ES", "de", "de_AT", "de_BE", "de_DE", "de_LU", "en_IE",
497  "es", "es_ES", "eu_ES", "fi", "fi_FI", "finnish", "fr", "fr_FR",
498  "fr_BE", "fr_LU", "french", "ga_IE", "gl_ES", "it", "it_IT", "oc_FR",
499  "nl", "nl_BE", "nl_NL", "pt", "pt_PT", "sv_FI", "wa_BE",
500  0 };
501 
502 static const char * const koi8_ulocales[] = {
503  "uk", "uk_UA", "ru_UA", "ukrainian", 0 };
504 
505 static const char * const tis_620locales[] = {
506  "th", "th_TH", "thai", 0 };
507 
508 // static const char * const tcvnlocales[] = {
509 // "vi", "vi_VN", 0 };
510 
511 static bool try_locale_list(const char * const locale[], const QByteArray &lang)
512 {
513  int i;
514  for(i=0; locale[i] && lang != locale[i]; i++)
515  ;
516  return locale[i] != 0;
517 }
518 
519 // For the probably_koi8_locales we have to look. the standard says
520 // these are 8859-5, but almost all Russian users use KOI8-R and
521 // incorrectly set $LANG to ru_RU. We'll check tolower() to see what
522 // it thinks ru_RU means.
523 
524 // If you read the history, it seems that many Russians blame ISO and
525 // Perestroika for the confusion.
526 //
527 // The real bug is that some programs break if the user specifies
528 // ru_RU.KOI8-R.
529 
530 static const char * const probably_koi8_rlocales[] = {
531  "ru", "ru_SU", "ru_RU", "russian", 0 };
532 
533 static QTextCodec * ru_RU_hack(const char * i) {
534  QTextCodec * ru_RU_codec = 0;
535 
536 #if !defined(QT_NO_SETLOCALE)
537  QByteArray origlocale(setlocale(LC_CTYPE, i));
538 #else
539  QByteArray origlocale(i);
540 #endif
541  // unicode koi8r latin5 name
542  // 0x044E 0xC0 0xEE CYRILLIC SMALL LETTER YU
543  // 0x042E 0xE0 0xCE CYRILLIC CAPITAL LETTER YU
544  int latin5 = tolower(0xCE);
545  int koi8r = tolower(0xE0);
546  if (koi8r == 0xC0 && latin5 != 0xEE) {
547  ru_RU_codec = QTextCodec::codecForName("KOI8-R");
548  } else if (koi8r != 0xC0 && latin5 == 0xEE) {
549  ru_RU_codec = QTextCodec::codecForName("ISO 8859-5");
550  } else {
551  // something else again... let's assume... *throws dice*
552  ru_RU_codec = QTextCodec::codecForName("KOI8-R");
553  qWarning("QTextCodec: Using KOI8-R, probe failed (%02x %02x %s)",
554  koi8r, latin5, i);
555  }
556 #if !defined(QT_NO_SETLOCALE)
557  setlocale(LC_CTYPE, origlocale);
558 #endif
559 
560  return ru_RU_codec;
561 }
562 
563 #endif
564 
565 #if !defined(Q_OS_WIN32) && !defined(Q_OS_WINCE)
566 static QTextCodec *checkForCodec(const QByteArray &name) {
568  if (!c) {
569  const int index = name.indexOf('@');
570  if (index != -1) {
571  c = QTextCodec::codecForName(name.left(index));
572  }
573  }
574  return c;
575 }
576 #endif
577 
578 /* the next two functions are implicitely thread safe,
579  as they are only called by setup() which uses a mutex.
580 */
581 static void setupLocaleMapper()
582 {
583 #ifdef Q_OS_SYMBIAN
585  if (localeMapper)
586  return;
587 #endif
588 
589 #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
591 #else
592 
593 #ifndef QT_NO_ICONV
595 #endif
596 
597 #if defined (_XOPEN_UNIX) && !defined(Q_OS_QNX) && !defined(Q_OS_OSF)
598  if (!localeMapper) {
599  char *charset = nl_langinfo (CODESET);
600  if (charset)
602  }
603 #endif
604 
605  if (!localeMapper) {
606  // Very poorly defined and followed standards causes lots of
607  // code to try to get all the cases... This logic is
608  // duplicated in QIconvCodec, so if you change it here, change
609  // it there too.
610 
611  // Try to determine locale codeset from locale name assigned to
612  // LC_CTYPE category.
613 
614  // First part is getting that locale name. First try setlocale() which
615  // definitely knows it, but since we cannot fully trust it, get ready
616  // to fall back to environment variables.
617 #if !defined(QT_NO_SETLOCALE)
618  const QByteArray ctype = setlocale(LC_CTYPE, 0);
619 #else
620  const QByteArray ctype;
621 #endif
622 
623  // Get the first nonempty value from $LC_ALL, $LC_CTYPE, and $LANG
624  // environment variables.
625  QByteArray lang = qgetenv("LC_ALL");
626  if (lang.isEmpty() || lang == "C") {
627  lang = qgetenv("LC_CTYPE");
628  }
629  if (lang.isEmpty() || lang == "C") {
630  lang = qgetenv("LANG");
631  }
632 
633  // Now try these in order:
634  // 1. CODESET from ctype if it contains a .CODESET part (e.g. en_US.ISO8859-15)
635  // 2. CODESET from lang if it contains a .CODESET part
636  // 3. ctype (maybe the locale is named "ISO-8859-1" or something)
637  // 4. locale (ditto)
638  // 5. check for "@euro"
639  // 6. guess locale from ctype unless ctype is "C"
640  // 7. guess locale from lang
641 
642  // 1. CODESET from ctype if it contains a .CODESET part (e.g. en_US.ISO8859-15)
643  int indexOfDot = ctype.indexOf('.');
644  if (indexOfDot != -1)
645  localeMapper = checkForCodec( ctype.mid(indexOfDot + 1) );
646 
647  // 2. CODESET from lang if it contains a .CODESET part
648  if (!localeMapper) {
649  indexOfDot = lang.indexOf('.');
650  if (indexOfDot != -1)
651  localeMapper = checkForCodec( lang.mid(indexOfDot + 1) );
652  }
653 
654  // 3. ctype (maybe the locale is named "ISO-8859-1" or something)
655  if (!localeMapper && !ctype.isEmpty() && ctype != "C")
656  localeMapper = checkForCodec(ctype);
657 
658  // 4. locale (ditto)
659  if (!localeMapper && !lang.isEmpty())
660  localeMapper = checkForCodec(lang);
661 
662  // 5. "@euro"
663  if ((!localeMapper && ctype.contains("@euro")) || lang.contains("@euro"))
664  localeMapper = checkForCodec("ISO 8859-15");
665 
666  // 6. guess locale from ctype unless ctype is "C"
667  // 7. guess locale from lang
668  const QByteArray &try_by_name = (!ctype.isEmpty() && ctype != "C") ? lang : ctype;
669 
670  // Now do the guessing.
671  if (!lang.isEmpty() && !localeMapper && !try_by_name.isEmpty()) {
672  if (try_locale_list(iso8859_15locales, lang))
673  localeMapper = QTextCodec::codecForName("ISO 8859-15");
674  else if (try_locale_list(iso8859_2locales, lang))
675  localeMapper = QTextCodec::codecForName("ISO 8859-2");
676  else if (try_locale_list(iso8859_3locales, lang))
677  localeMapper = QTextCodec::codecForName("ISO 8859-3");
678  else if (try_locale_list(iso8859_4locales, lang))
679  localeMapper = QTextCodec::codecForName("ISO 8859-4");
680  else if (try_locale_list(iso8859_5locales, lang))
681  localeMapper = QTextCodec::codecForName("ISO 8859-5");
682  else if (try_locale_list(iso8859_6locales, lang))
683  localeMapper = QTextCodec::codecForName("ISO 8859-6");
684  else if (try_locale_list(iso8859_7locales, lang))
685  localeMapper = QTextCodec::codecForName("ISO 8859-7");
686  else if (try_locale_list(iso8859_8locales, lang))
687  localeMapper = QTextCodec::codecForName("ISO 8859-8-I");
688  else if (try_locale_list(iso8859_9locales, lang))
689  localeMapper = QTextCodec::codecForName("ISO 8859-9");
690  else if (try_locale_list(iso8859_13locales, lang))
691  localeMapper = QTextCodec::codecForName("ISO 8859-13");
692  else if (try_locale_list(tis_620locales, lang))
693  localeMapper = QTextCodec::codecForName("ISO 8859-11");
694  else if (try_locale_list(koi8_ulocales, lang))
696  else if (try_locale_list(cp_1251locales, lang))
698  else if (try_locale_list(pt_154locales, lang))
700  else if (try_locale_list(probably_koi8_rlocales, lang))
701  localeMapper = ru_RU_hack(lang);
702  }
703 
704  }
705 
706  // If everything failed, we default to 8859-1
707  // We could perhaps default to 8859-15.
708  if (!localeMapper)
709  localeMapper = QTextCodec::codecForName("ISO 8859-1");
710 #endif
711 }
712 
713 #ifndef QT_NO_THREAD
715 #endif
716 
717 // textCodecsMutex need to be locked to enter this function
718 static void setup()
719 {
720  if (all)
721  return;
722 
723 #ifdef Q_OS_SYMBIAN
724  // If we don't have a trap handler, we're outside of the main() function,
725  // ie. in global constructors or destructors. Don't create codecs in this
726  // case as it would lead to crashes because of a missing cleanup stack on Symbian
727  if (User::TrapHandler() == NULL)
728  return;
729 #endif
730 
731 #ifdef Q_DEBUG_TEXTCODEC
732  if (destroying_is_ok)
733  qWarning("QTextCodec: Creating new codec during codec cleanup");
734 #endif
735  all = new QList<QTextCodec*>;
736  // create the cleanup object to cleanup all codecs on exit
737  (void) createQTextCodecCleanup();
738 
739 #ifndef QT_NO_CODECS
740  (void)new QTsciiCodec;
741  for (int i = 0; i < 9; ++i)
742  (void)new QIsciiCodec(i);
743 
744  for (int i = 0; i < QSimpleTextCodec::numSimpleCodecs; ++i)
745  (void)new QSimpleTextCodec(i);
746 
747 #ifdef Q_OS_SYMBIAN
749 #endif
750 
751 # if defined(Q_WS_X11) && !defined(QT_BOOTSTRAPPED)
752  // no font codecs when bootstrapping
753  (void)new QFontLaoCodec;
754 # if defined(QT_NO_ICONV)
755  // no iconv(3) support, must build all codecs into the library
756  (void)new QFontGb2312Codec;
757  (void)new QFontGbkCodec;
758  (void)new QFontGb18030_0Codec;
759  (void)new QFontJis0208Codec;
760  (void)new QFontJis0201Codec;
761  (void)new QFontKsc5601Codec;
762  (void)new QFontBig5hkscsCodec;
763  (void)new QFontBig5Codec;
764 # endif // QT_NO_ICONV && !QT_BOOTSTRAPPED
765 # endif // Q_WS_X11
766 
767 
768 #if !defined(Q_OS_SYMBIAN) && !defined(Q_OS_INTEGRITY)
769 # if defined(QT_NO_ICONV) && !defined(QT_BOOTSTRAPPED) && !defined(QT_CODEC_PLUGINS)
770  // no asian codecs when bootstrapping, sorry
771  (void)new QGb18030Codec;
772  (void)new QGbkCodec;
773  (void)new QGb2312Codec;
774  (void)new QEucJpCodec;
775  (void)new QJisCodec;
776  (void)new QSjisCodec;
777  (void)new QEucKrCodec;
778  (void)new QCP949Codec;
779  (void)new QBig5Codec;
780  (void)new QBig5hkscsCodec;
781 # endif // QT_NO_ICONV && !QT_BOOTSTRAPPED && !QT_CODEC_PLUGINS
782 #endif //Q_OS_SYMBIAN
783 #endif // QT_NO_CODECS
784 
785 #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
786  (void) new QWindowsLocalCodec;
787 #endif // Q_OS_WIN32
788 
789  (void)new QUtf16Codec;
790  (void)new QUtf16BECodec;
791  (void)new QUtf16LECodec;
792  (void)new QUtf32Codec;
793  (void)new QUtf32BECodec;
794  (void)new QUtf32LECodec;
795 #ifndef Q_OS_SYMBIAN
796  (void)new QLatin15Codec;
797 #endif
798  (void)new QLatin1Codec;
799  (void)new QUtf8Codec;
800 
801 #if !defined(Q_OS_SYMBIAN) && !defined(Q_OS_INTEGRITY)
802 #if defined(Q_OS_UNIX) && !defined(QT_NO_ICONV) && !defined(QT_BOOTSTRAPPED)
803  // QIconvCodec depends on the UTF-16 codec, so it needs to be created last
804  (void) new QIconvCodec();
805 #endif
806 #endif
807 
808  if (!localeMapper)
810 }
811 
836 {
837  if (flags & FreeFunction)
838  (QTextCodecUnalignedPointer::decode(state_data))(this);
839  else if (d)
840  qFree(d);
841 }
842 
986 {
987 #ifndef QT_NO_THREAD
988  QMutexLocker locker(textCodecsMutex());
989 #endif
990  setup();
991  all->prepend(this);
992 }
993 
994 
1005 {
1006 #ifdef Q_DEBUG_TEXTCODEC
1007  if (!destroying_is_ok)
1008  qWarning("QTextCodec::~QTextCodec: Called by application");
1009 #endif
1010  if (all) {
1011 #ifndef QT_NO_THREAD
1012  QMutexLocker locker(textCodecsMutex());
1013 #endif
1014  all->removeAll(this);
1015  QTextCodecCache *cache = qTextCodecCache();
1016  if (cache)
1017  cache->clear();
1018  }
1019 }
1020 
1035 {
1036  if (name.isEmpty())
1037  return 0;
1038 
1039 #ifndef QT_NO_THREAD
1040  QMutexLocker locker(textCodecsMutex());
1041 #endif
1042  setup();
1043 
1044  if (!validCodecs())
1045  return 0;
1046 
1047  QTextCodecCache *cache = qTextCodecCache();
1048  QTextCodec *codec;
1049  if (cache) {
1050  codec = cache->value(name);
1051  if (codec)
1052  return codec;
1053  }
1054 
1055  for (int i = 0; i < all->size(); ++i) {
1056  QTextCodec *cursor = all->at(i);
1057  if (nameMatch(cursor->name(), name)) {
1058  if (cache)
1059  cache->insert(name, cursor);
1060  return cursor;
1061  }
1062  QList<QByteArray> aliases = cursor->aliases();
1063  for (int y = 0; y < aliases.size(); ++y)
1064  if (nameMatch(aliases.at(y), name)) {
1065  if (cache)
1066  cache->insert(name, cursor);
1067  return cursor;
1068  }
1069  }
1070 
1071  codec = createForName(name);
1072  if (codec && cache)
1073  cache->insert(name, codec);
1074  return codec;
1075 }
1076 
1077 
1083 {
1084 #ifndef QT_NO_THREAD
1085  QMutexLocker locker(textCodecsMutex());
1086 #endif
1087  setup();
1088 
1089  if (!validCodecs())
1090  return 0;
1091 
1092  QByteArray key = "MIB: " + QByteArray::number(mib);
1093  QTextCodecCache *cache = qTextCodecCache();
1094  QTextCodec *codec;
1095  if (cache) {
1096  codec = cache->value(key);
1097  if (codec)
1098  return codec;
1099  }
1100 
1102  for (int i = 0; i < all->size(); ++i) {
1103  QTextCodec *cursor = all->at(i);
1104  if (cursor->mibEnum() == mib) {
1105  if (cache)
1106  cache->insert(key, cursor);
1107  return cursor;
1108  }
1109  }
1110 
1111  codec = createForMib(mib);
1112 
1113  // Qt 3 used 1000 (mib for UCS2) as its identifier for the utf16 codec. Map
1114  // this correctly for compatibility.
1115  if (!codec && mib == 1000)
1116  return codecForMib(1015);
1117 
1118  if (codec && cache)
1119  cache->insert(key, codec);
1120  return codec;
1121 }
1122 
1133 {
1134 #ifndef QT_NO_THREAD
1135  QMutexLocker locker(textCodecsMutex());
1136 #endif
1137  setup();
1138 
1140 
1141  if (!validCodecs())
1142  return codecs;
1143 
1144  for (int i = 0; i < all->size(); ++i) {
1145  codecs += all->at(i)->name();
1146  codecs += all->at(i)->aliases();
1147  }
1148 
1149 #ifndef QT_NO_THREAD
1150  locker.unlock();
1151 #endif
1152 
1153 #if !defined(QT_NO_LIBRARY) && !defined(QT_NO_TEXTCODECPLUGIN)
1154  QFactoryLoader *l = loader();
1155  QStringList keys = l->keys();
1156  for (int i = 0; i < keys.size(); ++i) {
1157  if (!keys.at(i).startsWith(QLatin1String("MIB: "))) {
1158  QByteArray name = keys.at(i).toLatin1();
1159  if (!codecs.contains(name))
1160  codecs += name;
1161  }
1162  }
1163 #endif
1164 
1165  return codecs;
1166 }
1167 
1175 {
1176 #ifndef QT_NO_THREAD
1177  QMutexLocker locker(textCodecsMutex());
1178 #endif
1179  setup();
1180 
1182 
1183  if (!validCodecs())
1184  return codecs;
1185 
1186  for (int i = 0; i < all->size(); ++i)
1187  codecs += all->at(i)->mibEnum();
1188 
1189 #ifndef QT_NO_THREAD
1190  locker.unlock();
1191 #endif
1192 
1193 #if !defined(QT_NO_LIBRARY) && !defined(QT_NO_TEXTCODECPLUGIN)
1194  QFactoryLoader *l = loader();
1195  QStringList keys = l->keys();
1196  for (int i = 0; i < keys.size(); ++i) {
1197  if (keys.at(i).startsWith(QLatin1String("MIB: "))) {
1198  int mib = keys.at(i).mid(5).toInt();
1199  if (!codecs.contains(mib))
1200  codecs += mib;
1201  }
1202  }
1203 #endif
1204 
1205  return codecs;
1206 }
1207 
1219 {
1220 #ifndef QT_NO_THREAD
1221  QMutexLocker locker(textCodecsMutex());
1222 #endif
1223  localeMapper = c;
1224  if (!localeMapper)
1226 }
1227 
1238 {
1239  if (!validCodecs())
1240  return 0;
1241 
1242  if (localeMapper)
1243  return localeMapper;
1244 
1245 #ifndef QT_NO_THREAD
1246  QMutexLocker locker(textCodecsMutex());
1247 #endif
1248  setup();
1249 
1250  return localeMapper;
1251 }
1252 
1253 
1288 {
1289  return QList<QByteArray>();
1290 }
1291 
1331 {
1332  return new QTextDecoder(this);
1333 }
1334 
1343 QTextDecoder* QTextCodec::makeDecoder(QTextCodec::ConversionFlags flags) const
1344 {
1345  return new QTextDecoder(this, flags);
1346 }
1347 
1348 
1356 {
1357  return new QTextEncoder(this);
1358 }
1359 
1368 QTextEncoder* QTextCodec::makeEncoder(QTextCodec::ConversionFlags flags) const
1369 {
1370  return new QTextEncoder(this, flags);
1371 }
1372 
1389 {
1390  return convertFromUnicode(str.constData(), str.length(), 0);
1391 }
1392 
1409 {
1410  return convertToUnicode(a.constData(), a.length(), 0);
1411 }
1412 
1418 {
1419  ConverterState state;
1420  state.flags = ConvertInvalidToNull;
1421  convertFromUnicode(&ch, 1, &state);
1422  return (state.invalidChars == 0);
1423 }
1424 
1430 bool QTextCodec::canEncode(const QString& s) const
1431 {
1432  ConverterState state;
1433  state.flags = ConvertInvalidToNull;
1434  convertFromUnicode(s.constData(), s.length(), &state);
1435  return (state.invalidChars == 0);
1436 }
1437 
1438 #ifdef QT3_SUPPORT
1439 
1445 const char *QTextCodec::locale()
1446 {
1447  static char locale[6];
1449  int len = qMin(l.length(), 5);
1450  memcpy(locale, l.constData(), len);
1451  locale[len] = '\0';
1452 
1453  return locale;
1454 }
1455 
1460 QByteArray QTextCodec::fromUnicode(const QString& uc, int& lenInOut) const
1461 {
1462  QByteArray result = convertFromUnicode(uc.constData(), lenInOut, 0);
1463  lenInOut = result.length();
1464  return result;
1465 }
1466 
1473 QString QTextCodec::toUnicode(const QByteArray& a, int len) const
1474 {
1475  len = qMin(a.size(), len);
1476  return convertToUnicode(a.constData(), len, 0);
1477 }
1478 #endif
1479 
1485 QString QTextCodec::toUnicode(const char *chars) const
1486 {
1487  int len = qstrlen(chars);
1488  return convertToUnicode(chars, len, 0);
1489 }
1490 
1491 
1521 QTextEncoder::QTextEncoder(const QTextCodec *codec, QTextCodec::ConversionFlags flags)
1522  : c(codec), state()
1523 {
1524  state.flags = flags;
1525 }
1526 
1531 {
1532 }
1533 
1544 {
1545  return state.invalidChars != 0;
1546 }
1547 
1552 {
1553  QByteArray result = c->fromUnicode(str.constData(), str.length(), &state);
1554  return result;
1555 }
1556 
1567 {
1568  QByteArray result = c->fromUnicode(uc, len, &state);
1569  return result;
1570 }
1571 
1572 #ifdef QT3_SUPPORT
1573 
1583 QByteArray QTextEncoder::fromUnicode(const QString& uc, int& lenInOut)
1584 {
1585  QByteArray result = c->fromUnicode(uc.constData(), lenInOut, &state);
1586  lenInOut = result.length();
1587  return result;
1588 }
1589 #endif
1590 
1621 QTextDecoder::QTextDecoder(const QTextCodec *codec, QTextCodec::ConversionFlags flags)
1622  : c(codec), state()
1623 {
1624  state.flags = flags;
1625 }
1626 
1631 {
1632 }
1633 
1644 QString QTextDecoder::toUnicode(const char *chars, int len)
1645 {
1646  return c->toUnicode(chars, len, &state);
1647 }
1648 
1649 
1657 void QTextDecoder::toUnicode(QString *target, const char *chars, int len)
1658 {
1659  Q_ASSERT(target);
1660  switch (c->mibEnum()) {
1661  case 106: // utf8
1662  static_cast<const QUtf8Codec*>(c)->convertToUnicode(target, chars, len, &state);
1663  break;
1664  case 4: { // latin1
1665  target->resize(len);
1666  ushort *data = (ushort*)target->data();
1667  for (int i = len; i >=0; --i)
1668  data[i] = (uchar) chars[i];
1669  } break;
1670  default:
1671  *target = c->toUnicode(chars, len, &state);
1672  }
1673 }
1674 
1675 
1686 {
1687  return c->toUnicode(ba.constData(), ba.length(), &state);
1688 }
1689 
1690 
1769 {
1770  // determine charset
1771  int pos;
1772  QTextCodec *c = 0;
1773 
1774  c = QTextCodec::codecForUtfText(ba, c);
1775  if (!c) {
1776  QByteArray header = ba.left(512).toLower();
1777  if ((pos = header.indexOf("http-equiv=")) != -1) {
1778  if ((pos = header.lastIndexOf("meta ", pos)) != -1) {
1779  pos = header.indexOf("charset=", pos) + int(strlen("charset="));
1780  if (pos != -1) {
1781  int pos2 = header.indexOf('\"', pos+1);
1782  QByteArray cs = header.mid(pos, pos2-pos);
1783  // qDebug("found charset: %s", cs.data());
1784  c = QTextCodec::codecForName(cs);
1785  }
1786  }
1787  }
1788  }
1789  if (!c)
1790  c = defaultCodec;
1791 
1792  return c;
1793 }
1794 
1808 {
1809  return codecForHtml(ba, QTextCodec::codecForMib(/*Latin 1*/ 4));
1810 }
1811 
1827 {
1828  const int arraySize = ba.size();
1829 
1830  if (arraySize > 3) {
1831  if ((uchar)ba[0] == 0x00
1832  && (uchar)ba[1] == 0x00
1833  && (uchar)ba[2] == 0xFE
1834  && (uchar)ba[3] == 0xFF)
1835  return QTextCodec::codecForMib(1018); // utf-32 be
1836  else if ((uchar)ba[0] == 0xFF
1837  && (uchar)ba[1] == 0xFE
1838  && (uchar)ba[2] == 0x00
1839  && (uchar)ba[3] == 0x00)
1840  return QTextCodec::codecForMib(1019); // utf-32 le
1841  }
1842 
1843  if (arraySize < 2)
1844  return defaultCodec;
1845  if ((uchar)ba[0] == 0xfe && (uchar)ba[1] == 0xff)
1846  return QTextCodec::codecForMib(1013); // utf16 be
1847  else if ((uchar)ba[0] == 0xff && (uchar)ba[1] == 0xfe)
1848  return QTextCodec::codecForMib(1014); // utf16 le
1849 
1850  if (arraySize < 3)
1851  return defaultCodec;
1852  if ((uchar)ba[0] == 0xef
1853  && (uchar)ba[1] == 0xbb
1854  && (uchar)ba[2] == 0xbf)
1855  return QTextCodec::codecForMib(106); // utf-8
1856 
1857  return defaultCodec;
1858 }
1859 
1874 {
1875  return codecForUtfText(ba, QTextCodec::codecForMib(/*Latin 1*/ 4));
1876 }
1877 
1878 
1889 {
1890  return state.invalidChars != 0;
1891 }
1892 
1924 
1925 #endif // QT_NO_TEXTCODEC
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
static QTextCodec * codecForLocale()
Returns a pointer to the codec most suitable for this locale.
void resize(int size)
double d
Definition: qnumeric_p.h:62
static QList< int > availableMibs()
Returns the list of MIBs for all available codecs.
The QIsciiCodec class provides conversion to and from the ISCII encoding.
Definition: qisciicodec_p.h:62
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
static char qtolower(register char c)
Definition: qtextcodec.cpp:119
static QTextCodec * createForName(const QByteArray &name)
Definition: qtextcodec.cpp:155
unsigned char c[8]
Definition: qnumeric_p.h:62
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static QList< QTextCodec * > * all
Definition: qtextcodec.cpp:188
The QMutex class provides access serialization between threads.
Definition: qmutex.h:60
Q_CORE_EXPORT QTextStream & ws(QTextStream &s)
static bool nameMatch(const QByteArray &name, const QByteArray &test)
Definition: qtextcodec.cpp:124
QTextCodec::ConverterState state
Definition: qtextcodec.h:189
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
const char * aliases[7]
int toInt(bool *ok=0, int base=10) const
Returns the string converted to an int using base base, which is 10 by default and must be between 2 ...
Definition: qstring.cpp:6090
#define it(className, varName)
Q_CORE_EXPORT void qFree(void *ptr)
Definition: qmalloc.cpp:58
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
static bool qisalnum(register char c)
Definition: qtextcodec.cpp:121
The QTextDecoder class provides a state-based decoder.
Definition: qtextcodec.h:177
static void setupLocaleMapper()
Definition: qtextcodec.cpp:581
void unlock()
Unlocks this mutex locker.
Definition: qmutex.h:117
static QTextCodec * codecForHtml(const QByteArray &ba)
Tries to detect the encoding of the provided snippet of HTML in the given byte array, ba, by checking the BOM (Byte Order Mark) and the content-type meta header and returns a QTextCodec instance that is capable of decoding the html to unicode.
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the list.
Definition: qlist.h:269
QTextCodec * QTextCodecCache
Definition: qtextcodec.cpp:115
The QList::const_iterator class provides an STL-style const iterator for QList and QQueue...
Definition: qlist.h:228
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
void insert(int i, const T &t)
Inserts value at index position i in the list.
Definition: qlist.h:575
#define QTextCodecFactoryInterface_iid
QTextDecoder * makeDecoder() const
Creates a QTextDecoder which stores enough state to decode chunks of char * data to create chunks of ...
QByteArray toLower() const
Returns a lowercase copy of the byte array.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
virtual ~QTextCodec()
Destroys the QTextCodec.
long ASN1_INTEGER_get ASN1_INTEGER * a
static const Codecs codecs[]
Definition: qisciicodec.cpp:64
~ConverterState()
Destroys the ConverterState object.
Definition: qtextcodec.cpp:835
QString name() const
Returns the language and country of this locale as a string of the form "language_country", where language is a lowercase, two-letter ISO 639 language code, and country is an uppercase, two- or three-letter ISO 3166 country code.
Definition: qlocale.cpp:963
static QList< QByteArray > availableCodecs()
Returns the list of all available codecs, by name.
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool hasFailure() const
Determines whether the eecoder encountered a failure while decoding the input.
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
~QTextDecoder()
Destroys the decoder.
~QTextEncoder()
Destroys the encoder.
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
QChar * data()
Returns a pointer to the data stored in the QString.
Definition: qstring.h:710
QStringList keys
virtual QList< QByteArray > aliases() const
Subclasses can return a number of aliases for the codec in question.
static QLocale system()
Returns a QLocale object initialized to the system locale.
Definition: qlocale.cpp:1917
bool canEncode(QChar) const
Returns true if the Unicode character ch can be fully encoded with this codec; otherwise returns fals...
unsigned char uchar
Definition: qglobal.h:994
QString convertToUnicode(const char *, int, ConverterState *) const
QTextCodec subclasses must reimplement this function.
Definition: qtextcodec.cpp:270
QStringList keys() const
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QByteArray fromUnicode(const QString &uc) const
Converts str from Unicode to the encoding of this codec, and returns the result in a QByteArray...
QBool contains(const T &t) const
Returns true if the list contains an occurrence of value; otherwise returns false.
Definition: qlist.h:880
int lastIndexOf(char c, int from=-1) const
Returns the index position of the last occurrence of character ch in the byte array, searching backward from index position from.
static bool init
const char * name
#define Q_GLOBAL_STATIC(TYPE, NAME)
Declares a global static variable with the given type and name.
Definition: qglobal.h:1968
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QByteArray name() const
QTextCodec subclasses must reimplement this function.
Definition: qtextcodec.cpp:444
The QTextEncoder class provides a state-based encoder.
Definition: qtextcodec.h:160
Q_CORE_EXPORT void qWarning(const char *,...)
int mibEnum() const
Subclasses of QTextCodec must reimplement this function.
Definition: qtextcodec.cpp:449
virtual int mibEnum() const =0
Subclasses of QTextCodec must reimplement this function.
static const char * data(const QByteArray &arr)
static QTextCodec * codec(MYSQL *mysql)
Definition: qsql_mysql.cpp:220
int mib
static QTextCodec * codecForMib(int mib)
Returns the QTextCodec which matches the MIBenum mib.
static void setCodecForLocale(QTextCodec *c)
Set the codec to c; this will be returned by codecForLocale().
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
QByteArray left(int len) const
Returns a byte array that contains the leftmost len bytes of this byte array.
static QTextCodec * createForMib(int mib)
Definition: qtextcodec.cpp:175
QByteArray mid(int index, int len=-1) const
Returns a byte array containing len bytes from this byte array, starting at position pos...
int indexOf(char c, int from=0) const
Returns the index position of the first occurrence of the character ch in the byte array...
int qstricmp(const char *str1, const char *str2)
A safe stricmp() function.
Definition: qbytearray.cpp:279
const QTextCodec * c
Definition: qtextcodec.h:173
int length() const
Same as size().
Definition: qbytearray.h:356
ConversionFlags flags
Definition: qtextcodec.h:106
void resize(int size)
Sets the size of the string to size characters.
Definition: qstring.cpp:1353
virtual QByteArray name() const =0
QTextCodec subclasses must reimplement this function.
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
QString toUnicode(const QByteArray &) const
Converts a from the encoding of this codec to Unicode, and returns the result in a QString...
bool hasFailure() const
Determines whether the decoder encountered a failure while decoding the input.
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
QTextDecoder(const QTextCodec *codec)
Constructs a text decoder for the given codec.
Definition: qtextcodec.h:180
uint qstrlen(const char *str)
Definition: qbytearray.h:79
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,(QTextCodecFactoryInterface_iid, QLatin1String("/codecs"))) typedef QHash< QByteArray
QString & append(QChar c)
Definition: qstring.cpp:1777
unsigned short ushort
Definition: qglobal.h:995
QTextCodec::ConverterState state
Definition: qtextcodec.h:174
QObject * instance(const QString &key) const
QByteArray convertFromUnicode(const QChar *, int, ConverterState *) const
QTextCodec subclasses must reimplement this function.
Definition: qtextcodec.cpp:412
int key
void resize(int size)
Sets the size of the byte array to size bytes.
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
QString toUnicode(const char *chars, int len)
Converts the first len bytes in chars to Unicode, returning the result.
The QGbkCodec class provides conversion to and from the Chinese GBK encoding.
Definition: qgb18030codec.h:70
QTextEncoder(const QTextCodec *codec)
Constructs a text encoder for the given codec.
Definition: qtextcodec.h:163
QFactoryLoader * l
static QTextCodec * codecForName(const QByteArray &name)
Searches all installed QTextCodec objects and returns the one which best matches name; the match is c...
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
static QTextCodecStateFreeFunction decode(const uint *src)
Definition: qtextcodec_p.h:67
static QTextCodec * cftr
Definition: qtextcodec.h:150
quint16 index
static QTextCodec * codecForUtfText(const QByteArray &ba)
Tries to detect the encoding of the provided snippet ba by using the BOM (Byte Order Mark) and return...
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
QTextEncoder * makeEncoder() const
Creates a QTextEncoder which stores enough state to encode chunks of Unicode data as char * data...
QTextCodec()
Constructs a QTextCodec, and gives it the highest precedence.
Definition: qtextcodec.cpp:985
static QTextCodec * localeMapper
Definition: qtextcodec.cpp:193
The QTextCodec class provides conversions between text encodings.
Definition: qtextcodec.h:62
QString convertToUnicodeCharByChar(const char *chars, int length, ConverterState *state) const
Definition: qtextcodec.cpp:351
const QTextCodec * c
Definition: qtextcodec.h:188
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
static QByteArray number(int, int base=10)
Returns a byte array containing the string equivalent of the number n to base base (10 by default)...
The QCP949Codec class handles encoding and decoding of text in CP949 encoding.
Definition: qeuckrcodec.h:91
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition: qstring.h:712
int size() const
static void setup()
Definition: qtextcodec.cpp:718
QByteArray fromUnicode(const QString &str)
Converts the Unicode string str into an encoded QByteArray.
QBool contains(char c) const
Returns true if the byte array contains the character ch; otherwise returns false.
Definition: qbytearray.h:525
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:272
Q_GUI_EXPORT QTextCodec * codecForHtml(const QByteArray &ba)
This function is defined in the <QTextDocument> header file.
The QGb2312Codec class provides conversion to and from the Chinese GB2312 encoding.
Definition: qgb18030codec.h:86