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
236#if defined(Q_QDOC) || QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
237namespace Qt {
238#else
239// This namespace only exists for 'using namespace' declarations.
240namespace QTextStreamFunctions {
241#endif
242
243Q_CORE_EXPORT QTextStream &bin(QTextStream &s);
244Q_CORE_EXPORT QTextStream &oct(QTextStream &s);
245Q_CORE_EXPORT QTextStream &dec(QTextStream &s);
246Q_CORE_EXPORT QTextStream &hex(QTextStream &s);
247
248Q_CORE_EXPORT QTextStream &showbase(QTextStream &s);
249Q_CORE_EXPORT QTextStream &forcesign(QTextStream &s);
250Q_CORE_EXPORT QTextStream &forcepoint(QTextStream &s);
251Q_CORE_EXPORT QTextStream &noshowbase(QTextStream &s);
252Q_CORE_EXPORT QTextStream &noforcesign(QTextStream &s);
253Q_CORE_EXPORT QTextStream &noforcepoint(QTextStream &s);
254
255Q_CORE_EXPORT QTextStream &uppercasebase(QTextStream &s);
256Q_CORE_EXPORT QTextStream &uppercasedigits(QTextStream &s);
257Q_CORE_EXPORT QTextStream &lowercasebase(QTextStream &s);
258Q_CORE_EXPORT QTextStream &lowercasedigits(QTextStream &s);
259
260Q_CORE_EXPORT QTextStream &fixed(QTextStream &s);
261Q_CORE_EXPORT QTextStream &scientific(QTextStream &s);
262
263Q_CORE_EXPORT QTextStream &left(QTextStream &s);
264Q_CORE_EXPORT QTextStream &right(QTextStream &s);
265Q_CORE_EXPORT QTextStream &center(QTextStream &s);
266
267Q_CORE_EXPORT QTextStream &endl(QTextStream &s);
268Q_CORE_EXPORT QTextStream &flush(QTextStream &s);
269Q_CORE_EXPORT QTextStream &reset(QTextStream &s);
270
271Q_CORE_EXPORT QTextStream &bom(QTextStream &s);
272
273Q_CORE_EXPORT QTextStream &ws(QTextStream &s);
274
275} // namespace QTextStreamFunctions
276
277#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && !defined(Q_QDOC)
278namespace Qt {
279using namespace QTextStreamFunctions;
280}
281
282// We use 'using namespace' as that doesn't cause
283// conflicting definitions compiler errors.
284using namespace QTextStreamFunctions;
285#endif // QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && !defined(Q_QDOC)
286
287inline QTextStreamManipulator qSetFieldWidth(int width)
288{
289 QTSMFI func = &QTextStream::setFieldWidth;
290 return QTextStreamManipulator(func,width);
291}
292
293inline QTextStreamManipulator qSetPadChar(QChar ch)
294{
295 QTSMFC func = &QTextStream::setPadChar;
296 return QTextStreamManipulator(func, ch);
297}
298
299inline QTextStreamManipulator qSetRealNumberPrecision(int precision)
300{
301 QTSMFI func = &QTextStream::setRealNumberPrecision;
302 return QTextStreamManipulator(func, precision);
303}
304
305QT_END_NAMESPACE
306
307#endif // QTEXTSTREAM_H
308