1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QTEXTCODEC_H
5#define QTEXTCODEC_H
6
7#include <QtCore/qlist.h>
8#include <QtCore/qstringconverter.h>
9
10#include <QtCore5Compat/qcore5global.h>
11
12QT_REQUIRE_CONFIG(textcodec);
13
14QT_BEGIN_NAMESPACE
15
16class QTextDecoder;
17class QTextEncoder;
18
19class Q_CORE5COMPAT_EXPORT QTextCodec : public QStringConverterBase
20{
21 Q_DISABLE_COPY(QTextCodec)
22public:
23 using ConversionFlags = QStringConverterBase::Flags;
24 using ConverterState = QStringConverterBase::State;
25
26 static constexpr Flag ConvertInvalidToNull = Flag::ConvertInvalidToNull;
27 static constexpr Flag DefaultConversion = Flag::WriteBom;
28 static constexpr Flag IgnoreHeader = Flag::ConvertInitialBom;
29
30 static QTextCodec* codecForName(const QByteArray &name);
31 static QTextCodec* codecForName(const char *name) { return codecForName(name: QByteArray(name)); }
32 static QTextCodec* codecForMib(int mib);
33
34 static QList<QByteArray> availableCodecs();
35 static QList<int> availableMibs();
36
37 static QTextCodec* codecForLocale();
38 static void setCodecForLocale(QTextCodec *c);
39
40 static QTextCodec *codecForTr() { return codecForMib(mib: 106); /* Utf8 */ }
41
42 static QTextCodec *codecForHtml(const QByteArray &ba);
43 static QTextCodec *codecForHtml(const QByteArray &ba, QTextCodec *defaultCodec);
44
45 static QTextCodec *codecForUtfText(const QByteArray &ba);
46 static QTextCodec *codecForUtfText(const QByteArray &ba, QTextCodec *defaultCodec);
47
48 bool canEncode(QChar) const;
49 bool canEncode(const QString&) const;
50 bool canEncode(QStringView) const;
51
52 QString toUnicode(const QByteArray&) const;
53 QString toUnicode(const char* chars) const;
54 QByteArray fromUnicode(const QString& uc) const;
55 QByteArray fromUnicode(QStringView uc) const;
56
57 QString toUnicode(const char *in, int length, ConverterState *state = nullptr) const
58 {
59 if (in == nullptr || length <= 0)
60 return {};
61 return convertToUnicode(in, length, state);
62 }
63 QByteArray fromUnicode(const QChar *in, int length, ConverterState *state = nullptr) const
64 {
65 if (in == nullptr || length <= 0)
66 return {};
67 return convertFromUnicode(in, length, state);
68 }
69
70 QTextDecoder* makeDecoder(ConversionFlags flags = DefaultConversion) const;
71 QTextEncoder* makeEncoder(ConversionFlags flags = DefaultConversion) const;
72
73 virtual QByteArray name() const = 0;
74 virtual QList<QByteArray> aliases() const;
75 virtual int mibEnum() const = 0;
76
77protected:
78 virtual QString convertToUnicode(const char *in, int length, ConverterState *state) const = 0;
79 virtual QByteArray convertFromUnicode(const QChar *in, int length, ConverterState *state) const = 0;
80
81 QTextCodec();
82 virtual ~QTextCodec();
83
84private:
85 friend struct QTextCodecData;
86};
87
88class Q_CORE5COMPAT_EXPORT QTextEncoder
89{
90 Q_DISABLE_COPY(QTextEncoder)
91public:
92 explicit QTextEncoder(const QTextCodec *codec) : c(codec), state() {}
93 explicit QTextEncoder(const QTextCodec *codec, QTextCodec::ConversionFlags flags);
94 ~QTextEncoder();
95 QByteArray fromUnicode(const QString& str);
96 QByteArray fromUnicode(QStringView str);
97 QByteArray fromUnicode(const QChar *uc, int len);
98 bool hasFailure() const;
99private:
100 const QTextCodec *c = nullptr;
101 QTextCodec::ConverterState state;
102};
103
104class Q_CORE5COMPAT_EXPORT QTextDecoder
105{
106 Q_DISABLE_COPY(QTextDecoder)
107public:
108 explicit QTextDecoder(const QTextCodec *codec) : c(codec), state() {}
109 explicit QTextDecoder(const QTextCodec *codec, QTextCodec::ConversionFlags flags);
110 ~QTextDecoder();
111 QString toUnicode(const char* chars, int len);
112 QString toUnicode(const QByteArray &ba);
113 void toUnicode(QString *target, const char *chars, int len);
114 bool hasFailure() const;
115 bool needsMoreData() const;
116private:
117 const QTextCodec *c = nullptr;
118 QTextCodec::ConverterState state;
119};
120
121namespace Qt
122{
123 Q_CORE5COMPAT_EXPORT QTextCodec *codecForHtml(const QByteArray &ba);
124}
125
126QT_END_NAMESPACE
127
128#endif // QTEXTCODEC_H
129

source code of qt5compat/src/core5/codecs/qtextcodec.h