1 /****************************************************************************
2 ** $Id: qt/src/tools/qutfcodec.cpp 2.3.2 edited 2001-01-26 $
4 ** Implementation of QEucCodec class
8 ** Copyright (C)1998-2000 Trolltech AS. All rights reserved.
10 ** This file is part of the tools module of the Qt GUI Toolkit.
12 ** This file may be distributed under the terms of the Q Public License
13 ** as defined by Trolltech AS of Norway and appearing in the file
14 ** LICENSE.QPL included in the packaging of this file.
16 ** This file may be distributed and/or modified under the terms of the
17 ** GNU General Public License version 2 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.GPL included in the
19 ** packaging of this file.
21 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22 ** licenses may use this file in accordance with the Qt Commercial License
23 ** Agreement provided with the Software.
25 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29 ** information about Qt Commercial License Agreements.
30 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
31 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
33 ** Contact info@trolltech.com if any conditions of this licensing are
36 **********************************************************************/
38 #include "qutfcodec.h"
40 #ifndef QT_NO_TEXTCODEC
42 int QUtf8Codec::mibEnum() const
47 QCString QUtf8Codec::fromUnicode(const QString& uc, int& len_in_out) const
49 int l = QMIN((int)uc.length(),len_in_out);
52 uchar* cursor = (uchar*)rstr.data();
53 for (int i=0; i<l; i++) {
55 if ( !ch.row() && ch.cell() < 0x80 ) {
56 *cursor++ = ch.cell();
58 uchar b = (ch.row() << 2) | (ch.cell() >> 6);
59 if ( ch.row() < 0x08 ) {
62 *cursor++ = 0xe0 | (ch.row() >> 4);
63 *cursor++ = 0x80 | (b&0x3f);
65 *cursor++ = 0x80 | (ch.cell()&0x3f);
68 len_in_out = cursor - (uchar*)rstr.data();
69 rstr.truncate(len_in_out);
73 const char* QUtf8Codec::name() const
78 int QUtf8Codec::heuristicContentMatch(const char* chars, int len) const
81 for (int i=0; i<len; i++) {
89 } else if ( (ch&0xe0) == 0xc0 ) {
91 uchar c2 = chars[++i];
92 if ( (c2&0xc0) != 0x80 )
96 } else if ( (ch&0xf0) == 0xe0 ) {
98 uchar c2 = chars[++i];
99 if ( (c2&0xc0) != 0x80 ) {
103 uchar c3 = chars[++i];
104 if ( (c3&0xc0) != 0x80 )
120 class QUtf8Decoder : public QTextDecoder {
124 QUtf8Decoder() : need(0)
128 QString toUnicode(const char* chars, int len)
131 for (int i=0; i<len; i++) {
134 if ( (ch&0xc0) == 0x80 ) {
135 uc = (uc << 6) | (ch & 0x3f);
142 result += QChar::replacement;
148 } else if ( (ch&0xe0) == 0xc0 ) {
151 } else if ( (ch&0xf0) == 0xe0 ) {
161 QTextDecoder* QUtf8Codec::makeDecoder() const
163 return new QUtf8Decoder;
171 int QUtf16Codec::mibEnum() const
176 const char* QUtf16Codec::name() const
178 return "ISO-10646-UCS-2";
181 int QUtf16Codec::heuristicContentMatch(const char* chars, int len) const
183 uchar* uchars = (uchar*)chars;
184 if ( len >= 2 && ((uchars[0] == 0xff && uchars[1] == 0xfe) ||
185 (uchars[1] == 0xff && uchars[0] == 0xfe)) )
194 class QUtf16Encoder : public QTextEncoder {
197 QUtf16Encoder() : headerdone(FALSE)
201 QCString fromUnicode(const QString& uc, int& len_in_out)
204 len_in_out = uc.length()*sizeof(QChar);
205 QCString d(len_in_out);
206 memcpy(d.data(),uc.unicode(),len_in_out);
210 len_in_out = (1+uc.length())*sizeof(QChar);
211 QCString d(len_in_out);
212 memcpy(d.data(),&QChar::byteOrderMark,sizeof(QChar));
213 memcpy(d.data()+sizeof(QChar),uc.unicode(),uc.length()*sizeof(QChar));
219 class QUtf16Decoder : public QTextDecoder {
226 QUtf16Decoder() : half(FALSE), swap(FALSE), headerdone(FALSE)
230 QString toUnicode(const char* chars, int len)
242 ch.cell() = *chars++;
245 if ( ch == QChar::byteOrderSwapped ) {
247 } else if ( ch == QChar::byteOrderMark ) {
266 QTextDecoder* QUtf16Codec::makeDecoder() const
268 return new QUtf16Decoder;
271 QTextEncoder* QUtf16Codec::makeEncoder() const
273 return new QUtf16Encoder;
276 #endif // QT_NO_TEXTCODEC