1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
45 #include <QtCore/qstring.h>
46 #include <QtCore/qlist.h>
53 #ifndef QT_NO_TEXTCODEC
61 class Q_CORE_EXPORT QTextCodec
63 Q_DISABLE_COPY(QTextCodec)
65 static QTextCodec* codecForName(const QByteArray &name);
66 static QTextCodec* codecForName(const char *name) { return codecForName(QByteArray(name)); }
67 static QTextCodec* codecForMib(int mib);
69 static QList<QByteArray> availableCodecs();
70 static QList<int> availableMibs();
72 static QTextCodec* codecForLocale();
73 static void setCodecForLocale(QTextCodec *c);
75 static QTextCodec* codecForTr();
76 static void setCodecForTr(QTextCodec *c);
78 static QTextCodec* codecForCStrings();
79 static void setCodecForCStrings(QTextCodec *c);
81 static QTextCodec *codecForHtml(const QByteArray &ba);
82 static QTextCodec *codecForHtml(const QByteArray &ba, QTextCodec *defaultCodec);
84 static QTextCodec *codecForUtfText(const QByteArray &ba);
85 static QTextCodec *codecForUtfText(const QByteArray &ba, QTextCodec *defaultCodec);
87 bool canEncode(QChar) const;
88 bool canEncode(const QString&) const;
90 QString toUnicode(const QByteArray&) const;
91 QString toUnicode(const char* chars) const;
92 QByteArray fromUnicode(const QString& uc) const;
95 ConvertInvalidToNull = 0x80000000,
99 Q_DECLARE_FLAGS(ConversionFlags, ConversionFlag)
101 struct Q_CORE_EXPORT ConverterState {
102 ConverterState(ConversionFlags f = DefaultConversion)
103 : flags(f), remainingChars(0), invalidChars(0), d(0) { state_data[0] = state_data[1] = state_data[2] = 0; }
105 ConversionFlags flags;
111 Q_DISABLE_COPY(ConverterState)
114 QString toUnicode(const char *in, int length, ConverterState *state = 0) const
115 { return convertToUnicode(in, length, state); }
116 QByteArray fromUnicode(const QChar *in, int length, ConverterState *state = 0) const
117 { return convertFromUnicode(in, length, state); }
119 QTextDecoder* makeDecoder(ConversionFlags flags = DefaultConversion) const;
120 QTextEncoder* makeEncoder(ConversionFlags flags = DefaultConversion) const;
122 virtual QByteArray name() const = 0;
123 virtual QList<QByteArray> aliases() const;
124 virtual int mibEnum() const = 0;
127 virtual QString convertToUnicode(const char *in, int length, ConverterState *state) const = 0;
128 virtual QByteArray convertFromUnicode(const QChar *in, int length, ConverterState *state) const = 0;
131 virtual ~QTextCodec();
134 friend class QTextCodecCleanup;
135 static QTextCodec *cftr;
136 static bool validCodecs();
138 Q_DECLARE_OPERATORS_FOR_FLAGS(QTextCodec::ConversionFlags)
140 inline QTextCodec* QTextCodec::codecForTr() { return validCodecs() ? cftr : 0; }
141 inline void QTextCodec::setCodecForTr(QTextCodec *c) { cftr = c; }
142 inline QTextCodec* QTextCodec::codecForCStrings() { return validCodecs() ? QString::codecForCStrings : 0; }
143 inline void QTextCodec::setCodecForCStrings(QTextCodec *c) { QString::codecForCStrings = c; }
145 class Q_CORE_EXPORT QTextEncoder {
146 Q_DISABLE_COPY(QTextEncoder)
148 explicit QTextEncoder(const QTextCodec *codec) : c(codec), state() {}
149 QTextEncoder(const QTextCodec *codec, QTextCodec::ConversionFlags flags);
151 QByteArray fromUnicode(const QString& str);
152 QByteArray fromUnicode(const QChar *uc, int len);
153 bool hasFailure() const;
156 QTextCodec::ConverterState state;
159 class Q_CORE_EXPORT QTextDecoder {
160 Q_DISABLE_COPY(QTextDecoder)
162 explicit QTextDecoder(const QTextCodec *codec) : c(codec), state() {}
163 QTextDecoder(const QTextCodec *codec, QTextCodec::ConversionFlags flags);
165 QString toUnicode(const char* chars, int len);
166 QString toUnicode(const QByteArray &ba);
167 void toUnicode(QString *target, const char *chars, int len);
168 bool hasFailure() const;
171 QTextCodec::ConverterState state;
174 #endif // QT_NO_TEXTCODEC
180 #endif // QTEXTCODEC_H