Qt 4.8
Public Functions | List of all members
QWindowsLocalCodec Class Reference
Inheritance diagram for QWindowsLocalCodec:
QTextCodec

Public Functions

QByteArray convertFromUnicode (const QChar *, int, ConverterState *) const
 QTextCodec subclasses must reimplement this function. More...
 
QString convertToUnicode (const char *, int, ConverterState *) const
 QTextCodec subclasses must reimplement this function. More...
 
QString convertToUnicodeCharByChar (const char *chars, int length, ConverterState *state) const
 
int mibEnum () const
 Subclasses of QTextCodec must reimplement this function. More...
 
QByteArray name () const
 QTextCodec subclasses must reimplement this function. More...
 
 QWindowsLocalCodec ()
 
 ~QWindowsLocalCodec ()
 
- Public Functions inherited from QTextCodec
virtual QList< QByteArrayaliases () const
 Subclasses can return a number of aliases for the codec in question. More...
 
bool canEncode (QChar) const
 Returns true if the Unicode character ch can be fully encoded with this codec; otherwise returns false. More...
 
bool canEncode (const QString &) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.s contains the string being tested for encode-ability. More...
 
QByteArray fromUnicode (const QString &uc) const
 Converts str from Unicode to the encoding of this codec, and returns the result in a QByteArray. More...
 
QByteArray fromUnicode (const QChar *in, int length, ConverterState *state=0) const
 Converts the first number of characters from the input array from Unicode to the encoding of this codec, and returns the result in a QByteArray. More...
 
QTextDecodermakeDecoder () const
 Creates a QTextDecoder which stores enough state to decode chunks of char * data to create chunks of Unicode data. More...
 
QTextDecodermakeDecoder (ConversionFlags flags) const
 
QTextEncodermakeEncoder () const
 Creates a QTextEncoder which stores enough state to encode chunks of Unicode data as char * data. More...
 
QTextEncodermakeEncoder (ConversionFlags flags) const
 
QString toUnicode (const QByteArray &) const
 Converts a from the encoding of this codec to Unicode, and returns the result in a QString. More...
 
QString toUnicode (const char *chars) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.chars contains the source characters. More...
 
QString toUnicode (const char *in, int length, ConverterState *state=0) const
 Converts the first size characters from the input from the encoding of this codec to Unicode, and returns the result in a QString. More...
 

Additional Inherited Members

- Public Types inherited from QTextCodec
enum  ConversionFlag { DefaultConversion, ConvertInvalidToNull = 0x80000000, IgnoreHeader = 0x1, FreeFunction = 0x2 }
 
- Static Public Functions inherited from QTextCodec
static QList< QByteArrayavailableCodecs ()
 Returns the list of all available codecs, by name. More...
 
static QList< int > availableMibs ()
 Returns the list of MIBs for all available codecs. More...
 
static QTextCodeccodecForCStrings ()
 Returns the codec used by QString to convert to and from const char * and QByteArrays. More...
 
static QTextCodeccodecForHtml (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. More...
 
static QTextCodeccodecForHtml (const QByteArray &ba, QTextCodec *defaultCodec)
 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. More...
 
static QTextCodeccodecForLocale ()
 Returns a pointer to the codec most suitable for this locale. More...
 
static QTextCodeccodecForMib (int mib)
 Returns the QTextCodec which matches the MIBenum mib. More...
 
static QTextCodeccodecForName (const QByteArray &name)
 Searches all installed QTextCodec objects and returns the one which best matches name; the match is case-insensitive. More...
 
static QTextCodeccodecForName (const char *name)
 Searches all installed QTextCodec objects and returns the one which best matches name; the match is case-insensitive. More...
 
static QTextCodeccodecForTr ()
 Returns the codec used by QObject::tr() on its argument. More...
 
static QTextCodeccodecForUtfText (const QByteArray &ba)
 Tries to detect the encoding of the provided snippet ba by using the BOM (Byte Order Mark) and returns a QTextCodec instance that is capable of decoding the text to unicode. More...
 
static QTextCodeccodecForUtfText (const QByteArray &ba, QTextCodec *defaultCodec)
 Tries to detect the encoding of the provided snippet ba by using the BOM (Byte Order Mark) and returns a QTextCodec instance that is capable of decoding the text to unicode. More...
 
static void setCodecForCStrings (QTextCodec *c)
 
static void setCodecForLocale (QTextCodec *c)
 Set the codec to c; this will be returned by codecForLocale(). More...
 
static void setCodecForTr (QTextCodec *c)
 
- Protected Functions inherited from QTextCodec
 QTextCodec ()
 Constructs a QTextCodec, and gives it the highest precedence. More...
 
virtual ~QTextCodec ()
 Destroys the QTextCodec. More...
 

Detailed Description

Definition at line 247 of file qtextcodec.cpp.

Constructors and Destructors

◆ QWindowsLocalCodec()

QWindowsLocalCodec::QWindowsLocalCodec ( )

Definition at line 262 of file qtextcodec.cpp.

263 {
264 }

◆ ~QWindowsLocalCodec()

QWindowsLocalCodec::~QWindowsLocalCodec ( )

Definition at line 266 of file qtextcodec.cpp.

267 {
268 }

Functions

◆ convertFromUnicode()

QByteArray QWindowsLocalCodec::convertFromUnicode ( const QChar input,
int  number,
ConverterState state 
) const
virtual

QTextCodec subclasses must reimplement this function.

Converts the first number of characters from the input array from Unicode to the encoding of the subclass, and returns the result in a QByteArray.

state can be 0 in which case the conversion is stateless and default conversion rules should be used. If state is not 0, the codec should save the state after the conversion in state, and adjust the remainingChars and invalidChars members of the struct.

Implements QTextCodec.

Definition at line 412 of file qtextcodec.cpp.

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 }
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
The QString class provides a Unicode character string.
Definition: qstring.h:83
Q_CORE_EXPORT void qWarning(const char *,...)
static const char * data(const QByteArray &arr)

◆ convertToUnicode()

QString QWindowsLocalCodec::convertToUnicode ( const char *  chars,
int  len,
ConverterState state 
) const
virtual

QTextCodec subclasses must reimplement this function.

Converts the first len characters of chars from the encoding of the subclass to Unicode, and returns the result in a QString.

state can be 0, in which case the conversion is stateless and default conversion rules should be used. If state is not 0, the codec should save the state after the conversion in state, and adjust the remainingChars and invalidChars members of the struct.

Implements QTextCodec.

Definition at line 270 of file qtextcodec.cpp.

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 }
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
Q_CORE_EXPORT void qWarning(const char *,...)
QString & append(QChar c)
Definition: qstring.cpp:1777
QString convertToUnicodeCharByChar(const char *chars, int length, ConverterState *state) const
Definition: qtextcodec.cpp:351

◆ convertToUnicodeCharByChar()

QString QWindowsLocalCodec::convertToUnicodeCharByChar ( const char *  chars,
int  length,
ConverterState state 
) const

Definition at line 351 of file qtextcodec.cpp.

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 }
Q_CORE_EXPORT QTextStream & ws(QTextStream &s)
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
QString & append(QChar c)
Definition: qstring.cpp:1777

◆ mibEnum()

int QWindowsLocalCodec::mibEnum ( ) const
virtual

Subclasses of QTextCodec must reimplement this function.

It returns the MIBenum (see IANA character-sets encoding file for more information). It is important that each QTextCodec subclass returns the correct unique value for this function.

Implements QTextCodec.

Definition at line 449 of file qtextcodec.cpp.

450 {
451  return 0;
452 }

◆ name()

QByteArray QWindowsLocalCodec::name ( ) const
virtual

QTextCodec subclasses must reimplement this function.

It returns the name of the encoding supported by the subclass.

If the codec is registered as a character set in the IANA character-sets encoding file this method should return the preferred mime name for the codec if defined, otherwise its name.

Implements QTextCodec.

Definition at line 444 of file qtextcodec.cpp.

445 {
446  return "System";
447 }

The documentation for this class was generated from the following file: