42 #include "qplatformdefs.h" 46 #ifndef QT_NO_TEXTCODEC 54 # include "private/qfactoryloader_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) 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) 80 # include "../../plugins/codecs/jp/qfontjpcodec.h" 82 #endif // QT_NO_SYMBIAN 83 #endif // QT_NO_CODECS 91 #if defined (_XOPEN_UNIX) && !defined(Q_OS_QNX) && !defined(Q_OS_OSF) 95 #if defined(Q_OS_WINCE) 96 # define QT_NO_SETLOCALE 100 #include "qtextcodec_symbian.cpp" 109 #if !defined(QT_NO_LIBRARY) && !defined(QT_NO_TEXTCODECPLUGIN) 120 {
if (c >=
'A' && c <=
'Z')
return c + 0x20;
return c; }
122 {
return (c >=
'0' && c <=
'9') || ((c | 0x20) >=
'a' && (c | 0x20) <=
'z'); }
157 #if !defined(QT_NO_LIBRARY) && !defined(QT_NO_TEXTCODECPLUGIN) 160 for (
int i = 0; i < keys.
size(); ++i) {
164 = qobject_cast<QTextCodecFactoryInterface*>(l->
instance(realName))) {
165 return factory->create(realName);
177 #ifndef QT_NO_TEXTCODECPLUGIN 180 = qobject_cast<QTextCodecFactoryInterface*>(loader()->instance(name)))
181 return factory->create(name);
189 #ifdef Q_DEBUG_TEXTCODEC 190 static bool destroying_is_ok =
false;
213 #ifdef Q_DEBUG_TEXTCODEC 214 destroying_is_ok =
true;
226 #ifdef Q_DEBUG_TEXTCODEC 227 destroying_is_ok =
false;
239 return (User::TrapHandler() != NULL);
246 #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE) 272 const char *mb = chars;
281 bool prepend =
false;
283 int remainingChars = 0;
292 if (state && remainingChars) {
294 prev[0] = state_data;
297 len = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,
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,
315 }
else if (r == ERROR_NO_UNICODE_TRANSLATION) {
317 while (mblen > 1 && !(mb[mblen-1]))
320 if ((mblen <= 1) || (remainingChars && state_data))
321 return convertToUnicodeCharByChar(chars, length, state);
323 state_data = mb[mblen-1];
328 qWarning(
"MultiByteToWideChar: Cannot convert multibyte text");
353 if (!chars || !length)
356 int copyLocation = 0;
360 extra += copyLocation;
362 int newLength = length + extra;
363 char *mbcs =
new char[newLength];
365 mbcs[newLength-1] = 0;
366 mbcs[newLength-2] = 0;
367 memcpy(&(mbcs[copyLocation]), chars, length);
373 const char *mb = mbcs;
375 const char *next = 0;
377 while((next = CharNextExA(CP_ACP, mb, 0)) != mb) {
379 int charlength = next - mb;
380 int len = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED|MB_ERR_INVALID_CHARS, mb, charlength, wc, 2);
384 int r = GetLastError();
386 if (r == ERROR_NO_UNICODE_TRANSLATION && mb == (mbcs+newLength -3) && state) {
395 int size = mbstowcs(NULL, mb, length);
400 wchar_t*
ws =
new wchar_t[size + 2];
403 size = mbstowcs(ws, mb, length);
404 for (
int i=0; i< size; i++)
421 while (!(len=WideCharToMultiByte(CP_ACP, 0, (
const wchar_t*)ch, uclen,
422 mb.
data(), mb.
size()-1, 0, &used_def)))
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));
433 qWarning(
"WideCharToMultiByte: Cannot convert multibyte text (error %d): %s (UTF-8)",
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 };
463 static const char *
const iso8859_3locales[] = {
466 static const char *
const iso8859_4locales[] = {
469 static const char *
const iso8859_5locales[] = {
470 "mk",
"mk_MK",
"sp",
"sp_YU", 0 };
472 static const char *
const cp_1251locales[] = {
473 "be",
"be_BY",
"bg",
"bg_BG",
"bulgarian", 0 };
475 static const char *
const pt_154locales[] = {
476 "ba_RU",
"ky",
"ky_KG",
"kk",
"kk_KZ", 0 };
478 static const char *
const iso8859_6locales[] = {
479 "ar_AA",
"ar_SA",
"arabic", 0 };
481 static const char *
const iso8859_7locales[] = {
482 "el",
"el_GR",
"greek", 0 };
484 static const char *
const iso8859_8locales[] = {
485 "hebrew",
"he",
"he_IL",
"iw",
"iw_IL", 0 };
487 static const char *
const iso8859_9locales[] = {
488 "tr",
"tr_TR",
"turkish", 0 };
490 static const char *
const iso8859_13locales[] = {
491 "lt",
"lt_LT",
"lv",
"lv_LV", 0 };
493 static const char *
const iso8859_15locales[] = {
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",
502 static const char *
const koi8_ulocales[] = {
503 "uk",
"uk_UA",
"ru_UA",
"ukrainian", 0 };
505 static const char *
const tis_620locales[] = {
506 "th",
"th_TH",
"thai", 0 };
511 static bool try_locale_list(
const char *
const locale[],
const QByteArray &lang)
514 for(i=0; locale[i] && lang != locale[i]; i++)
516 return locale[i] != 0;
530 static const char *
const probably_koi8_rlocales[] = {
531 "ru",
"ru_SU",
"ru_RU",
"russian", 0 };
533 static QTextCodec * ru_RU_hack(
const char * i) {
536 #if !defined(QT_NO_SETLOCALE) 537 QByteArray origlocale(setlocale(LC_CTYPE, i));
544 int latin5 = tolower(0xCE);
545 int koi8r = tolower(0xE0);
546 if (koi8r == 0xC0 && latin5 != 0xEE) {
548 }
else if (koi8r != 0xC0 && latin5 == 0xEE) {
553 qWarning(
"QTextCodec: Using KOI8-R, probe failed (%02x %02x %s)",
556 #if !defined(QT_NO_SETLOCALE) 557 setlocale(LC_CTYPE, origlocale);
565 #if !defined(Q_OS_WIN32) && !defined(Q_OS_WINCE) 589 #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE) 597 #if defined (_XOPEN_UNIX) && !defined(Q_OS_QNX) && !defined(Q_OS_OSF) 599 char *charset = nl_langinfo (CODESET);
617 #if !defined(QT_NO_SETLOCALE) 618 const QByteArray ctype = setlocale(LC_CTYPE, 0);
626 if (lang.
isEmpty() || lang ==
"C") {
629 if (lang.
isEmpty() || lang ==
"C") {
643 int indexOfDot = ctype.
indexOf(
'.');
644 if (indexOfDot != -1)
649 indexOfDot = lang.
indexOf(
'.');
650 if (indexOfDot != -1)
668 const QByteArray &try_by_name = (!ctype.
isEmpty() && ctype !=
"C") ? lang : ctype;
672 if (try_locale_list(iso8859_15locales, lang))
674 else if (try_locale_list(iso8859_2locales, lang))
676 else if (try_locale_list(iso8859_3locales, lang))
678 else if (try_locale_list(iso8859_4locales, lang))
680 else if (try_locale_list(iso8859_5locales, lang))
682 else if (try_locale_list(iso8859_6locales, lang))
684 else if (try_locale_list(iso8859_7locales, lang))
686 else if (try_locale_list(iso8859_8locales, lang))
688 else if (try_locale_list(iso8859_9locales, lang))
690 else if (try_locale_list(iso8859_13locales, lang))
692 else if (try_locale_list(tis_620locales, lang))
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))
727 if (User::TrapHandler() == NULL)
731 #ifdef Q_DEBUG_TEXTCODEC 732 if (destroying_is_ok)
733 qWarning(
"QTextCodec: Creating new codec during codec cleanup");
737 (void) createQTextCodecCleanup();
741 for (
int i = 0; i < 9; ++i)
751 # if defined(Q_WS_X11) && !defined(QT_BOOTSTRAPPED) 754 # if defined(QT_NO_ICONV) 764 # endif // QT_NO_ICONV && !QT_BOOTSTRAPPED 768 #if !defined(Q_OS_SYMBIAN) && !defined(Q_OS_INTEGRITY) 769 # if defined(QT_NO_ICONV) && !defined(QT_BOOTSTRAPPED) && !defined(QT_CODEC_PLUGINS) 781 # endif // QT_NO_ICONV && !QT_BOOTSTRAPPED && !QT_CODEC_PLUGINS 782 #endif //Q_OS_SYMBIAN 783 #endif // QT_NO_CODECS 785 #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE) 801 #if !defined(Q_OS_SYMBIAN) && !defined(Q_OS_INTEGRITY) 802 #if defined(Q_OS_UNIX) && !defined(QT_NO_ICONV) && !defined(QT_BOOTSTRAPPED) 837 if (flags & FreeFunction)
1006 #ifdef Q_DEBUG_TEXTCODEC 1007 if (!destroying_is_ok)
1008 qWarning(
"QTextCodec::~QTextCodec: Called by application");
1011 #ifndef QT_NO_THREAD 1014 all->removeAll(
this);
1039 #ifndef QT_NO_THREAD 1050 codec = cache->value(name);
1055 for (
int i = 0; i <
all->size(); ++i) {
1059 cache->insert(name, cursor);
1063 for (
int y = 0; y < aliases.
size(); ++y)
1066 cache->
insert(name, cursor);
1073 cache->insert(name, codec);
1084 #ifndef QT_NO_THREAD 1096 codec = cache->value(key);
1102 for (
int i = 0; i <
all->size(); ++i) {
1106 cache->insert(key, cursor);
1115 if (!codec && mib == 1000)
1116 return codecForMib(1015);
1119 cache->insert(key, codec);
1134 #ifndef QT_NO_THREAD 1144 for (
int i = 0; i <
all->size(); ++i) {
1145 codecs +=
all->at(i)->name();
1146 codecs +=
all->at(i)->aliases();
1149 #ifndef QT_NO_THREAD 1153 #if !defined(QT_NO_LIBRARY) && !defined(QT_NO_TEXTCODECPLUGIN) 1156 for (
int i = 0; i < keys.
size(); ++i) {
1176 #ifndef QT_NO_THREAD 1186 for (
int i = 0; i <
all->size(); ++i)
1187 codecs +=
all->at(i)->mibEnum();
1189 #ifndef QT_NO_THREAD 1193 #if !defined(QT_NO_LIBRARY) && !defined(QT_NO_TEXTCODECPLUGIN) 1196 for (
int i = 0; i < keys.
size(); ++i) {
1220 #ifndef QT_NO_THREAD 1245 #ifndef QT_NO_THREAD 1420 state.
flags = ConvertInvalidToNull;
1421 convertFromUnicode(&ch, 1, &state);
1433 state.
flags = ConvertInvalidToNull;
1445 const char *QTextCodec::locale()
1447 static char locale[6];
1463 lenInOut = result.
length();
1476 return convertToUnicode(a.
constData(), len, 0);
1488 return convertToUnicode(chars, len, 0);
1586 lenInOut = result.
length();
1662 static_cast<const QUtf8Codec*
>(
c)->convertToUnicode(target, chars, len, &
state);
1667 for (
int i = len; i >=0; --i)
1668 data[i] = (
uchar) chars[i];
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="));
1781 int pos2 = header.
indexOf(
'\"', pos+1);
1828 const int arraySize = ba.
size();
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)
1836 else if ((
uchar)ba[0] == 0xFF
1837 && (
uchar)ba[1] == 0xFE
1838 && (
uchar)ba[2] == 0x00
1839 && (
uchar)ba[3] == 0x00)
1844 return defaultCodec;
1845 if ((
uchar)ba[0] == 0xfe && (
uchar)ba[1] == 0xff)
1847 else if ((
uchar)ba[0] == 0xff && (
uchar)ba[1] == 0xfe)
1851 return defaultCodec;
1852 if ((
uchar)ba[0] == 0xef
1853 && (
uchar)ba[1] == 0xbb
1854 && (
uchar)ba[2] == 0xbf)
1857 return defaultCodec;
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...
static QTextCodec * codecForLocale()
Returns a pointer to the codec most suitable for this locale.
static QList< int > availableMibs()
Returns the list of MIBs for all available codecs.
The QIsciiCodec class provides conversion to and from the ISCII encoding.
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
static char qtolower(register char c)
static QTextCodec * createForName(const QByteArray &name)
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
#define QT_END_NAMESPACE
This macro expands to.
static QList< QTextCodec * > * all
The QMutex class provides access serialization between threads.
Q_CORE_EXPORT QTextStream & ws(QTextStream &s)
static bool nameMatch(const QByteArray &name, const QByteArray &test)
QTextCodec::ConverterState state
char * data()
Returns a pointer to the data stored in the byte array.
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 ...
#define it(className, varName)
Q_CORE_EXPORT void qFree(void *ptr)
The QByteArray class provides an array of bytes.
int length() const
Returns the number of characters in this string.
static bool qisalnum(register char c)
The QTextDecoder class provides a state-based decoder.
static void setupLocaleMapper()
void unlock()
Unlocks this mutex locker.
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.
QTextCodec * QTextCodecCache
The QList::const_iterator class provides an STL-style const iterator for QList and QQueue...
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
void insert(int i, const T &t)
Inserts value at index position i in the list.
#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[]
~ConverterState()
Destroys the ConverterState object.
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.
static QList< QByteArray > availableCodecs()
Returns the list of all available codecs, by name.
The QString class provides a Unicode character string.
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.
~QTextDecoder()
Destroys the decoder.
~QTextEncoder()
Destroys the encoder.
The QChar class provides a 16-bit Unicode character.
QChar * data()
Returns a pointer to the data stored in the QString.
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.
bool canEncode(QChar) const
Returns true if the Unicode character ch can be fully encoded with this codec; otherwise returns fals...
QString convertToUnicode(const char *, int, ConverterState *) const
QTextCodec subclasses must reimplement this function.
#define QT_BEGIN_NAMESPACE
This macro expands to.
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.
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.
#define Q_GLOBAL_STATIC(TYPE, NAME)
Declares a global static variable with the given type and name.
const T & at(int i) const
Returns the item at index position i in the list.
The QStringList class provides a list of strings.
QByteArray name() const
QTextCodec subclasses must reimplement this function.
The QTextEncoder class provides a state-based encoder.
Q_CORE_EXPORT void qWarning(const char *,...)
int mibEnum() const
Subclasses of QTextCodec must reimplement this function.
virtual int mibEnum() const =0
Subclasses of QTextCodec must reimplement this function.
static const char * data(const QByteArray &arr)
static QTextCodec * codec(MYSQL *mysql)
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.
QByteArray left(int len) const
Returns a byte array that contains the leftmost len bytes of this byte array.
static QTextCodec * createForMib(int mib)
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.
int length() const
Same as size().
void resize(int size)
Sets the size of the string to size characters.
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.
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...
QTextDecoder(const QTextCodec *codec)
Constructs a text decoder for the given codec.
uint qstrlen(const char *str)
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,(QTextCodecFactoryInterface_iid, QLatin1String("/codecs"))) typedef QHash< QByteArray
QString & append(QChar c)
QTextCodec::ConverterState state
QObject * instance(const QString &key) const
QByteArray convertFromUnicode(const QChar *, int, ConverterState *) const
QTextCodec subclasses must reimplement this function.
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.
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.
QTextEncoder(const QTextCodec *codec)
Constructs a text encoder for the given codec.
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.
static QTextCodecStateFreeFunction decode(const uint *src)
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.
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.
static QTextCodec * localeMapper
The QTextCodec class provides conversions between text encodings.
QString convertToUnicodeCharByChar(const char *chars, int length, ConverterState *state) const
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
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.
const QChar * constData() const
Returns a pointer to the data stored in the QString.
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.
The QList class is a template class that provides lists.
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list...
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.