1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtCore module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QIODEVICE_P_H |
41 | #define QIODEVICE_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists for the convenience |
48 | // of QIODevice. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include "QtCore/qiodevice.h" |
55 | #include "QtCore/qbytearray.h" |
56 | #include "QtCore/qobjectdefs.h" |
57 | #include "QtCore/qstring.h" |
58 | #include "private/qringbuffer_p.h" |
59 | #include "QtCore/qvector.h" |
60 | #ifndef QT_NO_QOBJECT |
61 | #include "private/qobject_p.h" |
62 | #endif |
63 | |
64 | QT_BEGIN_NAMESPACE |
65 | |
66 | #ifndef QIODEVICE_BUFFERSIZE |
67 | #define QIODEVICE_BUFFERSIZE 16384 |
68 | #endif |
69 | |
70 | Q_CORE_EXPORT int qt_subtract_from_timeout(int timeout, int elapsed); |
71 | |
72 | class Q_CORE_EXPORT QIODevicePrivate |
73 | #ifndef QT_NO_QOBJECT |
74 | : public QObjectPrivate |
75 | #endif |
76 | { |
77 | Q_DECLARE_PUBLIC(QIODevice) |
78 | |
79 | public: |
80 | QIODevicePrivate(); |
81 | virtual ~QIODevicePrivate(); |
82 | |
83 | QIODevice::OpenMode openMode; |
84 | QString errorString; |
85 | |
86 | QVector<QRingBuffer> readBuffers; |
87 | QVector<QRingBuffer> writeBuffers; |
88 | |
89 | class QRingBufferRef { |
90 | QRingBuffer *m_buf; |
91 | inline QRingBufferRef() : m_buf(nullptr) { } |
92 | friend class QIODevicePrivate; |
93 | public: |
94 | // wrap functions from QRingBuffer |
95 | inline void setChunkSize(int size) { Q_ASSERT(m_buf); m_buf->setChunkSize(size); } |
96 | inline int chunkSize() const { Q_ASSERT(m_buf); return m_buf->chunkSize(); } |
97 | inline qint64 nextDataBlockSize() const { return (m_buf ? m_buf->nextDataBlockSize() : Q_INT64_C(0)); } |
98 | inline const char *readPointer() const { return (m_buf ? m_buf->readPointer() : nullptr); } |
99 | inline const char *readPointerAtPosition(qint64 pos, qint64 &length) const { Q_ASSERT(m_buf); return m_buf->readPointerAtPosition(pos, length); } |
100 | inline void free(qint64 bytes) { Q_ASSERT(m_buf); m_buf->free(bytes); } |
101 | inline char *reserve(qint64 bytes) { Q_ASSERT(m_buf); return m_buf->reserve(bytes); } |
102 | inline char *reserveFront(qint64 bytes) { Q_ASSERT(m_buf); return m_buf->reserveFront(bytes); } |
103 | inline void truncate(qint64 pos) { Q_ASSERT(m_buf); m_buf->truncate(pos); } |
104 | inline void chop(qint64 bytes) { Q_ASSERT(m_buf); m_buf->chop(bytes); } |
105 | inline bool isEmpty() const { return !m_buf || m_buf->isEmpty(); } |
106 | inline int getChar() { return (m_buf ? m_buf->getChar() : -1); } |
107 | inline void putChar(char c) { Q_ASSERT(m_buf); m_buf->putChar(c); } |
108 | inline void ungetChar(char c) { Q_ASSERT(m_buf); m_buf->ungetChar(c); } |
109 | inline qint64 size() const { return (m_buf ? m_buf->size() : Q_INT64_C(0)); } |
110 | inline void clear() { if (m_buf) m_buf->clear(); } |
111 | inline qint64 indexOf(char c) const { return (m_buf ? m_buf->indexOf(c, maxLength: m_buf->size()) : Q_INT64_C(-1)); } |
112 | inline qint64 indexOf(char c, qint64 maxLength, qint64 pos = 0) const { return (m_buf ? m_buf->indexOf(c, maxLength, pos) : Q_INT64_C(-1)); } |
113 | inline qint64 read(char *data, qint64 maxLength) { return (m_buf ? m_buf->read(data, maxLength) : Q_INT64_C(0)); } |
114 | inline QByteArray read() { return (m_buf ? m_buf->read() : QByteArray()); } |
115 | inline qint64 peek(char *data, qint64 maxLength, qint64 pos = 0) const { return (m_buf ? m_buf->peek(data, maxLength, pos) : Q_INT64_C(0)); } |
116 | inline void append(const char *data, qint64 size) { Q_ASSERT(m_buf); m_buf->append(data, size); } |
117 | inline void append(const QByteArray &qba) { Q_ASSERT(m_buf); m_buf->append(qba); } |
118 | inline qint64 skip(qint64 length) { return (m_buf ? m_buf->skip(length) : Q_INT64_C(0)); } |
119 | inline qint64 readLine(char *data, qint64 maxLength) { return (m_buf ? m_buf->readLine(data, maxLength) : Q_INT64_C(-1)); } |
120 | inline bool canReadLine() const { return m_buf && m_buf->canReadLine(); } |
121 | }; |
122 | |
123 | QRingBufferRef buffer; |
124 | QRingBufferRef writeBuffer; |
125 | qint64 pos; |
126 | qint64 devicePos; |
127 | int readChannelCount; |
128 | int writeChannelCount; |
129 | int currentReadChannel; |
130 | int currentWriteChannel; |
131 | int readBufferChunkSize; |
132 | int writeBufferChunkSize; |
133 | qint64 transactionPos; |
134 | bool transactionStarted; |
135 | bool baseReadLineDataCalled; |
136 | |
137 | virtual bool putCharHelper(char c); |
138 | |
139 | enum AccessMode { |
140 | Unset, |
141 | Sequential, |
142 | RandomAccess |
143 | }; |
144 | mutable AccessMode accessMode; |
145 | inline bool isSequential() const |
146 | { |
147 | if (accessMode == Unset) |
148 | accessMode = q_func()->isSequential() ? Sequential : RandomAccess; |
149 | return accessMode == Sequential; |
150 | } |
151 | |
152 | inline bool isBufferEmpty() const |
153 | { |
154 | return buffer.isEmpty() || (transactionStarted && isSequential() |
155 | && transactionPos == buffer.size()); |
156 | } |
157 | bool allWriteBuffersEmpty() const; |
158 | |
159 | void seekBuffer(qint64 newPos); |
160 | |
161 | inline void setCurrentReadChannel(int channel) |
162 | { |
163 | buffer.m_buf = (channel < readBuffers.size() ? &readBuffers[channel] : nullptr); |
164 | currentReadChannel = channel; |
165 | } |
166 | inline void setCurrentWriteChannel(int channel) |
167 | { |
168 | writeBuffer.m_buf = (channel < writeBuffers.size() ? &writeBuffers[channel] : nullptr); |
169 | currentWriteChannel = channel; |
170 | } |
171 | void setReadChannelCount(int count); |
172 | void setWriteChannelCount(int count); |
173 | |
174 | qint64 read(char *data, qint64 maxSize, bool peeking = false); |
175 | virtual qint64 peek(char *data, qint64 maxSize); |
176 | virtual QByteArray peek(qint64 maxSize); |
177 | qint64 skipByReading(qint64 maxSize); |
178 | // ### Qt6: consider replacing with a protected virtual QIODevice::skipData(). |
179 | virtual qint64 skip(qint64 maxSize); |
180 | |
181 | #ifdef QT_NO_QOBJECT |
182 | QIODevice *q_ptr; |
183 | #endif |
184 | }; |
185 | |
186 | QT_END_NAMESPACE |
187 | |
188 | #endif // QIODEVICE_P_H |
189 | |