| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2018 The Qt Company Ltd. | 
|---|
| 4 | ** Contact: https://www.qt.io/licensing/ | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial License Usage | 
|---|
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
|---|
| 11 | ** accordance with the commercial license agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
|---|
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
|---|
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
|---|
| 16 | ** | 
|---|
| 17 | ** GNU Lesser General Public License Usage | 
|---|
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 19 | ** General Public License version 3 as published by the Free Software | 
|---|
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | 
|---|
| 21 | ** packaging of this file. Please review the following information to | 
|---|
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | 
|---|
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | 
|---|
| 24 | ** | 
|---|
| 25 | ** GNU General Public License Usage | 
|---|
| 26 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | 
|---|
| 28 | ** Public license version 3 or any later version approved by the KDE Free | 
|---|
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | 
|---|
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | 
|---|
| 31 | ** included in the packaging of this file. Please review the following | 
|---|
| 32 | ** information to ensure the GNU General Public License requirements will | 
|---|
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | 
|---|
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** $QT_END_LICENSE$ | 
|---|
| 37 | ** | 
|---|
| 38 | ****************************************************************************/ | 
|---|
| 39 |  | 
|---|
| 40 | #ifndef QTEXTCODEC_H | 
|---|
| 41 | #define QTEXTCODEC_H | 
|---|
| 42 |  | 
|---|
| 43 | #include <QtCore/qstring.h> | 
|---|
| 44 | #include <QtCore/qlist.h> | 
|---|
| 45 |  | 
|---|
| 46 | QT_REQUIRE_CONFIG(textcodec); | 
|---|
| 47 |  | 
|---|
| 48 | QT_BEGIN_NAMESPACE | 
|---|
| 49 |  | 
|---|
| 50 | class QTextCodec; | 
|---|
| 51 | class QIODevice; | 
|---|
| 52 |  | 
|---|
| 53 | class QTextDecoder; | 
|---|
| 54 | class QTextEncoder; | 
|---|
| 55 |  | 
|---|
| 56 | class Q_CORE_EXPORT QTextCodec | 
|---|
| 57 | { | 
|---|
| 58 | Q_DISABLE_COPY(QTextCodec) | 
|---|
| 59 | public: | 
|---|
| 60 | static QTextCodec* codecForName(const QByteArray &name); | 
|---|
| 61 | static QTextCodec* codecForName(const char *name) { return codecForName(QByteArray(name)); } | 
|---|
| 62 | static QTextCodec* codecForMib(int mib); | 
|---|
| 63 |  | 
|---|
| 64 | static QList<QByteArray> availableCodecs(); | 
|---|
| 65 | static QList<int> availableMibs(); | 
|---|
| 66 |  | 
|---|
| 67 | static QTextCodec* codecForLocale(); | 
|---|
| 68 | static void setCodecForLocale(QTextCodec *c); | 
|---|
| 69 |  | 
|---|
| 70 | #if QT_DEPRECATED_SINCE(5, 0) | 
|---|
| 71 | QT_DEPRECATED static QTextCodec *codecForTr() { return codecForMib(106); /* Utf8 */ } | 
|---|
| 72 | #endif | 
|---|
| 73 |  | 
|---|
| 74 | static QTextCodec *codecForHtml(const QByteArray &ba); | 
|---|
| 75 | static QTextCodec *codecForHtml(const QByteArray &ba, QTextCodec *defaultCodec); | 
|---|
| 76 |  | 
|---|
| 77 | static QTextCodec *codecForUtfText(const QByteArray &ba); | 
|---|
| 78 | static QTextCodec *codecForUtfText(const QByteArray &ba, QTextCodec *defaultCodec); | 
|---|
| 79 |  | 
|---|
| 80 | bool canEncode(QChar) const; | 
|---|
| 81 | #if QT_STRINGVIEW_LEVEL < 2 | 
|---|
| 82 | bool canEncode(const QString&) const; | 
|---|
| 83 | #endif | 
|---|
| 84 | bool canEncode(QStringView) const; | 
|---|
| 85 |  | 
|---|
| 86 | QString toUnicode(const QByteArray&) const; | 
|---|
| 87 | QString toUnicode(const char* chars) const; | 
|---|
| 88 | #if QT_STRINGVIEW_LEVEL < 2 | 
|---|
| 89 | QByteArray fromUnicode(const QString& uc) const; | 
|---|
| 90 | #endif | 
|---|
| 91 | QByteArray fromUnicode(QStringView uc) const; | 
|---|
| 92 | enum ConversionFlag { | 
|---|
| 93 | DefaultConversion, | 
|---|
| 94 | ConvertInvalidToNull = 0x80000000, | 
|---|
| 95 | = 0x1, | 
|---|
| 96 | FreeFunction = 0x2 | 
|---|
| 97 | }; | 
|---|
| 98 | Q_DECLARE_FLAGS(ConversionFlags, ConversionFlag) | 
|---|
| 99 |  | 
|---|
| 100 | struct Q_CORE_EXPORT ConverterState { | 
|---|
| 101 | ConverterState(ConversionFlags f = DefaultConversion) | 
|---|
| 102 | : flags(f), remainingChars(0), invalidChars(0), d(nullptr) { state_data[0] = state_data[1] = state_data[2] = 0; } | 
|---|
| 103 | ~ConverterState(); | 
|---|
| 104 | ConversionFlags flags; | 
|---|
| 105 | int remainingChars; | 
|---|
| 106 | int invalidChars; | 
|---|
| 107 | uint state_data[3]; | 
|---|
| 108 | void *d; | 
|---|
| 109 | private: | 
|---|
| 110 | Q_DISABLE_COPY(ConverterState) | 
|---|
| 111 | }; | 
|---|
| 112 |  | 
|---|
| 113 | QString toUnicode(const char *in, int length, ConverterState *state = nullptr) const | 
|---|
| 114 | { return convertToUnicode(in, length, state); } | 
|---|
| 115 | QByteArray fromUnicode(const QChar *in, int length, ConverterState *state = nullptr) const | 
|---|
| 116 | { return convertFromUnicode(in, length, state); } | 
|---|
| 117 |  | 
|---|
| 118 | QTextDecoder* makeDecoder(ConversionFlags flags = DefaultConversion) const; | 
|---|
| 119 | QTextEncoder* makeEncoder(ConversionFlags flags = DefaultConversion) const; | 
|---|
| 120 |  | 
|---|
| 121 | virtual QByteArray name() const = 0; | 
|---|
| 122 | virtual QList<QByteArray> aliases() const; | 
|---|
| 123 | virtual int mibEnum() const = 0; | 
|---|
| 124 |  | 
|---|
| 125 | protected: | 
|---|
| 126 | virtual QString convertToUnicode(const char *in, int length, ConverterState *state) const = 0; | 
|---|
| 127 | virtual QByteArray convertFromUnicode(const QChar *in, int length, ConverterState *state) const = 0; | 
|---|
| 128 |  | 
|---|
| 129 | QTextCodec(); | 
|---|
| 130 | virtual ~QTextCodec(); | 
|---|
| 131 |  | 
|---|
| 132 | private: | 
|---|
| 133 | friend struct QCoreGlobalData; | 
|---|
| 134 | }; | 
|---|
| 135 | Q_DECLARE_OPERATORS_FOR_FLAGS(QTextCodec::ConversionFlags) | 
|---|
| 136 |  | 
|---|
| 137 | class Q_CORE_EXPORT QTextEncoder { | 
|---|
| 138 | Q_DISABLE_COPY(QTextEncoder) | 
|---|
| 139 | public: | 
|---|
| 140 | explicit QTextEncoder(const QTextCodec *codec) : c(codec), state() {} | 
|---|
| 141 | explicit QTextEncoder(const QTextCodec *codec, QTextCodec::ConversionFlags flags); | 
|---|
| 142 | ~QTextEncoder(); | 
|---|
| 143 | #if QT_STRINGVIEW_LEVEL < 2 | 
|---|
| 144 | QByteArray fromUnicode(const QString& str); | 
|---|
| 145 | #endif | 
|---|
| 146 | QByteArray fromUnicode(QStringView str); | 
|---|
| 147 | QByteArray fromUnicode(const QChar *uc, int len); | 
|---|
| 148 | bool hasFailure() const; | 
|---|
| 149 | private: | 
|---|
| 150 | const QTextCodec *c; | 
|---|
| 151 | QTextCodec::ConverterState state; | 
|---|
| 152 | }; | 
|---|
| 153 |  | 
|---|
| 154 | class Q_CORE_EXPORT QTextDecoder { | 
|---|
| 155 | Q_DISABLE_COPY(QTextDecoder) | 
|---|
| 156 | public: | 
|---|
| 157 | explicit QTextDecoder(const QTextCodec *codec) : c(codec), state() {} | 
|---|
| 158 | explicit QTextDecoder(const QTextCodec *codec, QTextCodec::ConversionFlags flags); | 
|---|
| 159 | ~QTextDecoder(); | 
|---|
| 160 | QString toUnicode(const char* chars, int len); | 
|---|
| 161 | QString toUnicode(const QByteArray &ba); | 
|---|
| 162 | void toUnicode(QString *target, const char *chars, int len); | 
|---|
| 163 | bool hasFailure() const; | 
|---|
| 164 | bool needsMoreData() const; | 
|---|
| 165 | private: | 
|---|
| 166 | const QTextCodec *c; | 
|---|
| 167 | QTextCodec::ConverterState state; | 
|---|
| 168 | }; | 
|---|
| 169 |  | 
|---|
| 170 | QT_END_NAMESPACE | 
|---|
| 171 |  | 
|---|
| 172 | #endif // QTEXTCODEC_H | 
|---|
| 173 |  | 
|---|