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/qiodevicebase.h>
44#include <QtCore/qstring.h>
45#include <QtCore/qchar.h>
46#include <QtCore/qscopedpointer.h>
47#include <QtCore/qstringconverter.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
57class QIODevice;
58class QLocale;
59
60class QTextStreamPrivate;
61class Q_CORE_EXPORT QTextStream : public QIODeviceBase
62{
63 Q_DECLARE_PRIVATE(QTextStream)
64
65public:
66 enum RealNumberNotation {
67 SmartNotation,
68 FixedNotation,
69 ScientificNotation
70 };
71 enum FieldAlignment {
72 AlignLeft,
73 AlignRight,
74 AlignCenter,
75 AlignAccountingStyle
76 };
77 enum Status {
78 Ok,
79 ReadPastEnd,
80 ReadCorruptData,
81 WriteFailed
82 };
83 enum NumberFlag {
84 ShowBase = 0x1,
85 ForcePoint = 0x2,
86 ForceSign = 0x4,
87 UppercaseBase = 0x8,
88 UppercaseDigits = 0x10
89 };
90 Q_DECLARE_FLAGS(NumberFlags, NumberFlag)
91
92 QTextStream();
93 explicit QTextStream(QIODevice *device);
94 explicit QTextStream(FILE *fileHandle, OpenMode openMode = ReadWrite);
95 explicit QTextStream(QString *string, OpenMode openMode = ReadWrite);
96 explicit QTextStream(QByteArray *array, OpenMode openMode = ReadWrite);
97 explicit QTextStream(const QByteArray &array, OpenMode openMode = ReadOnly);
98 virtual ~QTextStream();
99
100 void setEncoding(QStringConverter::Encoding encoding);
101 QStringConverter::Encoding encoding() const;
102 void setAutoDetectUnicode(bool enabled);
103 bool autoDetectUnicode() const;
104 void setGenerateByteOrderMark(bool generate);
105 bool generateByteOrderMark() const;
106
107 void setLocale(const QLocale &locale);
108 QLocale locale() const;
109
110 void setDevice(QIODevice *device);
111 QIODevice *device() const;
112
113 void setString(QString *string, OpenMode openMode = ReadWrite);
114 QString *string() const;
115
116 Status status() const;
117 void setStatus(Status status);
118 void resetStatus();
119
120 bool atEnd() const;
121 void reset();
122 void flush();
123 bool seek(qint64 pos);
124 qint64 pos() const;
125
126 void skipWhiteSpace();
127
128 QString readLine(qint64 maxlen = 0);
129 bool readLineInto(QString *line, qint64 maxlen = 0);
130 QString readAll();
131 QString read(qint64 maxlen);
132
133 void setFieldAlignment(FieldAlignment alignment);
134 FieldAlignment fieldAlignment() const;
135
136 void setPadChar(QChar ch);
137 QChar padChar() const;
138
139 void setFieldWidth(int width);
140 int fieldWidth() const;
141
142 void setNumberFlags(NumberFlags flags);
143 NumberFlags numberFlags() const;
144
145 void setIntegerBase(int base);
146 int integerBase() const;
147
148 void setRealNumberNotation(RealNumberNotation notation);
149 RealNumberNotation realNumberNotation() const;
150
151 void setRealNumberPrecision(int precision);
152 int realNumberPrecision() const;
153
154 QTextStream &operator>>(QChar &ch);
155 QTextStream &operator>>(char &ch);
156 QTextStream &operator>>(signed short &i);
157 QTextStream &operator>>(unsigned short &i);
158 QTextStream &operator>>(signed int &i);
159 QTextStream &operator>>(unsigned int &i);
160 QTextStream &operator>>(signed long &i);
161 QTextStream &operator>>(unsigned long &i);
162 QTextStream &operator>>(qlonglong &i);
163 QTextStream &operator>>(qulonglong &i);
164 QTextStream &operator>>(float &f);
165 QTextStream &operator>>(double &f);
166 QTextStream &operator>>(QString &s);
167 QTextStream &operator>>(QByteArray &array);
168 QTextStream &operator>>(char *c);
169
170 QTextStream &operator<<(QChar ch);
171 QTextStream &operator<<(char ch);
172 QTextStream &operator<<(signed short i);
173 QTextStream &operator<<(unsigned short i);
174 QTextStream &operator<<(signed int i);
175 QTextStream &operator<<(unsigned int i);
176 QTextStream &operator<<(signed long i);
177 QTextStream &operator<<(unsigned long i);
178 QTextStream &operator<<(qlonglong i);
179 QTextStream &operator<<(qulonglong i);
180 QTextStream &operator<<(float f);
181 QTextStream &operator<<(double f);
182 QTextStream &operator<<(const QString &s);
183 QTextStream &operator<<(QStringView s);
184 QTextStream &operator<<(QLatin1String s);
185 QTextStream &operator<<(const QByteArray &array);
186 QTextStream &operator<<(const char *c);
187 QTextStream &operator<<(const void *ptr);
188
189private:
190 Q_DISABLE_COPY(QTextStream)
191 friend class QDebugStateSaverPrivate;
192 friend class QDebug;
193
194 QScopedPointer<QTextStreamPrivate> d_ptr;
195};
196
197Q_DECLARE_OPERATORS_FOR_FLAGS(QTextStream::NumberFlags)
198
199/*****************************************************************************
200 QTextStream manipulators
201 *****************************************************************************/
202
203typedef QTextStream & (*QTextStreamFunction)(QTextStream &);// manipulator function
204typedef void (QTextStream::*QTSMFI)(int); // manipulator w/int argument
205typedef void (QTextStream::*QTSMFC)(QChar); // manipulator w/QChar argument
206
207
208class Q_CORE_EXPORT QTextStreamManipulator
209{
210public:
211 constexpr QTextStreamManipulator(QTSMFI m, int a) noexcept : mf(m), mc(nullptr), arg(a), ch() {}
212 constexpr QTextStreamManipulator(QTSMFC m, QChar c) noexcept : mf(nullptr), mc(m), arg(-1), ch(c) {}
213 void exec(QTextStream &s) { if (mf) { (s.*mf)(arg); } else { (s.*mc)(ch); } }
214
215private:
216 QTSMFI mf; // QTextStream member function
217 QTSMFC mc; // QTextStream member function
218 int arg; // member function argument
219 QChar ch;
220};
221
222inline QTextStream &operator>>(QTextStream &s, QTextStreamFunction f)
223{ return (*f)(s); }
224
225inline QTextStream &operator<<(QTextStream &s, QTextStreamFunction f)
226{ return (*f)(s); }
227
228inline QTextStream &operator<<(QTextStream &s, QTextStreamManipulator m)
229{ m.exec(s); return s; }
230
231namespace Qt {
232Q_CORE_EXPORT QTextStream &bin(QTextStream &s);
233Q_CORE_EXPORT QTextStream &oct(QTextStream &s);
234Q_CORE_EXPORT QTextStream &dec(QTextStream &s);
235Q_CORE_EXPORT QTextStream &hex(QTextStream &s);
236
237Q_CORE_EXPORT QTextStream &showbase(QTextStream &s);
238Q_CORE_EXPORT QTextStream &forcesign(QTextStream &s);
239Q_CORE_EXPORT QTextStream &forcepoint(QTextStream &s);
240Q_CORE_EXPORT QTextStream &noshowbase(QTextStream &s);
241Q_CORE_EXPORT QTextStream &noforcesign(QTextStream &s);
242Q_CORE_EXPORT QTextStream &noforcepoint(QTextStream &s);
243
244Q_CORE_EXPORT QTextStream &uppercasebase(QTextStream &s);
245Q_CORE_EXPORT QTextStream &uppercasedigits(QTextStream &s);
246Q_CORE_EXPORT QTextStream &lowercasebase(QTextStream &s);
247Q_CORE_EXPORT QTextStream &lowercasedigits(QTextStream &s);
248
249Q_CORE_EXPORT QTextStream &fixed(QTextStream &s);
250Q_CORE_EXPORT QTextStream &scientific(QTextStream &s);
251
252Q_CORE_EXPORT QTextStream &left(QTextStream &s);
253Q_CORE_EXPORT QTextStream &right(QTextStream &s);
254Q_CORE_EXPORT QTextStream &center(QTextStream &s);
255
256Q_CORE_EXPORT QTextStream &endl(QTextStream &s);
257Q_CORE_EXPORT QTextStream &flush(QTextStream &s);
258Q_CORE_EXPORT QTextStream &reset(QTextStream &s);
259
260Q_CORE_EXPORT QTextStream &bom(QTextStream &s);
261
262Q_CORE_EXPORT QTextStream &ws(QTextStream &s);
263
264} // namespace Qt
265
266inline QTextStreamManipulator qSetFieldWidth(int width)
267{
268 QTSMFI func = &QTextStream::setFieldWidth;
269 return QTextStreamManipulator(func,width);
270}
271
272inline QTextStreamManipulator qSetPadChar(QChar ch)
273{
274 QTSMFC func = &QTextStream::setPadChar;
275 return QTextStreamManipulator(func, ch);
276}
277
278inline QTextStreamManipulator qSetRealNumberPrecision(int precision)
279{
280 QTSMFI func = &QTextStream::setRealNumberPrecision;
281 return QTextStreamManipulator(func, precision);
282}
283
284QT_END_NAMESPACE
285
286#endif // QTEXTSTREAM_H
287