Qt 4.8
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 
63 {
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 
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; }
159 
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 
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
double d
Definition: qnumeric_p.h:62
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QTextCodec::ConverterState state
Definition: qtextcodec.h:189
#define QT_MODULE(x)
Definition: qglobal.h:2783
const char * aliases[7]
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
#define Q_DECLARE_FLAGS(Flags, Enum)
The Q_DECLARE_FLAGS() macro expands to.
Definition: qglobal.h:2348
static QTextCodec * codecForCStrings()
Returns the codec used by QString to convert to and from const char * and QByteArrays.
Definition: qtextcodec.h:157
ConverterState(ConversionFlags f=DefaultConversion)
Constructs a ConverterState object initialized with the given flags.
Definition: qtextcodec.h:103
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
The QTextDecoder class provides a state-based decoder.
Definition: qtextcodec.h:177
#define Q_DISABLE_COPY(Class)
Disables the use of copy constructors and assignment operators for the given Class.
Definition: qglobal.h:2523
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
static void setCodecForCStrings(QTextCodec *c)
Definition: qtextcodec.h:158
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
The Q_DECLARE_OPERATORS_FOR_FLAGS() macro declares global operator|() functions for Flags...
Definition: qglobal.h:2355
QString toUnicode(const char *in, int length, ConverterState *state=0) const
Converts the first size characters from the input from the encoding of this codec to Unicode...
Definition: qtextcodec.h:115
const char * name
The QTextEncoder class provides a state-based encoder.
Definition: qtextcodec.h:160
unsigned int uint
Definition: qglobal.h:996
static QTextCodec * codec(MYSQL *mysql)
Definition: qsql_mysql.cpp:220
int mib
const QTextCodec * c
Definition: qtextcodec.h:173
static QString toUnicode(QTextCodec *tc, const char *str)
Definition: qsql_mysql.cpp:95
ConversionFlags flags
Definition: qtextcodec.h:106
QTextDecoder(const QTextCodec *codec)
Constructs a text decoder for the given codec.
Definition: qtextcodec.h:180
#define Q_CORE_EXPORT
Definition: qglobal.h:1449
QTextCodec::ConverterState state
Definition: qtextcodec.h:174
QByteArray fromUnicode(const QChar *in, int length, ConverterState *state=0) const
Converts the first number of characters from the input array from Unicode to the encoding of this cod...
Definition: qtextcodec.h:117
static QTextCodec * codecForCStrings
Definition: qstring.h:621
QTextEncoder(const QTextCodec *codec)
Constructs a text encoder for the given codec.
Definition: qtextcodec.h:163
static QTextCodec * cftr
Definition: qtextcodec.h:150
static QTextCodec * codecForName(const char *name)
Searches all installed QTextCodec objects and returns the one which best matches name; the match is c...
Definition: qtextcodec.h:67
static QByteArray fromUnicode(QTextCodec *tc, const QString &str)
Definition: qsql_mysql.cpp:115
The QTextCodec class provides conversions between text encodings.
Definition: qtextcodec.h:62
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66
#define QT_END_HEADER
Definition: qglobal.h:137
const QTextCodec * c
Definition: qtextcodec.h:188
static void setCodecForTr(QTextCodec *c)
Definition: qtextcodec.h:156
Q_GUI_EXPORT QTextCodec * codecForHtml(const QByteArray &ba)
This function is defined in the <QTextDocument> header file.