author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Wed, 21 Apr 2010 11:15:19 +0300 | |
branch | RCL_3 |
changeset 11 | 25a739ee40f4 |
parent 8 | 740e5562c97f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtCore module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qplatformdefs.h" |
|
43 |
#include "qtextcodec.h" |
|
44 |
#include "qtextcodec_p.h" |
|
45 |
||
46 |
#ifndef QT_NO_TEXTCODEC |
|
47 |
||
48 |
#include "qlist.h" |
|
49 |
#include "qfile.h" |
|
50 |
#ifndef QT_NO_LIBRARY |
|
51 |
# include "qcoreapplication.h" |
|
52 |
# include "qtextcodecplugin.h" |
|
53 |
# include "private/qfactoryloader_p.h" |
|
54 |
#endif |
|
55 |
#include "qstringlist.h" |
|
56 |
||
57 |
#ifdef Q_OS_UNIX |
|
58 |
# include "qiconvcodec_p.h" |
|
59 |
#endif |
|
60 |
||
61 |
#include "qutfcodec_p.h" |
|
62 |
#include "qsimplecodec_p.h" |
|
63 |
#include "qlatincodec_p.h" |
|
64 |
#ifndef QT_NO_CODECS |
|
65 |
# include "qtsciicodec_p.h" |
|
66 |
# include "qisciicodec_p.h" |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
67 |
#ifndef Q_OS_SYMBIAN |
0 | 68 |
# if defined(QT_NO_ICONV) && !defined(QT_BOOTSTRAPPED) |
69 |
// no iconv(3) support, must build all codecs into the library |
|
70 |
# include "../../plugins/codecs/cn/qgb18030codec.h" |
|
71 |
# include "../../plugins/codecs/jp/qeucjpcodec.h" |
|
72 |
# include "../../plugins/codecs/jp/qjiscodec.h" |
|
73 |
# include "../../plugins/codecs/jp/qsjiscodec.h" |
|
74 |
# include "../../plugins/codecs/kr/qeuckrcodec.h" |
|
75 |
# include "../../plugins/codecs/tw/qbig5codec.h" |
|
76 |
# endif // QT_NO_ICONV |
|
77 |
# if defined(Q_WS_X11) && !defined(QT_BOOTSTRAPPED) |
|
78 |
# include "qfontlaocodec_p.h" |
|
79 |
# include "../../plugins/codecs/jp/qfontjpcodec.h" |
|
80 |
# endif |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
81 |
#endif // QT_NO_SYMBIAN |
0 | 82 |
#endif // QT_NO_CODECS |
83 |
#include "qlocale.h" |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
84 |
#include "qmutex.h" |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
85 |
#include "qhash.h" |
0 | 86 |
|
87 |
#include <stdlib.h> |
|
88 |
#include <ctype.h> |
|
89 |
#include <locale.h> |
|
90 |
#if defined (_XOPEN_UNIX) && !defined(Q_OS_QNX) && !defined(Q_OS_OSF) |
|
91 |
#include <langinfo.h> |
|
92 |
#endif |
|
93 |
||
94 |
#if defined(Q_OS_WINCE) |
|
95 |
# define QT_NO_SETLOCALE |
|
96 |
#endif |
|
97 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
98 |
#ifdef Q_OS_SYMBIAN |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
99 |
#include "qtextcodec_symbian.cpp" |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
100 |
#endif |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
101 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
102 |
|
0 | 103 |
// enabling this is not exception safe! |
104 |
// #define Q_DEBUG_TEXTCODEC |
|
105 |
||
106 |
QT_BEGIN_NAMESPACE |
|
107 |
||
108 |
#ifndef QT_NO_TEXTCODECPLUGIN |
|
109 |
Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader, |
|
110 |
(QTextCodecFactoryInterface_iid, QLatin1String("/codecs"))) |
|
111 |
#endif |
|
112 |
||
8
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
113 |
|
0 | 114 |
static char qtolower(register char c) |
115 |
{ if (c >= 'A' && c <= 'Z') return c + 0x20; return c; } |
|
116 |
static bool qisalnum(register char c) |
|
117 |
{ return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); } |
|
118 |
||
119 |
static bool nameMatch(const QByteArray &name, const QByteArray &test) |
|
120 |
{ |
|
121 |
// if they're the same, return a perfect score |
|
122 |
if (qstricmp(name, test) == 0) |
|
123 |
return true; |
|
124 |
||
125 |
const char *n = name.constData(); |
|
126 |
const char *h = test.constData(); |
|
127 |
||
128 |
// if the letters and numbers are the same, we have a match |
|
129 |
while (*n != '\0') { |
|
130 |
if (qisalnum(*n)) { |
|
131 |
for (;;) { |
|
132 |
if (*h == '\0') |
|
133 |
return false; |
|
134 |
if (qisalnum(*h)) |
|
135 |
break; |
|
136 |
++h; |
|
137 |
} |
|
138 |
if (qtolower(*n) != qtolower(*h)) |
|
139 |
return false; |
|
140 |
++h; |
|
141 |
} |
|
142 |
++n; |
|
143 |
} |
|
144 |
while (*h && !qisalnum(*h)) |
|
145 |
++h; |
|
146 |
return (*h == '\0'); |
|
147 |
} |
|
148 |
||
149 |
||
150 |
static QTextCodec *createForName(const QByteArray &name) |
|
151 |
{ |
|
152 |
#ifndef QT_NO_TEXTCODECPLUGIN |
|
153 |
QFactoryLoader *l = loader(); |
|
154 |
QStringList keys = l->keys(); |
|
155 |
for (int i = 0; i < keys.size(); ++i) { |
|
156 |
if (nameMatch(name, keys.at(i).toLatin1())) { |
|
157 |
QString realName = keys.at(i); |
|
158 |
if (QTextCodecFactoryInterface *factory |
|
159 |
= qobject_cast<QTextCodecFactoryInterface*>(l->instance(realName))) { |
|
160 |
return factory->create(realName); |
|
161 |
} |
|
162 |
} |
|
163 |
} |
|
164 |
#else |
|
165 |
Q_UNUSED(name); |
|
166 |
#endif |
|
167 |
return 0; |
|
168 |
} |
|
169 |
||
170 |
static QTextCodec *createForMib(int mib) |
|
171 |
{ |
|
172 |
#ifndef QT_NO_TEXTCODECPLUGIN |
|
173 |
QString name = QLatin1String("MIB: ") + QString::number(mib); |
|
174 |
if (QTextCodecFactoryInterface *factory |
|
175 |
= qobject_cast<QTextCodecFactoryInterface*>(loader()->instance(name))) |
|
176 |
return factory->create(name); |
|
177 |
#else |
|
178 |
Q_UNUSED(mib); |
|
179 |
#endif |
|
180 |
return 0; |
|
181 |
} |
|
182 |
||
183 |
static QList<QTextCodec*> *all = 0; |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
184 |
static int clearCaches = 0; // flags specifying if caches should be invalided: 0x1 codecForName, 0x2 codecForMib |
0 | 185 |
#ifdef Q_DEBUG_TEXTCODEC |
186 |
static bool destroying_is_ok = false; |
|
187 |
#endif |
|
188 |
||
189 |
static QTextCodec *localeMapper = 0; |
|
190 |
QTextCodec *QTextCodec::cftr = 0; |
|
191 |
||
192 |
||
193 |
class QTextCodecCleanup |
|
194 |
{ |
|
195 |
public: |
|
196 |
~QTextCodecCleanup(); |
|
197 |
}; |
|
198 |
||
199 |
/* |
|
200 |
Deletes all the created codecs. This destructor is called just |
|
201 |
before exiting to delete any QTextCodec objects that may be lying |
|
202 |
around. |
|
203 |
*/ |
|
204 |
QTextCodecCleanup::~QTextCodecCleanup() |
|
205 |
{ |
|
206 |
if (!all) |
|
207 |
return; |
|
208 |
||
209 |
#ifdef Q_DEBUG_TEXTCODEC |
|
210 |
destroying_is_ok = true; |
|
211 |
#endif |
|
212 |
||
213 |
for (QList<QTextCodec *>::const_iterator it = all->constBegin() |
|
214 |
; it != all->constEnd(); ++it) { |
|
215 |
delete *it; |
|
216 |
} |
|
217 |
delete all; |
|
218 |
all = 0; |
|
219 |
localeMapper = 0; |
|
220 |
||
221 |
#ifdef Q_DEBUG_TEXTCODEC |
|
222 |
destroying_is_ok = false; |
|
223 |
#endif |
|
224 |
} |
|
225 |
||
226 |
Q_GLOBAL_STATIC(QTextCodecCleanup, createQTextCodecCleanup) |
|
227 |
||
8
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
228 |
bool QTextCodec::validCodecs() |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
229 |
{ |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
230 |
#ifdef Q_OS_SYMBIAN |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
231 |
// If we don't have a trap handler, we're outside of the main() function, |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
232 |
// ie. in global constructors or destructors. Don't use codecs in this |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
233 |
// case as it would lead to crashes because we don't have a cleanup stack on Symbian |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
234 |
return (User::TrapHandler() != NULL); |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
235 |
#else |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
236 |
return true; |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
237 |
#endif |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
238 |
} |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
239 |
|
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
240 |
|
0 | 241 |
#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE) |
242 |
class QWindowsLocalCodec: public QTextCodec |
|
243 |
{ |
|
244 |
public: |
|
245 |
QWindowsLocalCodec(); |
|
246 |
~QWindowsLocalCodec(); |
|
247 |
||
248 |
QString convertToUnicode(const char *, int, ConverterState *) const; |
|
249 |
QByteArray convertFromUnicode(const QChar *, int, ConverterState *) const; |
|
250 |
QString convertToUnicodeCharByChar(const char *chars, int length, ConverterState *state) const; |
|
251 |
||
252 |
QByteArray name() const; |
|
253 |
int mibEnum() const; |
|
254 |
||
255 |
}; |
|
256 |
||
257 |
QWindowsLocalCodec::QWindowsLocalCodec() |
|
258 |
{ |
|
259 |
} |
|
260 |
||
261 |
QWindowsLocalCodec::~QWindowsLocalCodec() |
|
262 |
{ |
|
263 |
} |
|
264 |
||
265 |
QString QWindowsLocalCodec::convertToUnicode(const char *chars, int length, ConverterState *state) const |
|
266 |
{ |
|
267 |
const char *mb = chars; |
|
268 |
int mblen = length; |
|
269 |
||
270 |
if (!mb || !mblen) |
|
271 |
return QString(); |
|
272 |
||
273 |
const int wclen_auto = 4096; |
|
274 |
wchar_t wc_auto[wclen_auto]; |
|
275 |
int wclen = wclen_auto; |
|
276 |
wchar_t *wc = wc_auto; |
|
277 |
int len; |
|
278 |
QString sp; |
|
279 |
bool prepend = false; |
|
280 |
char state_data = 0; |
|
281 |
int remainingChars = 0; |
|
282 |
||
283 |
//save the current state information |
|
284 |
if (state) { |
|
285 |
state_data = (char)state->state_data[0]; |
|
286 |
remainingChars = state->remainingChars; |
|
287 |
} |
|
288 |
||
289 |
//convert the pending charcter (if available) |
|
290 |
if (state && remainingChars) { |
|
291 |
char prev[3] = {0}; |
|
292 |
prev[0] = state_data; |
|
293 |
prev[1] = mb[0]; |
|
294 |
remainingChars = 0; |
|
295 |
len = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, |
|
296 |
prev, 2, wc, wclen); |
|
297 |
if (len) { |
|
298 |
prepend = true; |
|
299 |
sp.append(QChar(wc[0])); |
|
300 |
mb++; |
|
301 |
mblen--; |
|
302 |
wc[0] = 0; |
|
303 |
} |
|
304 |
} |
|
305 |
||
306 |
while (!(len=MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED|MB_ERR_INVALID_CHARS, |
|
307 |
mb, mblen, wc, wclen))) { |
|
308 |
int r = GetLastError(); |
|
309 |
if (r == ERROR_INSUFFICIENT_BUFFER) { |
|
310 |
if (wc != wc_auto) { |
|
311 |
qWarning("MultiByteToWideChar: Size changed"); |
|
312 |
break; |
|
313 |
} else { |
|
314 |
wclen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, |
|
315 |
mb, mblen, 0, 0); |
|
316 |
wc = new wchar_t[wclen]; |
|
317 |
// and try again... |
|
318 |
} |
|
319 |
} else if (r == ERROR_NO_UNICODE_TRANSLATION) { |
|
320 |
//find the last non NULL character |
|
321 |
while (mblen > 1 && !(mb[mblen-1])) |
|
322 |
mblen--; |
|
323 |
//check whether, we hit an invalid character in the middle |
|
324 |
if ((mblen <= 1) || (remainingChars && state_data)) |
|
325 |
return convertToUnicodeCharByChar(chars, length, state); |
|
326 |
//Remove the last character and try again... |
|
327 |
state_data = mb[mblen-1]; |
|
328 |
remainingChars = 1; |
|
329 |
mblen--; |
|
330 |
} else { |
|
331 |
// Fail. |
|
332 |
qWarning("MultiByteToWideChar: Cannot convert multibyte text"); |
|
333 |
break; |
|
334 |
} |
|
335 |
} |
|
336 |
if (len <= 0) |
|
337 |
return QString(); |
|
338 |
if (wc[len-1] == 0) // len - 1: we don't want terminator |
|
339 |
--len; |
|
340 |
||
341 |
//save the new state information |
|
342 |
if (state) { |
|
343 |
state->state_data[0] = (char)state_data; |
|
344 |
state->remainingChars = remainingChars; |
|
345 |
} |
|
346 |
QString s((QChar*)wc, len); |
|
347 |
if (wc != wc_auto) |
|
348 |
delete [] wc; |
|
349 |
if (prepend) { |
|
350 |
return sp+s; |
|
351 |
} |
|
352 |
return s; |
|
353 |
} |
|
354 |
||
355 |
QString QWindowsLocalCodec::convertToUnicodeCharByChar(const char *chars, int length, ConverterState *state) const |
|
356 |
{ |
|
357 |
if (!chars || !length) |
|
358 |
return QString(); |
|
359 |
||
360 |
int copyLocation = 0; |
|
361 |
int extra = 2; |
|
362 |
if (state && state->remainingChars) { |
|
363 |
copyLocation = state->remainingChars; |
|
364 |
extra += copyLocation; |
|
365 |
} |
|
366 |
int newLength = length + extra; |
|
367 |
char *mbcs = new char[newLength]; |
|
368 |
//ensure that we have a NULL terminated string |
|
369 |
mbcs[newLength-1] = 0; |
|
370 |
mbcs[newLength-2] = 0; |
|
371 |
memcpy(&(mbcs[copyLocation]), chars, length); |
|
372 |
if (copyLocation) { |
|
373 |
//copy the last character from the state |
|
374 |
mbcs[0] = (char)state->state_data[0]; |
|
375 |
state->remainingChars = 0; |
|
376 |
} |
|
377 |
const char *mb = mbcs; |
|
378 |
#ifndef Q_OS_WINCE |
|
379 |
const char *next = 0; |
|
380 |
QString s; |
|
381 |
while((next = CharNextExA(CP_ACP, mb, 0)) != mb) { |
|
382 |
wchar_t wc[2] ={0}; |
|
383 |
int charlength = next - mb; |
|
384 |
int len = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED|MB_ERR_INVALID_CHARS, mb, charlength, wc, 2); |
|
385 |
if (len>0) { |
|
386 |
s.append(QChar(wc[0])); |
|
387 |
} else { |
|
388 |
int r = GetLastError(); |
|
389 |
//check if the character being dropped is the last character |
|
390 |
if (r == ERROR_NO_UNICODE_TRANSLATION && mb == (mbcs+newLength -3) && state) { |
|
391 |
state->remainingChars = 1; |
|
392 |
state->state_data[0] = (char)*mb; |
|
393 |
} |
|
394 |
} |
|
395 |
mb = next; |
|
396 |
} |
|
397 |
#else |
|
398 |
QString s; |
|
399 |
int size = mbstowcs(NULL, mb, length); |
|
400 |
if (size < 0) { |
|
401 |
Q_ASSERT("Error in CE TextCodec"); |
|
402 |
return QString(); |
|
403 |
} |
|
404 |
wchar_t* ws = new wchar_t[size + 2]; |
|
405 |
ws[size +1] = 0; |
|
406 |
ws[size] = 0; |
|
407 |
size = mbstowcs(ws, mb, length); |
|
408 |
for (int i=0; i< size; i++) |
|
409 |
s.append(QChar(ws[i])); |
|
410 |
delete [] ws; |
|
411 |
#endif |
|
412 |
delete mbcs; |
|
413 |
return s; |
|
414 |
} |
|
415 |
||
416 |
QByteArray QWindowsLocalCodec::convertFromUnicode(const QChar *uc, int len, ConverterState *) const |
|
417 |
{ |
|
418 |
return qt_winQString2MB(uc, len); |
|
419 |
} |
|
420 |
||
421 |
||
422 |
QByteArray QWindowsLocalCodec::name() const |
|
423 |
{ |
|
424 |
return "System"; |
|
425 |
} |
|
426 |
||
427 |
int QWindowsLocalCodec::mibEnum() const |
|
428 |
{ |
|
429 |
return 0; |
|
430 |
} |
|
431 |
||
432 |
#else |
|
433 |
||
434 |
/* locale names mostly copied from XFree86 */ |
|
435 |
static const char * const iso8859_2locales[] = { |
|
436 |
"croatian", "cs", "cs_CS", "cs_CZ","cz", "cz_CZ", "czech", "hr", |
|
437 |
"hr_HR", "hu", "hu_HU", "hungarian", "pl", "pl_PL", "polish", "ro", |
|
438 |
"ro_RO", "rumanian", "serbocroatian", "sh", "sh_SP", "sh_YU", "sk", |
|
439 |
"sk_SK", "sl", "sl_CS", "sl_SI", "slovak", "slovene", "sr_SP", 0 }; |
|
440 |
||
441 |
static const char * const iso8859_3locales[] = { |
|
442 |
"eo", 0 }; |
|
443 |
||
444 |
static const char * const iso8859_4locales[] = { |
|
445 |
"ee", "ee_EE", 0 }; |
|
446 |
||
447 |
static const char * const iso8859_5locales[] = { |
|
448 |
"mk", "mk_MK", "sp", "sp_YU", 0 }; |
|
449 |
||
450 |
static const char * const cp_1251locales[] = { |
|
451 |
"be", "be_BY", "bg", "bg_BG", "bulgarian", 0 }; |
|
452 |
||
453 |
static const char * const pt_154locales[] = { |
|
454 |
"ba_RU", "ky", "ky_KG", "kk", "kk_KZ", 0 }; |
|
455 |
||
456 |
static const char * const iso8859_6locales[] = { |
|
457 |
"ar_AA", "ar_SA", "arabic", 0 }; |
|
458 |
||
459 |
static const char * const iso8859_7locales[] = { |
|
460 |
"el", "el_GR", "greek", 0 }; |
|
461 |
||
462 |
static const char * const iso8859_8locales[] = { |
|
463 |
"hebrew", "he", "he_IL", "iw", "iw_IL", 0 }; |
|
464 |
||
465 |
static const char * const iso8859_9locales[] = { |
|
466 |
"tr", "tr_TR", "turkish", 0 }; |
|
467 |
||
468 |
static const char * const iso8859_13locales[] = { |
|
469 |
"lt", "lt_LT", "lv", "lv_LV", 0 }; |
|
470 |
||
471 |
static const char * const iso8859_15locales[] = { |
|
472 |
"et", "et_EE", |
|
473 |
// Euro countries |
|
474 |
"br_FR", "ca_ES", "de", "de_AT", "de_BE", "de_DE", "de_LU", "en_IE", |
|
475 |
"es", "es_ES", "eu_ES", "fi", "fi_FI", "finnish", "fr", "fr_FR", |
|
476 |
"fr_BE", "fr_LU", "french", "ga_IE", "gl_ES", "it", "it_IT", "oc_FR", |
|
477 |
"nl", "nl_BE", "nl_NL", "pt", "pt_PT", "sv_FI", "wa_BE", |
|
478 |
0 }; |
|
479 |
||
480 |
static const char * const koi8_ulocales[] = { |
|
481 |
"uk", "uk_UA", "ru_UA", "ukrainian", 0 }; |
|
482 |
||
483 |
static const char * const tis_620locales[] = { |
|
484 |
"th", "th_TH", "thai", 0 }; |
|
485 |
||
486 |
// static const char * const tcvnlocales[] = { |
|
487 |
// "vi", "vi_VN", 0 }; |
|
488 |
||
489 |
static bool try_locale_list(const char * const locale[], const QByteArray &lang) |
|
490 |
{ |
|
491 |
int i; |
|
492 |
for(i=0; locale[i] && lang != locale[i]; i++) |
|
493 |
; |
|
494 |
return locale[i] != 0; |
|
495 |
} |
|
496 |
||
497 |
// For the probably_koi8_locales we have to look. the standard says |
|
498 |
// these are 8859-5, but almost all Russian users use KOI8-R and |
|
499 |
// incorrectly set $LANG to ru_RU. We'll check tolower() to see what |
|
500 |
// it thinks ru_RU means. |
|
501 |
||
502 |
// If you read the history, it seems that many Russians blame ISO and |
|
503 |
// Perestroika for the confusion. |
|
504 |
// |
|
505 |
// The real bug is that some programs break if the user specifies |
|
506 |
// ru_RU.KOI8-R. |
|
507 |
||
508 |
static const char * const probably_koi8_rlocales[] = { |
|
509 |
"ru", "ru_SU", "ru_RU", "russian", 0 }; |
|
510 |
||
511 |
static QTextCodec * ru_RU_hack(const char * i) { |
|
512 |
QTextCodec * ru_RU_codec = 0; |
|
513 |
||
514 |
#if !defined(QT_NO_SETLOCALE) |
|
515 |
QByteArray origlocale(setlocale(LC_CTYPE, i)); |
|
516 |
#else |
|
517 |
QByteArray origlocale(i); |
|
518 |
#endif |
|
519 |
// unicode koi8r latin5 name |
|
520 |
// 0x044E 0xC0 0xEE CYRILLIC SMALL LETTER YU |
|
521 |
// 0x042E 0xE0 0xCE CYRILLIC CAPITAL LETTER YU |
|
522 |
int latin5 = tolower(0xCE); |
|
523 |
int koi8r = tolower(0xE0); |
|
524 |
if (koi8r == 0xC0 && latin5 != 0xEE) { |
|
525 |
ru_RU_codec = QTextCodec::codecForName("KOI8-R"); |
|
526 |
} else if (koi8r != 0xC0 && latin5 == 0xEE) { |
|
527 |
ru_RU_codec = QTextCodec::codecForName("ISO 8859-5"); |
|
528 |
} else { |
|
529 |
// something else again... let's assume... *throws dice* |
|
530 |
ru_RU_codec = QTextCodec::codecForName("KOI8-R"); |
|
531 |
qWarning("QTextCodec: Using KOI8-R, probe failed (%02x %02x %s)", |
|
532 |
koi8r, latin5, i); |
|
533 |
} |
|
534 |
#if !defined(QT_NO_SETLOCALE) |
|
535 |
setlocale(LC_CTYPE, origlocale); |
|
536 |
#endif |
|
537 |
||
538 |
return ru_RU_codec; |
|
539 |
} |
|
540 |
||
541 |
#endif |
|
542 |
||
543 |
#if !defined(Q_OS_WIN32) && !defined(Q_OS_WINCE) |
|
544 |
static QTextCodec *checkForCodec(const QByteArray &name) { |
|
545 |
QTextCodec *c = QTextCodec::codecForName(name); |
|
546 |
if (!c) { |
|
547 |
const int index = name.indexOf('@'); |
|
548 |
if (index != -1) { |
|
549 |
c = QTextCodec::codecForName(name.left(index)); |
|
550 |
} |
|
551 |
} |
|
552 |
return c; |
|
553 |
} |
|
554 |
#endif |
|
555 |
||
556 |
/* the next two functions are implicitely thread safe, |
|
557 |
as they are only called by setup() which uses a mutex. |
|
558 |
*/ |
|
559 |
static void setupLocaleMapper() |
|
560 |
{ |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
561 |
#ifdef Q_OS_SYMBIAN |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
562 |
localeMapper = QSymbianTextCodec::localeMapper; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
563 |
if (localeMapper) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
564 |
return; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
565 |
#endif |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
566 |
|
0 | 567 |
#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE) |
568 |
localeMapper = QTextCodec::codecForName("System"); |
|
569 |
#else |
|
570 |
||
571 |
#ifndef QT_NO_ICONV |
|
572 |
localeMapper = QTextCodec::codecForName("System"); |
|
573 |
#endif |
|
574 |
||
575 |
#if defined (_XOPEN_UNIX) && !defined(Q_OS_QNX) && !defined(Q_OS_OSF) |
|
576 |
if (!localeMapper) { |
|
577 |
char *charset = nl_langinfo (CODESET); |
|
578 |
if (charset) |
|
579 |
localeMapper = QTextCodec::codecForName(charset); |
|
580 |
} |
|
581 |
#endif |
|
582 |
||
583 |
if (!localeMapper) { |
|
584 |
// Very poorly defined and followed standards causes lots of |
|
585 |
// code to try to get all the cases... This logic is |
|
586 |
// duplicated in QIconvCodec, so if you change it here, change |
|
587 |
// it there too. |
|
588 |
||
589 |
// Try to determine locale codeset from locale name assigned to |
|
590 |
// LC_CTYPE category. |
|
591 |
||
592 |
// First part is getting that locale name. First try setlocale() which |
|
593 |
// definitely knows it, but since we cannot fully trust it, get ready |
|
594 |
// to fall back to environment variables. |
|
595 |
#if !defined(QT_NO_SETLOCALE) |
|
596 |
const QByteArray ctype = setlocale(LC_CTYPE, 0); |
|
597 |
#else |
|
598 |
const QByteArray ctype; |
|
599 |
#endif |
|
600 |
||
601 |
// Get the first nonempty value from $LC_ALL, $LC_CTYPE, and $LANG |
|
602 |
// environment variables. |
|
603 |
QByteArray lang = qgetenv("LC_ALL"); |
|
604 |
if (lang.isEmpty() || lang == "C") { |
|
605 |
lang = qgetenv("LC_CTYPE"); |
|
606 |
} |
|
607 |
if (lang.isEmpty() || lang == "C") { |
|
608 |
lang = qgetenv("LANG"); |
|
609 |
} |
|
610 |
||
611 |
// Now try these in order: |
|
612 |
// 1. CODESET from ctype if it contains a .CODESET part (e.g. en_US.ISO8859-15) |
|
613 |
// 2. CODESET from lang if it contains a .CODESET part |
|
614 |
// 3. ctype (maybe the locale is named "ISO-8859-1" or something) |
|
615 |
// 4. locale (ditto) |
|
616 |
// 5. check for "@euro" |
|
617 |
// 6. guess locale from ctype unless ctype is "C" |
|
618 |
// 7. guess locale from lang |
|
619 |
||
620 |
// 1. CODESET from ctype if it contains a .CODESET part (e.g. en_US.ISO8859-15) |
|
621 |
int indexOfDot = ctype.indexOf('.'); |
|
622 |
if (indexOfDot != -1) |
|
623 |
localeMapper = checkForCodec( ctype.mid(indexOfDot + 1) ); |
|
624 |
||
625 |
// 2. CODESET from lang if it contains a .CODESET part |
|
626 |
if (!localeMapper) { |
|
627 |
indexOfDot = lang.indexOf('.'); |
|
628 |
if (indexOfDot != -1) |
|
629 |
localeMapper = checkForCodec( lang.mid(indexOfDot + 1) ); |
|
630 |
} |
|
631 |
||
632 |
// 3. ctype (maybe the locale is named "ISO-8859-1" or something) |
|
633 |
if (!localeMapper && !ctype.isEmpty() && ctype != "C") |
|
634 |
localeMapper = checkForCodec(ctype); |
|
635 |
||
636 |
// 4. locale (ditto) |
|
637 |
if (!localeMapper && !lang.isEmpty()) |
|
638 |
localeMapper = checkForCodec(lang); |
|
639 |
||
640 |
// 5. "@euro" |
|
641 |
if ((!localeMapper && ctype.contains("@euro")) || lang.contains("@euro")) |
|
642 |
localeMapper = checkForCodec("ISO 8859-15"); |
|
643 |
||
644 |
// 6. guess locale from ctype unless ctype is "C" |
|
645 |
// 7. guess locale from lang |
|
646 |
const QByteArray &try_by_name = (!ctype.isEmpty() && ctype != "C") ? lang : ctype; |
|
647 |
||
648 |
// Now do the guessing. |
|
649 |
if (!lang.isEmpty() && !localeMapper && !try_by_name.isEmpty()) { |
|
650 |
if (try_locale_list(iso8859_15locales, lang)) |
|
651 |
localeMapper = QTextCodec::codecForName("ISO 8859-15"); |
|
652 |
else if (try_locale_list(iso8859_2locales, lang)) |
|
653 |
localeMapper = QTextCodec::codecForName("ISO 8859-2"); |
|
654 |
else if (try_locale_list(iso8859_3locales, lang)) |
|
655 |
localeMapper = QTextCodec::codecForName("ISO 8859-3"); |
|
656 |
else if (try_locale_list(iso8859_4locales, lang)) |
|
657 |
localeMapper = QTextCodec::codecForName("ISO 8859-4"); |
|
658 |
else if (try_locale_list(iso8859_5locales, lang)) |
|
659 |
localeMapper = QTextCodec::codecForName("ISO 8859-5"); |
|
660 |
else if (try_locale_list(iso8859_6locales, lang)) |
|
661 |
localeMapper = QTextCodec::codecForName("ISO 8859-6"); |
|
662 |
else if (try_locale_list(iso8859_7locales, lang)) |
|
663 |
localeMapper = QTextCodec::codecForName("ISO 8859-7"); |
|
664 |
else if (try_locale_list(iso8859_8locales, lang)) |
|
665 |
localeMapper = QTextCodec::codecForName("ISO 8859-8-I"); |
|
666 |
else if (try_locale_list(iso8859_9locales, lang)) |
|
667 |
localeMapper = QTextCodec::codecForName("ISO 8859-9"); |
|
668 |
else if (try_locale_list(iso8859_13locales, lang)) |
|
669 |
localeMapper = QTextCodec::codecForName("ISO 8859-13"); |
|
670 |
else if (try_locale_list(tis_620locales, lang)) |
|
671 |
localeMapper = QTextCodec::codecForName("ISO 8859-11"); |
|
672 |
else if (try_locale_list(koi8_ulocales, lang)) |
|
673 |
localeMapper = QTextCodec::codecForName("KOI8-U"); |
|
674 |
else if (try_locale_list(cp_1251locales, lang)) |
|
675 |
localeMapper = QTextCodec::codecForName("CP 1251"); |
|
676 |
else if (try_locale_list(pt_154locales, lang)) |
|
677 |
localeMapper = QTextCodec::codecForName("PT 154"); |
|
678 |
else if (try_locale_list(probably_koi8_rlocales, lang)) |
|
679 |
localeMapper = ru_RU_hack(lang); |
|
680 |
} |
|
681 |
||
682 |
} |
|
683 |
||
684 |
// If everything failed, we default to 8859-1 |
|
685 |
// We could perhaps default to 8859-15. |
|
686 |
if (!localeMapper) |
|
687 |
localeMapper = QTextCodec::codecForName("ISO 8859-1"); |
|
688 |
#endif |
|
689 |
} |
|
690 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
691 |
#ifndef QT_NO_THREAD |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
692 |
Q_GLOBAL_STATIC_WITH_ARGS(QMutex, textCodecsMutex, (QMutex::Recursive)); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
693 |
#endif |
0 | 694 |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
695 |
// textCodecsMutex need to be locked to enter this function |
0 | 696 |
static void setup() |
697 |
{ |
|
698 |
if (all) |
|
699 |
return; |
|
700 |
||
8
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
701 |
#ifdef Q_OS_SYMBIAN |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
702 |
// If we don't have a trap handler, we're outside of the main() function, |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
703 |
// ie. in global constructors or destructors. Don't create codecs in this |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
704 |
// case as it would lead to crashes because of a missing cleanup stack on Symbian |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
705 |
if (User::TrapHandler() == NULL) |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
706 |
return; |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
707 |
#endif |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
708 |
|
0 | 709 |
#ifdef Q_DEBUG_TEXTCODEC |
710 |
if (destroying_is_ok) |
|
711 |
qWarning("QTextCodec: Creating new codec during codec cleanup"); |
|
712 |
#endif |
|
713 |
all = new QList<QTextCodec*>; |
|
714 |
// create the cleanup object to cleanup all codecs on exit |
|
715 |
(void) createQTextCodecCleanup(); |
|
716 |
||
717 |
#ifndef QT_NO_CODECS |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
718 |
(void)new QTsciiCodec; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
719 |
for (int i = 0; i < 9; ++i) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
720 |
(void)new QIsciiCodec(i); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
721 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
722 |
for (int i = 0; i < QSimpleTextCodec::numSimpleCodecs; ++i) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
723 |
(void)new QSimpleTextCodec(i); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
724 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
725 |
#ifdef Q_OS_SYMBIAN |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
726 |
localeMapper = QSymbianTextCodec::init(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
727 |
#endif |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
728 |
|
0 | 729 |
# if defined(Q_WS_X11) && !defined(QT_BOOTSTRAPPED) |
730 |
// no font codecs when bootstrapping |
|
731 |
(void)new QFontLaoCodec; |
|
732 |
# if defined(QT_NO_ICONV) |
|
733 |
// no iconv(3) support, must build all codecs into the library |
|
734 |
(void)new QFontGb2312Codec; |
|
735 |
(void)new QFontGbkCodec; |
|
736 |
(void)new QFontGb18030_0Codec; |
|
737 |
(void)new QFontJis0208Codec; |
|
738 |
(void)new QFontJis0201Codec; |
|
739 |
(void)new QFontKsc5601Codec; |
|
740 |
(void)new QFontBig5hkscsCodec; |
|
741 |
(void)new QFontBig5Codec; |
|
742 |
# endif // QT_NO_ICONV && !QT_BOOTSTRAPPED |
|
743 |
# endif // Q_WS_X11 |
|
744 |
||
745 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
746 |
#ifndef Q_OS_SYMBIAN |
0 | 747 |
# if defined(QT_NO_ICONV) && !defined(QT_BOOTSTRAPPED) |
748 |
// no asian codecs when bootstrapping, sorry |
|
749 |
(void)new QGb18030Codec; |
|
750 |
(void)new QGbkCodec; |
|
751 |
(void)new QGb2312Codec; |
|
752 |
(void)new QEucJpCodec; |
|
753 |
(void)new QJisCodec; |
|
754 |
(void)new QSjisCodec; |
|
755 |
(void)new QEucKrCodec; |
|
756 |
(void)new QCP949Codec; |
|
757 |
(void)new QBig5Codec; |
|
758 |
(void)new QBig5hkscsCodec; |
|
759 |
# endif // QT_NO_ICONV && !QT_BOOTSTRAPPED |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
760 |
#endif //Q_OS_SYMBIAN |
0 | 761 |
#endif // QT_NO_CODECS |
762 |
||
763 |
#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE) |
|
764 |
(void) new QWindowsLocalCodec; |
|
765 |
#endif // Q_OS_WIN32 |
|
766 |
||
767 |
(void)new QUtf16Codec; |
|
768 |
(void)new QUtf16BECodec; |
|
769 |
(void)new QUtf16LECodec; |
|
770 |
(void)new QUtf32Codec; |
|
771 |
(void)new QUtf32BECodec; |
|
772 |
(void)new QUtf32LECodec; |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
773 |
#ifndef Q_OS_SYMBIAN |
0 | 774 |
(void)new QLatin15Codec; |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
775 |
#endif |
0 | 776 |
(void)new QLatin1Codec; |
777 |
(void)new QUtf8Codec; |
|
778 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
779 |
#ifndef Q_OS_SYMBIAN |
0 | 780 |
#if defined(Q_OS_UNIX) && !defined(QT_NO_ICONV) && !defined(QT_BOOTSTRAPPED) |
781 |
// QIconvCodec depends on the UTF-16 codec, so it needs to be created last |
|
782 |
(void) new QIconvCodec(); |
|
783 |
#endif |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
784 |
#endif |
0 | 785 |
|
786 |
if (!localeMapper) |
|
787 |
setupLocaleMapper(); |
|
788 |
} |
|
789 |
||
790 |
/*! |
|
791 |
\enum QTextCodec::ConversionFlag |
|
792 |
||
793 |
\value DefaultConversion No flag is set. |
|
794 |
\value ConvertInvalidToNull If this flag is set, each invalid input |
|
795 |
character is output as a null character. |
|
796 |
\value IgnoreHeader Ignore any Unicode byte-order mark and don't generate any. |
|
797 |
||
798 |
\omitvalue FreeFunction |
|
799 |
*/ |
|
800 |
||
801 |
/*! |
|
802 |
\fn QTextCodec::ConverterState::ConverterState(ConversionFlags flags) |
|
803 |
||
804 |
Constructs a ConverterState object initialized with the given \a flags. |
|
805 |
*/ |
|
806 |
||
807 |
/*! |
|
808 |
Destroys the ConverterState object. |
|
809 |
*/ |
|
810 |
QTextCodec::ConverterState::~ConverterState() |
|
811 |
{ |
|
812 |
if (flags & FreeFunction) |
|
813 |
(QTextCodecUnalignedPointer::decode(state_data))(this); |
|
814 |
else if (d) |
|
815 |
qFree(d); |
|
816 |
} |
|
817 |
||
818 |
/*! |
|
819 |
\class QTextCodec |
|
820 |
\brief The QTextCodec class provides conversions between text encodings. |
|
821 |
\reentrant |
|
822 |
\ingroup i18n |
|
823 |
||
824 |
Qt uses Unicode to store, draw and manipulate strings. In many |
|
825 |
situations you may wish to deal with data that uses a different |
|
826 |
encoding. For example, most Japanese documents are still stored |
|
827 |
in Shift-JIS or ISO 2022-JP, while Russian users often have their |
|
828 |
documents in KOI8-R or Windows-1251. |
|
829 |
||
830 |
Qt provides a set of QTextCodec classes to help with converting |
|
831 |
non-Unicode formats to and from Unicode. You can also create your |
|
832 |
own codec classes. |
|
833 |
||
834 |
The supported encodings are: |
|
835 |
||
836 |
\list |
|
837 |
\o Apple Roman |
|
838 |
\o \l{Big5 Text Codec}{Big5} |
|
839 |
\o \l{Big5-HKSCS Text Codec}{Big5-HKSCS} |
|
840 |
\o CP949 |
|
841 |
\o \l{EUC-JP Text Codec}{EUC-JP} |
|
842 |
\o \l{EUC-KR Text Codec}{EUC-KR} |
|
843 |
\o \l{GBK Text Codec}{GB18030-0} |
|
844 |
\o IBM 850 |
|
845 |
\o IBM 866 |
|
846 |
\o IBM 874 |
|
847 |
\o \l{ISO 2022-JP (JIS) Text Codec}{ISO 2022-JP} |
|
848 |
\o ISO 8859-1 to 10 |
|
849 |
\o ISO 8859-13 to 16 |
|
850 |
\o Iscii-Bng, Dev, Gjr, Knd, Mlm, Ori, Pnj, Tlg, and Tml |
|
851 |
\o JIS X 0201 |
|
852 |
\o JIS X 0208 |
|
853 |
\o KOI8-R |
|
854 |
\o KOI8-U |
|
855 |
\o MuleLao-1 |
|
856 |
\o ROMAN8 |
|
857 |
\o \l{Shift-JIS Text Codec}{Shift-JIS} |
|
858 |
\o TIS-620 |
|
859 |
\o \l{TSCII Text Codec}{TSCII} |
|
860 |
\o UTF-8 |
|
861 |
\o UTF-16 |
|
862 |
\o UTF-16BE |
|
863 |
\o UTF-16LE |
|
864 |
\o UTF-32 |
|
865 |
\o UTF-32BE |
|
866 |
\o UTF-32LE |
|
867 |
\o Windows-1250 to 1258 |
|
868 |
\o WINSAMI2 |
|
869 |
\endlist |
|
870 |
||
871 |
QTextCodecs can be used as follows to convert some locally encoded |
|
872 |
string to Unicode. Suppose you have some string encoded in Russian |
|
873 |
KOI8-R encoding, and want to convert it to Unicode. The simple way |
|
874 |
to do it is like this: |
|
875 |
||
876 |
\snippet doc/src/snippets/code/src_corelib_codecs_qtextcodec.cpp 0 |
|
877 |
||
878 |
After this, \c string holds the text converted to Unicode. |
|
879 |
Converting a string from Unicode to the local encoding is just as |
|
880 |
easy: |
|
881 |
||
882 |
\snippet doc/src/snippets/code/src_corelib_codecs_qtextcodec.cpp 1 |
|
883 |
||
884 |
To read or write files in various encodings, use QTextStream and |
|
885 |
its \l{QTextStream::setCodec()}{setCodec()} function. See the |
|
886 |
\l{tools/codecs}{Codecs} example for an application of QTextCodec |
|
887 |
to file I/O. |
|
888 |
||
889 |
Some care must be taken when trying to convert the data in chunks, |
|
890 |
for example, when receiving it over a network. In such cases it is |
|
891 |
possible that a multi-byte character will be split over two |
|
892 |
chunks. At best this might result in the loss of a character and |
|
893 |
at worst cause the entire conversion to fail. |
|
894 |
||
895 |
The approach to use in these situations is to create a QTextDecoder |
|
896 |
object for the codec and use this QTextDecoder for the whole |
|
897 |
decoding process, as shown below: |
|
898 |
||
899 |
\snippet doc/src/snippets/code/src_corelib_codecs_qtextcodec.cpp 2 |
|
900 |
||
901 |
The QTextDecoder object maintains state between chunks and therefore |
|
902 |
works correctly even if a multi-byte character is split between |
|
903 |
chunks. |
|
904 |
||
905 |
\section1 Creating Your Own Codec Class |
|
906 |
||
907 |
Support for new text encodings can be added to Qt by creating |
|
908 |
QTextCodec subclasses. |
|
909 |
||
910 |
The pure virtual functions describe the encoder to the system and |
|
911 |
the coder is used as required in the different text file formats |
|
912 |
supported by QTextStream, and under X11, for the locale-specific |
|
913 |
character input and output. |
|
914 |
||
915 |
To add support for another encoding to Qt, make a subclass of |
|
916 |
QTextCodec and implement the functions listed in the table below. |
|
917 |
||
918 |
\table |
|
919 |
\header \o Function \o Description |
|
920 |
||
921 |
\row \o name() |
|
922 |
\o Returns the official name for the encoding. If the |
|
923 |
encoding is listed in the |
|
924 |
\l{IANA character-sets encoding file}, the name |
|
925 |
should be the preferred MIME name for the encoding. |
|
926 |
||
927 |
\row \o aliases() |
|
928 |
\o Returns a list of alternative names for the encoding. |
|
929 |
QTextCodec provides a default implementation that returns |
|
930 |
an empty list. For example, "ISO-8859-1" has "latin1", |
|
931 |
"CP819", "IBM819", and "iso-ir-100" as aliases. |
|
932 |
||
933 |
\row \o mibEnum() |
|
934 |
\o Return the MIB enum for the encoding if it is listed in |
|
935 |
the \l{IANA character-sets encoding file}. |
|
936 |
||
937 |
\row \o convertToUnicode() |
|
938 |
\o Converts an 8-bit character string to Unicode. |
|
939 |
||
940 |
\row \o convertFromUnicode() |
|
941 |
\o Converts a Unicode string to an 8-bit character string. |
|
942 |
\endtable |
|
943 |
||
944 |
You may find it more convenient to make your codec class |
|
945 |
available as a plugin; see \l{How to Create Qt Plugins} for |
|
946 |
details. |
|
947 |
||
948 |
\sa QTextStream, QTextDecoder, QTextEncoder, {Codecs Example} |
|
949 |
*/ |
|
950 |
||
951 |
/*! |
|
952 |
Constructs a QTextCodec, and gives it the highest precedence. The |
|
953 |
QTextCodec should always be constructed on the heap (i.e. with \c |
|
954 |
new). Qt takes ownership and will delete it when the application |
|
955 |
terminates. |
|
956 |
*/ |
|
957 |
QTextCodec::QTextCodec() |
|
958 |
{ |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
959 |
#ifndef QT_NO_THREAD |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
960 |
QMutexLocker locker(textCodecsMutex()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
961 |
#endif |
0 | 962 |
setup(); |
963 |
all->prepend(this); |
|
964 |
} |
|
965 |
||
966 |
||
967 |
/*! |
|
968 |
\nonreentrant |
|
969 |
||
970 |
Destroys the QTextCodec. Note that you should not delete codecs |
|
971 |
yourself: once created they become Qt's responsibility. |
|
972 |
*/ |
|
973 |
QTextCodec::~QTextCodec() |
|
974 |
{ |
|
975 |
#ifdef Q_DEBUG_TEXTCODEC |
|
976 |
if (!destroying_is_ok) |
|
977 |
qWarning("QTextCodec::~QTextCodec: Called by application"); |
|
978 |
#endif |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
979 |
if (all) { |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
980 |
#ifndef QT_NO_THREAD |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
981 |
QMutexLocker locker(textCodecsMutex()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
982 |
#endif |
0 | 983 |
all->removeAll(this); |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
984 |
clearCaches = 0x1 | 0x2; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
985 |
} |
0 | 986 |
} |
987 |
||
988 |
/*! |
|
989 |
\fn QTextCodec *QTextCodec::codecForName(const char *name) |
|
990 |
||
991 |
Searches all installed QTextCodec objects and returns the one |
|
992 |
which best matches \a name; the match is case-insensitive. Returns |
|
993 |
0 if no codec matching the name \a name could be found. |
|
994 |
*/ |
|
995 |
||
996 |
/*! |
|
997 |
Searches all installed QTextCodec objects and returns the one |
|
998 |
which best matches \a name; the match is case-insensitive. Returns |
|
999 |
0 if no codec matching the name \a name could be found. |
|
1000 |
*/ |
|
1001 |
QTextCodec *QTextCodec::codecForName(const QByteArray &name) |
|
1002 |
{ |
|
1003 |
if (name.isEmpty()) |
|
1004 |
return 0; |
|
1005 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1006 |
#ifndef QT_NO_THREAD |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1007 |
QMutexLocker locker(textCodecsMutex()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1008 |
#endif |
0 | 1009 |
setup(); |
1010 |
||
8
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
1011 |
if (!validCodecs()) |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
1012 |
return 0; |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
1013 |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1014 |
static QHash <QByteArray, QTextCodec *> cache; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1015 |
if (clearCaches & 0x1) { |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1016 |
cache.clear(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1017 |
clearCaches &= ~0x1; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1018 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1019 |
QTextCodec *codec = cache.value(name); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1020 |
if (codec) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1021 |
return codec; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1022 |
|
0 | 1023 |
for (int i = 0; i < all->size(); ++i) { |
1024 |
QTextCodec *cursor = all->at(i); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1025 |
if (nameMatch(cursor->name(), name)) { |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1026 |
cache.insert(name, cursor); |
0 | 1027 |
return cursor; |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1028 |
} |
0 | 1029 |
QList<QByteArray> aliases = cursor->aliases(); |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1030 |
for (int y = 0; y < aliases.size(); ++y) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1031 |
if (nameMatch(aliases.at(y), name)) { |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1032 |
cache.insert(name, cursor); |
0 | 1033 |
return cursor; |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1034 |
} |
0 | 1035 |
} |
1036 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1037 |
codec = createForName(name); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1038 |
if (codec) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1039 |
cache.insert(name, codec); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1040 |
return codec; |
0 | 1041 |
} |
1042 |
||
1043 |
||
1044 |
/*! |
|
1045 |
Returns the QTextCodec which matches the \link |
|
1046 |
QTextCodec::mibEnum() MIBenum\endlink \a mib. |
|
1047 |
*/ |
|
1048 |
QTextCodec* QTextCodec::codecForMib(int mib) |
|
1049 |
{ |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1050 |
#ifndef QT_NO_THREAD |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1051 |
QMutexLocker locker(textCodecsMutex()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1052 |
#endif |
0 | 1053 |
setup(); |
1054 |
||
8
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
1055 |
if (!validCodecs()) |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
1056 |
return 0; |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
1057 |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1058 |
static QHash <int, QTextCodec *> cache; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1059 |
if (clearCaches & 0x2) { |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1060 |
cache.clear(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1061 |
clearCaches &= ~0x2; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1062 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1063 |
QTextCodec *codec = cache.value(mib); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1064 |
if (codec) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1065 |
return codec; |
0 | 1066 |
|
1067 |
QList<QTextCodec*>::ConstIterator i; |
|
1068 |
for (int i = 0; i < all->size(); ++i) { |
|
1069 |
QTextCodec *cursor = all->at(i); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1070 |
if (cursor->mibEnum() == mib) { |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1071 |
cache.insert(mib, cursor); |
0 | 1072 |
return cursor; |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1073 |
} |
0 | 1074 |
} |
1075 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1076 |
codec = createForMib(mib); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1077 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1078 |
// Qt 3 used 1000 (mib for UCS2) as its identifier for the utf16 codec. Map |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1079 |
// this correctly for compatibility. |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1080 |
if (!codec && mib == 1000) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1081 |
return codecForMib(1015); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1082 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1083 |
if (codec) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1084 |
cache.insert(mib, codec); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1085 |
return codec; |
0 | 1086 |
} |
1087 |
||
1088 |
/*! |
|
1089 |
Returns the list of all available codecs, by name. Call |
|
1090 |
QTextCodec::codecForName() to obtain the QTextCodec for the name. |
|
1091 |
||
1092 |
The list may contain many mentions of the same codec |
|
1093 |
if the codec has aliases. |
|
1094 |
||
1095 |
\sa availableMibs(), name(), aliases() |
|
1096 |
*/ |
|
1097 |
QList<QByteArray> QTextCodec::availableCodecs() |
|
1098 |
{ |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1099 |
#ifndef QT_NO_THREAD |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1100 |
QMutexLocker locker(textCodecsMutex()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1101 |
#endif |
0 | 1102 |
setup(); |
1103 |
||
1104 |
QList<QByteArray> codecs; |
|
8
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
1105 |
|
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
1106 |
if (!validCodecs()) |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
1107 |
return codecs; |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
1108 |
|
0 | 1109 |
for (int i = 0; i < all->size(); ++i) { |
1110 |
codecs += all->at(i)->name(); |
|
1111 |
codecs += all->at(i)->aliases(); |
|
1112 |
} |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1113 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1114 |
#ifndef QT_NO_THREAD |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1115 |
locker.unlock(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1116 |
#endif |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1117 |
|
0 | 1118 |
#ifndef QT_NO_TEXTCODECPLUGIN |
1119 |
QFactoryLoader *l = loader(); |
|
1120 |
QStringList keys = l->keys(); |
|
1121 |
for (int i = 0; i < keys.size(); ++i) { |
|
1122 |
if (!keys.at(i).startsWith(QLatin1String("MIB: "))) { |
|
1123 |
QByteArray name = keys.at(i).toLatin1(); |
|
1124 |
if (!codecs.contains(name)) |
|
1125 |
codecs += name; |
|
1126 |
} |
|
1127 |
} |
|
1128 |
#endif |
|
1129 |
||
1130 |
return codecs; |
|
1131 |
} |
|
1132 |
||
1133 |
/*! |
|
1134 |
Returns the list of MIBs for all available codecs. Call |
|
1135 |
QTextCodec::codecForMib() to obtain the QTextCodec for the MIB. |
|
1136 |
||
1137 |
\sa availableCodecs(), mibEnum() |
|
1138 |
*/ |
|
1139 |
QList<int> QTextCodec::availableMibs() |
|
1140 |
{ |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1141 |
#ifndef QT_NO_THREAD |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1142 |
QMutexLocker locker(textCodecsMutex()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1143 |
#endif |
0 | 1144 |
setup(); |
1145 |
||
1146 |
QList<int> codecs; |
|
8
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
1147 |
|
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
1148 |
if (!validCodecs()) |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
1149 |
return codecs; |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
1150 |
|
0 | 1151 |
for (int i = 0; i < all->size(); ++i) |
1152 |
codecs += all->at(i)->mibEnum(); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1153 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1154 |
#ifndef QT_NO_THREAD |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1155 |
locker.unlock(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1156 |
#endif |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1157 |
|
0 | 1158 |
#ifndef QT_NO_TEXTCODECPLUGIN |
1159 |
QFactoryLoader *l = loader(); |
|
1160 |
QStringList keys = l->keys(); |
|
1161 |
for (int i = 0; i < keys.size(); ++i) { |
|
1162 |
if (keys.at(i).startsWith(QLatin1String("MIB: "))) { |
|
1163 |
int mib = keys.at(i).mid(5).toInt(); |
|
1164 |
if (!codecs.contains(mib)) |
|
1165 |
codecs += mib; |
|
1166 |
} |
|
1167 |
} |
|
1168 |
#endif |
|
1169 |
||
1170 |
return codecs; |
|
1171 |
} |
|
1172 |
||
1173 |
/*! |
|
1174 |
Set the codec to \a c; this will be returned by |
|
1175 |
codecForLocale(). If \a c is a null pointer, the codec is reset to |
|
1176 |
the default. |
|
1177 |
||
1178 |
This might be needed for some applications that want to use their |
|
1179 |
own mechanism for setting the locale. |
|
1180 |
||
1181 |
\sa codecForLocale() |
|
1182 |
*/ |
|
1183 |
void QTextCodec::setCodecForLocale(QTextCodec *c) |
|
1184 |
{ |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1185 |
#ifndef QT_NO_THREAD |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1186 |
QMutexLocker locker(textCodecsMutex()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1187 |
#endif |
0 | 1188 |
localeMapper = c; |
1189 |
if (!localeMapper) |
|
1190 |
setupLocaleMapper(); |
|
1191 |
} |
|
1192 |
||
1193 |
/*! |
|
1194 |
Returns a pointer to the codec most suitable for this locale. |
|
1195 |
||
1196 |
On Windows, the codec will be based on a system locale. On Unix |
|
1197 |
systems, starting with Qt 4.2, the codec will be using the \e |
|
1198 |
iconv library. Note that in both cases the codec's name will be |
|
1199 |
"System". |
|
1200 |
*/ |
|
1201 |
||
1202 |
QTextCodec* QTextCodec::codecForLocale() |
|
1203 |
{ |
|
8
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
1204 |
if (!validCodecs()) |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
1205 |
return 0; |
740e5562c97f
8b5beb2a553102639e9eb38c8f8f0f6775e8545b
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
1206 |
|
0 | 1207 |
if (localeMapper) |
1208 |
return localeMapper; |
|
1209 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1210 |
#ifndef QT_NO_THREAD |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1211 |
QMutexLocker locker(textCodecsMutex()); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1212 |
#endif |
0 | 1213 |
setup(); |
1214 |
||
1215 |
return localeMapper; |
|
1216 |
} |
|
1217 |
||
1218 |
||
1219 |
/*! |
|
1220 |
\fn QByteArray QTextCodec::name() const |
|
1221 |
||
1222 |
QTextCodec subclasses must reimplement this function. It returns |
|
1223 |
the name of the encoding supported by the subclass. |
|
1224 |
||
1225 |
If the codec is registered as a character set in the |
|
1226 |
\l{IANA character-sets encoding file} this method should |
|
1227 |
return the preferred mime name for the codec if defined, |
|
1228 |
otherwise its name. |
|
1229 |
*/ |
|
1230 |
||
1231 |
/*! |
|
1232 |
\fn int QTextCodec::mibEnum() const |
|
1233 |
||
1234 |
Subclasses of QTextCodec must reimplement this function. It |
|
1235 |
returns the MIBenum (see \l{IANA character-sets encoding file} |
|
1236 |
for more information). It is important that each QTextCodec |
|
1237 |
subclass returns the correct unique value for this function. |
|
1238 |
*/ |
|
1239 |
||
1240 |
/*! |
|
1241 |
Subclasses can return a number of aliases for the codec in question. |
|
1242 |
||
1243 |
Standard aliases for codecs can be found in the |
|
1244 |
\l{IANA character-sets encoding file}. |
|
1245 |
*/ |
|
1246 |
QList<QByteArray> QTextCodec::aliases() const |
|
1247 |
{ |
|
1248 |
return QList<QByteArray>(); |
|
1249 |
} |
|
1250 |
||
1251 |
/*! |
|
1252 |
\fn QString QTextCodec::convertToUnicode(const char *chars, int len, |
|
1253 |
ConverterState *state) const |
|
1254 |
||
1255 |
QTextCodec subclasses must reimplement this function. |
|
1256 |
||
1257 |
Converts the first \a len characters of \a chars from the |
|
1258 |
encoding of the subclass to Unicode, and returns the result in a |
|
1259 |
QString. |
|
1260 |
||
1261 |
\a state can be 0, in which case the conversion is stateless and |
|
1262 |
default conversion rules should be used. If state is not 0, the |
|
1263 |
codec should save the state after the conversion in \a state, and |
|
1264 |
adjust the remainingChars and invalidChars members of the struct. |
|
1265 |
*/ |
|
1266 |
||
1267 |
/*! |
|
1268 |
\fn QByteArray QTextCodec::convertFromUnicode(const QChar *input, int number, |
|
1269 |
ConverterState *state) const |
|
1270 |
||
1271 |
QTextCodec subclasses must reimplement this function. |
|
1272 |
||
1273 |
Converts the first \a number of characters from the \a input array |
|
1274 |
from Unicode to the encoding of the subclass, and returns the result |
|
1275 |
in a QByteArray. |
|
1276 |
||
1277 |
\a state can be 0 in which case the conversion is stateless and |
|
1278 |
default conversion rules should be used. If state is not 0, the |
|
1279 |
codec should save the state after the conversion in \a state, and |
|
1280 |
adjust the remainingChars and invalidChars members of the struct. |
|
1281 |
*/ |
|
1282 |
||
1283 |
/*! |
|
1284 |
Creates a QTextDecoder which stores enough state to decode chunks |
|
1285 |
of \c{char *} data to create chunks of Unicode data. |
|
1286 |
||
1287 |
The caller is responsible for deleting the returned object. |
|
1288 |
*/ |
|
1289 |
QTextDecoder* QTextCodec::makeDecoder() const |
|
1290 |
{ |
|
1291 |
return new QTextDecoder(this); |
|
1292 |
} |
|
1293 |
||
1294 |
||
1295 |
/*! |
|
1296 |
Creates a QTextEncoder which stores enough state to encode chunks |
|
1297 |
of Unicode data as \c{char *} data. |
|
1298 |
||
1299 |
The caller is responsible for deleting the returned object. |
|
1300 |
*/ |
|
1301 |
QTextEncoder* QTextCodec::makeEncoder() const |
|
1302 |
{ |
|
1303 |
return new QTextEncoder(this); |
|
1304 |
} |
|
1305 |
||
1306 |
/*! |
|
1307 |
\fn QByteArray QTextCodec::fromUnicode(const QChar *input, int number, |
|
1308 |
ConverterState *state) const |
|
1309 |
||
1310 |
Converts the first \a number of characters from the \a input array |
|
1311 |
from Unicode to the encoding of this codec, and returns the result |
|
1312 |
in a QByteArray. |
|
1313 |
||
1314 |
The \a state of the convertor used is updated. |
|
1315 |
*/ |
|
1316 |
||
1317 |
/*! |
|
1318 |
Converts \a str from Unicode to the encoding of this codec, and |
|
1319 |
returns the result in a QByteArray. |
|
1320 |
*/ |
|
1321 |
QByteArray QTextCodec::fromUnicode(const QString& str) const |
|
1322 |
{ |
|
1323 |
return convertFromUnicode(str.constData(), str.length(), 0); |
|
1324 |
} |
|
1325 |
||
1326 |
/*! |
|
1327 |
\fn QString QTextCodec::toUnicode(const char *input, int size, |
|
1328 |
ConverterState *state) const |
|
1329 |
||
1330 |
Converts the first \a size characters from the \a input from the |
|
1331 |
encoding of this codec to Unicode, and returns the result in a |
|
1332 |
QString. |
|
1333 |
||
1334 |
The \a state of the convertor used is updated. |
|
1335 |
*/ |
|
1336 |
||
1337 |
/*! |
|
1338 |
Converts \a a from the encoding of this codec to Unicode, and |
|
1339 |
returns the result in a QString. |
|
1340 |
*/ |
|
1341 |
QString QTextCodec::toUnicode(const QByteArray& a) const |
|
1342 |
{ |
|
1343 |
return convertToUnicode(a.constData(), a.length(), 0); |
|
1344 |
} |
|
1345 |
||
1346 |
/*! |
|
1347 |
Returns true if the Unicode character \a ch can be fully encoded |
|
1348 |
with this codec; otherwise returns false. |
|
1349 |
*/ |
|
1350 |
bool QTextCodec::canEncode(QChar ch) const |
|
1351 |
{ |
|
1352 |
ConverterState state; |
|
1353 |
state.flags = ConvertInvalidToNull; |
|
1354 |
convertFromUnicode(&ch, 1, &state); |
|
1355 |
return (state.invalidChars == 0); |
|
1356 |
} |
|
1357 |
||
1358 |
/*! |
|
1359 |
\overload |
|
1360 |
||
1361 |
\a s contains the string being tested for encode-ability. |
|
1362 |
*/ |
|
1363 |
bool QTextCodec::canEncode(const QString& s) const |
|
1364 |
{ |
|
1365 |
ConverterState state; |
|
1366 |
state.flags = ConvertInvalidToNull; |
|
1367 |
convertFromUnicode(s.constData(), s.length(), &state); |
|
1368 |
return (state.invalidChars == 0); |
|
1369 |
} |
|
1370 |
||
1371 |
#ifdef QT3_SUPPORT |
|
1372 |
/*! |
|
1373 |
Returns a string representing the current language and |
|
1374 |
sublanguage, e.g. "pt" for Portuguese, or "pt_br" for Portuguese/Brazil. |
|
1375 |
||
1376 |
\sa QLocale |
|
1377 |
*/ |
|
1378 |
const char *QTextCodec::locale() |
|
1379 |
{ |
|
1380 |
static char locale[6]; |
|
1381 |
QByteArray l = QLocale::system().name().toLatin1(); |
|
1382 |
int len = qMin(l.length(), 5); |
|
1383 |
memcpy(locale, l.constData(), len); |
|
1384 |
locale[len] = '\0'; |
|
1385 |
||
1386 |
return locale; |
|
1387 |
} |
|
1388 |
||
1389 |
/*! |
|
1390 |
\overload |
|
1391 |
*/ |
|
1392 |
||
1393 |
QByteArray QTextCodec::fromUnicode(const QString& uc, int& lenInOut) const |
|
1394 |
{ |
|
1395 |
QByteArray result = convertFromUnicode(uc.constData(), lenInOut, 0); |
|
1396 |
lenInOut = result.length(); |
|
1397 |
return result; |
|
1398 |
} |
|
1399 |
||
1400 |
/*! |
|
1401 |
\overload |
|
1402 |
||
1403 |
\a a contains the source characters; \a len contains the number of |
|
1404 |
characters in \a a to use. |
|
1405 |
*/ |
|
1406 |
QString QTextCodec::toUnicode(const QByteArray& a, int len) const |
|
1407 |
{ |
|
1408 |
len = qMin(a.size(), len); |
|
1409 |
return convertToUnicode(a.constData(), len, 0); |
|
1410 |
} |
|
1411 |
#endif |
|
1412 |
||
1413 |
/*! |
|
1414 |
\overload |
|
1415 |
||
1416 |
\a chars contains the source characters. |
|
1417 |
*/ |
|
1418 |
QString QTextCodec::toUnicode(const char *chars) const |
|
1419 |
{ |
|
1420 |
int len = qstrlen(chars); |
|
1421 |
return convertToUnicode(chars, len, 0); |
|
1422 |
} |
|
1423 |
||
1424 |
||
1425 |
/*! |
|
1426 |
\class QTextEncoder |
|
1427 |
\brief The QTextEncoder class provides a state-based encoder. |
|
1428 |
\reentrant |
|
1429 |
\ingroup i18n |
|
1430 |
||
1431 |
A text encoder converts text from Unicode into an encoded text format |
|
1432 |
using a specific codec. |
|
1433 |
||
1434 |
The encoder converts Unicode into another format, remembering any |
|
1435 |
state that is required between calls. |
|
1436 |
||
1437 |
\sa QTextCodec::makeEncoder(), QTextDecoder |
|
1438 |
*/ |
|
1439 |
||
1440 |
/*! |
|
1441 |
\fn QTextEncoder::QTextEncoder(const QTextCodec *codec) |
|
1442 |
||
1443 |
Constructs a text encoder for the given \a codec. |
|
1444 |
*/ |
|
1445 |
||
1446 |
/*! |
|
1447 |
Destroys the encoder. |
|
1448 |
*/ |
|
1449 |
QTextEncoder::~QTextEncoder() |
|
1450 |
{ |
|
1451 |
} |
|
1452 |
||
1453 |
/*! \internal |
|
1454 |
\since 4.5 |
|
1455 |
Determines whether the eecoder encountered a failure while decoding the input. If |
|
1456 |
an error was encountered, the produced result is undefined, and gets converted as according |
|
1457 |
to the conversion flags. |
|
1458 |
*/ |
|
1459 |
bool QTextEncoder::hasFailure() const |
|
1460 |
{ |
|
1461 |
return state.invalidChars != 0; |
|
1462 |
} |
|
1463 |
||
1464 |
/*! |
|
1465 |
Converts the Unicode string \a str into an encoded QByteArray. |
|
1466 |
*/ |
|
1467 |
QByteArray QTextEncoder::fromUnicode(const QString& str) |
|
1468 |
{ |
|
1469 |
QByteArray result = c->fromUnicode(str.constData(), str.length(), &state); |
|
1470 |
return result; |
|
1471 |
} |
|
1472 |
||
1473 |
/*! |
|
1474 |
\overload |
|
1475 |
||
1476 |
Converts \a len characters (not bytes) from \a uc, and returns the |
|
1477 |
result in a QByteArray. |
|
1478 |
*/ |
|
1479 |
QByteArray QTextEncoder::fromUnicode(const QChar *uc, int len) |
|
1480 |
{ |
|
1481 |
QByteArray result = c->fromUnicode(uc, len, &state); |
|
1482 |
return result; |
|
1483 |
} |
|
1484 |
||
1485 |
#ifdef QT3_SUPPORT |
|
1486 |
/*! |
|
1487 |
\overload |
|
1488 |
||
1489 |
Converts \a lenInOut characters (not bytes) from \a uc, and returns the |
|
1490 |
result in a QByteArray. The number of characters read is returned in |
|
1491 |
the \a lenInOut parameter. |
|
1492 |
*/ |
|
1493 |
QByteArray QTextEncoder::fromUnicode(const QString& uc, int& lenInOut) |
|
1494 |
{ |
|
1495 |
QByteArray result = c->fromUnicode(uc.constData(), lenInOut, &state); |
|
1496 |
lenInOut = result.length(); |
|
1497 |
return result; |
|
1498 |
} |
|
1499 |
#endif |
|
1500 |
||
1501 |
/*! |
|
1502 |
\class QTextDecoder |
|
1503 |
\brief The QTextDecoder class provides a state-based decoder. |
|
1504 |
\reentrant |
|
1505 |
\ingroup i18n |
|
1506 |
||
1507 |
A text decoder converts text from an encoded text format into Unicode |
|
1508 |
using a specific codec. |
|
1509 |
||
1510 |
The decoder converts text in this format into Unicode, remembering any |
|
1511 |
state that is required between calls. |
|
1512 |
||
1513 |
\sa QTextCodec::makeDecoder(), QTextEncoder |
|
1514 |
*/ |
|
1515 |
||
1516 |
/*! |
|
1517 |
\fn QTextDecoder::QTextDecoder(const QTextCodec *codec) |
|
1518 |
||
1519 |
Constructs a text decoder for the given \a codec. |
|
1520 |
*/ |
|
1521 |
||
1522 |
/*! |
|
1523 |
Destroys the decoder. |
|
1524 |
*/ |
|
1525 |
QTextDecoder::~QTextDecoder() |
|
1526 |
{ |
|
1527 |
} |
|
1528 |
||
1529 |
/*! |
|
1530 |
\fn QString QTextDecoder::toUnicode(const char *chars, int len) |
|
1531 |
||
1532 |
Converts the first \a len bytes in \a chars to Unicode, returning |
|
1533 |
the result. |
|
1534 |
||
1535 |
If not all characters are used (e.g. if only part of a multi-byte |
|
1536 |
encoding is at the end of the characters), the decoder remembers |
|
1537 |
enough state to continue with the next call to this function. |
|
1538 |
*/ |
|
1539 |
QString QTextDecoder::toUnicode(const char *chars, int len) |
|
1540 |
{ |
|
1541 |
return c->toUnicode(chars, len, &state); |
|
1542 |
} |
|
1543 |
||
1544 |
||
1545 |
/*! \overload |
|
1546 |
||
1547 |
The converted string is returned in \a target. |
|
1548 |
*/ |
|
1549 |
void QTextDecoder::toUnicode(QString *target, const char *chars, int len) |
|
1550 |
{ |
|
1551 |
Q_ASSERT(target); |
|
1552 |
switch (c->mibEnum()) { |
|
1553 |
case 106: // utf8 |
|
1554 |
static_cast<const QUtf8Codec*>(c)->convertToUnicode(target, chars, len, &state); |
|
1555 |
break; |
|
1556 |
case 4: { // latin1 |
|
1557 |
target->resize(len); |
|
1558 |
ushort *data = (ushort*)target->data(); |
|
1559 |
for (int i = len; i >=0; --i) |
|
1560 |
data[i] = (uchar) chars[i]; |
|
1561 |
} break; |
|
1562 |
default: |
|
1563 |
*target = c->toUnicode(chars, len, &state); |
|
1564 |
} |
|
1565 |
} |
|
1566 |
||
1567 |
||
1568 |
/*! |
|
1569 |
\overload |
|
1570 |
||
1571 |
Converts the bytes in the byte array specified by \a ba to Unicode |
|
1572 |
and returns the result. |
|
1573 |
*/ |
|
1574 |
QString QTextDecoder::toUnicode(const QByteArray &ba) |
|
1575 |
{ |
|
1576 |
return c->toUnicode(ba.constData(), ba.length(), &state); |
|
1577 |
} |
|
1578 |
||
1579 |
||
1580 |
/*! |
|
1581 |
\fn QTextCodec* QTextCodec::codecForTr() |
|
1582 |
||
1583 |
Returns the codec used by QObject::tr() on its argument. If this |
|
1584 |
function returns 0 (the default), tr() assumes Latin-1. |
|
1585 |
||
1586 |
\sa setCodecForTr() |
|
1587 |
*/ |
|
1588 |
||
1589 |
/*! |
|
1590 |
\fn void QTextCodec::setCodecForTr(QTextCodec *c) |
|
1591 |
\nonreentrant |
|
1592 |
||
1593 |
Sets the codec used by QObject::tr() on its argument to \a c. If |
|
1594 |
\a c is 0 (the default), tr() assumes Latin-1. |
|
1595 |
||
1596 |
If the literal quoted text in the program is not in the Latin-1 |
|
1597 |
encoding, this function can be used to set the appropriate |
|
1598 |
encoding. For example, software developed by Korean programmers |
|
1599 |
might use eucKR for all the text in the program, in which case the |
|
1600 |
main() function might look like this: |
|
1601 |
||
1602 |
\snippet doc/src/snippets/code/src_corelib_codecs_qtextcodec.cpp 3 |
|
1603 |
||
1604 |
Note that this is not the way to select the encoding that the \e |
|
1605 |
user has chosen. For example, to convert an application containing |
|
1606 |
literal English strings to Korean, all that is needed is for the |
|
1607 |
English strings to be passed through tr() and for translation |
|
1608 |
files to be loaded. For details of internationalization, see |
|
1609 |
\l{Internationalization with Qt}. |
|
1610 |
||
1611 |
\sa codecForTr(), setCodecForCStrings() |
|
1612 |
*/ |
|
1613 |
||
1614 |
||
1615 |
/*! |
|
1616 |
\fn QTextCodec* QTextCodec::codecForCStrings() |
|
1617 |
||
1618 |
Returns the codec used by QString to convert to and from \c{const |
|
1619 |
char *} and QByteArrays. If this function returns 0 (the default), |
|
1620 |
QString assumes Latin-1. |
|
1621 |
||
1622 |
\sa setCodecForCStrings() |
|
1623 |
*/ |
|
1624 |
||
1625 |
/*! |
|
1626 |
\fn void QTextCodec::setCodecForCStrings(QTextCodec *codec) |
|
1627 |
\nonreentrant |
|
1628 |
||
1629 |
Sets the codec used by QString to convert to and from \c{const |
|
1630 |
char *} and QByteArrays. If the \a codec is 0 (the default), |
|
1631 |
QString assumes Latin-1. |
|
1632 |
||
1633 |
\warning Some codecs do not preserve the characters in the ASCII |
|
1634 |
range (0x00 to 0x7F). For example, the Japanese Shift-JIS |
|
1635 |
encoding maps the backslash character (0x5A) to the Yen |
|
1636 |
character. To avoid undesirable side-effects, we recommend |
|
1637 |
avoiding such codecs with setCodecsForCString(). |
|
1638 |
||
1639 |
\sa codecForCStrings(), setCodecForTr() |
|
1640 |
*/ |
|
1641 |
||
1642 |
/*! |
|
1643 |
\since 4.4 |
|
1644 |
||
1645 |
Tries to detect the encoding of the provided snippet of HTML in |
|
1646 |
the given byte array, \a ba, by checking the BOM (Byte Order Mark) |
|
1647 |
and the content-type meta header and returns a QTextCodec instance |
|
1648 |
that is capable of decoding the html to unicode. If the codec |
|
1649 |
cannot be detected from the content provided, \a defaultCodec is |
|
1650 |
returned. |
|
1651 |
||
1652 |
\sa codecForUtfText() |
|
1653 |
*/ |
|
1654 |
QTextCodec *QTextCodec::codecForHtml(const QByteArray &ba, QTextCodec *defaultCodec) |
|
1655 |
{ |
|
1656 |
// determine charset |
|
1657 |
int pos; |
|
1658 |
QTextCodec *c = 0; |
|
1659 |
||
1660 |
c = QTextCodec::codecForUtfText(ba, c); |
|
1661 |
if (!c) { |
|
1662 |
QByteArray header = ba.left(512).toLower(); |
|
1663 |
if ((pos = header.indexOf("http-equiv=")) != -1) { |
|
1664 |
if ((pos = header.lastIndexOf("meta ", pos)) != -1) { |
|
1665 |
pos = header.indexOf("charset=", pos) + int(strlen("charset=")); |
|
1666 |
if (pos != -1) { |
|
1667 |
int pos2 = header.indexOf('\"', pos+1); |
|
1668 |
QByteArray cs = header.mid(pos, pos2-pos); |
|
1669 |
// qDebug("found charset: %s", cs.data()); |
|
1670 |
c = QTextCodec::codecForName(cs); |
|
1671 |
} |
|
1672 |
} |
|
1673 |
} |
|
1674 |
} |
|
1675 |
if (!c) |
|
1676 |
c = defaultCodec; |
|
1677 |
||
1678 |
return c; |
|
1679 |
} |
|
1680 |
||
1681 |
/*! |
|
1682 |
\overload |
|
1683 |
||
1684 |
Tries to detect the encoding of the provided snippet of HTML in |
|
1685 |
the given byte array, \a ba, by checking the BOM (Byte Order Mark) |
|
1686 |
and the content-type meta header and returns a QTextCodec instance |
|
1687 |
that is capable of decoding the html to unicode. If the codec cannot |
|
1688 |
be detected, this overload returns a Latin-1 QTextCodec. |
|
1689 |
*/ |
|
1690 |
QTextCodec *QTextCodec::codecForHtml(const QByteArray &ba) |
|
1691 |
{ |
|
1692 |
return codecForHtml(ba, QTextCodec::codecForMib(/*Latin 1*/ 4)); |
|
1693 |
} |
|
1694 |
||
1695 |
/*! |
|
1696 |
\since 4.6 |
|
1697 |
||
1698 |
Tries to detect the encoding of the provided snippet \a ba by |
|
1699 |
using the BOM (Byte Order Mark) and returns a QTextCodec instance |
|
1700 |
that is capable of decoding the text to unicode. If the codec |
|
1701 |
cannot be detected from the content provided, \a defaultCodec is |
|
1702 |
returned. |
|
1703 |
||
1704 |
\sa codecForHtml() |
|
1705 |
*/ |
|
1706 |
QTextCodec *QTextCodec::codecForUtfText(const QByteArray &ba, QTextCodec *defaultCodec) |
|
1707 |
{ |
|
1708 |
const int arraySize = ba.size(); |
|
1709 |
||
1710 |
if (arraySize > 3) { |
|
1711 |
if ((uchar)ba[0] == 0x00 |
|
1712 |
&& (uchar)ba[1] == 0x00 |
|
1713 |
&& (uchar)ba[2] == 0xFE |
|
1714 |
&& (uchar)ba[3] == 0xFF) |
|
1715 |
return QTextCodec::codecForMib(1018); // utf-32 be |
|
1716 |
else if ((uchar)ba[0] == 0xFF |
|
1717 |
&& (uchar)ba[1] == 0xFE |
|
1718 |
&& (uchar)ba[2] == 0x00 |
|
1719 |
&& (uchar)ba[3] == 0x00) |
|
1720 |
return QTextCodec::codecForMib(1019); // utf-32 le |
|
1721 |
} |
|
1722 |
||
1723 |
if (arraySize < 2) |
|
1724 |
return defaultCodec; |
|
1725 |
if ((uchar)ba[0] == 0xfe && (uchar)ba[1] == 0xff) |
|
1726 |
return QTextCodec::codecForMib(1013); // utf16 be |
|
1727 |
else if ((uchar)ba[0] == 0xff && (uchar)ba[1] == 0xfe) |
|
1728 |
return QTextCodec::codecForMib(1014); // utf16 le |
|
1729 |
||
1730 |
if (arraySize < 3) |
|
1731 |
return defaultCodec; |
|
1732 |
if ((uchar)ba[0] == 0xef |
|
1733 |
&& (uchar)ba[1] == 0xbb |
|
1734 |
&& (uchar)ba[2] == 0xbf) |
|
1735 |
return QTextCodec::codecForMib(106); // utf-8 |
|
1736 |
||
1737 |
return defaultCodec; |
|
1738 |
} |
|
1739 |
||
1740 |
/*! |
|
1741 |
\overload |
|
1742 |
||
1743 |
Tries to detect the encoding of the provided snippet \a ba by |
|
1744 |
using the BOM (Byte Order Mark) and returns a QTextCodec instance |
|
1745 |
that is capable of decoding the text to unicode. If the codec |
|
1746 |
cannot be detected, this overload returns a Latin-1 QTextCodec. |
|
1747 |
||
1748 |
\sa codecForHtml() |
|
1749 |
*/ |
|
1750 |
QTextCodec *QTextCodec::codecForUtfText(const QByteArray &ba) |
|
1751 |
{ |
|
1752 |
return codecForUtfText(ba, QTextCodec::codecForMib(/*Latin 1*/ 4)); |
|
1753 |
} |
|
1754 |
||
1755 |
||
1756 |
/*! \internal |
|
1757 |
\since 4.3 |
|
1758 |
Determines whether the decoder encountered a failure while decoding the input. If |
|
1759 |
an error was encountered, the produced result is undefined, and gets converted as according |
|
1760 |
to the conversion flags. |
|
1761 |
*/ |
|
1762 |
bool QTextDecoder::hasFailure() const |
|
1763 |
{ |
|
1764 |
return state.invalidChars != 0; |
|
1765 |
} |
|
1766 |
||
1767 |
/*! |
|
1768 |
\fn QTextCodec *QTextCodec::codecForContent(const char *str, int size) |
|
1769 |
||
1770 |
This functionality is no longer provided by Qt. This |
|
1771 |
compatibility function always returns a null pointer. |
|
1772 |
*/ |
|
1773 |
||
1774 |
/*! |
|
1775 |
\fn QTextCodec *QTextCodec::codecForName(const char *hint, int accuracy) |
|
1776 |
||
1777 |
Use the codecForName(const QByteArray &) overload instead. |
|
1778 |
*/ |
|
1779 |
||
1780 |
/*! |
|
1781 |
\fn QTextCodec *QTextCodec::codecForIndex(int i) |
|
1782 |
||
1783 |
Use availableCodecs() or availableMibs() instead and iterate |
|
1784 |
through the resulting list. |
|
1785 |
*/ |
|
1786 |
||
1787 |
||
1788 |
/*! |
|
1789 |
\fn QByteArray QTextCodec::mimeName() const |
|
1790 |
||
1791 |
Use name() instead. |
|
1792 |
*/ |
|
1793 |
||
1794 |
QT_END_NAMESPACE |
|
1795 |
||
1796 |
#endif // QT_NO_TEXTCODEC |