📄 qtextcodec.cpp
字号:
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 0xffffstruct 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;};static int getByte(char* &cursor){ int byte = 0; if ( *cursor ) { if ( cursor[1] == 'x' ) byte = strtol(cursor+2,&cursor,16); else if ( cursor[1] == 'd' ) byte = strtol(cursor+2,&cursor,10); else byte = strtol(cursor+2,&cursor,8); } return byte&0xff;}#ifndef QT_NO_CODECSclass 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; } if (!to_unicode) { to_unicode = new ushort[256]; } incmap = TRUE; } else if (0==qstrnicmp(line,"END CHARMAP",11)) break; else if (incmap) { char* cursor = line; int byte,unicode=-1; ushort* mb_unicode=0; const int maxmb=8; // more -> we'll need to improve datastructures char mb[maxmb+1]; int nmb=0; while (*cursor) { if (cursor[0]=='<' && cursor[1]=='U' && cursor[2]>='0' && cursor[2]<='9' &&
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -