forked from doxygen/doxygen
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqtextcodec.cpp
More file actions
2071 lines (1804 loc) · 78.5 KB
/
qtextcodec.cpp
File metadata and controls
2071 lines (1804 loc) · 78.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
**
** Implementation of QTextCodec class
**
** Created : 981015
**
** Copyright (C)1998-2000 Trolltech AS. All rights reserved.
**
** This file is part of the tools module of the Qt GUI Toolkit.
**
** This file may be distributed under the terms of the Q Public License
** as defined by Trolltech AS of Norway and appearing in the file
** LICENSE.QPL included in the packaging of this file.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
** licenses may use this file in accordance with the Qt Commercial License
** Agreement provided with the Software.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
** information about Qt Commercial License Agreements.
** See http://www.trolltech.com/qpl/ for QPL licensing information.
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
#include "qtextcodec.h"
#ifndef QT_NO_TEXTCODEC
#include "qinternallist.h"
#ifndef QT_NO_CODECS
#include "qutfcodec.h"
//#include "qgbkcodec.h"
//#include "qeucjpcodec.h"
//#include "qjiscodec.h"
//#include "qsjiscodec.h"
//#include "qeuckrcodec.h"
//#include "qbig5codec.h"
//#include "qrtlcodec.h"
//#include "qtsciicodec.h"
#endif
#include "qfile.h"
#include "qstrlist.h"
#include "qstring.h"
#include <stdlib.h>
#include <ctype.h>
#include <locale.h>
static QInternalList<QTextCodec> * all = 0;
static bool destroying_is_ok; // starts out as 0
/*! Deletes all the created codecs.
\warning Do not call this function.
QApplication calls this just before exiting, to delete any
QTextCodec objects that may be lying around. Since various other
classes hold pointers to QTextCodec objects, it is not safe to call
this function earlier.
If you are using the utility classes (like QString) but not using
QApplication, calling this function at the very end of your
application can be helpful to chasing down memory leaks, as
QTextCodec objects will not show up.
*/
void QTextCodec::deleteAllCodecs()
{
if ( !all )
return;
destroying_is_ok = TRUE;
QInternalList<QTextCodec> * ball = all;
all = 0;
ball->clear();
delete ball;
destroying_is_ok = FALSE;
}
static void setupBuiltinCodecs();
static void realSetup()
{
#if defined(CHECK_STATE)
if ( destroying_is_ok )
qWarning( "creating new codec during codec cleanup" );
#endif
all = new QInternalList<QTextCodec>;
all->setAutoDelete( TRUE );
setupBuiltinCodecs();
}
static inline void setup()
{
if ( !all )
realSetup();
}
class QTextStatelessEncoder: public QTextEncoder {
const QTextCodec* codec;
public:
QTextStatelessEncoder(const QTextCodec*);
QCString fromUnicode(const QString& uc, int& lenInOut);
};
class QTextStatelessDecoder : public QTextDecoder {
const QTextCodec* codec;
public:
QTextStatelessDecoder(const QTextCodec*);
QString toUnicode(const char* chars, int len);
};
QTextStatelessEncoder::QTextStatelessEncoder(const QTextCodec* c) :
codec(c)
{
}
QCString QTextStatelessEncoder::fromUnicode(const QString& uc, int& lenInOut)
{
return codec->fromUnicode(uc,lenInOut);
}
QTextStatelessDecoder::QTextStatelessDecoder(const QTextCodec* c) :
codec(c)
{
}
QString QTextStatelessDecoder::toUnicode(const char* chars, int len)
{
return codec->toUnicode(chars,len);
}
// NOT REVISED
/*!
\class QTextCodec qtextcodec.h
\brief Provides conversion between text encodings.
By making objects of subclasses of QTextCodec, support for
new text encodings can be added to Qt.
The abstract virtual functions describe the encoder to the
system and the coder is used as required in the different
text file formats supported QTextStream and, under X11 for the
locale-specific character input and output (under Windows NT
codecs are not needed for GUI I/O since the system works
with Unicode already, and Windows 95/98 has built-in convertors
for the 8-bit local encoding).
More recently created QTextCodec objects take precedence
over earlier ones.
To add support for another 8-bit encoding to Qt, make a subclass
or QTextCodec and implement at least the following methods:
<dl>
<dt>\c const char* name() const
<dd>Return the official name for the encoding.
<dt>\c int mibEnum() const
<dd>Return the MIB enum for the encoding if it is listed in the
<a href=ftp://ftp.isi.edu/in-notes/iana/assignments/character-sets>
IANA character-sets encoding file</a>.
</dl>
If the encoding is multi-byte then it will have "state"; that is,
the interpretation of some bytes will be dependent on some preceding
bytes. For such an encoding, you will need to implement
<dl>
<dt> \c QTextDecoder* makeDecoder() const
<dd>Return a QTextDecoder that remembers incomplete multibyte
sequence prefixes or other required state.
</dl>
If the encoding does \e not require state, you should implement:
<dl>
<dt> \c QString toUnicode(const char* chars, int len) const
<dd>Converts \e len characters from \e chars to Unicode.
</dl>
The base QTextCodec class has default implementations of the above
two functions, <i>but they are mutually recursive</i>, so you must
re-implement at least one of them, or both for improved efficiency.
For conversion from Unicode to 8-bit encodings, it is rarely necessary
to maintain state. However, two functions similar to the two above
are used for encoding:
<dl>
<dt> \c QTextEncoder* makeEncoder() const
<dd>Return a QTextDecoder.
<dt> \c QCString fromUnicode(const QString& uc, int& lenInOut ) const;
<dd>Converts \e lenInOut characters (of type QChar) from the start
of the string \a uc, returning a QCString result, and also returning
the \link QCString::length() length\endlink
of the result in lenInOut.
</dl>
Again, these are mutually recursive so only one needs to be implemented,
or both if better efficiency is possible.
Finally, you must implement:
<dl>
<dt> \c int heuristicContentMatch(const char* chars, int len) const
<dd>Gives a value indicating how likely it is that \e len characters
from \e chars are in the encoding.
</dl>
A good model for this function is the
QWindowsLocalCodec::heuristicContentMatch function found in the Qt sources.
A QTextCodec subclass might have improved performance if you also
re-implement:
<dl>
<dt> \c bool canEncode( QChar ) const
<dd>Test if a Unicode character can be encoded.
<dt> \c bool canEncode( const QString& ) const
<dd>Test if a string of Unicode characters can be encoded.
<dt> \c int heuristicNameMatch(const char* hint) const
<dd>Test if a possibly non-standard name is referring to the codec.
</dl>
*/
/*!
Constructs a QTextCodec, making it of highest precedence.
The QTextCodec should always be constructed on the heap
(with new), and once constructed it becomes the responsibility
of Qt to delete it (which is done at QApplication destruction).
*/
QTextCodec::QTextCodec()
{
setup();
all->insert(0,this);
}
/*!
Destructs the QTextCodec. Note that you should not delete
codecs yourself - once created they become the responsibility
of Qt to delete.
*/
QTextCodec::~QTextCodec()
{
if ( !destroying_is_ok )
qWarning("QTextCodec::~QTextCodec() called by application");
if ( all )
all->remove( this );
}
/*!
Returns a value indicating how likely this decoder is
for decoding some format that has the given name.
A good match returns a positive number around
the length of the string. A bad match is negative.
The default implementation calls simpleHeuristicNameMatch()
with the name of the codec.
*/
int QTextCodec::heuristicNameMatch(const char* hint) const
{
return simpleHeuristicNameMatch(name(),hint);
}
// returns a string cotnaining the letters and numbers from input,
// with a space separating run of a character class. e.g. "iso8859-1"
// becomes "iso 8859 1"
static QString lettersAndNumbers( const char * input )
{
QString result;
QChar c;
while( input && *input ) {
c = *input;
if ( c.isLetter() || c.isNumber() )
result += c.lower();
if ( input[1] ) {
// add space at character class transition, except
// transition from upper-case to lower-case letter
QChar n( input[1] );
if ( c.isLetter() && n.isLetter() ) {
if ( c == c.lower() && n == n.upper() )
result += ' ';
} else if ( c.category() != n.category() ) {
result += ' ';
}
}
input++;
}
return result.simplifyWhiteSpace();
}
/*!
A simple utility function for heuristicNameMatch() - it
does some very minor character-skipping
so that almost-exact matches score high.
*/
int QTextCodec::simpleHeuristicNameMatch(const char* name, const char* hint)
{
// if they're the same, return a perfect score.
if ( name && hint && qstrcmp( name, hint ) == 0 )
return qstrlen( hint );
// if the letters and numbers are the same, we have an "almost"
// perfect match.
QString h( lettersAndNumbers( hint ) );
QString n( lettersAndNumbers( name ) );
if ( h == n )
return qstrlen( hint )-1;
if ( h.stripWhiteSpace() == n.stripWhiteSpace() )
return qstrlen( hint )-2;
// could do some more here, but I don't think it's worth it
return 0;
}
/*!
Returns the QTextCodec \a i places from the more recently
inserted, or NULL if there is no such QTextCodec. Thus,
codecForIndex(0) returns the most recently created QTextCodec.
*/
QTextCodec* QTextCodec::codecForIndex(int i)
{
setup();
return (uint)i >= all->count() ? 0 : all->at(i);
}
/*!
Returns the QTextCodec which matches the
\link QTextCodec::mibEnum() MIBenum\endlink \a mib.
*/
QTextCodec* QTextCodec::codecForMib(int mib)
{
setup();
QInternalListIterator<QTextCodec> i(*all);
QTextCodec* result;
for ( ; (result=i); ++i ) {
if ( result->mibEnum()==mib )
break;
}
return result;
}
#ifdef _OS_WIN32_
class QWindowsLocalCodec: public QTextCodec
{
public:
QWindowsLocalCodec();
~QWindowsLocalCodec();
QString toUnicode(const char* chars, int len) const;
QCString fromUnicode(const QString& uc, int& lenInOut ) const;
const char* name() const;
int mibEnum() const;
int heuristicContentMatch(const char* chars, int len) const;
};
QWindowsLocalCodec::QWindowsLocalCodec()
{
}
QWindowsLocalCodec::~QWindowsLocalCodec()
{
}
QString QWindowsLocalCodec::toUnicode(const char* chars, int len) const
{
if ( len == 1 && chars ) { // Optimization; avoids allocation
char c[2];
c[0] = *chars;
c[1] = 0;
return qt_winMB2QString( c, 2 );
}
if ( len < 0 )
return qt_winMB2QString( chars );
QCString s(chars,len+1);
return qt_winMB2QString(s);
}
QCString QWindowsLocalCodec::fromUnicode(const QString& uc, int& lenInOut ) const
{
QCString r = qt_winQString2MB( uc, lenInOut );
lenInOut = r.length();
return r;
}
const char* QWindowsLocalCodec::name() const
{
return "System";
}
int QWindowsLocalCodec::mibEnum() const
{
return 0;
}
int QWindowsLocalCodec::heuristicContentMatch(const char* chars, int len) const
{
// ### Not a bad default implementation?
QString t = toUnicode(chars,len);
int l = t.length();
QCString mb = fromUnicode(t,l);
int i=0;
while ( i < len )
if ( chars[i] == mb[i] )
i++;
return i;
}
#else
/* locale names mostly copied from XFree86 */
static const char * const iso8859_2locales[] = {
"croatian", "cs", "cs_CS", "cs_CZ","cz", "cz_CZ", "czech", "hr",
"hr_HR", "hu", "hu_HU", "hungarian", "pl", "pl_PL", "polish", "ro",
"ro_RO", "rumanian", "serbocroatian", "sh", "sh_SP", "sh_YU", "sk",
"sk_SK", "sl", "sl_CS", "sl_SI", "slovak", "slovene", "sr_SP", 0 };
static const char * const iso8859_3locales[] = {
"eo", 0 };
static const char * const iso8859_4locales[] = {
"ee", "ee_EE", "lt", "lt_LT", "lv", "lv_LV", 0 };
static const char * const iso8859_5locales[] = {
"bg", "bg_BG", "bulgarian", "mk", "mk_MK",
"sp", "sp_YU", 0 };
static const char * const iso8859_6locales[] = {
"ar_AA", "ar_SA", "arabic", 0 };
static const char * const iso8859_7locales[] = {
"el", "el_GR", "greek", 0 };
static const char * const iso8859_8locales[] = {
"hebrew", "he", "he_IL", "iw", "iw_IL", 0 };
static const char * const iso8859_9locales[] = {
"tr", "tr_TR", "turkish", 0 };
static const char * const iso8859_15locales[] = {
"fr", "fi", "french", "finnish", "et", "et_EE", 0 };
static const char * const koi8_ulocales[] = {
"uk", "uk_UA", "ru_UA", "ukrainian", 0 };
static const char * const tis_620locales[] = {
"th", "th_TH", "thai", 0 };
static bool try_locale_list( const char * const locale[], const char * lang )
{
int i;
for( i=0; locale[i] && qstrcmp(locale[i], lang); i++ )
{ }
return locale[i] != 0;
}
// For the probably_koi8_locales we have to look. the standard says
// these are 8859-5, but almost all Russian users uses KOI8-R and
// incorrectly set $LANG to ru_RU. We'll check tolower() to see what
// tolower() thinks ru_RU means.
// If you read the history, it seems that many Russians blame ISO and
// Peristroika for the confusion.
//
// The real bug is that some programs break if the user specifies
// ru_RU.KOI8-R.
static const char * const probably_koi8_rlocales[] = {
"ru", "ru_SU", "ru_RU", "russian", 0 };
// this means ANY of these locale aliases. if they're aliases for
// different locales, the code breaks.
static QTextCodec * ru_RU_codec = 0;
static QTextCodec * ru_RU_hack( const char * i ) {
if ( ! ru_RU_codec ) {
QCString origlocale = setlocale( LC_CTYPE, i );
// unicode koi8r latin5 name
// 0x044E 0xC0 0xEE CYRILLIC SMALL LETTER YU
// 0x042E 0xE0 0xCE CYRILLIC CAPITAL LETTER YU
int latin5 = tolower( 0xCE );
int koi8r = tolower( 0xE0 );
if ( koi8r == 0xC0 && latin5 != 0xEE ) {
ru_RU_codec = QTextCodec::codecForName( "KOI8-R" );
} else if ( koi8r != 0xC0 && latin5 == 0xEE ) {
ru_RU_codec = QTextCodec::codecForName( "ISO 8859-5" );
} else {
// something else again... let's assume... *throws dice*
ru_RU_codec = QTextCodec::codecForName( "KOI8-R" );
qWarning( "QTextCodec: using KOI8-R, probe failed (%02x %02x %s)",
koi8r, latin5, i );
}
setlocale( LC_CTYPE, origlocale.data() );
}
return ru_RU_codec;
}
#endif
static QTextCodec * localeMapper = 0;
void qt_set_locale_codec( QTextCodec *codec )
{
localeMapper = codec;
}
/*! Returns a pointer to the codec most suitable for this locale. */
QTextCodec* QTextCodec::codecForLocale()
{
if ( localeMapper )
return localeMapper;
setup();
#ifdef _OS_WIN32_
localeMapper = new QWindowsLocalCodec;
#else
// Very poorly defined and followed standards causes lots of code
// to try to get all the cases...
char * lang = qstrdup( getenv("LANG") );
char * p = lang ? strchr( lang, '.' ) : 0;
if ( !p || *p != '.' ) {
// Some versions of setlocale return encoding, others not.
char *ctype = qstrdup( setlocale( LC_CTYPE, 0 ) );
// Some Linux distributions have broken locales which will return
// "C" for LC_CTYPE
if ( qstrcmp( ctype, "C" ) == 0 ) {
delete [] ctype;
} else {
if ( lang )
delete [] lang;
lang = ctype;
p = lang ? strchr( lang, '.' ) : 0;
}
}
if( p && *p == '.' ) {
// if there is an encoding and we don't know it, we return 0
// User knows what they are doing. Codecs will believe them.
localeMapper = codecForName( lang );
if ( !localeMapper ) {
// Use or codec disagree.
localeMapper = codecForName( p+1 );
}
}
if ( !localeMapper || !(p && *p == '.') ) {
// if there is none, we default to 8859-1
// We could perhaps default to 8859-15.
if ( try_locale_list( iso8859_2locales, lang ) )
localeMapper = codecForName( "ISO 8859-2" );
else if ( try_locale_list( iso8859_3locales, lang ) )
localeMapper = codecForName( "ISO 8859-3" );
else if ( try_locale_list( iso8859_4locales, lang ) )
localeMapper = codecForName( "ISO 8859-4" );
else if ( try_locale_list( iso8859_5locales, lang ) )
localeMapper = codecForName( "ISO 8859-5" );
else if ( try_locale_list( iso8859_6locales, lang ) )
localeMapper = codecForName( "ISO 8859-6-I" );
else if ( try_locale_list( iso8859_7locales, lang ) )
localeMapper = codecForName( "ISO 8859-7" );
else if ( try_locale_list( iso8859_8locales, lang ) )
localeMapper = codecForName( "ISO 8859-8-I" );
else if ( try_locale_list( iso8859_9locales, lang ) )
localeMapper = codecForName( "ISO 8859-9" );
else if ( try_locale_list( iso8859_15locales, lang ) )
localeMapper = codecForName( "ISO 8859-15" );
else if ( try_locale_list( tis_620locales, lang ) )
localeMapper = codecForName( "ISO 8859-11" );
else if ( try_locale_list( koi8_ulocales, lang ) )
localeMapper = codecForName( "KOI8-U" );
else if ( try_locale_list( probably_koi8_rlocales, lang ) )
localeMapper = ru_RU_hack( lang );
else if (!lang || !(localeMapper = codecForName(lang) ))
localeMapper = codecForName( "ISO 8859-1" );
}
delete[] lang;
#endif
return localeMapper;
}
/*!
Searches all installed QTextCodec objects, returning the one
which best matches given name. Returns NULL if no codec has
a match closeness above \a accuracy.
\sa heuristicNameMatch()
*/
QTextCodec* QTextCodec::codecForName(const char* hint, int accuracy)
{
setup();
QInternalListIterator<QTextCodec> i(*all);
QTextCodec* result = 0;
int best=accuracy;
for ( QTextCodec* cursor; (cursor=i); ++i ) {
int s = cursor->heuristicNameMatch(hint);
if ( s > best ) {
best = s;
result = cursor;
}
}
return result;
}
/*!
Searches all installed QTextCodec objects, returning the one
which most recognizes the given content. May return 0.
Note that this is often a poor choice, since character
encodings often use most of the available character sequences,
and so only by linguistic analysis could a true match be made.
\sa heuristicContentMatch()
*/
QTextCodec* QTextCodec::codecForContent(const char* chars, int len)
{
setup();
QInternalListIterator<QTextCodec> i(*all);
QTextCodec* result = 0;
int best=0;
for ( QTextCodec* cursor; (cursor=i); ++i ) {
int s = cursor->heuristicContentMatch(chars,len);
if ( s > best ) {
best = s;
result = cursor;
}
}
return result;
}
/*!
\fn const char* QTextCodec::name() const
Subclasses of QTextCodec must reimplement this function. It returns
the name of the encoding supported by the subclass. When choosing
a name for an encoding, consider these points:
<ul>
<li>On X11, heuristicNameMatch( const char * hint )
is used to test if a the QTextCodec
can convert between Unicode and the encoding of a font
with encoding \e hint, such as "iso8859-1" for Latin-1 fonts,
"koi8-r" for Russian KOI8 fonts.
The default algorithm of heuristicNameMatch() uses name().
<li>Some applications may use this function to present
encodings to the end user.
</ul>
*/
/*!
\fn int QTextCodec::mibEnum() const
Subclasses of QTextCodec must reimplement this function. It returns the
MIBenum (see
<a href="ftp://ftp.isi.edu/in-notes/iana/assignments/character-sets">
the IANA character-sets encoding file</a> for more information).
It is important that each QTextCodec subclass return the correct unique
value for this function.
*/
/*!
\fn int QTextCodec::heuristicContentMatch(const char* chars, int len) const
Subclasses of QTextCodec must reimplement this function. It examines
the first \a len bytes of \a chars and returns a value indicating how
likely it is that the string is a prefix of text encoded in the
encoding of the subclass. Any negative return value indicates that the text
is detectably not in the encoding (eg. it contains undefined characters).
A return value of 0 indicates that the text should be decoded with this
codec rather than as ASCII, but there
is no particular evidence. The value should range up to \a len. Thus,
most decoders will return -1, 0, or -\a len.
The characters are not null terminated.
\sa codecForContent().
*/
/*!
Creates a QTextDecoder which stores enough state to decode chunks
of char* data to create chunks of Unicode data. The default implementation
creates a stateless decoder, which is sufficient for only the simplest
encodings where each byte corresponds to exactly one Unicode character.
The caller is responsible for deleting the returned object.
*/
QTextDecoder* QTextCodec::makeDecoder() const
{
return new QTextStatelessDecoder(this);
}
/*!
Creates a QTextEncoder which stores enough state to encode chunks
of Unicode data as char* data. The default implementation
creates a stateless encoder, which is sufficient for only the simplest
encodings where each Unicode character corresponds to exactly one char.
The caller is responsible for deleting the returned object.
*/
QTextEncoder* QTextCodec::makeEncoder() const
{
return new QTextStatelessEncoder(this);
}
/*!
Subclasses of QTextCodec must reimplement this function or
makeDecoder(). It converts the first \a len characters of \a chars
to Unicode.
The default implementation makes a decoder with makeDecoder() and
converts the input with that. Note that the default makeDecoder()
implementation makes a decoder that simply calls
this function, hence subclasses \e must reimplement one function or
the other to avoid infinite recursion.
*/
QString QTextCodec::toUnicode(const char* chars, int len) const
{
QTextDecoder* i = makeDecoder();
QString result = i->toUnicode(chars,len);
delete i;
return result;
}
/*!
Subclasses of QTextCodec must reimplement either this function or
makeEncoder(). It converts the first \a lenInOut characters of \a
uc from Unicode to the encoding of the subclass. If \a lenInOut
is negative or too large, the length of \a uc is used instead.
The value returned is the property of the caller, which is
responsible for deleting it with "delete []". The length of the
resulting Unicode character sequence is returned in \a lenInOut.
The default implementation makes an encoder with makeEncoder() and
converts the input with that. Note that the default makeEncoder()
implementation makes an encoder that simply calls
this function, hence subclasses \e must reimplement one function or
the other to avoid infinite recursion.
*/
QCString QTextCodec::fromUnicode(const QString& uc, int& lenInOut) const
{
QTextEncoder* i = makeEncoder();
QCString result = i->fromUnicode(uc, lenInOut);
delete i;
return result;
}
/*!
\overload QCString QTextCodec::fromUnicode(const QString& uc) const
*/
QCString QTextCodec::fromUnicode(const QString& uc) const
{
int l = uc.length();
return fromUnicode(uc,l);
}
/*!
\overload QString QTextCodec::toUnicode(const QByteArray& a, int len) const
*/
QString QTextCodec::toUnicode(const QByteArray& a, int len) const
{
int l = a.size();
if( l > 0 && a.data()[l - 1] == '\0' ) l--;
l = QMIN( l, len );
return toUnicode( a.data(), l );
}
/*!
\overload QString QTextCodec::toUnicode(const QByteArray& a) const
*/
QString QTextCodec::toUnicode(const QByteArray& a) const
{
int l = a.size();
if( l > 0 && a.data()[l - 1] == '\0' ) l--;
return toUnicode( a.data(), l );
}
/*!
\overload QString QTextCodec::toUnicode(const char* chars) const
*/
QString QTextCodec::toUnicode(const char* chars) const
{
return toUnicode(chars,qstrlen(chars));
}
/*!
Returns TRUE if the unicode character \a ch can be fully encoded
with this codec. The default implementation tests if the result of
toUnicode(fromUnicode(ch)) is the original \a ch. Subclasses may be
able to improve the efficiency.
*/
bool QTextCodec::canEncode( QChar ch ) const
{
return toUnicode(fromUnicode(ch)) == ch;
}
/*!
Returns TRUE if the unicode string \a s can be fully encoded
with this codec. The default implementation tests if the result of
toUnicode(fromUnicode(s)) is the original \a s. Subclasses may be
able to improve the efficiency.
*/
bool QTextCodec::canEncode( const QString& s ) const
{
return toUnicode(fromUnicode(s)) == s;
}
/*!
\class QTextEncoder qtextcodec.h
\brief State-based encoder
A QTextEncoder converts Unicode into another format, remembering
any state that is required between calls.
\sa QTextCodec::makeEncoder()
*/
/*!
Destructs the encoder.
*/
QTextEncoder::~QTextEncoder()
{
}
/*!
\fn QCString QTextEncoder::fromUnicode(const QString& uc, int& lenInOut)
Converts \a lenInOut characters (not bytes) from \a uc, producing
a QCString. \a lenInOut will also be set to the
\link QCString::length() length\endlink of the result (in bytes).
The encoder is free to record state to use when subsequent calls are
made to this function (for example, it might change modes with escape
sequences if needed during the encoding of one string, then assume that
mode applies when a subsequent call begins).
*/
/*!
\class QTextDecoder qtextcodec.h
\brief State-based decoder
A QTextEncoder converts a text format into Unicode, remembering
any state that is required between calls.
\sa QTextCodec::makeEncoder()
*/
/*!
Destructs the decoder.
*/
QTextDecoder::~QTextDecoder()
{
}
/*!
\fn QString QTextDecoder::toUnicode(const char* chars, int len)
Converts the first \a len bytes at \a chars to Unicode, returning the
result.
If not all characters are used (eg. only part of a multi-byte
encoding is at the end of the characters), the decoder remembers
enough state to continue with the next call to this function.
*/
#define CHAINED 0xffff
struct QMultiByteUnicodeTable {
// If multibyte, ignore unicode and index into multibyte
// with the next character.
QMultiByteUnicodeTable() : unicode(0xfffd), multibyte(0) { }
~QMultiByteUnicodeTable()
{
if ( multibyte )
delete [] multibyte;
}
ushort unicode;
QMultiByteUnicodeTable* multibyte;
};
#ifndef QT_NO_CODECS
static int getByte(char* &cursor)
{
int byte = 0;
if ( *cursor ) {
if ( cursor[1] == 'x' )
byte = (int)strtol(cursor+2,&cursor,16);
else if ( cursor[1] == 'd' )
byte = (int)strtol(cursor+2,&cursor,10);
else
byte = (int)strtol(cursor+2,&cursor,8);
}
return byte&0xff;
}
class QTextCodecFromIOD;
class QTextCodecFromIODDecoder : public QTextDecoder {
const QTextCodecFromIOD* codec;
QMultiByteUnicodeTable* mb;
public:
QTextCodecFromIODDecoder(const QTextCodecFromIOD* c);
QString toUnicode(const char* chars, int len);
};
class QTextCodecFromIOD : public QTextCodec {
friend class QTextCodecFromIODDecoder;
QCString n;
// If from_unicode_page[row()][cell()] is 0 and from_unicode_page_multibyte,
// use from_unicode_page_multibyte[row()][cell()] as string.
char** from_unicode_page;
char*** from_unicode_page_multibyte;
char unkn;
// Only one of these is used
ushort* to_unicode;
QMultiByteUnicodeTable* to_unicode_multibyte;
int max_bytes_per_char;
QStrList aliases;
bool stateless() const { return !to_unicode_multibyte; }
public:
QTextCodecFromIOD(QIODevice* iod)
{
from_unicode_page = 0;
to_unicode_multibyte = 0;
to_unicode = 0;
from_unicode_page_multibyte = 0;
max_bytes_per_char = 1;
const int maxlen=100;
char line[maxlen];
char esc='\\';
char comm='%';
bool incmap = FALSE;
while (iod->readLine(line,maxlen) > 0) {
if (0==qstrnicmp(line,"<code_set_name>",15))
n = line+15;
else if (0==qstrnicmp(line,"<escape_char> ",14))
esc = line[14];
else if (0==qstrnicmp(line,"<comment_char> ",15))
comm = line[15];
else if (line[0]==comm && 0==qstrnicmp(line+1," alias ",7)) {
aliases.append(line+8);
} else if (0==qstrnicmp(line,"CHARMAP",7)) {
if (!from_unicode_page) {
from_unicode_page = new char*[256];
for (int i=0; i<256; i++)
from_unicode_page[i]=0;
}