00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kcharsets.h"
00021
00022 #include "kentities.c"
00023
00024 #include <kapplication.h>
00025 #include <kglobal.h>
00026 #include <klocale.h>
00027 #include <kconfig.h>
00028
00029 #include <qfontinfo.h>
00030 #include <qstrlist.h>
00031 #include <qfontdatabase.h>
00032 #include <kdebug.h>
00033
00034 #include <qtextcodec.h>
00035 #include <qmap.h>
00036 #include <qcstring.h>
00037
00038 #include <assert.h>
00039
00040 #define CHARSETS_COUNT 33
00041
00042 static const char * const language_names[] = {
00043 I18N_NOOP( "Other" ),
00044 I18N_NOOP( "Arabic" ),
00045 I18N_NOOP( "Baltic" ),
00046 I18N_NOOP( "Central European" ),
00047 I18N_NOOP( "Chinese Simplified" ),
00048 I18N_NOOP( "Chinese Traditional" ),
00049 I18N_NOOP( "Cyrillic" ),
00050 I18N_NOOP( "Greek" ),
00051 I18N_NOOP( "Hebrew" ),
00052 I18N_NOOP( "Japanese" ),
00053 I18N_NOOP( "Korean" ),
00054 I18N_NOOP( "Thai" ),
00055 I18N_NOOP( "Turkish" ),
00056 I18N_NOOP( "Western European" ),
00057 I18N_NOOP( "Tamil" ),
00058 I18N_NOOP( "Unicode" ),
00059 I18N_NOOP( "Northern Saami" )
00060 };
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 static const char* const charsets_for_encoding[] = {
00071 "koi8-r", "koi8-r","cp 1251","koi8-u","iso-8859-5", 0,
00072 "koi8-u", "koi8-u","cp 1251","iso-8859-5","koi8-r", 0,
00073 "iso 8859-1", "iso8859-1","iso8859-15", 0,
00074 "iso 8859-2", "iso8859-2","unicode","iso8859-1", 0,
00075 "iso 8859-3", "iso8859-3","unicode","iso8859-1", 0,
00076 "iso 8859-4", "iso8859-4","unicode","iso8859-13", "iso8859-1", 0,
00077 "iso 8859-5", "iso8859-5","koi8-u","koi8-r", 0,
00078 "iso 8859-6", "unicode","iso8859-6", 0,
00079 "iso 8859-7", "iso8859-7", 0,
00080 "iso 8859-8", "iso8859-8", 0,
00081 "iso 8859-8-i", "iso8859-8", 0,
00082 "iso 8859-9", "iso8859-9","unicode","iso8859-1", 0,
00083 "iso 8859-11", "iso8859-11", 0,
00084 "iso 8859-13", "iso8859-13","unicode","iso8859-4", "iso8859-1", 0,
00085 "iso 8859-15", "iso8859-15","unicode","iso8859-1", 0,
00086 "utf8", "unicode","iso8859-1", 0,
00087 "utf16", "unicode","iso8859-1", 0,
00088 "iso-10646-ucs-2", "unicode","iso8859-1", 0,
00089 "cp 1250", "iso8859-2", 0,
00090 "cp 1251", "cp 1251","koi8-u","koi8-r","iso8859-5", 0,
00091 "cp 1252", "iso8859-1", 0,
00092 "cp 1253", "iso8859-7", 0,
00093 "cp 1254", "iso8859-9", 0,
00094 "cp 1255", "iso8859-8", 0,
00095 "cp 1256", "unicode","iso8859-6", 0,
00096 "cp 1257", "iso8859-13", "iso8859-4", 0,
00097 "ibm850", "ibm850","unicode","iso8859-1", 0,
00098 "ibm852", "unicode","iso-8859-2", 0,
00099 "ibm866", "ibm866","cp 1251","koi8-u","koi8-r","iso8859-5", 0,
00100 "tis620", "iso8859-11", 0,
00101 "eucjp", "eucjp","unicode","iso8859-1", 0,
00102 "sjis", "eucjp","unicode","iso8859-1", 0,
00103 "jis7", "eucjp","unicode","iso8859-1", 0,
00104 "big5", "big5","unicode","iso8859-1", 0,
00105 "gbk", "gb2312.1980-0","gbk-0","unicode","iso8859-1", 0,
00106 "gb18030", "gb18030.2000-1", "gb18030.2000-0", "unicode", "gbk-0", "gb2313.1980-0", "iso8859-1", 0,
00107 "gb2312", "gb2312.1980-0","unicode","iso8859-1", 0,
00108 "euckr", "euckr","unicode","iso8859-1", 0,
00109 "tscii", "tscii", 0,
00110 "pt 154", "pt 154","cp 1251","koi8-u","koi8-r","iso8859-5", 0,
00111 "winsami2", "winsami2", "cp1252", "unicode", 0,
00112 0 };
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 static struct LanguageForEncoding
00132 {
00133 const char* index;
00134 int data;
00135 } const language_for_encoding[] = {
00136 { "iso 8859-1", 13 },
00137 { "iso 8859-15", 13 },
00138 { "cp 1252", 13 },
00139 { "ibm850", 13 },
00140 { "iso 8859-2", 3 },
00141 { "iso 8859-3", 3 },
00142 { "iso 8859-4", 2 },
00143 { "iso 8859-13", 2 },
00144 { "cp 1250", 3 },
00145 { "cp 1254", 12 },
00146 { "cp 1257", 2 },
00147 { "ibm852", 3 },
00148 { "koi8-r", 6 },
00149 { "iso 8859-5", 6 },
00150 { "cp 1251", 6 },
00151 { "koi8-u", 6 },
00152 { "pt 154", 6 },
00153 { "ibm866", 6 },
00154 { "big5", 5 },
00155 { "gb18030", 4 },
00156 { "gbk", 4 },
00157 { "gb2312", 4 },
00158 { "euckr", 10 },
00159 { "sjis", 9 },
00160 { "jis7", 9 },
00161 { "eucjp", 9 },
00162 { "iso 8859-7", 7 },
00163 { "cp 1253", 7 },
00164 { "iso 8859-6", 1 },
00165 { "cp 1256", 1 },
00166 { "iso 8859-8", 8 },
00167 { "iso 8859-8-i", 8 },
00168 { "cp 1255", 8 },
00169 { "iso 8859-9", 12 },
00170 { "tis620", 11 },
00171 { "iso 8859-11", 11 },
00172 { "utf8", 15 },
00173 { "utf16", 15 },
00174 { "utf7", 15 },
00175 { "ucs2", 15 },
00176 { "iso-10646-ucs-2", 15 },
00177 { "winsami2", 16},
00178 { 0, 0 } };
00179
00180
00181 static struct Builtin
00182 {
00183 const char* index;
00184 const char* data;
00185 } const builtin[] = {
00186 { "iso-ir-111", "koi8-r" },
00187 { "koi8-ru", "koi8-u" },
00188 { "koi8r", "koi8-r" },
00189 { "koi8u", "koi8-u" },
00190 { "koi unified", "koi8-r" },
00191 { "us-ascii", "iso 8859-1" },
00192 { "usascii", "iso 8859-1" },
00193 { "x-utf-8", "utf-8" },
00194 { "x-utf-7", "utf-7" },
00195 { "unicode-1-1-utf-7", "utf-7" },
00196 { "ucs2", "iso-10646-ucs-2" },
00197 { "iso10646-1", "iso-10646-ucs-2" },
00198 { "gb18030.2000-1", "gb18030" },
00199 { "gb18030.2000-0", "gb18030" },
00200 { "gbk-0", "gbk" },
00201 { "gb2312", "gbk" },
00202 { "gb2312.1980-0", "gbk" },
00203 { "big5-0", "big5" },
00204 { "euc-kr", "euckr" },
00205 { "x-euc-kr", "euckr" },
00206 { "euc-jp", "eucjp" },
00207 { "x-euc-jp", "eucjp" },
00208 { "jisx0201.1976-0", "eucjp" },
00209 { "jisx0208.1983-0", "eucjp" },
00210 { "jisx0208.1990-0", "eucjp" },
00211 { "jisx0208.1997-0", "eucjp" },
00212 { "jisx0212.1990-0", "eucjp" },
00213 { "jisx0213.2000-1", "eucjp" },
00214 { "jisx0213.2000-2", "eucjp" },
00215 { "shift_jis", "sjis" },
00216 { "shift-jis", "sjis" },
00217 { "x-sjis", "sjis" },
00218 { "iso-2022-jp", "jis7" },
00219 { "windows850", "ibm850" },
00220 { "windows866", "ibm866" },
00221 { "windows1251", "cp 1251" },
00222 { "windows1252", "cp 1252" },
00223 { "windows1253", "cp 1253" },
00224 { "windows1254", "cp 1254" },
00225 { "windows1255", "cp 1255" },
00226 { "windows1256", "cp 1256" },
00227 { "windows1257", "cp 1257" },
00228 { "windows-850", "ibm850" },
00229 { "windows-866", "ibm866" },
00230 { "windows-1250", "cp 1250" },
00231 { "windows-1251", "cp 1251" },
00232 { "windows-1252", "cp 1252" },
00233 { "windows-1253", "cp 1253" },
00234 { "windows-1254", "cp 1254" },
00235 { "windows-1255", "cp 1255" },
00236 { "windows-1256", "cp 1256" },
00237 { "windows-1257", "cp 1257" },
00238 { "x-windows-850", "ibm850" },
00239 { "x-windows-866", "ibm866" },
00240 { "x-windows-1250", "cp 1250" },
00241 { "x-windows-1251", "cp 1251" },
00242 { "x-windows-1252", "cp 1252" },
00243 { "x-windows-1253", "cp 1253" },
00244 { "x-windows-1254", "cp 1254" },
00245 { "x-windows-1255", "cp 1255" },
00246 { "x-windows-1256", "cp 1256" },
00247 { "x-windows-1257", "cp 1257" },
00248 { "cp850", "ibm850" },
00249 { "cp866", "ibm866" },
00250 { "cp-850", "ibm850" },
00251 { "cp-866", "ibm866" },
00252 { "cp-1250", "cp 1250" },
00253 { "cp-1251", "cp 1251" },
00254 { "cp-1252", "cp 1252" },
00255 { "cp-1253", "cp 1253" },
00256 { "cp-1254", "cp 1254" },
00257 { "cp-1255", "cp 1255" },
00258 { "cp-1256", "cp 1256" },
00259 { "cp-1257", "cp 1257" },
00260 { "x-cp-850", "ibm850" },
00261 { "x-cp-866", "ibm866" },
00262 { "x-cp-1250", "cp 1250" },
00263 { "x-cp-1251", "cp 1251" },
00264 { "x-cp-1252", "cp 1252" },
00265 { "x-cp-1253", "cp 1253" },
00266 { "x-cp-1254", "cp 1254" },
00267 { "x-cp-1255", "cp 1255" },
00268 { "x-cp-1256", "cp 1256" },
00269 { "x-cp-1257", "cp 1257" },
00270 { "tis620", "iso 8859-11" },
00271 { "tis-620", "iso 8859-11" },
00272 { "thai-tis620", "iso 8859-11" },
00273 { "windows-874", "iso 8859-11" },
00274 { "windows874", "iso 8859-11" },
00275 { "x-windows-874", "iso 8859-11" },
00276 { "cp874", "iso 8859-11" },
00277 { "cp-874", "iso 8859-11" },
00278 { "x-cp-874", "iso 8859-11" },
00279 { "ksc5601.1987-0", "euckr" },
00280 { "ks_c_5601-1987", "euckr" },
00281 { "iso-8859-1", "iso 8859-1" },
00282 { "iso-8859-2", "iso 8859-2" },
00283 { "iso-8859-3", "iso 8859-3" },
00284 { "iso-8859-4", "iso 8859-4" },
00285 { "iso-8859-5", "iso 8859-5" },
00286 { "iso-8859-6", "iso 8859-6" },
00287 { "iso-8859-7", "iso 8859-7" },
00288 { "iso-8859-8", "iso 8859-8" },
00289 { "iso-8859-9", "iso 8859-9" },
00290 { "iso-8859-10", "iso 8859-10" },
00291 { "iso-8859-11", "iso 8859-11" },
00292 { "iso-8859-12", "iso 8859-12" },
00293 { "iso-8859-13", "iso 8859-13" },
00294 { "iso-8859-14", "iso 8859-14" },
00295 { "iso-8859-15", "iso 8859-15" },
00296 { "tscii", "tscii" },
00297 { "paratype-154", "pt 154" },
00298 { "pt-154", "pt 154" },
00299 { "x-winsami2", "winsami2" },
00300 { "x-mac-roman", "apple roman" },
00301 { "macintosh", "apple roman" },
00302 { "mac", "apple roman" },
00303 { 0, 0 }};
00304
00305
00306
00307 static struct Aliases
00308 {
00309 const char* index;
00310 const char* data;
00311 } const aliases[] = {
00312 { "cp852", "ibm852" },
00313 { "cp-852", "ibm852" },
00314 { "x-cp-852", "ibm852" },
00315 { "windows852", "ibm852" },
00316 { "windows-852", "ibm852" },
00317 { "x-windows-852", "ibm852" },
00318 { 0, 0 }};
00319
00320
00321
00322
00323 static struct ConversionHints
00324 {
00325 const char* index;
00326 const char* data;
00327 } const conversion_hints[] = {
00328 { "cp1250", "iso-8859-2" },
00329 { "koi8-r", "iso-8859-5" },
00330 { "koi8-u", "koi8-r" },
00331 { "utf16", "iso-10646-ucs-2" },
00332 { 0, 0 }};
00333
00334
00335
00336
00337 template< typename T, typename Data >
00338 static Data kcharsets_array_search( const T* start, const char* entry )
00339 {
00340 for( const T* pos = start;
00341 pos->index != 0;
00342 ++pos )
00343 if( qstrcmp( pos->index, entry ) == 0 )
00344 return pos->data;
00345 return 0;
00346 }
00347
00348
00349 class KCharsetsPrivate
00350 {
00351 public:
00352 KCharsetsPrivate(KCharsets* _kc)
00353 : codecForNameDict(43, false)
00354 {
00355 db = 0;
00356 kc = _kc;
00357 }
00358 ~KCharsetsPrivate()
00359 {
00360 delete db;
00361 }
00362 QFontDatabase *db;
00363 QAsciiDict<QTextCodec> codecForNameDict;
00364 KCharsets* kc;
00365 };
00366
00367
00368
00369 KCharsets::KCharsets()
00370 {
00371 d = new KCharsetsPrivate(this);
00372 }
00373
00374 KCharsets::~KCharsets()
00375 {
00376 delete d;
00377 }
00378
00379 QChar KCharsets::fromEntity(const QString &str)
00380 {
00381 QChar res = QChar::null;
00382
00383 int pos = 0;
00384 if(str[pos] == '&') pos++;
00385
00386
00387 if (str[pos] == '#' && str.length()-pos > 1) {
00388 bool ok;
00389 pos++;
00390 if (str[pos] == 'x' || str[pos] == 'X') {
00391 pos++;
00392
00393 QString tmp(str.unicode()+pos, str.length()-pos);
00394 res = tmp.toInt(&ok, 16);
00395 } else {
00396
00397 QString tmp(str.unicode()+pos, str.length()-pos);
00398 res = tmp.toInt(&ok, 10);
00399 }
00400 return res;
00401 }
00402
00403 const entity *e = kde_findEntity(str.ascii(), str.length());
00404
00405 if(!e)
00406 {
00407
00408 return QChar::null;
00409 }
00410
00411
00412 return QChar(e->code);
00413 }
00414
00415 QChar KCharsets::fromEntity(const QString &str, int &len)
00416 {
00417
00418
00419 len = 8;
00420 while(len > 0)
00421 {
00422 QString tmp = str.left(len);
00423 QChar res = fromEntity(tmp);
00424 if( res != QChar::null ) return res;
00425 len--;
00426 }
00427 return QChar::null;
00428 }
00429
00430
00431 QString KCharsets::toEntity(const QChar &ch)
00432 {
00433 QString ent;
00434 ent.sprintf("�x%x;", ch.unicode());
00435 return ent;
00436 }
00437
00438 QString KCharsets::resolveEntities( const QString &input )
00439 {
00440 QString text = input;
00441 const QChar *p = text.unicode();
00442 const QChar *end = p + text.length();
00443 const QChar *ampersand = 0;
00444 bool scanForSemicolon = false;
00445
00446 for ( ; p < end; ++p ) {
00447 const QChar ch = *p;
00448
00449 if ( ch == '&' ) {
00450 ampersand = p;
00451 scanForSemicolon = true;
00452 continue;
00453 }
00454
00455 if ( ch != ';' || scanForSemicolon == false )
00456 continue;
00457
00458 assert( ampersand );
00459
00460 scanForSemicolon = false;
00461
00462 const QChar *entityBegin = ampersand + 1;
00463
00464 const uint entityLength = p - entityBegin;
00465 if ( entityLength == 0 )
00466 continue;
00467
00468 const QChar entityValue = KCharsets::fromEntity( QConstString( entityBegin, entityLength ).string() );
00469 if ( entityValue.isNull() )
00470 continue;
00471
00472 const uint ampersandPos = ampersand - text.unicode();
00473
00474 text[ ampersandPos ] = entityValue;
00475 text.remove( ampersandPos + 1, entityLength + 1 );
00476 p = text.unicode() + ampersandPos;
00477 end = text.unicode() + text.length();
00478 ampersand = 0;
00479 }
00480
00481 return text;
00482 }
00483
00484 QStringList KCharsets::availableEncodingNames()
00485 {
00486 QStringList available;
00487
00488 const char* const* pos = charsets_for_encoding;
00489 while( *pos != 0 ) {
00490
00491
00492
00493
00494 for( const char* const* charsets = pos + 1;
00495 *charsets != 0;
00496 ++charsets ) {
00497
00498 #ifdef __GNUC__
00499 #warning FIXME?
00500 #endif
00501 if( true ) {
00502
00503 available.append( QString::fromLatin1( *pos ));
00504 break;
00505 }
00506 }
00507 while( *pos != 0 )
00508 ++pos;
00509 ++pos;
00510 }
00511 return available;
00512 }
00513
00514 QString KCharsets::languageForEncoding( const QString &encoding )
00515 {
00516 int lang = kcharsets_array_search< LanguageForEncoding, int >
00517 ( language_for_encoding, encoding.latin1());
00518 return i18n( language_names[lang] );
00519 }
00520
00521 QString KCharsets::encodingForName( const QString &descriptiveName )
00522 {
00523 const int left = descriptiveName.findRev( '(' );
00524
00525 if (left<0)
00526 return descriptiveName.stripWhiteSpace();
00527
00528 QString name(descriptiveName.mid(left+1));
00529
00530 const int right = name.findRev( ')' );
00531
00532 if (right<0)
00533 return name;
00534
00535 return name.left(right).stripWhiteSpace();
00536 }
00537
00538 QStringList KCharsets::descriptiveEncodingNames()
00539 {
00540 QStringList encodings = availableEncodingNames();
00541 QStringList::Iterator it;
00542 for( it = encodings.begin(); it != encodings.end(); ++it ) {
00543 QString lang = KGlobal::charsets()->languageForEncoding( *it );
00544 *it = i18n("Descriptive Encoding Name", "%1 ( %2 )") .arg(lang) .arg(*it);
00545 }
00546 encodings.sort();
00547 return encodings;
00548 }
00549
00550 QTextCodec *KCharsets::codecForName(const QString &n) const
00551 {
00552 bool b;
00553 return codecForName( n, b );
00554 }
00555
00556 QTextCodec *KCharsets::codecForName(const QString &n, bool &ok) const
00557 {
00558 ok = true;
00559
00560 QTextCodec* codec = 0;
00561
00562 if((codec = d->codecForNameDict[n.isEmpty() ? "->locale<-" : n.latin1()]))
00563 return codec;
00564
00565 if (n.isEmpty()) {
00566 codec = KGlobal::locale()->codecForEncoding();
00567 d->codecForNameDict.replace("->locale<-", codec);
00568 return codec;
00569 }
00570
00571 QCString name = n.lower().latin1();
00572 QCString key = name;
00573 if (name.right(8) == "_charset")
00574 name.truncate(name.length()-8);
00575
00576 if (name.isEmpty()) {
00577 ok = false;
00578 return QTextCodec::codecForName("iso8859-1");
00579 }
00580
00581 codec = QTextCodec::codecForName(name);
00582
00583 if(codec) {
00584 d->codecForNameDict.replace(key, codec);
00585 return codec;
00586 }
00587
00588
00589
00590 QCString cname = kcharsets_array_search< Builtin, const char* >( builtin, name.data());
00591
00592 if(!cname.isEmpty())
00593 codec = QTextCodec::codecForName(cname);
00594
00595 if(codec)
00596 {
00597 d->codecForNameDict.replace(key, codec);
00598 return codec;
00599 }
00600
00601 QString dir;
00602 {
00603 KConfigGroupSaver cfgsav( KGlobal::config(), "i18n" );
00604 dir = KGlobal::config()->readPathEntry("i18ndir", QString::fromLatin1("/usr/share/i18n/charmaps"));
00605 dir += "/";
00606 }
00607
00608
00609
00610 cname = kcharsets_array_search< Aliases, const char* >( aliases, name.data());
00611
00612 if(cname.isEmpty())
00613 cname = name;
00614 cname = cname.upper();
00615
00616 codec = QTextCodec::loadCharmapFile((QString)(dir + cname.data()));
00617
00618 if(codec) {
00619 d->codecForNameDict.replace(key, codec);
00620 return codec;
00621 }
00622
00623
00624
00625 cname = cname.lower();
00626 cname = kcharsets_array_search< ConversionHints, const char* >( conversion_hints, (const char*)cname );
00627
00628 if(!cname.isEmpty())
00629 codec = QTextCodec::codecForName(cname);
00630
00631 if(codec) {
00632 d->codecForNameDict.replace(key, codec);
00633 return codec;
00634 }
00635
00636
00637 ok = false;
00638 return QTextCodec::codecForName("iso8859-1");
00639 }