📄 qgb18030codec.cpp
字号:
} 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: if (ch < 0x80) { // ASCII result += QLatin1Char(ch); } else if (IsByteInGb2312(ch)) { // GB2312 1st byte? buf[0] = ch; nbuf = 1; } else { // Invalid result += replacement; ++invalid; } break; case 1: // GB2312 2nd byte if (IsByteInGb2312(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 QGb2312Codec::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("QGb2312Codec::fromUnicode(const QString& uc, int& lenInOut = %d) const", 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) && (buf[0] >= 0xA1) && (buf[1] >= 0xA1)) { *cursor++ = buf[0]; *cursor++ = buf[1]; } else { // Error *cursor++ = replacement; ++invalid; } } rstr.resize(cursor - (uchar*)rstr.constData()); if (state) { state->invalidChars += invalid; } return rstr;}#ifdef Q_WS_X11QFontGb2312Codec::QFontGb2312Codec(){}QByteArray QFontGb2312Codec::_name(){ return "gb2312.1980-0";}int QFontGb2312Codec::_mibEnum(){ return 57;}QString QFontGb2312Codec::convertToUnicode(const char* /*chars*/, int /*len*/, ConverterState *) const{ return QString();}QByteArray QFontGb2312Codec::convertFromUnicode(const QChar *uc, int len, ConverterState *) const{ QByteArray result; result.resize(len * 2); uchar *rdata = (uchar *) result.data(); const QChar *ucp = uc; //qDebug("QFontGb2312Codec::fromUnicode(const QString& uc, int& lenInOut = %d)", lenInOut); uchar buf[8]; for (int i = 0; i < len; i++) { QChar ch(*ucp++); int len = qt_UnicodeToGbk(ch.unicode(), buf); if (len == 2 && buf[0] > 0xa0 && buf[1] > 0xa0) { *rdata++ = buf[0] & 0x7f; *rdata++ = buf[1] & 0x7f; } else { *rdata++ = 0; *rdata++ = 0; } } return result;}QFontGbkCodec::QFontGbkCodec(){}QByteArray QFontGbkCodec::_name(){ return "gbk-0";}int QFontGbkCodec::_mibEnum(){ return -113;}QString QFontGbkCodec::convertToUnicode(const char* /*chars*/, int /*len*/, ConverterState *) const{ return QString();}QByteArray QFontGbkCodec::convertFromUnicode(const QChar *uc, int len, ConverterState *) const{ QByteArray result; result.resize(len * 2); uchar *rdata = (uchar *) result.data(); const QChar *ucp = uc; //qDebug("QFontGbkCodec::fromUnicode(const QString& uc, int& lenInOut = %d)", lenInOut); for (int i = 0; i < len; i++) { QChar ch(*ucp++); uchar buf[8]; int len = qt_UnicodeToGbk(ch.unicode(), buf); if (len == 2) { *rdata++ = buf[0]; *rdata++ = buf[1]; } else { *rdata++ = 0; *rdata++ = 0; } } return result;}QFontGb18030_0Codec::QFontGb18030_0Codec(){ //qDebug("QFontGb18030_0Codec::QFontGb18030_0Codec()");}QByteArray QFontGb18030_0Codec::_name(){ //qDebug("QFontGb18030_0Codec::name() = \"gb18030-0\""); return "gb18030-0";}int QFontGb18030_0Codec::_mibEnum(){ //qDebug("QFontGb18030_0Codec::mibEnum() = -114"); return -114;}QString QFontGb18030_0Codec::convertToUnicode(const char* /*chars*/, int /*len*/, ConverterState *) const{ return QString();}QByteArray QFontGb18030_0Codec::convertFromUnicode(const QChar *uc, int len, ConverterState *) const{ QByteArray result; result.resize(len * 2);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -