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 { 0, 0 }};
00301
00302
00303
00304 static struct Aliases
00305 {
00306 const char* index;
00307 const char* data;
00308 } const aliases[] = {
00309 { "cp852", "ibm852" },
00310 { "cp-852", "ibm852" },
00311 { "x-cp-852", "ibm852" },
00312 { "windows852", "ibm852" },
00313 { "windows-852", "ibm852" },
00314 { "x-windows-852", "ibm852" },
00315 { 0, 0 }};
00316
00317
00318
00319
00320 static struct ConversionHints
00321 {
00322 const char* index;
00323 const char* data;
00324 } const conversion_hints[] = {
00325 { "cp1250", "iso-8859-2" },
00326 { "koi8-r", "iso-8859-5" },
00327 { "koi8-u", "koi8-r" },
00328 { "utf16", "iso-10646-ucs-2" },
00329 { 0, 0 }};
00330
00331
00332
00333
00334 template< typename T, typename Data >
00335 static Data kcharsets_array_search( const T* start, const char* entry )
00336 {
00337 for( const T* pos = start;
00338 pos->index != 0;
00339 ++pos )
00340 if( qstrcmp( pos->index, entry ) == 0 )
00341 return pos->data;
00342 return 0;
00343 }
00344
00345
00346 class KCharsetsPrivate
00347 {
00348 public:
00349 KCharsetsPrivate(KCharsets* _kc)
00350 : codecForNameDict(43, false)
00351 {
00352 db = 0;
00353 kc = _kc;
00354 }
00355 ~KCharsetsPrivate()
00356 {
00357 delete db;
00358 }
00359 QFontDatabase *db;
00360 QAsciiDict<QTextCodec> codecForNameDict;
00361 KCharsets* kc;
00362 };
00363
00364
00365
00366 KCharsets::KCharsets()
00367 {
00368 d = new KCharsetsPrivate(this);
00369 }
00370
00371 KCharsets::~KCharsets()
00372 {
00373 delete d;
00374 }
00375
00376 QChar KCharsets::fromEntity(const QString &str)
00377 {
00378 QChar res = QChar::null;
00379
00380 int pos = 0;
00381 if(str[pos] == '&') pos++;
00382
00383
00384 if (str[pos] == '#' && str.length()-pos > 1) {
00385 bool ok;
00386 pos++;
00387 if (str[pos] == 'x' || str[pos] == 'X') {
00388 pos++;
00389
00390 QString tmp(str.unicode()+pos, str.length()-pos);
00391 res = tmp.toInt(&ok, 16);
00392 } else {
00393
00394 QString tmp(str.unicode()+pos, str.length()-pos);
00395 res = tmp.toInt(&ok, 10);
00396 }
00397 return res;
00398 }
00399
00400 const entity *e = kde_findEntity(str.ascii(), str.length());
00401
00402 if(!e)
00403 {
00404
00405 return QChar::null;
00406 }
00407
00408
00409 return QChar(e->code);
00410 }
00411
00412 QChar KCharsets::fromEntity(const QString &str, int &len)
00413 {
00414
00415
00416 len = 8;
00417 while(len > 0)
00418 {
00419 QString tmp = str.left(len);
00420 QChar res = fromEntity(tmp);
00421 if( res != QChar::null ) return res;
00422 len--;
00423 }
00424 return QChar::null;
00425 }
00426
00427
00428 QString KCharsets::toEntity(const QChar &ch)
00429 {
00430 QString ent;
00431 ent.sprintf("�x%x;", ch.unicode());
00432 return ent;
00433 }
00434
00435 QString KCharsets::resolveEntities( const QString &input )
00436 {
00437 QString text = input;
00438 const QChar *p = text.unicode();
00439 const QChar *end = p + text.length();
00440 const QChar *ampersand = 0;
00441 bool scanForSemicolon = false;
00442
00443 for ( ; p < end; ++p ) {
00444 const QChar ch = *p;
00445
00446 if ( ch == '&' ) {
00447 ampersand = p;
00448 scanForSemicolon = true;
00449 continue;
00450 }
00451
00452 if ( ch != ';' || scanForSemicolon == false )
00453 continue;
00454
00455 assert( ampersand );
00456
00457 scanForSemicolon = false;
00458
00459 const QChar *entityBegin = ampersand + 1;
00460
00461 const uint entityLength = p - entityBegin;
00462 if ( entityLength == 0 )
00463 continue;
00464
00465 const QChar entityValue = KCharsets::fromEntity( QConstString( entityBegin, entityLength ).string() );
00466 if ( entityValue.isNull() )
00467 continue;
00468
00469 const uint ampersandPos = ampersand - text.unicode();
00470
00471 text[ ampersandPos ] = entityValue;
00472 text.remove( ampersandPos + 1, entityLength + 1 );
00473 p = text.unicode() + ampersandPos;
00474 end = text.unicode() + text.length();
00475 ampersand = 0;
00476 }
00477
00478 return text;
00479 }
00480
00481 QStringList KCharsets::availableEncodingNames()
00482 {
00483 QStringList available;
00484
00485 const char* const* pos = charsets_for_encoding;
00486 while( *pos != 0 ) {
00487
00488
00489
00490
00491 for( const char* const* charsets = pos + 1;
00492 *charsets != 0;
00493 ++charsets ) {
00494
00495 #ifdef __GNUC__
00496 #warning FIXME?
00497 #endif
00498 if( true ) {
00499
00500 available.append( QString::fromLatin1( *pos ));
00501 break;
00502 }
00503 }
00504 while( *pos != 0 )
00505 ++pos;
00506 ++pos;
00507 }
00508 return available;
00509 }
00510
00511 QString KCharsets::languageForEncoding( const QString &encoding )
00512 {
00513 int lang = kcharsets_array_search< LanguageForEncoding, int >
00514 ( language_for_encoding, encoding.latin1());
00515 return i18n( language_names[lang] );
00516 }
00517
00518 QString KCharsets::encodingForName( const QString &descriptiveName )
00519 {
00520 const int left = descriptiveName.findRev( '(' );
00521
00522 if (left<0)
00523 return descriptiveName.stripWhiteSpace();
00524
00525 QString name(descriptiveName.mid(left+1));
00526
00527 const int right = name.findRev( ')' );
00528
00529 if (right<0)
00530 return name;
00531
00532 return name.left(right).stripWhiteSpace();
00533 }
00534
00535 QStringList KCharsets::descriptiveEncodingNames()
00536 {
00537 QStringList encodings = availableEncodingNames();
00538 QStringList::Iterator it;
00539 for( it = encodings.begin(); it != encodings.end(); ++it ) {
00540 QString lang = KGlobal::charsets()->languageForEncoding( *it );
00541 *it = i18n("Descriptive Encoding Name", "%1 ( %2 )") .arg(lang) .arg(*it);
00542 }
00543 encodings.sort();
00544 return encodings;
00545 }
00546
00547 QTextCodec *KCharsets::codecForName(const QString &n) const
00548 {
00549 bool b;
00550 return codecForName( n, b );
00551 }
00552
00553 QTextCodec *KCharsets::codecForName(const QString &n, bool &ok) const
00554 {
00555 ok = true;
00556
00557 QTextCodec* codec = 0;
00558
00559 if((codec = d->codecForNameDict[n.isEmpty() ? "->locale<-" : n.latin1()]))
00560 return codec;
00561
00562 if (n.isEmpty()) {
00563 codec = KGlobal::locale()->codecForEncoding();
00564 d->codecForNameDict.replace("->locale<-", codec);
00565 return codec;
00566 }
00567
00568 QCString name = n.lower().latin1();
00569 QCString key = name;
00570 if (name.right(8) == "_charset")
00571 name.truncate(name.length()-8);
00572
00573 if (name.isEmpty()) {
00574 ok = false;
00575 return QTextCodec::codecForName("iso8859-1");
00576 }
00577
00578 codec = QTextCodec::codecForName(name);
00579
00580 if(codec) {
00581 d->codecForNameDict.replace(key, codec);
00582 return codec;
00583 }
00584
00585
00586
00587 QCString cname = kcharsets_array_search< Builtin, const char* >( builtin, name.data());
00588
00589 if(!cname.isEmpty())
00590 codec = QTextCodec::codecForName(cname);
00591
00592 if(codec)
00593 {
00594 d->codecForNameDict.replace(key, codec);
00595 return codec;
00596 }
00597
00598 QString dir;
00599 {
00600 KConfigGroupSaver cfgsav( KGlobal::config(), "i18n" );
00601 dir = KGlobal::config()->readPathEntry("i18ndir", QString::fromLatin1("/usr/share/i18n/charmaps"));
00602 dir += "/";
00603 }
00604
00605
00606
00607 cname = kcharsets_array_search< Aliases, const char* >( aliases, name.data());
00608
00609 if(cname.isEmpty())
00610 cname = name;
00611 cname = cname.upper();
00612
00613 codec = QTextCodec::loadCharmapFile((QString)(dir + cname.data()));
00614
00615 if(codec) {
00616 d->codecForNameDict.replace(key, codec);
00617 return codec;
00618 }
00619
00620
00621
00622 cname = cname.lower();
00623 cname = kcharsets_array_search< ConversionHints, const char* >( conversion_hints, (const char*)cname );
00624
00625 if(!cname.isEmpty())
00626 codec = QTextCodec::codecForName(cname);
00627
00628 if(codec) {
00629 d->codecForNameDict.replace(key, codec);
00630 return codec;
00631 }
00632
00633
00634 ok = false;
00635 return QTextCodec::codecForName("iso8859-1");
00636 }