📄 qgb18030codec.cpp
字号:
} nbuf = 0; break; } } if (state) { state->remainingChars = nbuf; state->state_data[0] = (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + buf[3]; state->invalidChars += invalid; } return result;}/*! \class QGbkCodec \reentrant \internal \brief The QGbkCodec class provides conversion to and from the Chinese GBK encoding. GBK, formally the Chinese Internal Code Specification, is a commonly used extension of GB 2312-80. Microsoft Windows uses it under the name code page 936. The GBK encoding has been superseded by the GB18030 encoding and GB18030 is backward compatible to GBK. For this reason the QGbkCodec class is implemented in terms of the GB18030 codec and uses its 1-byte and 2-byte portion for conversion from and to Unicode. The QGbkCodec is kept mainly for compatibility reasons with older software.*//*! Constructs a QGbkCodec object.*/QGbkCodec::QGbkCodec() : QGb18030Codec(){}int QGbkCodec::_mibEnum(){ return 113;}QByteArray QGbkCodec::_name(){ return "GBK";}QList<QByteArray> QGbkCodec::_aliases(){ QList<QByteArray> list; list << "CP936" << "MS936" << "windows-936"; return list;}QString QGbkCodec::convertToUnicode(const char* chars, int len, ConverterState *state) const{ uchar buf[2]; int nbuf = 0; QChar replacement = QChar::ReplacementCharacter; if (state) { if (state->flags & ConvertInvalidToNull) replacement = QChar::Null; nbuf = state->remainingChars; buf[0] = state->state_data[0]; buf[1] = state->state_data[1]; } int invalid = 0; QString result; //qDebug("QGbkDecoder::toUnicode(const char* chars = \"%s\", int len = %d)", chars, len); for (int i=0; i<len; i++) { uchar ch = chars[i]; switch (nbuf) { case 0: if (ch < 0x80) { // ASCII result += QLatin1Char(ch); } else if (Is1stByte(ch)) { // GBK 1st byte? buf[0] = ch; nbuf = 1; } else { // Invalid result += replacement; ++invalid; } break; case 1: // GBK 2nd byte if (Is2ndByteIn2Bytes(ch)) { buf[1] = ch; int clen = 2; uint u = qt_Gb18030ToUnicode(buf, clen); if (clen == 2) { result += QValidChar(u); } else { result += replacement; ++invalid; } nbuf = 0; } else { // Error result += replacement; ++invalid; nbuf = 0; } break; } } if (state) { state->remainingChars = nbuf; state->state_data[0] = buf[0]; state->state_data[1] = buf[1]; state->invalidChars += invalid; } return result;}QByteArray QGbkCodec::convertFromUnicode(const QChar *uc, int len, ConverterState *state) const{ char replacement = '?'; if (state) { if (state->flags & ConvertInvalidToNull) replacement = 0; } int invalid = 0; int rlen = 2*len + 1; QByteArray rstr; rstr.resize(rlen); uchar* cursor = (uchar*)rstr.data(); //qDebug("QGbkCodec::fromUnicode(const QString& uc, int& lenInOut = %d)", lenInOut); for (int i = 0; i < len; i++) { QChar ch = uc[i]; uchar buf[2]; if (ch.row() == 0x00 && ch.cell() < 0x80) { // ASCII *cursor++ = ch.cell(); } else if (qt_UnicodeToGbk(ch.unicode(), buf) == 2) { *cursor++ = buf[0]; *cursor++ = buf[1]; } else { // Error *cursor += replacement; ++invalid; } } rstr.resize(cursor - (uchar*)rstr.constData()); if (state) { state->invalidChars += invalid; } return rstr;}/*! \class QGb2312Codec \internal \brief The QGb2312Codec class provides conversion to and from the Chinese GB2312 encoding. The GB2312 encoding has been superseded by the GB18030 encoding and GB18030 is backward compatible to GB2312. For this reason the QGb2312Codec class is implemented in terms of the GB18030 codec and uses its 0xA1A1-0xFEFE subset for conversion from and to Unicode. The QGb2312Codec is kept mainly for compatibility reasons with older software.*//*! Constructs a QGb2312Codec object.*/QGb2312Codec::QGb2312Codec() : QGb18030Codec(){}int QGb2312Codec::_mibEnum(){ return 2025;}QByteArray QGb2312Codec::_name(){ return "GB2312";}QString QGb2312Codec::convertToUnicode(const char* chars, int len, ConverterState *state) const{ uchar buf[2]; int nbuf = 0; QChar replacement = QChar::ReplacementCharacter; if (state) { if (state->flags & ConvertInvalidToNull) replacement = QChar::Null; nbuf = state->remainingChars; buf[0] = state->state_data[0]; buf[1] = state->state_data[1]; } int invalid = 0; QString result; //qDebug("QGb2312Decoder::toUnicode(const char* chars, int len = %d)", len); for (int i=0; i<len; i++) { uchar ch = chars[i]; switch (nbuf) { case 0:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -