1 /****************************************************************************
2 **
3 ** DQt - D bindings for the Qt Toolkit
4 **
5 ** GNU Lesser General Public License Usage
6 ** This file may be used under the terms of the GNU Lesser
7 ** General Public License version 3 as published by the Free Software
8 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
9 ** packaging of this file. Please review the following information to
10 ** ensure the GNU Lesser General Public License version 3 requirements
11 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
12 **
13 ****************************************************************************/
14 module qt.core.textcodec;
15 extern(C++):
16 
17 import qt.config;
18 import qt.core.bytearray;
19 import qt.core.flags;
20 import qt.core.list;
21 import qt.core.qchar;
22 import qt.core.string;
23 import qt.core.stringview;
24 import qt.helpers;
25 
26 /+ QT_REQUIRE_CONFIG(textcodec);
27 
28 
29 class QTextCodec;
30 class QIODevice;
31 
32 class QTextDecoder;
33 class QTextEncoder; +/
34 
35 class /+ Q_CORE_EXPORT +/ QTextCodec
36 {
37 private:
38     /+ Q_DISABLE_COPY(QTextCodec) +/
39 public:
40     static QTextCodec codecForName(ref const(QByteArray) name);
41     static QTextCodec codecForName(const(char)* name) { auto tmp = QByteArray(name); return codecForName(tmp); }
42     static QTextCodec codecForMib(int mib);
43 
44     static QList!(QByteArray) availableCodecs();
45     static QList!(int) availableMibs();
46 
47     static QTextCodec codecForLocale();
48     static void setCodecForLocale(QTextCodec c);
49 
50 /+ #if QT_DEPRECATED_SINCE(5, 0)
51     QT_DEPRECATED static QTextCodec *codecForTr() { return codecForMib(106); /* Utf8 */ }
52 #endif +/
53 
54     static QTextCodec codecForHtml(ref const(QByteArray) ba);
55     static QTextCodec codecForHtml(ref const(QByteArray) ba, QTextCodec defaultCodec);
56 
57     static QTextCodec codecForUtfText(ref const(QByteArray) ba);
58     static QTextCodec codecForUtfText(ref const(QByteArray) ba, QTextCodec defaultCodec);
59 
60     final bool canEncode(QChar) const;
61     static if(QT_STRINGVIEW_LEVEL < 2)
62     {
63         final bool canEncode(ref const(QString)) const;
64     }
65     final bool canEncode(QStringView) const;
66 
67     final QString toUnicode(ref const(QByteArray)) const;
68     final QString toUnicode(const(char)* chars) const;
69     static if(QT_STRINGVIEW_LEVEL < 2)
70     {
71         final QByteArray fromUnicode(ref const(QString) uc) const;
72     }
73     final QByteArray fromUnicode(QStringView uc) const;
74     enum ConversionFlag {
75         DefaultConversion,
76         ConvertInvalidToNull = 0x80000000,
77         IgnoreHeader = 0x1,
78         FreeFunction = 0x2
79     }
80     /+ Q_DECLARE_FLAGS(ConversionFlags, ConversionFlag) +/
81 alias ConversionFlags = QFlags!(ConversionFlag);
82     struct /+ Q_CORE_EXPORT +/ ConverterState {
83         this(ConversionFlags f/+ = ConversionFlag.DefaultConversion+/)
84         {
85             this.flags = f;
86             this.remainingChars = 0;
87             this.invalidChars = 0;
88             this.d = null;
89             state_data[0] = state_data[1] = state_data[2] = 0;
90         }
91         ~this();
92         ConversionFlags flags = ConversionFlag.DefaultConversion;
93         int remainingChars = 0;
94         int invalidChars = 0;
95         uint[3] state_data = [0, 0, 0];
96         void* d = null;
97     private:
98         /+ Q_DISABLE_COPY(ConverterState) +/
99 @disable this(this);
100 /+this(ref const(ConverterState));+//+ref ConverterState operator =(ref const(ConverterState));+/    }
101 
102     final QString toUnicode(const(char)* in_, int length, ConverterState* state = null) const
103         { return convertToUnicode(in_, length, state); }
104     final QByteArray fromUnicode(const(QChar)* in_, int length, ConverterState* state = null) const
105         { return convertFromUnicode(in_, length, state); }
106 
107     final QTextDecoder* makeDecoder(ConversionFlags flags = ConversionFlag.DefaultConversion) const;
108     final QTextEncoder* makeEncoder(ConversionFlags flags = ConversionFlag.DefaultConversion) const;
109 
110     /+ virtual +/ abstract QByteArray name() const;
111     /+ virtual +/ QList!(QByteArray) aliases() const;
112     /+ virtual +/ abstract int mibEnum() const;
113 
114 protected:
115     /+ virtual +/ abstract QString convertToUnicode(const(char)* in_, int length, ConverterState* state) const;
116     /+ virtual +/ abstract QByteArray convertFromUnicode(const(QChar)* in_, int length, ConverterState* state) const;
117 
118     this();
119     /*public*/ /+ virtual +/~this();
120 
121 private:
122     /+ friend struct QCoreGlobalData; +/
123 }
124 /+pragma(inline, true) QFlags!(QTextCodec.ConversionFlags.enum_type) operator |(QTextCodec.ConversionFlags.enum_type f1, QTextCodec.ConversionFlags.enum_type f2)/+noexcept+/{return QFlags!(QTextCodec.ConversionFlags.enum_type)(f1)|f2;}+/
125 /+pragma(inline, true) QFlags!(QTextCodec.ConversionFlags.enum_type) operator |(QTextCodec.ConversionFlags.enum_type f1, QFlags!(QTextCodec.ConversionFlags.enum_type) f2)/+noexcept+/{return f2|f1;}+/
126 /+pragma(inline, true) QIncompatibleFlag operator |(QTextCodec.ConversionFlags.enum_type f1, int f2)/+noexcept+/{return QIncompatibleFlag(int(f1)|f2);}+/
127 /+ Q_DECLARE_OPERATORS_FOR_FLAGS(QTextCodec::ConversionFlags) +/
128 extern(C++, class) struct /+ Q_CORE_EXPORT +/ QTextEncoder {
129 private:
130     /+ Q_DISABLE_COPY(QTextEncoder) +/
131 @disable this(this);
132 /+this(ref const(QTextEncoder));+//+ref QTextEncoder operator =(ref const(QTextEncoder));+/public:
133     /+ explicit +/this(const(QTextCodec) codec)
134     {
135         this.c = codec;
136         this.state = typeof(this.state)();
137     }
138     /+ explicit +/this(const(QTextCodec) codec, QTextCodec.ConversionFlags flags);
139     ~this();
140     static if(QT_STRINGVIEW_LEVEL < 2)
141     {
142         QByteArray fromUnicode(ref const(QString) str);
143     }
144     QByteArray fromUnicode(QStringView str);
145     QByteArray fromUnicode(const(QChar)* uc, int len);
146     bool hasFailure() const;
147 private:
148     const(QTextCodec) c;
149     QTextCodec.ConverterState state;
150 }
151 
152 extern(C++, class) struct /+ Q_CORE_EXPORT +/ QTextDecoder {
153 private:
154     /+ Q_DISABLE_COPY(QTextDecoder) +/
155 @disable this(this);
156 /+this(ref const(QTextDecoder));+//+ref QTextDecoder operator =(ref const(QTextDecoder));+/public:
157     /+ explicit +/this(const(QTextCodec) codec)
158     {
159         this.c = codec;
160         this.state = typeof(this.state)();
161     }
162     /+ explicit +/this(const(QTextCodec) codec, QTextCodec.ConversionFlags flags);
163     ~this();
164     QString toUnicode(const(char)* chars, int len);
165     QString toUnicode(ref const(QByteArray) ba);
166     void toUnicode(QString* target, const(char)* chars, int len);
167     bool hasFailure() const;
168     bool needsMoreData() const;
169 private:
170     const(QTextCodec) c;
171     QTextCodec.ConverterState state;
172 }
173