1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2016 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#ifndef QTEXTSTREAM_P_H
6#define QTEXTSTREAM_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtCore/private/qglobal_p.h>
20#include <QtCore/qstringconverter.h>
21#include <QtCore/qiodevice.h>
22#include <QtCore/qlocale.h>
23#include "qtextstream.h"
24
25QT_BEGIN_NAMESPACE
26
27#ifndef QT_NO_QOBJECT
28class QDeviceClosedNotifier : public QObject
29{
30 Q_OBJECT
31public:
32 inline QDeviceClosedNotifier()
33 { }
34 ~QDeviceClosedNotifier() override;
35
36 inline void setupDevice(QTextStream *stream, QIODevice *device)
37 {
38 disconnect();
39 if (device) {
40 // Force direct connection here so that QTextStream can be used
41 // from multiple threads when the application code is handling
42 // synchronization (see also QTBUG-12055).
43 connect(sender: device, SIGNAL(aboutToClose()), receiver: this, SLOT(flushStream()),
44 Qt::DirectConnection);
45 }
46 m_stream = stream;
47 }
48
49public Q_SLOTS:
50 void flushStream() { m_stream->flush(); }
51
52private:
53 QTextStream *m_stream;
54};
55#endif
56
57class QTextStreamPrivate
58{
59 Q_DECLARE_PUBLIC(QTextStream)
60public:
61 // streaming parameters
62 class Params
63 {
64 public:
65 void reset();
66
67 int realNumberPrecision;
68 int integerBase;
69 int fieldWidth;
70 QChar padChar;
71 QTextStream::FieldAlignment fieldAlignment;
72 QTextStream::RealNumberNotation realNumberNotation;
73 QTextStream::NumberFlags numberFlags;
74 };
75
76 QTextStreamPrivate(QTextStream *q_ptr);
77 ~QTextStreamPrivate();
78 void reset();
79
80 // device
81 QIODevice *device;
82#ifndef QT_NO_QOBJECT
83 QDeviceClosedNotifier deviceClosedNotifier;
84#endif
85
86 // string
87 QString *string;
88 int stringOffset;
89 QIODevice::OpenMode stringOpenMode;
90
91 QStringConverter::Encoding encoding = QStringConverter::Utf8;
92 QStringEncoder fromUtf16;
93 QStringDecoder toUtf16;
94 QStringDecoder savedToUtf16;
95
96 QString writeBuffer;
97 QString readBuffer;
98 int readBufferOffset;
99 int readConverterSavedStateOffset; //the offset between readBufferStartDevicePos and that start of the buffer
100 qint64 readBufferStartDevicePos;
101
102 Params params;
103
104 // status
105 QTextStream::Status status;
106 QLocale locale;
107 QTextStream *q_ptr;
108
109 int lastTokenSize;
110 bool deleteDevice;
111 bool autoDetectUnicode;
112 bool hasWrittenData = false;
113 bool generateBOM = false;
114
115 // i/o
116 enum TokenDelimiter {
117 Space,
118 NotSpace,
119 EndOfLine
120 };
121
122 QString read(int maxlen);
123 bool scan(const QChar **ptr, int *tokenLength,
124 int maxlen, TokenDelimiter delimiter);
125 inline const QChar *readPtr() const;
126 inline void consumeLastToken();
127 inline void consume(int nchars);
128 void saveConverterState(qint64 newPos);
129 void restoreToSavedConverterState();
130
131 // Return value type for getNumber()
132 enum NumberParsingStatus {
133 npsOk,
134 npsMissingDigit,
135 npsInvalidPrefix
136 };
137
138 inline bool getChar(QChar *ch);
139 inline void ungetChar(QChar ch);
140 NumberParsingStatus getNumber(qulonglong *l);
141 bool getReal(double *f);
142
143 inline void write(QStringView data) { write(data: data.begin(), len: data.size()); }
144 inline void write(QChar ch);
145 void write(const QChar *data, qsizetype len);
146 void write(QLatin1StringView data);
147 void writePadding(qsizetype len);
148 inline void putString(QStringView string, bool number = false)
149 {
150 putString(data: string.constData(), len: string.size(), number);
151 }
152 void putString(const QChar *data, qsizetype len, bool number = false);
153 void putString(QLatin1StringView data, bool number = false);
154 void putString(QUtf8StringView data, bool number = false);
155 inline void putChar(QChar ch);
156 void putNumber(qulonglong number, bool negative);
157
158 struct PaddingResult {
159 int left, right;
160 };
161 PaddingResult padding(qsizetype len) const;
162
163 // buffers
164 bool fillReadBuffer(qint64 maxBytes = -1);
165 void resetReadBuffer();
166 void flushWriteBuffer();
167};
168
169QT_END_NAMESPACE
170
171#endif // QTEXTSTREAM_P_H
172

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