1/****************************************************************************
2**
3** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Digia. For licensing terms and
14** conditions see http://qt.digia.com/licensing. For further information
15** use the contact form at http://qt.digia.com/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37**
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qlatincodec_p.h"
43#include "qlist.h"
44
45#ifndef QT_NO_TEXTCODEC
46
47QT_BEGIN_NAMESPACE
48
49QLatin1Codec::~QLatin1Codec()
50{
51}
52
53QString QLatin1Codec::convertToUnicode(const char *chars, int len, ConverterState *) const
54{
55 if (chars == 0)
56 return QString();
57
58 return QString::fromLatin1(chars, len);
59}
60
61
62QByteArray QLatin1Codec::convertFromUnicode(const QChar *ch, int len, ConverterState *state) const
63{
64 const char replacement = (state && state->flags & ConvertInvalidToNull) ? 0 : '?';
65 QByteArray r(len, Qt::Uninitialized);
66 char *d = r.data();
67 int invalid = 0;
68 for (int i = 0; i < len; ++i) {
69 if (ch[i] > 0xff) {
70 d[i] = replacement;
71 ++invalid;
72 } else {
73 d[i] = (char)ch[i].cell();
74 }
75 }
76 if (state) {
77 state->invalidChars += invalid;
78 }
79 return r;
80}
81
82QByteArray QLatin1Codec::name() const
83{
84 return "ISO-8859-1";
85}
86
87QList<QByteArray> QLatin1Codec::aliases() const
88{
89 QList<QByteArray> list;
90 list << "latin1"
91 << "CP819"
92 << "IBM819"
93 << "iso-ir-100"
94 << "csISOLatin1";
95 return list;
96}
97
98
99int QLatin1Codec::mibEnum() const
100{
101 return 4;
102}
103
104
105QLatin15Codec::~QLatin15Codec()
106{
107}
108
109QString QLatin15Codec::convertToUnicode(const char* chars, int len, ConverterState *) const
110{
111 if (chars == 0)
112 return QString();
113
114 QString str = QString::fromLatin1(chars, len);
115 QChar *uc = str.data();
116 while(len--) {
117 switch(uc->unicode()) {
118 case 0xa4:
119 *uc = 0x20ac;
120 break;
121 case 0xa6:
122 *uc = 0x0160;
123 break;
124 case 0xa8:
125 *uc = 0x0161;
126 break;
127 case 0xb4:
128 *uc = 0x017d;
129 break;
130 case 0xb8:
131 *uc = 0x017e;
132 break;
133 case 0xbc:
134 *uc = 0x0152;
135 break;
136 case 0xbd:
137 *uc = 0x0153;
138 break;
139 case 0xbe:
140 *uc = 0x0178;
141 break;
142 default:
143 break;
144 }
145 uc++;
146 }
147 return str;
148}
149
150QByteArray QLatin15Codec::convertFromUnicode(const QChar *in, int length, ConverterState *state) const
151{
152 const char replacement = (state && state->flags & ConvertInvalidToNull) ? 0 : '?';
153 QByteArray r(length, Qt::Uninitialized);
154 char *d = r.data();
155 int invalid = 0;
156 for (int i = 0; i < length; ++i) {
157 uchar c;
158 ushort uc = in[i].unicode();
159 if (uc < 0x0100) {
160 if (uc > 0xa3) {
161 switch(uc) {
162 case 0xa4:
163 case 0xa6:
164 case 0xa8:
165 case 0xb4:
166 case 0xb8:
167 case 0xbc:
168 case 0xbd:
169 case 0xbe:
170 c = replacement;
171 ++invalid;
172 break;
173 default:
174 c = (unsigned char) uc;
175 break;
176 }
177 } else {
178 c = (unsigned char) uc;
179 }
180 } else {
181 if (uc == 0x20ac)
182 c = 0xa4;
183 else if ((uc & 0xff00) == 0x0100) {
184 switch(uc) {
185 case 0x0160:
186 c = 0xa6;
187 break;
188 case 0x0161:
189 c = 0xa8;
190 break;
191 case 0x017d:
192 c = 0xb4;
193 break;
194 case 0x017e:
195 c = 0xb8;
196 break;
197 case 0x0152:
198 c = 0xbc;
199 break;
200 case 0x0153:
201 c = 0xbd;
202 break;
203 case 0x0178:
204 c = 0xbe;
205 break;
206 default:
207 c = replacement;
208 ++invalid;
209 }
210 } else {
211 c = replacement;
212 ++invalid;
213 }
214 }
215 d[i] = (char)c;
216 }
217 if (state) {
218 state->remainingChars = 0;
219 state->invalidChars += invalid;
220 }
221 return r;
222}
223
224
225QByteArray QLatin15Codec::name() const
226{
227 return "ISO-8859-15";
228}
229
230QList<QByteArray> QLatin15Codec::aliases() const
231{
232 QList<QByteArray> list;
233 list << "latin9";
234 return list;
235}
236
237int QLatin15Codec::mibEnum() const
238{
239 return 111;
240}
241
242QT_END_NAMESPACE
243
244#endif // QT_NO_TEXTCODEC
245