55 #if defined(_XOPEN_UNIX) && !defined(Q_OS_QNX) && !defined(Q_OS_OSF) 56 # include <langinfo.h> 59 #if defined(Q_OS_HPUX) 62 #elif defined(Q_OS_AIX) 64 # define UTF16 "UCS-2" 65 #elif defined(Q_OS_FREEBSD) || defined(Q_OS_MAC) 67 # if Q_BYTE_ORDER == Q_BIG_ENDIAN 68 # define UTF16 "UTF-16BE" 70 # define UTF16 "UTF-16LE" 73 # define UTF16 "UTF-16" 98 "QIconvCodec::convertToUnicode",
99 "internal error, UTF-16 codec not found");
101 fprintf(stderr,
"QIconvCodec::convertToUnicode: internal error, UTF-16 codec not found\n");
104 #if defined(Q_OS_MAC) 120 "QIconvCodec::QIconvCodec()",
121 "internal error, could not resolve the iconv functions");
124 # define iconv_open ptr_iconv_open 126 # define iconv ptr_iconv 128 # define iconv_close ptr_iconv_close 138 : buffer(array), bufferLen(sizeof array), cd(x)
144 if (
cd != reinterpret_cast<iconv_t>(-1))
170 if (
utf16Codec == reinterpret_cast<QTextCodec *>(~0))
173 int invalidCount = 0;
174 int remainingCount = 0;
175 char *remainingBuffer = 0;
181 pstate =
reinterpret_cast<IconvState **
>(&convState->d);
184 remainingCount = convState->remainingChars;
185 remainingBuffer = (*pstate)->
buffer;
197 pstate = &temporaryState;
200 pstate = &toUnicodeState()->localData();
207 if (cd == reinterpret_cast<iconv_t>(-1)) {
208 static int reported = 0;
211 "QIconvCodec::convertToUnicode: using Latin-1 for conversion, iconv_open failed\n");
220 size_t inBytesLeft = len;
224 const char *inBytes = chars;
226 char *inBytes =
const_cast<char *
>(chars);
230 if (remainingCount) {
232 inBytesLeft += remainingCount;
236 memcpy(in.
data(), remainingBuffer, remainingCount);
237 memcpy(in.
data() + remainingCount, chars, len);
242 size_t outBytesLeft = len * 2 + 2;
244 char *outBytes = ba.
data();
246 size_t ret =
iconv(state->
cd, &inBytes, &inBytesLeft, &outBytes, &outBytesLeft);
247 if (ret == (
size_t) -1) {
248 if (
errno == E2BIG) {
249 int offset = ba.
size() - outBytesLeft;
251 outBytes = ba.
data() + offset;
252 outBytesLeft = ba.
size() - offset;
257 if (
errno == EILSEQ) {
260 }
else if (
errno == EINVAL && convState) {
264 remainingCount = inBytesLeft;
277 perror(
"QIconvCodec::convertToUnicode: using Latin-1 for conversion, iconv failed");
281 iconv(state->
cd, 0, &inBytesLeft, 0, &outBytesLeft);
284 delete temporaryState;
287 }
while (inBytesLeft != 0);
294 convState->invalidChars = invalidCount;
295 convState->remainingChars = remainingCount;
300 iconv(state->
cd, 0, &inBytesLeft, 0, &outBytesLeft);
303 delete temporaryState;
316 char *outBytes = buf;
317 char *inBytes =
reinterpret_cast<char *
>(
bom);
318 size_t outBytesLeft =
sizeof buf;
319 size_t inBytesLeft =
sizeof bom;
321 #if defined(GNU_LIBICONV) 322 const char **inBytesPtr =
const_cast<const char **
>(&inBytes);
324 char **inBytesPtr = &inBytes;
327 if (
iconv(
cd, inBytesPtr, &inBytesLeft, &outBytes, &outBytesLeft) == (size_t) -1) {
341 #if defined(GNU_LIBICONV) 342 const char **inBytesPtr =
const_cast<const char **
>(&inBytes);
344 char **inBytesPtr = &inBytes;
352 if (cd != reinterpret_cast<iconv_t>(-1)) {
354 perror(
"QIconvCodec::convertFromUnicode: using Latin-1 for conversion, iconv failed for BOM");
357 cd =
reinterpret_cast<iconv_t>(-1);
364 if (state->
cd == reinterpret_cast<iconv_t>(-1)) {
365 static int reported = 0;
368 "QIconvCodec::convertFromUnicode: using Latin-1 for conversion, iconv_open failed\n");
370 delete temporaryState;
374 size_t outBytesLeft = len;
376 outBytes = ba.
data();
379 inBytes =
const_cast<char *
>(
reinterpret_cast<const char *
>(uc));
380 inBytesLeft = len *
sizeof(
QChar);
389 memcpy(in.data(), &remaining,
sizeof(
QChar));
390 memcpy(in.data() +
sizeof(
QChar), uc, inBytesLeft);
392 inBytesLeft +=
sizeof(
QChar);
396 int invalidCount = 0;
397 while (inBytesLeft != 0) {
398 if (
iconv(state->
cd, inBytesPtr, &inBytesLeft, &outBytes, &outBytesLeft) == (size_t) -1) {
399 if (
errno == EINVAL && convState) {
413 inBytes +=
sizeof(
QChar);
414 inBytesLeft -=
sizeof(
QChar);
419 int offset = ba.
size() - outBytesLeft;
421 outBytes = ba.
data() + offset;
422 outBytesLeft = ba.
size() - offset;
428 perror(
"QIconvCodec::convertFromUnicode: using Latin-1 for conversion, iconv failed");
431 iconv(state->
cd, 0, &inBytesLeft, 0, &outBytesLeft);
433 delete temporaryState;
441 iconv(state->
cd, 0, &inBytesLeft, 0, &outBytesLeft);
449 delete temporaryState;
465 Q_ASSERT((to == 0 && from != 0) || (to != 0 && from == 0));
468 #if defined(__GLIBC__) || defined(GNU_LIBICONV) || defined(Q_OS_QNX) 469 #if defined(Q_OS_QNX) 471 static const char empty_codeset[] =
"UTF-8";
474 static const char empty_codeset[] =
"";
476 const char *codeset = empty_codeset;
477 cd =
iconv_open(to ? to : codeset, from ? from : codeset);
482 #if defined(_XOPEN_UNIX) && !defined(Q_OS_QNX) && !defined(Q_OS_OSF) 484 codeset = nl_langinfo(CODESET);
486 cd =
iconv_open(to ? to : codeset, from ? from : codeset);
502 char * ctype =
qstrdup(setlocale(LC_CTYPE, 0));
507 if (!lang || lang[0] == 0 || strcmp(lang,
"C") == 0) {
508 if (lang)
delete [] lang;
511 if (!lang || lang[0] == 0 || strcmp(lang,
"C") == 0) {
512 if (lang)
delete [] lang;
524 codeset = ctype ? strchr(ctype,
'.') : 0;
525 if (codeset && *codeset ==
'.') {
527 cd =
iconv_open(to ? to : codeset, from ? from : codeset);
531 codeset = lang ? strchr(lang,
'.') : 0;
532 if (cd == (
iconv_t) -1 && codeset && *codeset ==
'.') {
534 cd =
iconv_open(to ? to : codeset, from ? from : codeset);
538 if (cd == (
iconv_t) -1 && ctype && *ctype != 0 && strcmp (ctype,
"C") != 0)
539 cd =
iconv_open(to ? to : ctype, from ? from : ctype);
543 if (cd == (
iconv_t) -1 && lang && *lang != 0)
544 cd =
iconv_open(to ? to : lang, from ? from : lang);
547 if ((cd == (
iconv_t) -1 && ctype && strstr(ctype,
"@euro")) || (lang && strstr(lang,
"@euro")))
548 cd =
iconv_open(to ? to :
"ISO8859-15", from ? from :
"ISO8859-15");
Q_CORE_EXPORT QTextStream & bom(QTextStream &s)
static Ptr_iconv ptr_iconv
ConverterState internalState
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
#define QT_END_NAMESPACE
This macro expands to.
char * data()
Returns a pointer to the data stored in the byte array.
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
The QByteArray class provides an array of bytes.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
void * resolve(const char *symbol)
Returns the address of the exported symbol symbol.
The QString class provides a Unicode character string.
static bool setByteOrder(iconv_t cd)
QByteArray name() const
QTextCodec subclasses must reimplement this function.
The QChar class provides a 16-bit Unicode character.
#define QT_BEGIN_NAMESPACE
This macro expands to.
int mibEnum() const
Subclasses of QTextCodec must reimplement this function.
#define Q_GLOBAL_STATIC(TYPE, NAME)
Declares a global static variable with the given type and name.
T & localData()
Returns a reference to the data that was set by the calling thread.
iconv_t(* Ptr_iconv_open)(const char *, const char *)
static QTextCodec * codecForMib(int mib)
Returns the QTextCodec which matches the MIBenum mib.
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
static Ptr_iconv_open ptr_iconv_open
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...
#define Q_ASSERT_X(cond, where, what)
static void qIconvCodecStateFree(QTextCodec::ConverterState *state)
static void encode(uint *dst, QTextCodecStateFreeFunction fn)
static Ptr_iconv_close ptr_iconv_close
Q_CORE_EXPORT char * qstrdup(const char *)
QString convertToUnicode(const char *, int, ConverterState *) const
QTextCodec subclasses must reimplement this function.
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
QByteArray convertFromUnicode(const QChar *, int, ConverterState *) const
QTextCodec subclasses must reimplement this function.
void saveChars(const char *c, int count)
void resize(int size)
Sets the size of the byte array to size bytes.
int size() const
Returns the number of bytes in this byte array.
void setLoadHints(LoadHints hints)
bool qt_locale_initialized
int(* Ptr_iconv_close)(iconv_t)
The QTextCodec class provides conversions between text encodings.
size_t(* Ptr_iconv)(iconv_t, const char **, size_t *, char **, size_t *)
The QThreadStorage class provides per-thread data storage.
The QLibrary class loads shared libraries at runtime.
static iconv_t createIconv_t(const char *to, const char *from)