📄 qeuckrcodec.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the plugins of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************//*! \class QEucKrCodec qeuckrcodec.h \reentrant \internal*//* these must be made \internal virtual int mibEnum() const; const char* name() const; QTextDecoder* makeDecoder() const; QByteArray fromUnicode(const QString& uc, int& lenInOut) const; QString toUnicode(const char* chars, int len) const;*/#include "qeuckrcodec.h"#ifndef QT_NO_TEXTCODECunsigned int qt_Ksc5601ToUnicode(unsigned int code);unsigned int qt_UnicodeToKsc5601(unsigned int unicode);#define IsEucChar(c) (((c) >= 0xa1) && ((c) <= 0xfe))#define QValidChar(u) ((u) ? QChar((ushort)(u)) : QChar(QChar::ReplacementCharacter))/*! \reimp*/QByteArray QEucKrCodec::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(); for (int i = 0; i < len; i++) { unsigned short ch = uc[i].unicode(); uint j; if (ch < 0x80) { // ASCII *cursor++ = ch; } else if ((j = qt_UnicodeToKsc5601(ch))) { // KSC 5601 *cursor++ = (j >> 8) | 0x80; *cursor++ = (j & 0xff) | 0x80; } else { // Error *cursor++ = replacement; ++invalid; } } rstr.resize(cursor - (uchar*)rstr.constData()); if (state) { state->invalidChars += invalid; } return rstr;}QString QEucKrCodec::convertToUnicode(const char* chars, int len, ConverterState *state) const{ uchar buf[2] = {0, 0}; 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; for (int i=0; i<len; i++) { uchar ch = chars[i]; if (ch == 0) break; switch (nbuf) { case 0: if (ch < 0x80) { // ASCII result += QLatin1Char(ch); } else if (IsEucChar(ch)) { // KSC 5601 buf[0] = ch; nbuf = 1; } else { // Invalid result += replacement; ++invalid; } break; case 1: // KSC 5601 if (IsEucChar(ch)) { uint u = qt_Ksc5601ToUnicode((buf[0] << 8) | ch); result += QValidChar(u); } 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;}int QEucKrCodec::_mibEnum(){ return 38;}QByteArray QEucKrCodec::_name(){ return "EUC-KR";}#ifdef Q_WS_X11QFontKsc5601Codec::QFontKsc5601Codec(){}QByteArray QFontKsc5601Codec::_name(){ return "ksc5601.1987-0";}int QFontKsc5601Codec::_mibEnum(){ return 36;}QString QFontKsc5601Codec::convertToUnicode(const char*, int, ConverterState *) const{ return QString();}QByteArray QFontKsc5601Codec::convertFromUnicode(const QChar *uc, int len, ConverterState *) const{ QByteArray result; result.resize(len * 2); uchar *rdata = (uchar *) result.data(); const QChar *ucp = uc; for (int i = 0; i < len; i++) { QChar ch(*ucp++); ch = qt_UnicodeToKsc5601(ch.unicode()); if (! ch.isNull()) { *rdata++ = ch.row() & 0x7f ; *rdata++ = ch.cell() & 0x7f; } else { //white square *rdata++ = 0x21; *rdata++ = 0x60; } } return result;}#endif // Q_WS_X11// code converter wrapperstatic unsigned short ksc2unicode (unsigned short code);static unsigned short unicode2ksc (unsigned short code);unsigned int qt_Ksc5601ToUnicode(unsigned int code){#if 0 printf("qt_Ksc5601ToUnicode : code = %x, unicode = %x\n", code, ksc2unicode((unsigned short)code));#endif return ksc2unicode((unsigned short)code);}unsigned int qt_UnicodeToKsc5601(unsigned int unicode){#if 0 printf("qt_UnicodeToKsc5601 : unicode = %x, %x\n", unicode, unicode2ksc((unsigned short)unicode));#endif return unicode2ksc((unsigned short)unicode);}/* Table including ksc5601 hangul to unicode */static const unsigned short ksc5601_hangul_to_unicode[2350]={ 0xac00, 0xac01, 0xac04, 0xac07, 0xac08, 0xac09, 0xac0a, 0xac10, 0xac11, 0xac12, 0xac13, 0xac14, 0xac15, 0xac16, 0xac17, 0xac19, 0xac1a, 0xac1b, 0xac1c, 0xac1d, 0xac20, 0xac24, 0xac2c, 0xac2d, 0xac2f, 0xac30, 0xac31, 0xac38, 0xac39, 0xac3c, 0xac40, 0xac4b, 0xac4d, 0xac54, 0xac58, 0xac5c, 0xac70, 0xac71, 0xac74, 0xac77,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -