9e46863964250c895af14cd0418783417049f9e7
[profile/ivi/qtbase.git] / src / corelib / codecs / qtextcodec.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QTEXTCODEC_H
43 #define QTEXTCODEC_H
44
45 #include <QtCore/qstring.h>
46 #include <QtCore/qlist.h>
47
48 QT_BEGIN_HEADER
49
50 QT_BEGIN_NAMESPACE
51
52
53 #ifndef QT_NO_TEXTCODEC
54
55 class QTextCodec;
56 class QIODevice;
57
58 class QTextDecoder;
59 class QTextEncoder;
60
61 class Q_CORE_EXPORT QTextCodec
62 {
63     Q_DISABLE_COPY(QTextCodec)
64 public:
65     static QTextCodec* codecForName(const QByteArray &name);
66     static QTextCodec* codecForName(const char *name) { return codecForName(QByteArray(name)); }
67     static QTextCodec* codecForMib(int mib);
68
69     static QList<QByteArray> availableCodecs();
70     static QList<int> availableMibs();
71
72     static QTextCodec* codecForLocale();
73     static void setCodecForLocale(QTextCodec *c);
74
75     static QTextCodec* codecForTr();
76     static void setCodecForTr(QTextCodec *c);
77
78     static QTextCodec* codecForCStrings();
79     static void setCodecForCStrings(QTextCodec *c);
80
81     static QTextCodec *codecForHtml(const QByteArray &ba);
82     static QTextCodec *codecForHtml(const QByteArray &ba, QTextCodec *defaultCodec);
83
84     static QTextCodec *codecForUtfText(const QByteArray &ba);
85     static QTextCodec *codecForUtfText(const QByteArray &ba, QTextCodec *defaultCodec);
86
87     bool canEncode(QChar) const;
88     bool canEncode(const QString&) const;
89
90     QString toUnicode(const QByteArray&) const;
91     QString toUnicode(const char* chars) const;
92     QByteArray fromUnicode(const QString& uc) const;
93     enum ConversionFlag {
94         DefaultConversion,
95         ConvertInvalidToNull = 0x80000000,
96         IgnoreHeader = 0x1,
97         FreeFunction = 0x2
98     };
99     Q_DECLARE_FLAGS(ConversionFlags, ConversionFlag)
100
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; }
104         ~ConverterState();
105         ConversionFlags flags;
106         int remainingChars;
107         int invalidChars;
108         uint state_data[3];
109         void *d;
110     private:
111         Q_DISABLE_COPY(ConverterState)
112     };
113
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); }
118
119     QTextDecoder* makeDecoder(ConversionFlags flags = DefaultConversion) const;
120     QTextEncoder* makeEncoder(ConversionFlags flags = DefaultConversion) const;
121
122     virtual QByteArray name() const = 0;
123     virtual QList<QByteArray> aliases() const;
124     virtual int mibEnum() const = 0;
125
126 protected:
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;
129
130     QTextCodec();
131     virtual ~QTextCodec();
132
133 private:
134     friend class QTextCodecCleanup;
135     static QTextCodec *cftr;
136     static bool validCodecs();
137 };
138 Q_DECLARE_OPERATORS_FOR_FLAGS(QTextCodec::ConversionFlags)
139
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; }
144
145 class Q_CORE_EXPORT QTextEncoder {
146     Q_DISABLE_COPY(QTextEncoder)
147 public:
148     explicit QTextEncoder(const QTextCodec *codec) : c(codec), state() {}
149     QTextEncoder(const QTextCodec *codec, QTextCodec::ConversionFlags flags);
150     ~QTextEncoder();
151     QByteArray fromUnicode(const QString& str);
152     QByteArray fromUnicode(const QChar *uc, int len);
153     bool hasFailure() const;
154 private:
155     const QTextCodec *c;
156     QTextCodec::ConverterState state;
157 };
158
159 class Q_CORE_EXPORT QTextDecoder {
160     Q_DISABLE_COPY(QTextDecoder)
161 public:
162     explicit QTextDecoder(const QTextCodec *codec) : c(codec), state() {}
163     QTextDecoder(const QTextCodec *codec, QTextCodec::ConversionFlags flags);
164     ~QTextDecoder();
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;
169 private:
170     const QTextCodec *c;
171     QTextCodec::ConverterState state;
172 };
173
174 #endif // QT_NO_TEXTCODEC
175
176 QT_END_NAMESPACE
177
178 QT_END_HEADER
179
180 #endif // QTEXTCODEC_H