~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/plugins/codecs/jp/qeucjpcodec.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the internationalization module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
// Most of the code here was originally written by Serika Kurusugawa
 
30
// a.k.a. Junji Takagi, and is included in Qt with the author's permission,
 
31
// and the grateful thanks of the Trolltech team.
 
32
 
 
33
/*! \class QEucJpCodec qeucjpcodec.h
 
34
    \reentrant
 
35
    \internal
 
36
 
 
37
  \brief The QEucJpCodec class provides conversion to and from EUC-JP character sets.
 
38
 
 
39
  More precisely, the QEucJpCodec class subclasses QTextCodec to
 
40
  provide support for EUC-JP, the main legacy encoding for Unix
 
41
  machines in Japan.
 
42
 
 
43
  The environment variable \c UNICODEMAP_JP can be used to fine-tune
 
44
  QJisCodec, QSjisCodec and QEucJpCodec. The \l QJisCodec
 
45
  documentation describes how to use this variable.
 
46
 
 
47
  Most of the code here was written by Serika Kurusugawa,
 
48
  a.k.a. Junji Takagi, and is included in Qt with the author's
 
49
  permission and the grateful thanks of the Trolltech team. Here is
 
50
  the copyright statement for that code:
 
51
 
 
52
  \legalese
 
53
 
 
54
  Copyright (C) 1999 Serika Kurusugawa. All rights reserved.
 
55
 
 
56
  Redistribution and use in source and binary forms, with or without
 
57
  modification, are permitted provided that the following conditions
 
58
  are met:
 
59
  \list 1
 
60
  \i Redistributions of source code must retain the above copyright
 
61
     notice, this list of conditions and the following disclaimer.
 
62
  \i Redistributions in binary form must reproduce the above copyright
 
63
     notice, this list of conditions and the following disclaimer in the
 
64
     documentation and/or other materials provided with the distribution.
 
65
  \endlist
 
66
 
 
67
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS".
 
68
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
69
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
70
  ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 
71
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
72
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 
73
  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
74
  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
75
  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
76
  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
77
  SUCH DAMAGE.
 
78
*/
 
79
 
 
80
/*
 
81
 * Copyright (C) 1999 Serika Kurusugawa, All rights reserved.
 
82
 *
 
83
 * Redistribution and use in source and binary forms, with or without
 
84
 * modification, are permitted provided that the following conditions
 
85
 * are met:
 
86
 * 1. Redistributions of source code must retain the above copyright
 
87
 *    notice, this list of conditions and the following disclaimer.
 
88
 * 2. Redistributions in binary form must reproduce the above copyright
 
89
 *    notice, this list of conditions and the following disclaimer in the
 
90
 *    documentation and/or other materials provided with the distribution.
 
91
 *
 
92
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 
93
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
94
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
95
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 
96
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
97
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 
98
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
99
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
100
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
101
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
102
 * SUCH DAMAGE.
 
103
 */
 
104
 
 
105
#include "qeucjpcodec.h"
 
106
 
 
107
static const uchar Esc = 0x1b;
 
108
static const uchar Ss2 = 0x8e;        // Single Shift 2
 
109
static const uchar Ss3 = 0x8f;        // Single Shift 3
 
110
 
 
111
#define        IsKana(c)        (((c) >= 0xa1) && ((c) <= 0xdf))
 
112
#define        IsEucChar(c)        (((c) >= 0xa1) && ((c) <= 0xfe))
 
113
 
 
114
#define        QValidChar(u)        ((u) ? QChar((ushort)(u)) : QChar(QChar::ReplacementCharacter))
 
115
 
 
116
/*!
 
117
  Constructs a QEucJpCodec.
 
118
*/
 
119
QEucJpCodec::QEucJpCodec() : conv(QJpUnicodeConv::newConverter(QJpUnicodeConv::Default))
 
120
{
 
121
}
 
122
 
 
123
/*!
 
124
  Destroys the codec.
 
125
*/
 
126
QEucJpCodec::~QEucJpCodec()
 
127
{
 
128
    delete (QJpUnicodeConv*)conv;
 
129
    conv = 0;
 
130
}
 
131
 
 
132
QByteArray QEucJpCodec::convertFromUnicode(const QChar *uc, int len, ConverterState *state) const
 
133
{
 
134
    char replacement = '?';
 
135
    if (state) {
 
136
        if (state->flags & ConvertInvalidToNull)
 
137
            replacement = 0;
 
138
    }
 
139
    int invalid = 0;
 
140
 
 
141
    int rlen = 3*len + 1;
 
142
    QByteArray rstr;
 
143
    rstr.resize(rlen);
 
144
    uchar* cursor = (uchar*)rstr.data();
 
145
    for (int i = 0; i < len; i++) {
 
146
        QChar ch = uc[i];
 
147
        uint j;
 
148
        if (ch.unicode() < 0x80) {
 
149
            // ASCII
 
150
            *cursor++ = ch.cell();
 
151
        } else if ((j = conv->unicodeToJisx0201(ch.row(), ch.cell())) != 0) {
 
152
            if (j < 0x80) {
 
153
                // JIS X 0201 Latin ?
 
154
                *cursor++ = j;
 
155
            } else {
 
156
                // JIS X 0201 Kana
 
157
                *cursor++ = Ss2;
 
158
                *cursor++ = j;
 
159
            }
 
160
        } else if ((j = conv->unicodeToJisx0208(ch.row(), ch.cell())) != 0) {
 
161
            // JIS X 0208
 
162
            *cursor++ = (j >> 8)   | 0x80;
 
163
            *cursor++ = (j & 0xff) | 0x80;
 
164
        } else if ((j = conv->unicodeToJisx0212(ch.row(), ch.cell())) != 0) {
 
165
            // JIS X 0212
 
166
            *cursor++ = Ss3;
 
167
            *cursor++ = (j >> 8)   | 0x80;
 
168
            *cursor++ = (j & 0xff) | 0x80;
 
169
        } else {
 
170
            // Error
 
171
            *cursor++ = replacement;
 
172
            ++invalid;
 
173
        }
 
174
    }
 
175
    rstr.resize(cursor - (uchar*)rstr.constData());
 
176
 
 
177
    if (state) {
 
178
        state->invalidChars += invalid;
 
179
    }
 
180
    return rstr;
 
181
}
 
182
 
 
183
 
 
184
QString QEucJpCodec::convertToUnicode(const char* chars, int len, ConverterState *state) const
 
185
{
 
186
    uchar buf[2];
 
187
    int nbuf = 0;
 
188
    QChar replacement = QChar::ReplacementCharacter;
 
189
    if (state) {
 
190
        if (state->flags & ConvertInvalidToNull)
 
191
            replacement = QChar::Null;
 
192
        nbuf = state->remainingChars;
 
193
        buf[0] = state->state_data[0];
 
194
        buf[1] = state->state_data[1];
 
195
    }
 
196
    int invalid = 0;
 
197
 
 
198
    QString result;
 
199
    for (int i=0; i<len; i++) {
 
200
        uchar ch = chars[i];
 
201
        switch (nbuf) {
 
202
        case 0:
 
203
            if (ch < 0x80) {
 
204
                // ASCII
 
205
                result += QLatin1Char(ch);
 
206
            } else if (ch == Ss2 || ch == Ss3) {
 
207
                // JIS X 0201 Kana or JIS X 0212
 
208
                buf[0] = ch;
 
209
                nbuf = 1;
 
210
            } else if (IsEucChar(ch)) {
 
211
                // JIS X 0208
 
212
                buf[0] = ch;
 
213
                nbuf = 1;
 
214
            } else {
 
215
                // Invalid
 
216
                result += replacement;
 
217
                ++invalid;
 
218
            }
 
219
            break;
 
220
        case 1:
 
221
            if (buf[0] == Ss2) {
 
222
                // JIS X 0201 Kana
 
223
                if (IsKana(ch)) {
 
224
                    uint u = conv->jisx0201ToUnicode(ch);
 
225
                    result += QValidChar(u);
 
226
                } else {
 
227
                    result += replacement;
 
228
                    ++invalid;
 
229
                }
 
230
                nbuf = 0;
 
231
            } else if (buf[0] == Ss3) {
 
232
                // JIS X 0212-1990
 
233
                if (IsEucChar(ch)) {
 
234
                    buf[1] = ch;
 
235
                    nbuf = 2;
 
236
                } else {
 
237
                    // Error
 
238
                    result += replacement;
 
239
                    ++invalid;
 
240
                    nbuf = 0;
 
241
                }
 
242
            } else {
 
243
                // JIS X 0208-1990
 
244
                if (IsEucChar(ch)) {
 
245
                    uint u = conv->jisx0208ToUnicode(buf[0] & 0x7f, ch & 0x7f);
 
246
                    result += QValidChar(u);
 
247
                } else {
 
248
                    // Error
 
249
                    result += replacement;
 
250
                    ++invalid;
 
251
                }
 
252
                nbuf = 0;
 
253
            }
 
254
            break;
 
255
        case 2:
 
256
            // JIS X 0212
 
257
            if (IsEucChar(ch)) {
 
258
                uint u = conv->jisx0212ToUnicode(buf[1] & 0x7f, ch & 0x7f);
 
259
                result += QValidChar(u);
 
260
            } else {
 
261
                result += replacement;
 
262
                ++invalid;
 
263
            }
 
264
            nbuf = 0;
 
265
        }
 
266
    }
 
267
    if (state) {
 
268
        state->remainingChars = nbuf;
 
269
        state->state_data[0] = buf[0];
 
270
        state->state_data[1] = buf[1];
 
271
        state->invalidChars += invalid;
 
272
    }
 
273
    return result;
 
274
}
 
275
 
 
276
int QEucJpCodec::_mibEnum()
 
277
{
 
278
    return 18;
 
279
}
 
280
 
 
281
QByteArray QEucJpCodec::_name()
 
282
{
 
283
    return "EUC-JP";
 
284
}