Qt 4.8
Classes | Macros | Functions | Variables
qtextcodec.cpp File Reference
#include "qplatformdefs.h"
#include "qtextcodec.h"
#include "qtextcodec_p.h"
#include "qlist.h"
#include "qfile.h"
#include "qvarlengtharray.h"
#include "qcoreapplication.h"
#include "qtextcodecplugin.h"
#include "private/qfactoryloader_p.h"
#include "qstringlist.h"
#include "qiconvcodec_p.h"
#include "qutfcodec_p.h"
#include "qsimplecodec_p.h"
#include "qlatincodec_p.h"
#include "qtsciicodec_p.h"
#include "qisciicodec_p.h"
#include "qfontlaocodec_p.h"
#include "../../plugins/codecs/jp/qfontjpcodec.h"
#include "qlocale.h"
#include "qmutex.h"
#include "qhash.h"
#include <stdlib.h>
#include <ctype.h>
#include <locale.h>

Go to the source code of this file.

Classes

class  QTextCodecCleanup
 
class  QWindowsLocalCodec
 

Macros

#define QT_NO_SETLOCALE
 

Functions

static QTextCodeccreateForMib (int mib)
 
static QTextCodeccreateForName (const QByteArray &name)
 
static bool nameMatch (const QByteArray &name, const QByteArray &test)
 
 Q_GLOBAL_STATIC_WITH_ARGS (QFactoryLoader, loader,(QTextCodecFactoryInterface_iid, QLatin1String("/codecs"))) typedef QHash< QByteArray
 
 Q_GLOBAL_STATIC_WITH_ARGS (QMutex, textCodecsMutex,(QMutex::Recursive))
 
static bool qisalnum (register char c)
 
static char qtolower (register char c)
 
static void setup ()
 
static void setupLocaleMapper ()
 

Variables

static QList< QTextCodec * > * all = 0
 
static QTextCodeclocaleMapper = 0
 
QTextCodecQTextCodecCache
 

Macro Definition Documentation

◆ QT_NO_SETLOCALE

#define QT_NO_SETLOCALE

Definition at line 96 of file qtextcodec.cpp.

Function Documentation

◆ createForMib()

static QTextCodec* createForMib ( int  mib)
static

Definition at line 175 of file qtextcodec.cpp.

Referenced by QTextCodec::codecForMib().

176 {
177 #ifndef QT_NO_TEXTCODECPLUGIN
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 }
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
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
const char * name
int mib
#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

◆ createForName()

static QTextCodec* createForName ( const QByteArray name)
static

Definition at line 155 of file qtextcodec.cpp.

Referenced by QTextCodec::codecForName().

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 }
static bool nameMatch(const QByteArray &name, const QByteArray &test)
Definition: qtextcodec.cpp:124
The QString class provides a Unicode character string.
Definition: qstring.h:83
QStringList keys
QStringList keys() const
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 toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
QObject * instance(const QString &key) const
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
QFactoryLoader * l
#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

◆ nameMatch()

static bool nameMatch ( const QByteArray name,
const QByteArray test 
)
static

Definition at line 124 of file qtextcodec.cpp.

Referenced by QTextCodec::codecForName(), and createForName().

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 }
static char qtolower(register char c)
Definition: qtextcodec.cpp:119
static bool qisalnum(register char c)
Definition: qtextcodec.cpp:121
int qstricmp(const char *str1, const char *str2)
A safe stricmp() function.
Definition: qbytearray.cpp:279
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433

◆ Q_GLOBAL_STATIC_WITH_ARGS() [1/2]

Q_GLOBAL_STATIC_WITH_ARGS ( QFactoryLoader  ,
loader  ,
(QTextCodecFactoryInterface_iid, QLatin1String("/codecs"))   
)

Referenced by setupLocaleMapper().

◆ Q_GLOBAL_STATIC_WITH_ARGS() [2/2]

Q_GLOBAL_STATIC_WITH_ARGS ( QMutex  ,
textCodecsMutex  ,
(QMutex::Recursive  
)

◆ qisalnum()

static bool qisalnum ( register char  c)
static

Definition at line 121 of file qtextcodec.cpp.

Referenced by nameMatch().

122 { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); }
unsigned char c[8]
Definition: qnumeric_p.h:62

◆ qtolower()

static char qtolower ( register char  c)
static

Definition at line 119 of file qtextcodec.cpp.

Referenced by nameMatch().

120 { if (c >= 'A' && c <= 'Z') return c + 0x20; return c; }
unsigned char c[8]
Definition: qnumeric_p.h:62

◆ setup()

static void setup ( )
static

Definition at line 718 of file qtextcodec.cpp.

Referenced by QTextCodec::availableCodecs(), QTextCodec::availableMibs(), QTextCodec::codecForLocale(), QTextCodec::codecForMib(), QTextCodec::codecForName(), QDeclarativeBindingCompilerPrivate::fetch(), IapMonitor::IapMonitor(), QListWidgetPrivate::listModel(), QPrinter::printRange(), QRegExpEngine::QRegExpEngine(), QTextCodec::QTextCodec(), QTextEditMimeData::QTextEditMimeData(), QWizardHeader::QWizardHeader(), QTextEditMimeData::retrieveData(), QTableWidgetPrivate::tableModel(), QIntKeyboardListenThread::~QIntKeyboardListenThread(), and QIntMouseListenThread::~QIntMouseListenThread().

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 }
The QIsciiCodec class provides conversion to and from the ISCII encoding.
Definition: qisciicodec_p.h:62
static QList< QTextCodec * > * all
Definition: qtextcodec.cpp:188
static void setupLocaleMapper()
Definition: qtextcodec.cpp:581
static bool init
Q_CORE_EXPORT void qWarning(const char *,...)
The QGbkCodec class provides conversion to and from the Chinese GBK encoding.
Definition: qgb18030codec.h:70
static QTextCodec * localeMapper
Definition: qtextcodec.cpp:193
The QCP949Codec class handles encoding and decoding of text in CP949 encoding.
Definition: qeuckrcodec.h:91
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
The QGb2312Codec class provides conversion to and from the Chinese GB2312 encoding.
Definition: qgb18030codec.h:86

◆ setupLocaleMapper()

static void setupLocaleMapper ( )
static

Definition at line 581 of file qtextcodec.cpp.

Referenced by QTextCodec::setCodecForLocale().

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 }
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
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...
static QTextCodec * codecForName(const QByteArray &name)
Searches all installed QTextCodec objects and returns the one which best matches name; the match is c...
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
static QTextCodec * localeMapper
Definition: qtextcodec.cpp:193
QBool contains(char c) const
Returns true if the byte array contains the character ch; otherwise returns false.
Definition: qbytearray.h:525

Variable Documentation

◆ all

QList<QTextCodec*>* all = 0
static

◆ localeMapper

QTextCodec* localeMapper = 0
static

◆ QTextCodecCache

QTextCodec* QTextCodecCache