source: trunk/src/corelib/codecs/qtsciicodec.cpp@ 168

Last change on this file since 168 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 14.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42// Most of the code here was originally written by Hans Petter Bieker,
43// and is included in Qt with the author's permission, and the grateful
44// thanks of the Trolltech team.
45
46#include "qtsciicodec_p.h"
47#include "qlist.h"
48
49#ifndef QT_NO_CODECS
50
51QT_BEGIN_NAMESPACE
52
53static unsigned char qt_UnicodeToTSCII(ushort u1, ushort u2, ushort u3);
54static unsigned int qt_TSCIIToUnicode(unsigned int code, uint *s);
55
56#define IsTSCIIChar(c) (((c) >= 0x80) && ((c) <= 0xfd))
57
58/*! \class QTsciiCodec
59 \reentrant
60 \internal
61*/
62
63/*!
64 Destroys the text codec object.
65*/
66QTsciiCodec::~QTsciiCodec()
67{
68}
69
70/*!
71 Converts the first \a len characters in \a uc from Unicode to this
72 encoding, and returns the result in a byte array. The \a state contains
73 some conversion flags, and is used by the codec to maintain state
74 information.
75*/
76QByteArray QTsciiCodec::convertFromUnicode(const QChar *uc, int len, ConverterState *state) const
77{
78 char replacement = '?';
79 if (state) {
80 if (state->flags & ConvertInvalidToNull)
81 replacement = 0;
82 }
83 int invalid = 0;
84
85 QByteArray rstr;
86 rstr.resize(len);
87 uchar* cursor = (uchar*)rstr.data();
88 for (int i = 0; i < len; i++) {
89 QChar ch = uc[i];
90 uchar j;
91 if (ch.row() == 0x00 && ch.cell() < 0x80) {
92 // ASCII
93 j = ch.cell();
94 } else if ((j = qt_UnicodeToTSCII(uc[i].unicode(),
95 uc[i + 1].unicode(),
96 uc[i + 2].unicode()))) {
97 // We have to check the combined chars first!
98 i += 2;
99 } else if ((j = qt_UnicodeToTSCII(uc[i].unicode(),
100 uc[i + 1].unicode(), 0))) {
101 i++;
102 } else if ((j = qt_UnicodeToTSCII(uc[i].unicode(), 0, 0))) {
103 } else {
104 // Error
105 j = replacement;
106 ++invalid;
107 }
108 *cursor++ = j;
109 }
110 rstr.resize(cursor - (const uchar*)rstr.constData());
111
112 if (state) {
113 state->invalidChars += invalid;
114 }
115 return rstr;
116}
117
118/*!
119 Converts the first \a len characters in \a chars from this encoding
120 to Unicode, and returns the result in a QString. The \a state contains
121 some conversion flags, and is used by the codec to maintain state
122 information.
123*/
124QString QTsciiCodec::convertToUnicode(const char* chars, int len, ConverterState *state) const
125{
126 QChar replacement = QChar::ReplacementCharacter;
127 if (state) {