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 | |
25 | QT_BEGIN_NAMESPACE |
26 | |
27 | #ifndef QT_NO_QOBJECT |
28 | class QDeviceClosedNotifier : public QObject |
29 | { |
30 | Q_OBJECT |
31 | public: |
32 | inline QDeviceClosedNotifier() |
33 | { } |
34 | |
35 | inline void setupDevice(QTextStream *stream, QIODevice *device) |
36 | { |
37 | disconnect(); |
38 | if (device) { |
39 | // Force direct connection here so that QTextStream can be used |
40 | // from multiple threads when the application code is handling |
41 | // synchronization (see also QTBUG-12055). |
42 | connect(sender: device, SIGNAL(aboutToClose()), receiver: this, SLOT(flushStream()), |
43 | Qt::DirectConnection); |
44 | } |
45 | this->stream = stream; |
46 | } |
47 | |
48 | public Q_SLOTS: |
49 | inline void flushStream() { stream->flush(); } |
50 | |
51 | private: |
52 | QTextStream *stream; |
53 | }; |
54 | #endif |
55 | |
56 | class QTextStreamPrivate |
57 | { |
58 | Q_DECLARE_PUBLIC(QTextStream) |
59 | public: |
60 | // streaming parameters |
61 | class Params |
62 | { |
63 | public: |
64 | void reset(); |
65 | |
66 | int realNumberPrecision; |
67 | int integerBase; |
68 | int fieldWidth; |
69 | QChar padChar; |
70 | QTextStream::FieldAlignment fieldAlignment; |
71 | QTextStream::RealNumberNotation realNumberNotation; |
72 | QTextStream::NumberFlags numberFlags; |
73 | }; |
74 | |
75 | QTextStreamPrivate(QTextStream *q_ptr); |
76 | ~QTextStreamPrivate(); |
77 | void reset(); |
78 | |
79 | // device |
80 | QIODevice *device; |
81 | #ifndef QT_NO_QOBJECT |
82 | QDeviceClosedNotifier deviceClosedNotifier; |
83 | #endif |
84 | |
85 | // string |
86 | QString *string; |
87 | int stringOffset; |
88 | QIODevice::OpenMode stringOpenMode; |
89 | |
90 | QStringConverter::Encoding encoding = QStringConverter::Utf8; |
91 | QStringEncoder fromUtf16; |
92 | QStringDecoder toUtf16; |
93 | QStringDecoder savedToUtf16; |
94 | |
95 | QString writeBuffer; |
96 | QString readBuffer; |
97 | int readBufferOffset; |
98 | int readConverterSavedStateOffset; //the offset between readBufferStartDevicePos and that start of the buffer |
99 | qint64 readBufferStartDevicePos; |
100 | |
101 | Params params; |
102 | |
103 | // status |
104 | QTextStream::Status status; |
105 | QLocale locale; |
106 | QTextStream *q_ptr; |
107 | |
108 | int lastTokenSize; |
109 | bool deleteDevice; |
110 | bool autoDetectUnicode; |
111 | bool hasWrittenData = false; |
112 | bool generateBOM = false; |
113 | |
114 | // i/o |
115 | enum TokenDelimiter { |
116 | Space, |
117 | NotSpace, |
118 | EndOfLine |
119 | }; |
120 | |
121 | QString read(int maxlen); |
122 | bool scan(const QChar **ptr, int *tokenLength, |
123 | int maxlen, TokenDelimiter delimiter); |
124 | inline const QChar *readPtr() const; |
125 | inline void consumeLastToken(); |
126 | inline void consume(int nchars); |
127 | void saveConverterState(qint64 newPos); |
128 | void restoreToSavedConverterState(); |
129 | |
130 | // Return value type for getNumber() |
131 | enum NumberParsingStatus { |
132 | npsOk, |
133 | npsMissingDigit, |
134 | npsInvalidPrefix |
135 | }; |
136 | |
137 | inline bool getChar(QChar *ch); |
138 | inline void ungetChar(QChar ch); |
139 | NumberParsingStatus getNumber(qulonglong *l); |
140 | bool getReal(double *f); |
141 | |
142 | inline void write(QStringView data) { write(data: data.begin(), len: data.size()); } |
143 | inline void write(QChar ch); |
144 | void write(const QChar *data, qsizetype len); |
145 | void write(QLatin1StringView data); |
146 | void writePadding(qsizetype len); |
147 | inline void putString(QStringView string, bool number = false) |
148 | { |
149 | putString(data: string.constData(), len: string.size(), number); |
150 | } |
151 | void putString(const QChar *data, qsizetype len, bool number = false); |
152 | void putString(QLatin1StringView data, bool number = false); |
153 | void putString(QUtf8StringView data, bool number = false); |
154 | inline void putChar(QChar ch); |
155 | void putNumber(qulonglong number, bool negative); |
156 | |
157 | struct PaddingResult { |
158 | int left, right; |
159 | }; |
160 | PaddingResult padding(qsizetype len) const; |
161 | |
162 | // buffers |
163 | bool fillReadBuffer(qint64 maxBytes = -1); |
164 | void resetReadBuffer(); |
165 | void flushWriteBuffer(); |
166 | }; |
167 | |
168 | QT_END_NAMESPACE |
169 | |
170 | #endif // QTEXTSTREAM_P_H |
171 | |