qtextcodec.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtCore module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QTEXTCODEC_H
00043 #define QTEXTCODEC_H
00044 
00045 #include <QtCore/qstring.h>
00046 #include <QtCore/qlist.h>
00047 
00048 QT_BEGIN_HEADER
00049 
00050 QT_BEGIN_NAMESPACE
00051 
00052 QT_MODULE(Core)
00053 
00054 #ifndef QT_NO_TEXTCODEC
00055 
00056 class QTextCodec;
00057 class QIODevice;
00058 
00059 class QTextDecoder;
00060 class QTextEncoder;
00061 
00062 class Q_CORE_EXPORT QTextCodec
00063 {
00064     Q_DISABLE_COPY(QTextCodec)
00065 public:
00066     static QTextCodec* codecForName(const QByteArray &name);
00067     static QTextCodec* codecForName(const char *name) { return codecForName(QByteArray(name)); }
00068     static QTextCodec* codecForMib(int mib);
00069 
00070     static QList<QByteArray> availableCodecs();
00071     static QList<int> availableMibs();
00072 
00073     static QTextCodec* codecForLocale();
00074     static void setCodecForLocale(QTextCodec *c);
00075 
00076     static QTextCodec* codecForTr();
00077     static void setCodecForTr(QTextCodec *c);
00078 
00079     static QTextCodec* codecForCStrings();
00080     static void setCodecForCStrings(QTextCodec *c);
00081 
00082     static QTextCodec *codecForHtml(const QByteArray &ba);
00083     static QTextCodec *codecForHtml(const QByteArray &ba, QTextCodec *defaultCodec);
00084 
00085     static QTextCodec *codecForUtfText(const QByteArray &ba);
00086     static QTextCodec *codecForUtfText(const QByteArray &ba, QTextCodec *defaultCodec);
00087 
00088     bool canEncode(QChar) const;
00089     bool canEncode(const QString&) const;
00090 
00091     QString toUnicode(const QByteArray&) const;
00092     QString toUnicode(const char* chars) const;
00093     QByteArray fromUnicode(const QString& uc) const;
00094     enum ConversionFlag {
00095         DefaultConversion,
00096         ConvertInvalidToNull = 0x80000000,
00097         IgnoreHeader = 0x1,
00098         FreeFunction = 0x2
00099     };
00100     Q_DECLARE_FLAGS(ConversionFlags, ConversionFlag)
00101 
00102     struct Q_CORE_EXPORT ConverterState {
00103         ConverterState(ConversionFlags f = DefaultConversion)
00104             : flags(f), remainingChars(0), invalidChars(0), d(0) { state_data[0] = state_data[1] = state_data[2] = 0; }
00105         ~ConverterState();
00106         ConversionFlags flags;
00107         int remainingChars;
00108         int invalidChars;
00109         uint state_data[3];
00110         void *d;
00111     private:
00112         Q_DISABLE_COPY(ConverterState)
00113     };
00114 
00115     QString toUnicode(const char *in, int length, ConverterState *state = 0) const
00116         { return convertToUnicode(in, length, state); }
00117     QByteArray fromUnicode(const QChar *in, int length, ConverterState *state = 0) const
00118         { return convertFromUnicode(in, length, state); }
00119 
00120     // ### Qt 5: merge these functions.
00121     QTextDecoder* makeDecoder() const;
00122     QTextDecoder* makeDecoder(ConversionFlags flags) const;
00123     QTextEncoder* makeEncoder() const;
00124     QTextEncoder* makeEncoder(ConversionFlags flags) const;
00125 
00126     virtual QByteArray name() const = 0;
00127     virtual QList<QByteArray> aliases() const;
00128     virtual int mibEnum() const = 0;
00129 
00130 protected:
00131     virtual QString convertToUnicode(const char *in, int length, ConverterState *state) const = 0;
00132     virtual QByteArray convertFromUnicode(const QChar *in, int length, ConverterState *state) const = 0;
00133 
00134     QTextCodec();
00135     virtual ~QTextCodec();
00136 
00137 public:
00138 #ifdef QT3_SUPPORT
00139     static QT3_SUPPORT QTextCodec* codecForContent(const char*, int) { return 0; }
00140     static QT3_SUPPORT const char* locale();
00141     static QT3_SUPPORT QTextCodec* codecForName(const char* hint, int) { return codecForName(QByteArray(hint)); }
00142     QT3_SUPPORT QByteArray fromUnicode(const QString& uc, int& lenInOut) const;
00143     QT3_SUPPORT QString toUnicode(const QByteArray&, int len) const;
00144     QT3_SUPPORT QByteArray mimeName() const { return name(); }
00145     static QT3_SUPPORT QTextCodec *codecForIndex(int i) { return codecForName(availableCodecs().value(i)); }
00146 #endif
00147 
00148 private:
00149     friend class QTextCodecCleanup;
00150     static QTextCodec *cftr;
00151     static bool validCodecs();
00152 };
00153 Q_DECLARE_OPERATORS_FOR_FLAGS(QTextCodec::ConversionFlags)
00154 
00155         inline QTextCodec* QTextCodec::codecForTr() { return validCodecs() ? cftr : 0; }
00156 inline void QTextCodec::setCodecForTr(QTextCodec *c) { cftr = c; }
00157 inline QTextCodec* QTextCodec::codecForCStrings() { return validCodecs() ? QString::codecForCStrings : 0; }
00158 inline void QTextCodec::setCodecForCStrings(QTextCodec *c) { QString::codecForCStrings = c; }
00159 
00160 class Q_CORE_EXPORT QTextEncoder {
00161     Q_DISABLE_COPY(QTextEncoder)
00162 public:
00163     explicit QTextEncoder(const QTextCodec *codec) : c(codec), state() {}
00164     QTextEncoder(const QTextCodec *codec, QTextCodec::ConversionFlags flags);
00165     ~QTextEncoder();
00166     QByteArray fromUnicode(const QString& str);
00167     QByteArray fromUnicode(const QChar *uc, int len);
00168 #ifdef QT3_SUPPORT
00169     QT3_SUPPORT QByteArray fromUnicode(const QString& uc, int& lenInOut);
00170 #endif
00171     bool hasFailure() const;
00172 private:
00173     const QTextCodec *c;
00174     QTextCodec::ConverterState state;
00175 };
00176 
00177 class Q_CORE_EXPORT QTextDecoder {
00178     Q_DISABLE_COPY(QTextDecoder)
00179 public:
00180     explicit QTextDecoder(const QTextCodec *codec) : c(codec), state() {}
00181     QTextDecoder(const QTextCodec *codec, QTextCodec::ConversionFlags flags);
00182     ~QTextDecoder();
00183     QString toUnicode(const char* chars, int len);
00184     QString toUnicode(const QByteArray &ba);
00185     void toUnicode(QString *target, const char *chars, int len);
00186     bool hasFailure() const;
00187 private:
00188     const QTextCodec *c;
00189     QTextCodec::ConverterState state;
00190 };
00191 
00192 #endif // QT_NO_TEXTCODEC
00193 
00194 QT_END_NAMESPACE
00195 
00196 QT_END_HEADER
00197 
00198 #endif // QTEXTCODEC_H