📄 qeuckrcodec.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 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://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/// Most of the cp949 code was originally written by Joon-Kyu Park, and is included // in Qt with the author's permission and the grateful thanks of the Trolltech team./*! \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"#include "cp949codetbl.h"#include <stdlib.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 IsCP949Char(c) (((c) >= 0x81) && ((c) <= 0xa0))#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);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -