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

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