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/qbytearray.h"
55#include "QtCore/qiodevice.h"
56#include "QtCore/qlist.h"
57#include "QtCore/qobjectdefs.h"
58#include "QtCore/qstring.h"
59#include "private/qringbuffer_p.h"
60#ifndef QT_NO_QOBJECT
61#include "private/qobject_p.h"
62#endif
63
64QT_BEGIN_NAMESPACE
65
66#ifndef QIODEVICE_BUFFERSIZE
67#define QIODEVICE_BUFFERSIZE 16384
68#endif
69
70Q_CORE_EXPORT int qt_subtract_from_timeout(int timeout, int elapsed);
71
72class Q_CORE_EXPORT QIODevicePrivate
73#ifndef QT_NO_QOBJECT
74 : public QObjectPrivate
75#endif
76{
77 Q_DECLARE_PUBLIC(QIODevice)
78
79public:
80 QIODevicePrivate();
81 virtual ~QIODevicePrivate();
82
83 // The size of this class is a subject of the library hook data.
84 // When adding a new member, do not make gaps and be aware
85 // about the padding. Accordingly, adjust offsets in
86 // tests/auto/other/toolsupport and bump the TypeInformationVersion
87 // field in src/corelib/global/qhooks.cpp, to notify the developers.
88 qint64 pos = 0;
89 qint64 devicePos = 0;
90 qint64 transactionPos = 0;
91
92 class QRingBufferRef
93 {
94 QRingBuffer *m_buf;
95 inline QRingBufferRef() : m_buf(nullptr) { }
96 friend class QIODevicePrivate;
97 public:
98 // wrap functions from QRingBuffer
99 inline void setChunkSize(int size) { Q_ASSERT(m_buf); m_buf->setChunkSize(size); }
100 inline int chunkSize() const { Q_ASSERT(m_buf); return m_buf->chunkSize(); }
101 inline qint64 nextDataBlockSize() const { return (m_buf ? m_buf->nextDataBlockSize() : Q_INT64_C(0)); }
102 inline const char *readPointer() const { return (m_buf ? m_buf->readPointer() : nullptr); }
103 inline const char *readPointerAtPosition(qint64 pos, qint64 &length) const { Q_ASSERT(m_buf); return m_buf->readPointerAtPosition(pos, length); }
104 inline void free(qint64 bytes) { Q_ASSERT(m_buf); m_buf->free(bytes); }
105 inline char *reserve(qint64 bytes) { Q_ASSERT(m_buf); return m_buf->reserve(bytes); }
106 inline char *reserveFront(qint64 bytes) { Q_ASSERT(m_buf); return m_buf->reserveFront(bytes); }
107 inline void truncate(qint64 pos) { Q_ASSERT(m_buf); m_buf->truncate(pos); }
108 inline void chop(qint64 bytes) { Q_ASSERT(m_buf); m_buf->chop(bytes); }
109 inline bool isEmpty() const { return !m_buf || m_buf->isEmpty(); }
110 inline int getChar() { return (m_buf ? m_buf->getChar() : -1); }
111 inline void putChar(char c) { Q_ASSERT(m_buf); m_buf->putChar(c); }
112 inline void ungetChar(char c) { Q_ASSERT(m_buf); m_buf->ungetChar(c); }
113 inline qint64 size() const { return (m_buf ? m_buf->size() : Q_INT64_C(0)); }
114 inline void clear() { if (m_buf) m_buf->clear(); }
115 inline qint64 indexOf(char c) const { return (m_buf ? m_buf->indexOf(c, m_buf->size()) : Q_INT64_C(-1)); }
116 inline qint64 indexOf(char c, qint64 maxLength, qint64 pos = 0) const { return (m_buf ? m_buf->indexOf(c, maxLength, pos) : Q_INT64_C(-1)); }
117 inline qint64 read(char *data, qint64 maxLength) { return (m_buf ? m_buf->read(data, maxLength) : Q_INT64_C(0)); }
118 inline QByteArray read() { return (m_buf ? m_buf->read() : QByteArray()); }
119 inline qint64 peek(char *data, qint64 maxLength, qint64 pos = 0) const { return (m_buf ? m_buf->peek(data, maxLength, pos) : Q_INT64_C(0)); }
120 inline void append(const char *data, qint64 size) { Q_ASSERT(m_buf); m_buf->append(data, size); }
121 inline void append(const QByteArray &qba) { Q_ASSERT(m_buf); m_buf->append(qba); }
122 inline qint64 skip(qint64 length) { return (m_buf ? m_buf->skip(length) : Q_INT64_C(0)); }
123 inline qint64 readLine(char *data, qint64 maxLength) { return (m_buf ? m_buf->readLine(data, maxLength) : Q_INT64_C(-1)); }
124 inline bool canReadLine() const { return m_buf && m_buf->canReadLine(); }
125 };
126
127 QRingBufferRef buffer;
128 QRingBufferRef writeBuffer;
129 const QByteArray *currentWriteChunk = nullptr;
130 int readChannelCount = 0;
131 int writeChannelCount = 0;
132 int currentReadChannel = 0;
133 int currentWriteChannel = 0;
134 int readBufferChunkSize = QIODEVICE_BUFFERSIZE;
135 int writeBufferChunkSize = 0;
136
137 QList<QRingBuffer> readBuffers;
138 QList<QRingBuffer> writeBuffers;
139 QString errorString;
140 QIODevice::OpenMode openMode = QIODevice::NotOpen;
141
142 bool transactionStarted = false;
143 bool baseReadLineDataCalled = false;
144
145 virtual bool putCharHelper(char c);
146
147 enum AccessMode : quint8 {
148 Unset,
149 Sequential,
150 RandomAccess
151 };
152 mutable AccessMode accessMode = Unset;
153 inline bool isSequential() const
154 {
155 if (accessMode == Unset)
156 accessMode = q_func()->isSequential() ? Sequential : RandomAccess;
157 return accessMode == Sequential;
158 }
159
160 inline bool isBufferEmpty() const
161 {
162 return buffer.isEmpty() || (transactionStarted && isSequential()
163 && transactionPos == buffer.size());
164 }
165 bool allWriteBuffersEmpty() const;
166
167 void seekBuffer(qint64 newPos);
168
169 inline void setCurrentReadChannel(int channel)
170 {
171 buffer.m_buf = (channel < readBuffers.size() ? &readBuffers[channel] : nullptr);
172 currentReadChannel = channel;
173 }
174 inline void setCurrentWriteChannel(int channel)
175 {
176 writeBuffer.m_buf = (channel < writeBuffers.size() ? &writeBuffers[channel] : nullptr);
177 currentWriteChannel = channel;
178 }
179 void setReadChannelCount(int count);
180 void setWriteChannelCount(int count);
181
182 qint64 read(char *data, qint64 maxSize, bool peeking = false);
183 virtual qint64 peek(char *data, qint64 maxSize);
184 virtual QByteArray peek(qint64 maxSize);
185 qint64 skipByReading(qint64 maxSize);
186 void write(const char *data, qint64 size);
187
188#ifdef QT_NO_QOBJECT
189 QIODevice *q_ptr = nullptr;
190#endif
191};
192
193QT_END_NAMESPACE
194
195#endif // QIODEVICE_P_H
196