QtCore/qtextcodec.h Source File

qtextcodec.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 
49 
51 
52 QT_MODULE(Core)
53 
54 #ifndef QT_NO_TEXTCODEC
55 
56 class QTextCodec;
57 class QIODevice;
58 
59 class QTextDecoder;
60 class QTextEncoder;
61 
62 class Q_CORE_EXPORT QTextCodec
63 {
64  Q_DISABLE_COPY(QTextCodec)
65 public:
66  static QTextCodec* codecForName(const QByteArray &name);
67  static QTextCodec* codecForName(const char *name) { return codecForName(QByteArray(name)); }
68  static QTextCodec* codecForMib(int mib);
69 
70  static QList<QByteArray> availableCodecs();
71  static QList<int> availableMibs();
72 
73  static QTextCodec* codecForLocale();
74  static void setCodecForLocale(QTextCodec *c);
75 
76  static QTextCodec* codecForTr();
77  static void setCodecForTr(QTextCodec *c);
78 
79  static QTextCodec* codecForCStrings();
80  static void setCodecForCStrings(QTextCodec *c);
81 
82  static QTextCodec *codecForHtml(const QByteArray &ba);
83  static QTextCodec *codecForHtml(const QByteArray &ba, QTextCodec *defaultCodec);
84 
85  static QTextCodec *codecForUtfText(const QByteArray &ba);
86  static QTextCodec *codecForUtfText(const QByteArray &ba, QTextCodec *defaultCodec);
87 
88  bool canEncode(QChar) const;
89  bool canEncode(const QString&) const;
90 
91  QString toUnicode(const QByteArray&) const;
92  QString toUnicode(const char* chars) const;
93  QByteArray fromUnicode(const QString& uc) const;
96  ConvertInvalidToNull = 0x80000000,
97  IgnoreHeader = 0x1,
98  FreeFunction = 0x2
99  };
100  Q_DECLARE_FLAGS(ConversionFlags, ConversionFlag)
101 
102  struct Q_CORE_EXPORT ConverterState {
103  ConverterState(ConversionFlags f = DefaultConversion)
104  : flags(f), remainingChars(0), invalidChars(0), d(0) { state_data[0] = state_data[1] = state_data[2] = 0; }
105  ~ConverterState();
106  ConversionFlags flags;
109  uint state_data[3];
110  void *d;
111  private:
112  Q_DISABLE_COPY(ConverterState)
113  };
114 
115  QString toUnicode(const char *in, int length, ConverterState *state = 0) const
116  { return convertToUnicode(in, length, state); }
117  QByteArray fromUnicode(const QChar *in, int length, ConverterState *state = 0) const
118  { return convertFromUnicode(in, length, state); }
119 
120  // ### Qt 5: merge these functions.
121  QTextDecoder* makeDecoder() const;
122  QTextDecoder* makeDecoder(ConversionFlags flags) const;
123  QTextEncoder* makeEncoder() const;
124  QTextEncoder* makeEncoder(ConversionFlags flags) const;
125 
126  virtual QByteArray name() const = 0;
127  virtual QList<QByteArray> aliases() const;
128  virtual int mibEnum() const = 0;
129 
130 protected:
131  virtual QString convertToUnicode(const char *in, int length, ConverterState *state) const = 0;
132  virtual QByteArray convertFromUnicode(const QChar *in, int length, ConverterState *state) const = 0;
133 
134  QTextCodec();
135  virtual ~QTextCodec();
136 
137 public:
138 #ifdef QT3_SUPPORT
139  static QT3_SUPPORT QTextCodec* codecForContent(const char*, int) { return 0; }
140  static QT3_SUPPORT const char* locale();
141  static QT3_SUPPORT QTextCodec* codecForName(const char* hint, int) { return codecForName(QByteArray(hint)); }
142  QT3_SUPPORT QByteArray fromUnicode(const QString& uc, int& lenInOut) const;
143  QT3_SUPPORT QString toUnicode(const QByteArray&, int len) const;
144  QT3_SUPPORT QByteArray mimeName() const { return name(); }
145  static QT3_SUPPORT QTextCodec *codecForIndex(int i) { return codecForName(availableCodecs().value(i)); }
146 #endif
147 
148 private:
149  friend class QTextCodecCleanup;
150  static QTextCodec *cftr;
151  static bool validCodecs();
152 };
153 Q_DECLARE_OPERATORS_FOR_FLAGS(QTextCodec::ConversionFlags)
154 
155  inline QTextCodec* QTextCodec::codecForTr() { return validCodecs() ? cftr : 0; }
156 inline void QTextCodec::setCodecForTr(QTextCodec *c) { cftr = c; }
157 inline QTextCodec* QTextCodec::codecForCStrings() { return validCodecs() ? QString::codecForCStrings : 0; }
158 inline void QTextCodec::setCodecForCStrings(QTextCodec *c) { QString::codecForCStrings = c; }
159 
160 class Q_CORE_EXPORT QTextEncoder {
161  Q_DISABLE_COPY(QTextEncoder)
162 public:
163  explicit QTextEncoder(const QTextCodec *codec) : c(codec), state() {}
164  QTextEncoder(const QTextCodec *codec, QTextCodec::ConversionFlags flags);
165  ~QTextEncoder();
166  QByteArray fromUnicode(const QString& str);
167  QByteArray fromUnicode(const QChar *uc, int len);
168 #ifdef QT3_SUPPORT
169  QT3_SUPPORT QByteArray fromUnicode(const QString& uc, int& lenInOut);
170 #endif
171  bool hasFailure() const;
172 private:
173  const QTextCodec *c;
175 };
176 
177 class Q_CORE_EXPORT QTextDecoder {
178  Q_DISABLE_COPY(QTextDecoder)
179 public:
180  explicit QTextDecoder(const QTextCodec *codec) : c(codec), state() {}
181  QTextDecoder(const QTextCodec *codec, QTextCodec::ConversionFlags flags);
182  ~QTextDecoder();
183  QString toUnicode(const char* chars, int len);
184  QString toUnicode(const QByteArray &ba);
185  void toUnicode(QString *target, const char *chars, int len);
186  bool hasFailure() const;
187 private:
188  const QTextCodec *c;
190 };
191 
192 #endif // QT_NO_TEXTCODEC
193 
195 
197 
198 #endif // QTEXTCODEC_H
static void setCodecForCStrings(QTextCodec *c)
Definition: qtextcodec.h:158
#define QT_END_NAMESPACE
Definition: qglobal.h:128
#define QT_BEGIN_HEADER
Definition: qglobal.h:141
QTextDecoder(const QTextCodec *codec)
Definition: qtextcodec.h:180
QByteArray fromUnicode(const QChar *in, int length, ConverterState *state=0) const
Definition: qtextcodec.h:117
#define inline
Definition: image.h:2490
QTextEncoder(const QTextCodec *codec)
Definition: qtextcodec.h:163
#define QT_BEGIN_NAMESPACE
Definition: qglobal.h:127
static QTextCodec * codecForCStrings()
Definition: qtextcodec.h:157
QString toUnicode(const char *in, int length, ConverterState *state=0) const
Definition: qtextcodec.h:115
GLuint GLsizei GLsizei * length
Definition: GLee.h:1713
Q_GUI_EXPORT QTextCodec * codecForHtml(const QByteArray &ba)
GLsizei const GLfloat * value
Definition: GLee.h:1742
const GLubyte * c
Definition: GLee.h:5419
ConverterState(ConversionFlags f=DefaultConversion)
Definition: qtextcodec.h:103
GLuint const GLchar * name
Definition: GLee.h:1704
GLuint in
Definition: GLee.h:7188
static void setCodecForTr(QTextCodec *c)
Definition: qtextcodec.h:156
static QTextCodec * codecForName(const char *name)
Definition: qtextcodec.h:67
Definition: qchar.h:72
ConversionFlags flags
Definition: qtextcodec.h:106
GLenum GLsizei len
Definition: GLee.h:2695
#define QT_END_HEADER
Definition: qglobal.h:142
GLclampf f
Definition: GLee.h:9303