Qt 4.8
qlatincodec.cpp
Go to the documentation of this file.
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 
48 
50 {
51 }
52 
53 QString 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 
63 {
64  const char replacement = (state && state->flags & ConvertInvalidToNull) ? 0 : '?';
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 
83 {
84  return "ISO-8859-1";
85 }
86 
88 {
89  QList<QByteArray> list;
90  list << "latin1"
91  << "CP819"
92  << "IBM819"
93  << "iso-ir-100"
94  << "csISOLatin1";
95  return list;
96 }
97 
98 
100 {
101  return 4;
102 }
103 
104 
106 {
107 }
108 
109 QString 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 
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 
226 {
227  return "ISO-8859-15";
228 }
229 
231 {
232  QList<QByteArray> list;
233  list << "latin9";
234  return list;
235 }
236 
238 {
239  return 111;
240 }
241 
243 
244 #endif // QT_NO_TEXTCODEC
double d
Definition: qnumeric_p.h:62
QByteArray convertFromUnicode(const QChar *, int, ConverterState *) const
QTextCodec subclasses must reimplement this function.
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QByteArray name() const
QTextCodec subclasses must reimplement this function.
Definition: qlatincodec.cpp:82
QByteArray name() const
QTextCodec subclasses must reimplement this function.
int mibEnum() const
Subclasses of QTextCodec must reimplement this function.
Definition: qlatincodec.cpp:99
QString convertToUnicode(const char *, int, ConverterState *) const
QTextCodec subclasses must reimplement this function.
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
QChar * data()
Returns a pointer to the data stored in the QString.
Definition: qstring.h:710
unsigned char uchar
Definition: qglobal.h:994
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
ConversionFlags flags
Definition: qtextcodec.h:106
unsigned short ushort
Definition: qglobal.h:995
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
QList< QByteArray > aliases() const
Subclasses can return a number of aliases for the codec in question.
Definition: qlatincodec.cpp:87
QByteArray convertFromUnicode(const QChar *, int, ConverterState *) const
QTextCodec subclasses must reimplement this function.
Definition: qlatincodec.cpp:62
int mibEnum() const
Subclasses of QTextCodec must reimplement this function.
QString convertToUnicode(const char *, int, ConverterState *) const
QTextCodec subclasses must reimplement this function.
Definition: qlatincodec.cpp:53
QList< QByteArray > aliases() const
Subclasses can return a number of aliases for the codec in question.