📄 qgbkcodec.cpp
字号:
/****************************************************************************** $Id: qt/src/tools/qgbkcodec.cpp 2.3.1 edited 2001-01-26 $**** Implementation of QGbkCodec template/macro class**** 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.************************************************************************//*! \class QGbkCodec qgbkcodec.h \brief This 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 codec was contributed to Qt by Justin Yu \<justiny@turbolinux.com.cn\> and Sean Chen \<seanc@turbolinux.com.cn\>. The copyright notice for their code follows: \mustquote Copyright 2000 TurboLinux, Inc. Written by Justin Yu and Sean Chen. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:<ol> <li> Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. <li> Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. </ol> THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/#include "qgbkcodec.h"#ifndef QT_NO_CODECS#define IsLatin(c) (((c) <= 0x80))#define IsFirstByte(c) (((c) >= 0x81) && ((c) <= 0xFE))#define IsSecondByte(c) ((((c) >= 0x40) && ((c) <= 0x7E)) || ((((c) >= 0x80) && ((c) <= 0xFE))))#define QValidChar(u) ((u) ? QChar((ushort)(u)) : QChar::replacement)static unsigned int qt_GBKToUnicode(unsigned int code);unsigned int qt_UnicodeToGBK(unsigned int unicode);/*! \internal */QGbkCodec::QGbkCodec(){}QGbkCodec::~QGbkCodec(){}/*! \reimp */int QGbkCodec::mibEnum() const{ /* There is no MIBEnum for GBK now */ return 2027;}/*! \reimp */QCString QGbkCodec::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_UnicodeToGBK((ch.row() << 8) | ch.cell())) ) { // GBK *cursor++ = (j >> 8) | 0x80; *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;}/*! \reimp */QString QGbkCodec::toUnicode(const char* chars, int len) const{ QString result; for (int i=0; i<len; i++) { uchar ch = chars[i]; if ( IsLatin(ch) ) { // ASCII result += QChar(ch); } else if ( IsFirstByte(ch) ) { // GBK ? if ( i < len-1 ) { uchar c2 = chars[++i]; if ( IsSecondByte(c2) ) { uint u = qt_GBKToUnicode((ch << 8) | c2); result += QValidChar(u); } else { i--; result += QChar::replacement; } } else { //Bad String result += QChar::replacement; } } else { // Invalid result += QChar::replacement; } } return result;}/*! \reimp */const char* QGbkCodec::name() const{ return "GBK";}/*! \reimp */int QGbkCodec::heuristicNameMatch(const char* hint) const{ int score = 0; bool zh = FALSE; if (qstrnicmp(hint, "zh_CN", 5) == 0){ score += 10; zh = TRUE; } const char *p; if ( zh ) { p = strchr(hint, '.'); if ( p == 0 ) return score; p++; } else { p = hint; } if (p) { if (qstricmp(p, "GBK") == 0) return score + 4; else if (qstricmp(p, "GB2312") == 0) return score + 2; } return 0;}/*! \reimp */int QGbkCodec::heuristicContentMatch(const char* /*chars*/, int /*len*/) const{ return 0;}static const unsigned short __gbk_to_ucs[] ={ 0x4e02, 0x4e04, 0x4e05, 0x4e06, 0x4e0f, 0x4e12, 0x4e17, 0x4e1f, 0x4e20, 0x4e21, 0x4e23, 0x4e26, 0x4e29, 0x4e2e, 0x4e2f, 0x4e31, 0x4e33, 0x4e35, 0x4e37, 0x4e3c, 0x4e40, 0x4e41, 0x4e42, 0x4e44, 0x4e46, 0x4e4a, 0x4e51, 0x4e55, 0x4e57, 0x4e5a, 0x4e5b, 0x4e62, 0x4e63, 0x4e64, 0x4e65, 0x4e67, 0x4e68, 0x4e6a, 0x4e6b, 0x4e6c, 0x4e6d, 0x4e6e, 0x4e6f, 0x4e72, 0x4e74, 0x4e75, 0x4e76, 0x4e77, 0x4e78, 0x4e79, 0x4e7a, 0x4e7b, 0x4e7c, 0x4e7d, 0x4e7f, 0x4e80, 0x4e81, 0x4e82, 0x4e83, 0x4e84, 0x4e85, 0x4e87, 0x4e8a, 0x0000, 0x4e90, 0x4e96, 0x4e97, 0x4e99, 0x4e9c, 0x4e9d, 0x4e9e, 0x4ea3, 0x4eaa, 0x4eaf, 0x4eb0, 0x4eb1, 0x4eb4, 0x4eb6, 0x4eb7, 0x4eb8, 0x4eb9, 0x4ebc, 0x4ebd, 0x4ebe, 0x4ec8, 0x4ecc, 0x4ecf, 0x4ed0, 0x4ed2, 0x4eda, 0x4edb, 0x4edc, 0x4ee0, 0x4ee2, 0x4ee6, 0x4ee7, 0x4ee9, 0x4eed, 0x4eee, 0x4eef, 0x4ef1, 0x4ef4, 0x4ef8, 0x4ef9, 0x4efa, 0x4efc, 0x4efe, 0x4f00, 0x4f02, 0x4f03, 0x4f04, 0x4f05, 0x4f06, 0x4f07, 0x4f08, 0x4f0b, 0x4f0c, 0x4f12, 0x4f13, 0x4f14, 0x4f15, 0x4f16, 0x4f1c, 0x4f1d, 0x4f21, 0x4f23, 0x4f28, 0x4f29, 0x4f2c, 0x4f2d, 0x4f2e, 0x4f31, 0x4f33, 0x4f35, 0x4f37, 0x4f39, 0x4f3b, 0x4f3e, 0x4f3f, 0x4f40, 0x4f41, 0x4f42, 0x4f44, 0x4f45, 0x4f47, 0x4f48, 0x4f49, 0x4f4a, 0x4f4b, 0x4f4c, 0x4f52, 0x4f54, 0x4f56, 0x4f61, 0x4f62, 0x4f66, 0x4f68, 0x4f6a, 0x4f6b, 0x4f6d,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -