| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Copyright (C) 2016 Intel Corporation. |
| 5 | ** Contact: https://www.qt.io/licensing/ |
| 6 | ** |
| 7 | ** This file is part of the QtCore module of the Qt Toolkit. |
| 8 | ** |
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 10 | ** Commercial License Usage |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 12 | ** accordance with the commercial license agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. |
| 17 | ** |
| 18 | ** GNU Lesser General Public License Usage |
| 19 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 20 | ** General Public License version 3 as published by the Free Software |
| 21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 22 | ** packaging of this file. Please review the following information to |
| 23 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 25 | ** |
| 26 | ** GNU General Public License Usage |
| 27 | ** Alternatively, this file may be used under the terms of the GNU |
| 28 | ** General Public License version 2.0 or (at your option) the GNU General |
| 29 | ** Public license version 3 or any later version approved by the KDE Free |
| 30 | ** Qt Foundation. The licenses are as published by the Free Software |
| 31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 32 | ** included in the packaging of this file. Please review the following |
| 33 | ** information to ensure the GNU General Public License requirements will |
| 34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 35 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 36 | ** |
| 37 | ** $QT_END_LICENSE$ |
| 38 | ** |
| 39 | ****************************************************************************/ |
| 40 | |
| 41 | #ifndef QTEXTSTREAM_P_H |
| 42 | #define QTEXTSTREAM_P_H |
| 43 | |
| 44 | // |
| 45 | // W A R N I N G |
| 46 | // ------------- |
| 47 | // |
| 48 | // This file is not part of the Qt API. It exists purely as an |
| 49 | // implementation detail. This header file may change from version to |
| 50 | // version without notice, or even be removed. |
| 51 | // |
| 52 | // We mean it. |
| 53 | // |
| 54 | |
| 55 | #include <QtCore/private/qglobal_p.h> |
| 56 | #include "qiodevice.h" |
| 57 | #include "qlocale.h" |
| 58 | #include "qtextstream.h" |
| 59 | |
| 60 | QT_BEGIN_NAMESPACE |
| 61 | |
| 62 | #ifndef QT_NO_QOBJECT |
| 63 | class QDeviceClosedNotifier : public QObject |
| 64 | { |
| 65 | Q_OBJECT |
| 66 | public: |
| 67 | inline QDeviceClosedNotifier() |
| 68 | { } |
| 69 | |
| 70 | inline void setupDevice(QTextStream *stream, QIODevice *device) |
| 71 | { |
| 72 | disconnect(); |
| 73 | if (device) |
| 74 | connect(device, SIGNAL(aboutToClose()), this, SLOT(flushStream())); |
| 75 | this->stream = stream; |
| 76 | } |
| 77 | |
| 78 | public Q_SLOTS: |
| 79 | inline void flushStream() { stream->flush(); } |
| 80 | |
| 81 | private: |
| 82 | QTextStream *stream; |
| 83 | }; |
| 84 | #endif |
| 85 | |
| 86 | class QTextStreamPrivate |
| 87 | { |
| 88 | Q_DECLARE_PUBLIC(QTextStream) |
| 89 | public: |
| 90 | // streaming parameters |
| 91 | class Params |
| 92 | { |
| 93 | public: |
| 94 | void reset(); |
| 95 | |
| 96 | int realNumberPrecision; |
| 97 | int integerBase; |
| 98 | int fieldWidth; |
| 99 | QChar padChar; |
| 100 | QTextStream::FieldAlignment fieldAlignment; |
| 101 | QTextStream::RealNumberNotation realNumberNotation; |
| 102 | QTextStream::NumberFlags numberFlags; |
| 103 | }; |
| 104 | |
| 105 | QTextStreamPrivate(QTextStream *q_ptr); |
| 106 | ~QTextStreamPrivate(); |
| 107 | void reset(); |
| 108 | |
| 109 | // device |
| 110 | QIODevice *device; |
| 111 | #ifndef QT_NO_QOBJECT |
| 112 | QDeviceClosedNotifier deviceClosedNotifier; |
| 113 | #endif |
| 114 | |
| 115 | // string |
| 116 | QString *string; |
| 117 | int stringOffset; |
| 118 | QIODevice::OpenMode stringOpenMode; |
| 119 | |
| 120 | QStringConverter::Encoding encoding = QStringConverter::Utf8; |
| 121 | QStringEncoder fromUtf16; |
| 122 | QStringDecoder toUtf16; |
| 123 | QStringDecoder savedToUtf16; |
| 124 | |
| 125 | QString writeBuffer; |
| 126 | QString readBuffer; |
| 127 | int readBufferOffset; |
| 128 | int readConverterSavedStateOffset; //the offset between readBufferStartDevicePos and that start of the buffer |
| 129 | qint64 readBufferStartDevicePos; |
| 130 | |
| 131 | Params params; |
| 132 | |
| 133 | // status |
| 134 | QTextStream::Status status; |
| 135 | QLocale locale; |
| 136 | QTextStream *q_ptr; |
| 137 | |
| 138 | int lastTokenSize; |
| 139 | bool deleteDevice; |
| 140 | bool autoDetectUnicode; |
| 141 | bool hasWrittenData = false; |
| 142 | bool generateBOM = false; |
| 143 | |
| 144 | // i/o |
| 145 | enum TokenDelimiter { |
| 146 | Space, |
| 147 | NotSpace, |
| 148 | EndOfLine |
| 149 | }; |
| 150 | |
| 151 | QString read(int maxlen); |
| 152 | bool scan(const QChar **ptr, int *tokenLength, |
| 153 | int maxlen, TokenDelimiter delimiter); |
| 154 | inline const QChar *readPtr() const; |
| 155 | inline void consumeLastToken(); |
| 156 | inline void consume(int nchars); |
| 157 | void saveConverterState(qint64 newPos); |
| 158 | void restoreToSavedConverterState(); |
| 159 | |
| 160 | // Return value type for getNumber() |
| 161 | enum NumberParsingStatus { |
| 162 | npsOk, |
| 163 | npsMissingDigit, |
| 164 | npsInvalidPrefix |
| 165 | }; |
| 166 | |
| 167 | inline bool getChar(QChar *ch); |
| 168 | inline void ungetChar(QChar ch); |
| 169 | NumberParsingStatus getNumber(qulonglong *l); |
| 170 | bool getReal(double *f); |
| 171 | |
| 172 | inline void write(const QString &data) { write(data.begin(), data.length()); } |
| 173 | inline void write(QChar ch); |
| 174 | void write(const QChar *data, int len); |
| 175 | void write(QLatin1String data); |
| 176 | void writePadding(int len); |
| 177 | inline void putString(const QString &ch, bool number = false) { putString(ch.constData(), ch.length(), number); } |
| 178 | void putString(const QChar *data, int len, bool number = false); |
| 179 | void putString(QLatin1String data, bool number = false); |
| 180 | void putString(QUtf8StringView data, bool number = false); |
| 181 | inline void putChar(QChar ch); |
| 182 | void putNumber(qulonglong number, bool negative); |
| 183 | |
| 184 | struct PaddingResult { |
| 185 | int left, right; |
| 186 | }; |
| 187 | PaddingResult padding(int len) const; |
| 188 | |
| 189 | // buffers |
| 190 | bool fillReadBuffer(qint64 maxBytes = -1); |
| 191 | void resetReadBuffer(); |
| 192 | void flushWriteBuffer(); |
| 193 | }; |
| 194 | |
| 195 | QT_END_NAMESPACE |
| 196 | |
| 197 | #endif // QTEXTSTREAM_P_H |
| 198 | |