⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qtextcodec.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 3 页
字号:
                localeMapper = ru_RU_hack(lang);        }        delete [] ctype;        delete [] lang;    }    // If everything failed, we default to 8859-1    // We could perhaps default to 8859-15.    if (!localeMapper)        localeMapper = QTextCodec::codecForName("ISO 8859-1");#endif}static void setup(){#ifndef QT_NO_THREAD    QMutexLocker locker(QMutexPool::globalInstanceGet(&all));#endif    if (all)        return;    if (destroying_is_ok)        qWarning("QTextCodec: Creating new codec during codec cleanup");    all = new QList<QTextCodec*>;    // create the cleanup object to cleanup all codecs on exit    (void) createQTextCodecCleanup();#ifndef QT_NO_CODECS#  if defined(Q_WS_X11) && !defined(QT_BOOTSTRAPPED)    // no font codecs when bootstrapping    (void)new QFontLaoCodec;#    if defined(QT_NO_ICONV)    // no iconv(3) support, must build all codecs into the library    (void)new QFontGb2312Codec;    (void)new QFontGbkCodec;    (void)new QFontGb18030_0Codec;    (void)new QFontJis0208Codec;    (void)new QFontJis0201Codec;    (void)new QFontKsc5601Codec;    (void)new QFontBig5hkscsCodec;    (void)new QFontBig5Codec;#    endif // QT_NO_ICONV && !QT_BOOTSTRAPPED#  endif // Q_WS_X11    (void)new QTsciiCodec;    for (int i = 0; i < 9; ++i)        (void)new QIsciiCodec(i);    for (int i = 0; i < QSimpleTextCodec::numSimpleCodecs; ++i)        (void)new QSimpleTextCodec(i);#  if defined(QT_NO_ICONV) && !defined(QT_BOOTSTRAPPED)    // no asian codecs when bootstrapping, sorry    (void)new QGb18030Codec;    (void)new QGbkCodec;    (void)new QGb2312Codec;    (void)new QEucJpCodec;    (void)new QJisCodec;    (void)new QSjisCodec;    (void)new QEucKrCodec;    (void)new QBig5Codec;    (void)new QBig5hkscsCodec;#  endif // QT_NO_ICONV && !QT_BOOTSTRAPPED#endif // QT_NO_CODECS#ifdef Q_OS_WIN32    (void) new QWindowsLocalCodec;#endif // Q_OS_WIN32    (void)new QUtf16Codec;    (void)new QUtf16BECodec;    (void)new QUtf16LECodec;    (void)new QLatin15Codec;    (void)new QLatin1Codec;    (void)new QUtf8Codec;#if defined(Q_OS_UNIX) && !defined(QT_NO_ICONV) && !defined(QT_BOOTSTRAPPED)    // QIconvCodec depends on the UTF-16 codec, so it needs to be created last    (void) new QIconvCodec();#endif    if (!localeMapper)        setupLocaleMapper();}/*!    \class QTextCodec    \brief The QTextCodec class provides conversions between text encodings.    \reentrant    \ingroup i18n    Qt uses Unicode to store, draw and manipulate strings. In many    situations you may wish to deal with data that uses a different    encoding. For example, most Japanese documents are still stored    in Shift-JIS or ISO 2022-JP, while Russian users often have their    documents in KOI8-R or Windows-1251.    Qt provides a set of QTextCodec classes to help with converting    non-Unicode formats to and from Unicode. You can also create your    own codec classes.    The supported encodings are:    \list    \o Apple Roman    \o \l{Big5 Text Codec}{Big5}    \o \l{Big5-HKSCS Text Codec}{Big5-HKSCS}    \o \l{EUC-JP Text Codec}{EUC-JP}    \o \l{EUC-KR Text Codec}{EUC-KR}    \o \l{GBK Text Codec}{GB18030-0}    \o IBM 850    \o IBM 866    \o IBM 874    \o \l{ISO 2022-JP (JIS) Text Codec}{ISO 2022-JP}    \o ISO 8859-1 to 10    \o ISO 8859-13 to 16    \o Iscii-Bng, Dev, Gjr, Knd, Mlm, Ori, Pnj, Tlg, and Tml    \o JIS X 0201    \o JIS X 0208    \o KOI8-R    \o KOI8-U    \o MuleLao-1    \o ROMAN8    \o \l{Shift-JIS Text Codec}{Shift-JIS}    \o TIS-620    \o \l{TSCII Text Codec}{TSCII}    \o UTF-8    \o UTF-16    \o UTF-16BE    \o UTF-16LE    \o Windows-1250 to 1258    \o WINSAMI2    \endlist    QTextCodecs can be used as follows to convert some locally encoded    string to Unicode. Suppose you have some string encoded in Russian    KOI8-R encoding, and want to convert it to Unicode. The simple way    to do it is like this:    \code        QByteArray encodedString = "...";        QTextCodec *codec = QTextCodec::codecForName("KOI8-R");        QString string = codec->toUnicode(encodedString);    \endcode    After this, \c string holds the text converted to Unicode.    Converting a string from Unicode to the local encoding is just as    easy:    \code        QString string = "...";        QTextCodec *codec = QTextCodec::codecForName("KOI8-R");        QByteArray encodedString = codec->fromUnicode(string);    \endcode    To read or write files in various encodings, use QTextStream and    its \l{QTextStream::setCodec()}{setCodec()} function. See the    \l{tools/codecs}{Codecs} example for an application of QTextCodec    to file I/O.    Some care must be taken when trying to convert the data in chunks,    for example, when receiving it over a network. In such cases it is    possible that a multi-byte character will be split over two    chunks. At best this might result in the loss of a character and    at worst cause the entire conversion to fail.    The approach to use in these situations is to create a QTextDecoder    object for the codec and use this QTextDecoder for the whole    decoding process, as shown below:    \code        QTextCodec *codec = QTextCodec::codecForName("Shift-JIS");        QTextDecoder *decoder = codec->makeDecoder();        QString string;        while (new_data_available()) {            QByteArray chunk = get_new_data();            string += decoder->toUnicode(chunk);        }    \endcode    The QTextDecoder object maintains state between chunks and therefore    works correctly even if a multi-byte character is split between    chunks.    \section1 Creating Your Own Codec Class    Support for new text encodings can be added to Qt by creating    QTextCodec subclasses.    The pure virtual functions describe the encoder to the system and    the coder is used as required in the different text file formats    supported by QTextStream, and under X11, for the locale-specific    character input and output.    To add support for another encoding to Qt, make a subclass of    QTextCodec and implement the functions listed in the table below.    \table    \header \o Function \o Description    \row \o name()         \o Returns the official name for the encoding. If the            ncoding is listed in the            \l{http://www.iana.org/assignments/character-sets}{IANA            character-sets encoding file}, the name should be the            preferred MIME name for the encoding.    \row \o aliases()         \o Returns a list of alternative names for the encoding.            QTextCodec provides a default implementation that returns            an empty list. For example, "ISO-8859-1" has "latin1",	    "CP819", "IBM819", and "iso-ir-100" as aliases.    \row \o mibEnum()         \o Return the MIB enum for the encoding if it is listed in            the \l{http://www.iana.org/assignments/character-sets}{IANA            character-sets encoding file}.    \row \o convertToUnicode()         \o Converts an 8-bit character string to Unicode.    \row \o convertFromUnicode()         \o Converts a Unicode string to an 8-bit character string.    \endtable    You may find it more convenient to make your codec class    available as a plugin; see \l{How to Create Qt Plugins} for    details.    \sa QTextStream, QTextDecoder, QTextEncoder, {Codecs Example}*//*!    \enum QTextCodec::ConversionFlag    \value DefaultConversion  No flag is set.    \value ConvertInvalidToNull  If this flag is set, invalid input results in           an empty string.    \value IgnoreHeader  Ignore any Unicode byte-order mark and don't generate any.*//*!    \fn QTextCodec::ConverterState::ConverterState(ConversionFlags flags)    Constructs a ConverterState object initialized with the given \a flags.*//*!    \fn QTextCodec::ConverterState::~ConverterState()    Destroys the ConverterState object.*//*!    \nonreentrant    Constructs a QTextCodec, and gives it the highest precedence. The    QTextCodec should always be constructed on the heap (i.e. with \c    new). Qt takes ownership and will delete it when the application    terminates.*/QTextCodec::QTextCodec(){    setup();    all->prepend(this);}/*!    \nonreentrant    Destroys the QTextCodec. Note that you should not delete codecs    yourself: once created they become Qt's responsibility.*/QTextCodec::~QTextCodec(){    if (!destroying_is_ok)        qWarning("QTextCodec::~QTextCodec: Called by application");    if (all)        all->removeAll(this);}/*!    \fn QTextCodec *QTextCodec::codecForName(const char *name)    Searches all installed QTextCodec objects and returns the one    which best matches \a name; the match is case-insensitive. Returns    0 if no codec matching the name \a name could be found.*//*!    Searches all installed QTextCodec objects and returns the one    which best matches \a name; the match is case-insensitive. Returns    0 if no codec matching the name \a name could be found.*/QTextCodec *QTextCodec::codecForName(const QByteArray &name){    if (name.isEmpty())        return 0;    setup();    for (int i = 0; i < all->size(); ++i) {        QTextCodec *cursor = all->at(i);        if (nameMatch(cursor->name(), name))            return cursor;        QList<QByteArray> aliases = cursor->aliases();        for (int i = 0; i < aliases.size(); ++i)            if (nameMatch(aliases.at(i), name))                return cursor;    }    return createForName(name);}/*!    Returns the QTextCodec which matches the \link    QTextCodec::mibEnum() MIBenum\endlink \a mib.*/QTextCodec* QTextCodec::codecForMib(int mib){    setup();    // Qt 3 used 1000 (mib for UCS2) as it's identifier for the utf16 codec. Map    // this correctly for compatibility.    if (mib == 1000)        mib = 1015;    QList<QTextCodec*>::ConstIterator i;    for (int i = 0; i < all->size(); ++i) {        QTextCodec *cursor = all->at(i);        if (cursor->mibEnum() == mib)            return cursor;    }    return createForMib(mib);}/*!    Returns the list of all available codecs, by name. Call    QTextCodec::codecForName() to obtain the QTextCodec for the name.    The list may contain many mentions of the same codec    if the codec has aliases.    \sa availableMibs(), name(), aliases()*/QList<QByteArray> QTextCodec::availableCodecs(){    setup();    QList<QByteArray> codecs;    for (int i = 0; i < all->size(); ++i) {        codecs += all->at(i)->name();        codecs += all->at(i)->aliases();    }#ifndef QT_NO_TEXTCODECPLUGIN    QFactoryLoader *l = loader();    QStringList keys = l->keys();    for (int i = 0; i < keys.size(); ++i) {        if (!keys.at(i).startsWith(QLatin1String("MIB: "))) {            QByteArray name = keys.at(i).toLatin1();            if (!codecs.contains(name))                codecs += name;        }    }#endif    return codecs;}/*!    Returns the list of MIBs for all available codecs. Call    QTextCodec::codecForMib() to obtain the QTextCodec for the MIB.    \sa availableCodecs(), mibEnum()*/QList<int> QTextCodec::availableMibs(){    setup();    QList<int> codecs;    for (int i = 0; i < all->size(); ++i)        codecs += all->at(i)->mibEnum();#ifndef QT_NO_TEXTCODECPLUGIN    QFactoryLoader *l = loader();    QStringList keys = l->keys();    for (int i = 0; i < keys.size(); ++i) {        if (keys.at(i).startsWith(QLatin1String("MIB: "))) {            int mib = keys.at(i).mid(5).toInt();            if (!codecs.contains(mib))                codecs += mib;        }    }#endif    return codecs;}/*!    Set the codec to \a c; this will be returned by codecForLocale().    This might be needed for some applications that want to use their    own mechanism for setting the locale.    Setting this codec is not supported on DOS based Windows.    \sa codecForLocale()*/void QTextCodec::setCodecForLocale(QTextCodec *c){#ifdef Q_WS_WIN    if (QSysInfo::WindowsVersion& QSysInfo::WV_DOS_based)	return;#endif    localeMapper = c;}/*!    Returns a pointer to the codec most suitable for this locale.    On Windows, the codec will be based on a system locale. On Unix    systems, starting with Qt 4.2, the codec will be using the \e    iconv library. Note that in both cases the codec's name will be    "System".*/QTextCodec* QTextCodec::codecForLocale(){    if (localeMapper)        return localeMapper;    setup();    return localeMapper;}/*!    \fn QByteArray QTextCodec::name() const    QTextCodec subclasses must reimplement this function. It returns    the name of the encoding supported by the subclass.    If the codec is registered as a character set in the    \link http://www.iana.org/assignments/character-sets    IANA character-sets encoding file\endlink this method should return    the preferred mime name for the codec if defined, otherwise its name.*//*!    \fn int QTextCodec::mibEnum() const    Subclasses of QTextCodec must reimplement this function. It    returns the MIBenum (see \link    http://www.iana.org/assignments/character-sets the    IANA character-sets encoding file\endlink for more information).    It is important that each QTextCodec subclass returns the correct    unique value for this function.*//*!  Subclasses can return a number of aliases for the codec in question.  Standard aliases for codecs can be found in the

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -