📄 qtextcodec.cpp
字号:
cursor[3]>='0' && cursor[3]<='9') { unicode = strtol(cursor+2,&cursor,16); } else if (*cursor==esc) { byte = getByte(cursor); if ( *cursor == esc ) { if ( !to_unicode_multibyte ) { to_unicode_multibyte = new QMultiByteUnicodeTable[256]; for (int i=0; i<256; i++) { to_unicode_multibyte[i].unicode = to_unicode[i]; to_unicode_multibyte[i].multibyte = 0; } delete [] to_unicode; to_unicode = 0; } QMultiByteUnicodeTable* mbut = to_unicode_multibyte+byte; mb[nmb++] = byte; while ( nmb < maxmb && *cursor == esc ) { // Always at least once mbut->unicode = CHAINED; byte = getByte(cursor); mb[nmb++] = byte; if (!mbut->multibyte) { mbut->multibyte = new QMultiByteUnicodeTable[256]; } mbut = mbut->multibyte+byte; mb_unicode = & mbut->unicode; } if ( nmb > max_bytes_per_char ) max_bytes_per_char = nmb; } } else { cursor++; } } if (unicode >= 0 && unicode <= 0xffff) { QChar ch((ushort)unicode); if (!from_unicode_page[ch.row()]) { from_unicode_page[ch.row()] = new char[256]; for (int i=0; i<256; i++) from_unicode_page[ch.row()][i]=0; } if ( mb_unicode ) { from_unicode_page[ch.row()][ch.cell()] = 0; if (!from_unicode_page_multibyte) { from_unicode_page_multibyte = new char**[256]; for (int i=0; i<256; i++) from_unicode_page_multibyte[i]=0; } if (!from_unicode_page_multibyte[ch.row()]) { from_unicode_page_multibyte[ch.row()] = new char*[256]; for (int i=0; i<256; i++) from_unicode_page_multibyte[ch.row()][i] = 0; } mb[nmb++] = 0; from_unicode_page_multibyte[ch.row()][ch.cell()] = qstrdup(mb); *mb_unicode = unicode; } else { from_unicode_page[ch.row()][ch.cell()] = (char)byte; if ( to_unicode ) to_unicode[byte] = unicode; else to_unicode_multibyte[byte].unicode = unicode; } } else { } } } n = n.stripWhiteSpace(); unkn = '?'; // ##### Might be a bad choice. } ~QTextCodecFromIOD() { if ( from_unicode_page ) { for (int i=0; i<256; i++) if (from_unicode_page[i]) delete [] from_unicode_page[i]; } if ( from_unicode_page_multibyte ) { for (int i=0; i<256; i++) if (from_unicode_page_multibyte[i]) for (int j=0; j<256; j++) if (from_unicode_page_multibyte[i][j]) delete [] from_unicode_page_multibyte[i][j]; } if ( to_unicode ) delete [] to_unicode; if ( to_unicode_multibyte ) delete [] to_unicode_multibyte; } bool ok() const { return !!from_unicode_page; } QTextDecoder* makeDecoder() const { if ( stateless() ) return QTextCodec::makeDecoder(); else return new QTextCodecFromIODDecoder(this); } const char* name() const { return n; } int mibEnum() const { return 0; // #### Unknown. } int heuristicContentMatch(const char*, int) const { return 0; } int heuristicNameMatch(const char* hint) const { int bestr = QTextCodec::heuristicNameMatch(hint); QStrListIterator it(aliases); char* a; while ((a=it.current())) { ++it; int r = simpleHeuristicNameMatch(a,hint); if (r > bestr) bestr = r; } return bestr; } QString toUnicode(const char* chars, int len) const { const uchar* uchars = (const uchar*)chars; QString result; QMultiByteUnicodeTable* multibyte=to_unicode_multibyte; if ( multibyte ) { while (len--) { QMultiByteUnicodeTable& mb = multibyte[*uchars]; if ( mb.multibyte ) { // Chained multi-byte multibyte = mb.multibyte; } else { result += QChar(mb.unicode); multibyte=to_unicode_multibyte; } uchars++; } } else { while (len--) result += QChar(to_unicode[*uchars++]); } return result; } QCString fromUnicode(const QString& uc, int& lenInOut) const { if (lenInOut > (int)uc.length()) lenInOut = uc.length(); int rlen = lenInOut*max_bytes_per_char; QCString rstr(rlen); char* cursor = rstr.data(); char* s=0; int l = lenInOut; int lout = 0; for (int i=0; i<l; i++) { QChar ch = uc[i]; if ( ch == QChar::null ) { // special *cursor++ = 0; } else if ( from_unicode_page[ch.row()] && from_unicode_page[ch.row()][ch.cell()] ) { *cursor++ = from_unicode_page[ch.row()][ch.cell()]; lout++; } else if ( from_unicode_page_multibyte && from_unicode_page_multibyte[ch.row()] && (s=from_unicode_page_multibyte[ch.row()][ch.cell()]) ) { while (*s) { *cursor++ = *s++; lout++; } } else { *cursor++ = unkn; lout++; } } *cursor = 0; lenInOut = lout; return rstr; }};QTextCodecFromIODDecoder::QTextCodecFromIODDecoder(const QTextCodecFromIOD* c) : codec(c){ mb = codec->to_unicode_multibyte;}QString QTextCodecFromIODDecoder::toUnicode(const char* chars, int len){ const uchar* uchars = (const uchar*)chars; QString result; while (len--) { QMultiByteUnicodeTable& t = mb[*uchars]; if ( t.multibyte ) { // Chained multi-byte mb = t.multibyte; } else { if ( t.unicode ) result += QChar(t.unicode); mb=codec->to_unicode_multibyte; } uchars++; } return result;}/*! Reads a POSIX2 charmap definition from \a iod. The parser recognizes the following lines:<pre> <code_set_name> <i>name</i> <escape_char> <i>character</i> % alias <i>alias</i> CHARMAP <<i>token</i>> /x<i>hexbyte</i> <U<i>unicode</i>> ... <<i>token</i>> /d<i>decbyte</i> <U<i>unicode</i>> ... <<i>token</i>> /<i>octbyte</i> <U<i>unicode</i>> ... <<i>token</i>> /<i>any</i>/<i>any</i>... <U<i>unicode</i>> ... END CHARMAP</pre> The resulting QTextCodec is returned (and also added to the global list of codecs). The name() of the result is taken from the code_set_name. Note that a codec constructed in this way uses much more memory and is slower than a hand-written QTextCodec subclass, since tables in code are in memory shared by all applications simultaneously using Qt. \sa loadCharmapFile()*/QTextCodec* QTextCodec::loadCharmap(QIODevice* iod){ QTextCodecFromIOD* r = new QTextCodecFromIOD(iod); if ( !r->ok() ) { delete r; r = 0; } return r;}/*! A convenience function for loadCharmap().*/QTextCodec* QTextCodec::loadCharmapFile(QString filename){ QFile f(filename); if (f.open(IO_ReadOnly)) { QTextCodecFromIOD* r = new QTextCodecFromIOD(&f); if ( !r->ok() ) delete r; else return r; } return 0;}#endif //QT_NO_CODECS/*! Returns a string representing the current language.*/const char* QTextCodec::locale(){ static QCString lang; if ( lang.isEmpty() ) { lang = getenv( "LANG" ); //########Windows?? if ( lang.isEmpty() ) lang = "C"; } return lang;}#ifndef QT_NO_CODECSclass QSimpleTextCodec: public QTextCodec{public: QSimpleTextCodec( int ); ~QSimpleTextCodec(); 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; int heuristicNameMatch(const char* hint) const;private: int forwardIndex;};#define LAST_MIB 2259static struct { const char * cs; int mib; Q_UINT16 values[128];} unicodevalues[] = { // from RFC 1489, ftp://ftp.isi.edu/in-notes/rfc1489.txt { "KOI8-R", 2084, { 0x2500, 0x2502, 0x250C, 0x2510, 0x2514, 0x2518, 0x251C, 0x2524,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -