source: trunk/src/plugins/codecs/jp/qsjiscodec.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: 7.0 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 plugins 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 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 Trolltech team.
45
46/*! \class QSjisCodec
47 \reentrant
48 \internal
49*/
50
51#include "qsjiscodec.h"
52#include "qlist.h"
53
54QT_BEGIN_NAMESPACE
55
56#ifndef QT_NO_TEXTCODEC
57enum {
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*/
73QSjisCodec::QSjisCodec() : conv(QJpUnicodeConv::newConverter(QJpUnicodeConv::Default))
74{
75}
76
77
78/*!
79 Destroys the Shift-JIS codec.
80*/
81QSjisCodec::~QSjisCodec()
82{
83 delete (QJpUnicodeConv*)conv;
84 conv = 0;
85}
86
87
88QByteArray 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
140QString 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
205int QSjisCodec::_mibEnum()
206{
207 return 17;
208}
209
210QByteArray QSjisCodec::_name()
211{
212 return "Shift_JIS";
213}
214
215/*!
216 Returns the codec's mime name.
217*/
218QList<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
227QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.