| 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 | // Qt-Security score:significant reason:header-decls-only |
| 5 | |
| 6 | #ifndef QTEXTSTREAM_P_H |
| 7 | #define QTEXTSTREAM_P_H |
| 8 | |
| 9 | // |
| 10 | // W A R N I N G |
| 11 | // ------------- |
| 12 | // |
| 13 | // This file is not part of the Qt API. It exists purely as an |
| 14 | // implementation detail. This header file may change from version to |
| 15 | // version without notice, or even be removed. |
| 16 | // |
| 17 | // We mean it. |
| 18 | // |
| 19 | |
| 20 | #include <QtCore/private/qglobal_p.h> |
| 21 | #include <QtCore/qstringconverter.h> |
| 22 | #include <QtCore/qiodevice.h> |
| 23 | #include <QtCore/qlocale.h> |
| 24 | #include "qtextstream.h" |
| 25 | |
| 26 | QT_BEGIN_NAMESPACE |
| 27 | |
| 28 | class QTextStreamPrivate |
| 29 | { |
| 30 | Q_DECLARE_PUBLIC(QTextStream) |
| 31 | public: |
| 32 | // streaming parameters |
| 33 | class Params |
| 34 | { |
| 35 | public: |
| 36 | void reset(); |
| 37 | |
| 38 | // As for QString, QLocale functions taking these: the values of |
| 39 | // precision, base and width can't sensibly need even eight bits, so |
| 40 | // there's no sense expanding beyond int. |
| 41 | int realNumberPrecision; |
| 42 | int integerBase; |
| 43 | int fieldWidth; |
| 44 | QChar padChar; |
| 45 | QTextStream::FieldAlignment fieldAlignment; |
| 46 | QTextStream::RealNumberNotation realNumberNotation; |
| 47 | QTextStream::NumberFlags numberFlags; |
| 48 | }; |
| 49 | |
| 50 | QTextStreamPrivate(QTextStream *q_ptr); |
| 51 | ~QTextStreamPrivate(); |
| 52 | void reset(); |
| 53 | |
| 54 | inline void setupDevice(QIODevice *device); |
| 55 | inline void disconnectFromDevice(); |
| 56 | |
| 57 | // device |
| 58 | QIODevice *device; |
| 59 | #ifndef QT_NO_QOBJECT |
| 60 | QMetaObject::Connection aboutToCloseConnection; |
| 61 | #endif |
| 62 | |
| 63 | // string |
| 64 | QString *string; |
| 65 | qsizetype stringOffset; |
| 66 | QIODevice::OpenMode stringOpenMode; |
| 67 | |
| 68 | QStringConverter::Encoding encoding = QStringConverter::Utf8; |
| 69 | QStringEncoder fromUtf16; |
| 70 | QStringDecoder toUtf16; |
| 71 | QStringDecoder savedToUtf16; |
| 72 | |
| 73 | QString writeBuffer; |
| 74 | QString readBuffer; |
| 75 | qsizetype readBufferOffset; |
| 76 | qsizetype readConverterSavedStateOffset; //the offset between readBufferStartDevicePos and that start of the buffer |
| 77 | qint64 readBufferStartDevicePos; |
| 78 | |
| 79 | Params params; |
| 80 | |
| 81 | // status |
| 82 | QTextStream::Status status; |
| 83 | QLocale locale; |
| 84 | QTextStream *q_ptr; |
| 85 | |
| 86 | qsizetype lastTokenSize; |
| 87 | bool deleteDevice; |
| 88 | bool autoDetectUnicode; |
| 89 | bool hasWrittenData = false; |
| 90 | bool generateBOM = false; |
| 91 | |
| 92 | // i/o |
| 93 | enum TokenDelimiter { |
| 94 | Space, |
| 95 | NotSpace, |
| 96 | EndOfLine |
| 97 | }; |
| 98 | |
| 99 | QString read(qsizetype maxlen); |
| 100 | bool scan(const QChar **ptr, qsizetype *tokenLength, |
| 101 | qsizetype maxlen, TokenDelimiter delimiter); |
| 102 | inline const QChar *readPtr() const; |
| 103 | inline void consumeLastToken(); |
| 104 | inline void consume(qsizetype nchars); |
| 105 | void saveConverterState(qint64 newPos); |
| 106 | void restoreToSavedConverterState(); |
| 107 | |
| 108 | // Return value type for getNumber() |
| 109 | enum NumberParsingStatus { |
| 110 | npsOk, |
| 111 | npsMissingDigit, |
| 112 | npsInvalidPrefix |
| 113 | }; |
| 114 | |
| 115 | inline bool getChar(QChar *ch); |
| 116 | inline void ungetChar(QChar ch); |
| 117 | NumberParsingStatus getNumber(qulonglong *l); |
| 118 | bool getReal(double *f); |
| 119 | |
| 120 | void write(QStringView data); |
| 121 | void write(QChar ch); |
| 122 | void write(QLatin1StringView data); |
| 123 | void writePadding(qsizetype len); |
| 124 | |
| 125 | enum class PutStringMode : bool { String, Number }; |
| 126 | void putString(QStringView string, PutStringMode = PutStringMode::String); |
| 127 | void putString(QLatin1StringView data, PutStringMode = PutStringMode::String); |
| 128 | void putString(QUtf8StringView data, PutStringMode = PutStringMode::String); |
| 129 | |
| 130 | inline void putChar(QChar ch); |
| 131 | void putNumber(qulonglong number, bool negative); |
| 132 | |
| 133 | struct PaddingResult { |
| 134 | qsizetype left, right; |
| 135 | }; |
| 136 | PaddingResult padding(qsizetype len) const; |
| 137 | |
| 138 | // buffers |
| 139 | bool fillReadBuffer(qint64 maxBytes = -1); |
| 140 | void resetReadBuffer(); |
| 141 | void flushWriteBuffer(); |
| 142 | |
| 143 | private: |
| 144 | template <typename Appendable> |
| 145 | void writeImpl(Appendable data); |
| 146 | template <typename StringView> |
| 147 | void putStringImpl(StringView view, PutStringMode mode); |
| 148 | }; |
| 149 | |
| 150 | QT_END_NAMESPACE |
| 151 | |
| 152 | #endif // QTEXTSTREAM_P_H |
| 153 | |