📄 qbig5codec.cpp
字号:
/****************************************************************************** $Id: qt/src/3rdparty/tools/qbig5codec.cpp 2.3.8 edited 2004-08-05 $**** Implementation of QBig5Codec class**** Created : 990713**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of the tools module of the Qt GUI Toolkit.**** This file may be distributed under the terms of the Q Public License** as defined by Trolltech AS of Norway and appearing in the file** LICENSE.QPL included in the packaging of this file.**** This file may be distributed and/or modified under the terms of the** GNU General Public License version 2 as published by the Free Software** Foundation and appearing in the file LICENSE.GPL included in the** packaging of this file.**** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition** licenses may use this file in accordance with the Qt Commercial License** Agreement provided with the Software.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.**** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for** information about Qt Commercial License Agreements.** See http://www.trolltech.com/qpl/ for QPL licensing information.** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/// Most of the code here was originally written by Ming-Che Chuang and// is included in Qt with the author's permission, and the grateful// thanks of the Trolltech team.// (Note: Ming-Che's code is also QPL'd, ref mail to into@trolltech.com)#include "qbig5codec.h"#ifndef QT_NO_BIG5CODECunsigned int qt_UnicodeToBig5(unsigned int unicode);static unsigned int qt_Big5ToUnicode(unsigned int code);static bool big5ucs(unsigned char *s,unsigned char *t);static bool ucsbig5(unsigned char *s,unsigned char *t);#define IsBig5Char(c) (((c) >= 0x81) && ((c) <= 0xfe))#define QValidChar(u) ((u) ? QChar((ushort)(u)) : QChar::replacement)int QBig5Codec::mibEnum() const{ return 2026;}QCString QBig5Codec::fromUnicode(const QString& uc, int& len_in_out) const{ int l = QMIN((int)uc.length(),len_in_out); int rlen = l*3+1; QCString rstr(rlen); uchar* cursor = (uchar*)rstr.data(); for (int i=0; i<l; i++) { QChar ch = uc[i]; uint j; if ( ch.row() == 0x00 && ch.cell() < 0x80 ) { // ASCII *cursor++ = ch.cell(); } else if ((j = qt_UnicodeToBig5((ch.row() << 8) | ch.cell())) ) { //Big5 1984 *cursor++ = (j >> 8); *cursor++ = (j & 0xff); } else if ( ch.unicode() == 0xa0 ) { *cursor++ = ' '; } else { // Error *cursor++ = '?'; // unknown char } } len_in_out = cursor - (uchar*)rstr.data(); rstr.truncate(len_in_out); return rstr;}QString QBig5Codec::toUnicode(const char* chars, int len) const{ QString result; for (int i=0; i<len; i++) { uchar ch = chars[i]; if ( ch < 0x80 ) { // ASCII result += QChar(ch); } else if ( IsBig5Char(ch) ) { // Big5 1984 if ( i < len-1 ) { uchar c2 = chars[++i]; if ( (c2 >=0x40) && (c2 <=0xfe) ) { uint u = qt_Big5ToUnicode((ch << 8) | c2); result += QValidChar(u); } else { i--; result += QChar::replacement; } } else { result += QChar::replacement; } } else { // Invalid result += QChar::replacement; } } return result;}const char* QBig5Codec::name() const{ return "Big5";}int QBig5Codec::heuristicNameMatch(const char* hint) const{ if (qstrnicmp(hint, "Big5", 4) == 0 ) return 4; else if (qstrnicmp(hint, "zh_TW.Big5", 10) == 0) return 10; else return QTextCodec::heuristicNameMatch(hint);}int QBig5Codec::heuristicContentMatch(const char* chars, int len) const{ int score = 0; for (int i=0; i<len; i++) { uchar ch = chars[i]; // No nulls allowed. if ( !ch ) return -1; if ( ch < 32 && ch != '\t' && ch != '\n' && ch != '\r' ) { // Suspicious if ( score ) score--; } else if ( ch < 0x80 ) { // Inconclusive score++; } else if ( IsBig5Char(ch) ) { // Big5 1984 if ( i < len-1 ) { uchar c2 = chars[++i]; if ( !((c2 >=0x40) && (c2<=0xfe) )) return -1; score+=2; } score++; } else { // Invalid return -1; } } return score;}unsigned int qt_UnicodeToBig5(unsigned int unicode){ unsigned char source[2],target[2]; source[0]=(unicode>>8); source[1]=(unicode & 0xff); ucsbig5(source,target); return ( (target[0]<<8) | target[1]);}static unsigned int qt_Big5ToUnicode(unsigned int code){ unsigned char source[2],target[2]; source[0]=(code >> 8); source[1]=(code & 0xff); big5ucs(source,target); return ((target[0]<<8) | target[1]);}class QBig5Decoder : public QTextDecoder { uchar buf[2]; int nbuf;public: QBig5Decoder() : nbuf(0) { } QString toUnicode(const char* chars, int len) { QString result; for (int i=0; i<len; i++) { uchar ch = chars[i]; switch (nbuf) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -