1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qlatincodec_p.h"
5#include "qlist.h"
6
7QT_BEGIN_NAMESPACE
8
9QLatin1Codec::~QLatin1Codec()
10{
11}
12
13QString QLatin1Codec::convertToUnicode(const char *chars, int len, ConverterState *) const
14{
15 return QString::fromLatin1(str: chars, size: len);
16}
17
18
19QByteArray QLatin1Codec::convertFromUnicode(const QChar *ch, int len, ConverterState *state) const
20{
21 const char replacement = (state && state->flags & ConvertInvalidToNull) ? 0 : '?';
22 QByteArray r(len, QT_PREPEND_NAMESPACE(Qt::Uninitialized));
23 char *d = r.data();
24 int invalid = 0;
25 for (int i = 0; i < len; ++i) {
26 if (ch[i] > QChar(0xff)) {
27 d[i] = replacement;
28 ++invalid;
29 } else {
30 d[i] = (char)ch[i].cell();
31 }
32 }
33 if (state) {
34 state->invalidChars += invalid;
35 }
36 return r;
37}
38
39QByteArray QLatin1Codec::name() const
40{
41 return "ISO-8859-1";
42}
43
44QList<QByteArray> QLatin1Codec::aliases() const
45{
46 QList<QByteArray> list;
47 list << "latin1"
48 << "CP819"
49 << "IBM819"
50 << "iso-ir-100"
51 << "csISOLatin1";
52 return list;
53}
54
55
56int QLatin1Codec::mibEnum() const
57{
58 return 4;
59}
60
61
62QLatin15Codec::~QLatin15Codec()
63{
64}
65
66QString QLatin15Codec::convertToUnicode(const char* chars, int len, ConverterState *) const
67{
68 QString str = QString::fromLatin1(str: chars, size: len);
69 QChar *uc = str.data();
70 while(len--) {
71 switch(uc->unicode()) {
72 case 0xa4:
73 *uc = QChar(0x20ac);
74 break;
75 case 0xa6:
76 *uc = QChar(0x0160);
77 break;
78 case 0xa8:
79 *uc = QChar(0x0161);
80 break;
81 case 0xb4:
82 *uc = QChar(0x017d);
83 break;
84 case 0xb8:
85 *uc = QChar(0x017e);
86 break;
87 case 0xbc:
88 *uc = QChar(0x0152);
89 break;
90 case 0xbd:
91 *uc = QChar(0x0153);
92 break;
93 case 0xbe:
94 *uc = QChar(0x0178);
95 break;
96 default:
97 break;
98 }
99 uc++;
100 }
101 return str;
102}
103
104QByteArray QLatin15Codec::convertFromUnicode(const QChar *in, int length, ConverterState *state) const
105{
106 const char replacement = (state && state->flags & ConvertInvalidToNull) ? 0 : '?';
107 QByteArray r(length, QT_PREPEND_NAMESPACE(Qt::Uninitialized));
108 char *d = r.data();
109 int invalid = 0;
110 for (int i = 0; i < length; ++i) {
111 uchar c;
112 ushort uc = in[i].unicode();
113 if (uc < 0x0100) {
114 if (uc > 0xa3) {
115 switch(uc) {
116 case 0xa4:
117 case 0xa6:
118 case 0xa8:
119 case 0xb4:
120 case 0xb8:
121 case 0xbc:
122 case 0xbd:
123 case 0xbe:
124 c = replacement;
125 ++invalid;
126 break;
127 default:
128 c = (unsigned char) uc;
129 break;
130 }
131 } else {
132 c = (unsigned char) uc;
133 }
134 } else {
135 if (uc == 0x20ac)
136 c = 0xa4;
137 else if ((uc & 0xff00) == 0x0100) {
138 switch(uc) {
139 case 0x0160:
140 c = 0xa6;
141 break;
142 case 0x0161:
143 c = 0xa8;
144 break;
145 case 0x017d:
146 c = 0xb4;
147 break;
148 case 0x017e:
149 c = 0xb8;
150 break;
151 case 0x0152:
152 c = 0xbc;
153 break;
154 case 0x0153:
155 c = 0xbd;
156 break;
157 case 0x0178:
158 c = 0xbe;
159 break;
160 default:
161 c = replacement;
162 ++invalid;
163 }
164 } else {
165 c = replacement;
166 ++invalid;
167 }
168 }
169 d[i] = (char)c;
170 }
171 if (state) {
172 state->remainingChars = 0;
173 state->invalidChars += invalid;
174 }
175 return r;
176}
177
178
179QByteArray QLatin15Codec::name() const
180{
181 return "ISO-8859-15";
182}
183
184QList<QByteArray> QLatin15Codec::aliases() const
185{
186 QList<QByteArray> list;
187 list << "latin9";
188 return list;
189}
190
191int QLatin15Codec::mibEnum() const
192{
193 return 111;
194}
195
196QT_END_NAMESPACE
197

source code of qt5compat/src/core5/codecs/qlatincodec.cpp