1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QTEXTSTREAM_H
41#define QTEXTSTREAM_H
42
43#include <QtCore/qiodevice.h>
44#include <QtCore/qstring.h>
45#include <QtCore/qchar.h>
46#include <QtCore/qlocale.h>
47#include <QtCore/qscopedpointer.h>
48
49#include <stdio.h>
50
51#ifdef Status
52#error qtextstream.h must be included before any header file that defines Status
53#endif
54
55QT_BEGIN_NAMESPACE
56
57
58class QTextCodec;
59class QTextDecoder;
60
61class QTextStreamPrivate;
62class Q_CORE_EXPORT QTextStream // text stream class
63{
64 Q_DECLARE_PRIVATE(QTextStream)
65
66public:
67 enum RealNumberNotation {
68 SmartNotation,
69 FixedNotation,
70 ScientificNotation
71 };
72 enum FieldAlignment {
73 AlignLeft,
74 AlignRight,
75 AlignCenter,
76 AlignAccountingStyle
77 };
78 enum Status {
79 Ok,
80 ReadPastEnd,
81 ReadCorruptData,
82 WriteFailed
83 };
84 enum NumberFlag {
85 ShowBase = 0x1,
86 ForcePoint = 0x2,
87 ForceSign = 0x4,
88 UppercaseBase = 0x8,
89 UppercaseDigits = 0x10
90 };
91 Q_DECLARE_FLAGS(NumberFlags, NumberFlag)
92
93 QTextStream();
94 explicit QTextStream(QIODevice *device);
95 explicit QTextStream(FILE *fileHandle, QIODevice::OpenMode openMode = QIODevice::ReadWrite);
96 explicit QTextStream(QString *string, QIODevice::OpenMode openMode = QIODevice::ReadWrite);
97 explicit QTextStream(QByteArray *array, QIODevice::OpenMode openMode = QIODevice::ReadWrite);
98 explicit QTextStream(const QByteArray &array, QIODevice::OpenMode openMode = QIODevice::ReadOnly);
99 virtual ~QTextStream();
100
101#if QT_CONFIG(textcodec)
102 void setCodec(QTextCodec *codec);
103 void setCodec(const char *codecName);
104 QTextCodec *codec() const;
105 void setAutoDetectUnicode(bool enabled);
106 bool autoDetectUnicode() const;
107 void setGenerateByteOrderMark(bool generate);
108 bool generateByteOrderMark() const;
109#endif
110
111 void setLocale(const QLocale &locale);
112 QLocale locale() const;
113
114 void setDevice(QIODevice *device);
115 QIODevice *device() const;
116
117 void setString(QString *string, QIODevice::OpenMode openMode = QIODevice::ReadWrite);
118 QString *string() const;
119
120 Status status() const;
121 void setStatus(Status status);
122 void resetStatus();
123
124 bool atEnd() const;
125 void reset();
126 void flush();
127 bool seek(qint64 pos);
128 qint64 pos() const;
129
130 void skipWhiteSpace();
131
132 QString readLine(qint64 maxlen = 0);
133 bool readLineInto(QString *line, qint64 maxlen = 0);
134 QString readAll();
135 QString read(qint64 maxlen);
136
137 void setFieldAlignment(FieldAlignment alignment);
138 FieldAlignment fieldAlignment() const;
139
140 void setPadChar(QChar ch);
141 QChar padChar() const;
142
143 void setFieldWidth(int width);
144 int fieldWidth() const;
145
146 void setNumberFlags(NumberFlags flags);
147 NumberFlags numberFlags() const;
148
149 void setIntegerBase(int base);
150 int integerBase() const;
151
152 void setRealNumberNotation(RealNumberNotation notation);
153 RealNumberNotation realNumberNotation() const;
154
155 void setRealNumberPrecision(int precision);
156 int realNumberPrecision() const;
157
158 QTextStream &operator>>(QChar &ch);
159 QTextStream &operator>>(char &ch);
160 QTextStream &operator>>(signed short &i);
161 QTextStream &operator>>(unsigned short &i);
162 QTextStream &operator>>(signed int &i);
163 QTextStream &operator>>(unsigned int &i);
164 QTextStream &operator>>(signed long &i);
165 QTextStream &operator>>(unsigned long &i);
166 QTextStream &operator>>(qlonglong &i);
167 QTextStream &operator>>(qulonglong &i);
168 QTextStream &operator>>(float &f);
169 QTextStream &operator>>(double &f);
170 QTextStream &operator>>(QString &s);
171 QTextStream &operator>>(QByteArray &array);
172 QTextStream &operator>>(char *c);
173
174 QTextStream &operator<<(QChar ch);
175 QTextStream &operator<<(char ch);
176 QTextStream &operator<<(signed short i);
177 QTextStream &operator<<(unsigned short i);
178 QTextStream &operator<<(signed int i);
179 QTextStream &operator<<(unsigned int i);
180 QTextStream &operator<<(signed long i);
181 QTextStream &operator<<(unsigned long i);
182 QTextStream &operator<<(qlonglong i);
183 QTextStream &operator<<(qulonglong i);
184 QTextStream &operator<<(float f);
185 QTextStream &operator<<(double f);
186 QTextStream &operator<<(const QString &s);
187 QTextStream &operator<<(QStringView s);
188 QTextStream &operator<<(QLatin1String s);
189 QTextStream &operator<<(const QStringRef &s);
190 QTextStream &operator<<(const QByteArray &array);
191 QTextStream &operator<<(const char *c);
192 QTextStream &operator<<(const void *ptr);
193
194private:
195 Q_DISABLE_COPY(QTextStream)
196 friend class QDebugStateSaverPrivate;
197 friend class QDebug;
198
199 QScopedPointer<QTextStreamPrivate> d_ptr;
200};
201
202Q_DECLARE_OPERATORS_FOR_FLAGS(QTextStream::NumberFlags)
203
204/*****************************************************************************
205 QTextStream manipulators
206 *****************************************************************************/
207
208typedef QTextStream & (*QTextStreamFunction)(QTextStream &);// manipulator function
209typedef void (QTextStream::*QTSMFI)(int); // manipulator w/int argument
210typedef void (QTextStream::*QTSMFC)(QChar); // manipulator w/QChar argument
211
212
213class Q_CORE_EXPORT QTextStreamManipulator
214{
215public:
216 Q_DECL_CONSTEXPR QTextStreamManipulator(QTSMFI m, int a) noexcept : mf(m), mc(nullptr), arg(a), ch() {}
217 Q_DECL_CONSTEXPR QTextStreamManipulator(QTSMFC m, QChar c) noexcept : mf(nullptr), mc(m), arg(-1), ch(c) {}
218 void exec(QTextStream &s) { if (mf) { (s.*mf)(arg); } else { (s.*mc)(ch); } }
219
220private:
221 QTSMFI mf; // QTextStream member function
222 QTSMFC mc; // QTextStream member function
223 int arg; // member function argument
224 QChar ch;
225};
226
227inline QTextStream &operator>>(QTextStream &s, QTextStreamFunction f)
228{ return (*f)(s); }
229
230inline QTextStream &operator<<(QTextStream &s, QTextStreamFunction f)
231{ return (*f)(s); }
232
233inline QTextStream &operator<<(QTextStream &s, QTextStreamManipulator m)
234{ m.exec(s); return s; }
235
236namespace Qt {
237Q_CORE_EXPORT QTextStream &bin(QTextStream &s);
238Q_CORE_EXPORT QTextStream &oct(QTextStream &s);
239Q_CORE_EXPORT QTextStream &dec(QTextStream &s);
240Q_CORE_EXPORT QTextStream &hex(QTextStream &s);
241
242Q_CORE_EXPORT QTextStream &showbase(QTextStream &s);
243Q_CORE_EXPORT QTextStream &forcesign(QTextStream &s);
244Q_CORE_EXPORT QTextStream &forcepoint(QTextStream &s);
245Q_CORE_EXPORT QTextStream &noshowbase(QTextStream &s);
246Q_CORE_EXPORT QTextStream &noforcesign(QTextStream &s);
247Q_CORE_EXPORT QTextStream &noforcepoint(QTextStream &s);
248
249Q_CORE_EXPORT QTextStream &uppercasebase(QTextStream &s);
250Q_CORE_EXPORT QTextStream &uppercasedigits(QTextStream &s);
251Q_CORE_EXPORT QTextStream &lowercasebase(QTextStream &s);
252Q_CORE_EXPORT QTextStream &lowercasedigits(QTextStream &s);
253
254Q_CORE_EXPORT QTextStream &fixed(QTextStream &s);
255Q_CORE_EXPORT QTextStream &scientific(QTextStream &s);
256
257Q_CORE_EXPORT QTextStream &left(QTextStream &s);
258Q_CORE_EXPORT QTextStream &right(QTextStream &s);
259Q_CORE_EXPORT QTextStream &center(QTextStream &s);
260
261Q_CORE_EXPORT QTextStream &endl(QTextStream &s);
262Q_CORE_EXPORT QTextStream &flush(QTextStream &s);
263Q_CORE_EXPORT QTextStream &reset(QTextStream &s);
264
265Q_CORE_EXPORT QTextStream &bom(QTextStream &s);
266
267Q_CORE_EXPORT QTextStream &ws(QTextStream &s);
268
269} // namespace Qt
270
271#if QT_DEPRECATED_SINCE(5, 15)
272// This namespace only exists for 'using namespace' declarations.
273namespace QTextStreamFunctions {
274Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::bin") QTextStream &bin(QTextStream &s);
275Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::oct") QTextStream &oct(QTextStream &s);
276Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::dec") QTextStream &dec(QTextStream &s);
277Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::hex") QTextStream &hex(QTextStream &s);
278Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::showbase") QTextStream &showbase(QTextStream &s);
279Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::forcesign") QTextStream &forcesign(QTextStream &s);
280Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::forcepoint") QTextStream &forcepoint(QTextStream &s);
281Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::noshowbase") QTextStream &noshowbase(QTextStream &s);
282Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::noforcesign") QTextStream &noforcesign(QTextStream &s);
283Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::noforcepoint") QTextStream &noforcepoint(QTextStream &s);
284Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::uppercasebase") QTextStream &uppercasebase(QTextStream &s);
285Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::uppercasedigits") QTextStream &uppercasedigits(QTextStream &s);
286Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::lowercasebase") QTextStream &lowercasebase(QTextStream &s);
287Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::lowercasedigits") QTextStream &lowercasedigits(QTextStream &s);
288Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::fixed") QTextStream &fixed(QTextStream &s);
289Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::scientific") QTextStream &scientific(QTextStream &s);
290Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::left") QTextStream &left(QTextStream &s);
291Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::right") QTextStream &right(QTextStream &s);
292Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::center") QTextStream &center(QTextStream &s);
293Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::endl") QTextStream &endl(QTextStream &s);
294Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::flush") QTextStream &flush(QTextStream &s);
295Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::reset") QTextStream &reset(QTextStream &s);
296Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::bom") QTextStream &bom(QTextStream &s);
297Q_CORE_EXPORT QT_DEPRECATED_VERSION_X(5, 15, "Use Qt::ws") QTextStream &ws(QTextStream &s);
298} // namespace QTextStreamFunctions
299
300QT_WARNING_PUSH
301QT_WARNING_DISABLE_CLANG("-Wheader-hygiene")
302// We use 'using namespace' as that doesn't cause
303// conflicting definitions compiler errors.
304using namespace QTextStreamFunctions;
305QT_WARNING_POP
306#endif // QT_DEPRECATED_SINCE(5, 15)
307
308inline QTextStreamManipulator qSetFieldWidth(int width)
309{
310 QTSMFI func = &QTextStream::setFieldWidth;
311 return QTextStreamManipulator(func,width);
312}
313
314inline QTextStreamManipulator qSetPadChar(QChar ch)
315{
316 QTSMFC func = &QTextStream::setPadChar;
317 return QTextStreamManipulator(func, ch);
318}
319
320inline QTextStreamManipulator qSetRealNumberPrecision(int precision)
321{
322 QTSMFI func = &QTextStream::setRealNumberPrecision;
323 return QTextStreamManipulator(func, precision);
324}
325
326QT_END_NAMESPACE
327
328#endif // QTEXTSTREAM_H
329

source code of qtbase/src/corelib/serialization/qtextstream.h