0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the plugins of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
// Most of the code here was originally written by Serika Kurusugawa
|
|
43 |
// a.k.a. Junji Takagi, and is included in Qt with the author's permission,
|
|
44 |
// and the grateful thanks of the Qt team.
|
|
45 |
|
|
46 |
/*! \class QSjisCodec
|
|
47 |
\reentrant
|
|
48 |
\internal
|
|
49 |
*/
|
|
50 |
|
|
51 |
#include "qsjiscodec.h"
|
|
52 |
#include "qlist.h"
|
|
53 |
|
|
54 |
QT_BEGIN_NAMESPACE
|
|
55 |
|
|
56 |
#ifndef QT_NO_TEXTCODEC
|
|
57 |
enum {
|
|
58 |
Esc = 0x1b
|
|
59 |
};
|
|
60 |
|
|
61 |
#define IsKana(c) (((c) >= 0xa1) && ((c) <= 0xdf))
|
|
62 |
#define IsSjisChar1(c) ((((c) >= 0x81) && ((c) <= 0x9f)) || \
|
|
63 |
(((c) >= 0xe0) && ((c) <= 0xfc)))
|
|
64 |
#define IsSjisChar2(c) (((c) >= 0x40) && ((c) != 0x7f) && ((c) <= 0xfc))
|
|
65 |
#define IsUserDefinedChar1(c) (((c) >= 0xf0) && ((c) <= 0xfc))
|
|
66 |
|
|
67 |
#define QValidChar(u) ((u) ? QChar((ushort)(u)) : QChar(QChar::ReplacementCharacter))
|
|
68 |
|
|
69 |
/*!
|
|
70 |
Creates a Shift-JIS codec. Note that this is done automatically by
|
|
71 |
the QApplication, you do not need construct your own.
|
|
72 |
*/
|
|
73 |
QSjisCodec::QSjisCodec() : conv(QJpUnicodeConv::newConverter(QJpUnicodeConv::Default))
|
|
74 |
{
|
|
75 |
}
|
|
76 |
|
|
77 |
|
|
78 |
/*!
|
|
79 |
Destroys the Shift-JIS codec.
|
|
80 |
*/
|
|
81 |
QSjisCodec::~QSjisCodec()
|
|
82 |
{
|
|
83 |
delete (QJpUnicodeConv*)conv;
|
|
84 |
conv = 0;
|
|
85 |
}
|
|
86 |
|
|
87 |
|
|
88 |
QByteArray QSjisCodec::convertFromUnicode(const QChar *uc, int len, ConverterState *state) const
|
|
89 |
{
|
|
90 |
char replacement = '?';
|
|
91 |
if (state) {
|
|
92 |
if (state->flags & ConvertInvalidToNull)
|
|
93 |
replacement = 0;
|
|
94 |
}
|
|
95 |
int invalid = 0;
|
|
96 |
|
|
97 |
int rlen = 2*len + 1;
|
|
98 |
QByteArray rstr;
|
|
99 |
rstr.resize(rlen);
|
|
100 |
uchar* cursor = (uchar*)rstr.data();
|
|
101 |
for (int i = 0; i < len; i++) {
|
|
102 |
QChar ch = uc[i];
|
|
103 |
uint j;
|
|
104 |
if (ch.row() == 0x00 && ch.cell() < 0x80) {
|
|
105 |
// ASCII
|
|
106 |
*cursor++ = ch.cell();
|
|
107 |
} else if ((j = conv->unicodeToJisx0201(ch.row(), ch.cell())) != 0) {
|
|
108 |
// JIS X 0201 Latin or JIS X 0201 Kana
|
|
109 |
*cursor++ = j;
|
|
110 |
} else if ((j = conv->unicodeToSjis(ch.row(), ch.cell())) != 0) {
|
|
111 |
// JIS X 0208
|
|
112 |
*cursor++ = (j >> 8);
|
|
113 |
*cursor++ = (j & 0xff);
|
|
114 |
} else if ((j = conv->unicodeToSjisibmvdc(ch.row(), ch.cell())) != 0) {
|
|
115 |
// JIS X 0208 IBM VDC
|
|
116 |
*cursor++ = (j >> 8);
|
|
117 |
*cursor++ = (j & 0xff);
|
|
118 |
} else if ((j = conv->unicodeToCp932(ch.row(), ch.cell())) != 0) {
|
|
119 |
// CP932 (for lead bytes 87, ee & ed)
|
|
120 |
*cursor++ = (j >> 8);
|
|
121 |
*cursor++ = (j & 0xff);
|
|
122 |
} else if ((j = conv->unicodeToJisx0212(ch.row(), ch.cell())) != 0) {
|
|
123 |
// JIS X 0212 (can't be encoded in ShiftJIS !)
|
|
124 |
*cursor++ = 0x81; // white square
|
|
125 |
*cursor++ = 0xa0; // white square
|
|
126 |
} else {
|
|
127 |
// Error
|
|
128 |
*cursor++ = replacement;
|
|
129 |
++invalid;
|
|
130 |
}
|
|
131 |
}
|
|
132 |
rstr.resize(cursor - (const uchar*)rstr.constData());
|
|
133 |
|
|
134 |
if (state) {
|
|
135 |
state->invalidChars += invalid;
|
|
136 |
}
|
|
137 |
return rstr;
|
|
138 |
}
|
|
139 |
|
|
140 |
QString QSjisCodec::convertToUnicode(const char* chars, int len, ConverterState *state) const
|
|
141 |
{
|
|
142 |
uchar buf[1] = {0};
|
|
143 |
int nbuf = 0;
|
|
144 |
QChar replacement = QChar::ReplacementCharacter;
|
|
145 |
if (state) {
|
|
146 |
if (state->flags & ConvertInvalidToNull)
|
|
147 |
replacement = QChar::Null;
|
|
148 |
nbuf = state->remainingChars;
|
|
149 |
buf[0] = state->state_data[0];
|
|
150 |
}
|
|
151 |
int invalid = 0;
|
|
152 |
uint u= 0;
|
|
153 |
QString result;
|
|
154 |
for (int i=0; i<len; i++) {
|
|
155 |
uchar ch = chars[i];
|
|
156 |
switch (nbuf) {
|
|
157 |
case 0:
|
|
158 |
if (ch < 0x80 || IsKana(ch)) {
|
|
159 |
// JIS X 0201 Latin or JIS X 0201 Kana
|
|
160 |
u = conv->jisx0201ToUnicode(ch);
|
|
161 |
result += QValidChar(u);
|
|
162 |
} else if (IsSjisChar1(ch)) {
|
|
163 |
// JIS X 0208
|
|
164 |
buf[0] = ch;
|
|
165 |
nbuf = 1;
|
|
166 |
} else {
|
|
167 |
// Invalid
|
|
168 |
result += replacement;
|
|
169 |
++invalid;
|
|
170 |
}
|
|
171 |
break;
|
|
172 |
case 1:
|
|
173 |
// JIS X 0208
|
|
174 |
if (IsSjisChar2(ch)) {
|
|
175 |
if ((u = conv->sjisibmvdcToUnicode(buf[0], ch))) {
|
|
176 |
result += QValidChar(u);
|
|
177 |
} else if ((u = conv->cp932ToUnicode(buf[0], ch))) {
|
|
178 |
result += QValidChar(u);
|
|
179 |
}
|
|
180 |
else if (IsUserDefinedChar1(buf[0])) {
|
|
181 |
result += QChar::ReplacementCharacter;
|
|
182 |
} else {
|
|
183 |
u = conv->sjisToUnicode(buf[0], ch);
|
|
184 |
result += QValidChar(u);
|
|
185 |
}
|
|
186 |
} else {
|
|
187 |
// Invalid
|
|
188 |
result += replacement;
|
|
189 |
++invalid;
|
|
190 |
}
|
|
191 |
nbuf = 0;
|
|
192 |
break;
|
|
193 |
}
|
|
194 |
}
|
|
195 |
|
|
196 |
if (state) {
|
|
197 |
state->remainingChars = nbuf;
|
|
198 |
state->state_data[0] = buf[0];
|
|
199 |
state->invalidChars += invalid;
|
|
200 |
}
|
|
201 |
return result;
|
|
202 |
}
|
|
203 |
|
|
204 |
|
|
205 |
int QSjisCodec::_mibEnum()
|
|
206 |
{
|
|
207 |
return 17;
|
|
208 |
}
|
|
209 |
|
|
210 |
QByteArray QSjisCodec::_name()
|
|
211 |
{
|
|
212 |
return "Shift_JIS";
|
|
213 |
}
|
|
214 |
|
|
215 |
/*!
|
|
216 |
Returns the codec's mime name.
|
|
217 |
*/
|
|
218 |
QList<QByteArray> QSjisCodec::_aliases()
|
|
219 |
{
|
|
220 |
QList<QByteArray> list;
|
|
221 |
list << "SJIS" // Qt 3 compat
|
|
222 |
<< "MS_Kanji";
|
|
223 |
return list;
|
|
224 |
}
|
|
225 |
#endif // QT_NO_TEXTCODEC
|
|
226 |
|
|
227 |
QT_END_NAMESPACE
|