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

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