Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qiodevice_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:critical reason:network-protocol
4
5#ifndef QIODEVICE_P_H
6#define QIODEVICE_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 for the convenience
13// of QIODevice. 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/qbytearray.h"
20#include "QtCore/qiodevice.h"
21#include "QtCore/qobjectdefs.h"
22#include "QtCore/qstring.h"
23#include "QtCore/qvarlengtharray.h"
24#include "private/qringbuffer_p.h"
25#ifndef QT_NO_QOBJECT
26#include "private/qobject_p.h"
27#else
28static constexpr int QObjectPrivateVersion = QT_VERSION;
29#endif
30
32
33#ifndef QIODEVICE_BUFFERSIZE
34#define QIODEVICE_BUFFERSIZE 16384
35#endif
36
37Q_CORE_EXPORT int qt_subtract_from_timeout(int timeout, int elapsed);
38
39class Q_CORE_EXPORT QIODevicePrivate
40#ifndef QT_NO_QOBJECT
41 : public QObjectPrivate
42#endif
43{
44 Q_DECLARE_PUBLIC(QIODevice)
45 Q_DISABLE_COPY_MOVE(QIODevicePrivate)
46
47public:
48 QIODevicePrivate(decltype(QObjectPrivateVersion) version = QObjectPrivateVersion);
49 virtual ~QIODevicePrivate();
50
51 enum class ReadLineOption {
52 NotNullTerminated,
53 NullTerminated,
54 };
55 Q_DECLARE_FLAGS(ReadLineOptions, ReadLineOption)
56
57 // The size of this class is a subject of the library hook data.
58 // When adding a new member, do not make gaps and be aware
59 // about the padding. Accordingly, adjust offsets in
60 // tests/auto/other/toolsupport and bump the TypeInformationVersion
61 // field in src/corelib/global/qhooks.cpp, to notify the developers.
62 qint64 pos = 0;
63 qint64 devicePos = 0;
64 qint64 transactionPos = 0;
65
66 class QRingBufferRef
67 {
68 QRingBuffer *m_buf;
69 inline QRingBufferRef() : m_buf(nullptr) { }
70 friend class QIODevicePrivate;
71 public:
72 // wrap functions from QRingBuffer
73 inline void setChunkSize(int size) { Q_ASSERT(m_buf); m_buf->setChunkSize(size); }
74 inline int chunkSize() const { Q_ASSERT(m_buf); return m_buf->chunkSize(); }
75 inline qint64 nextDataBlockSize() const { return (m_buf ? m_buf->nextDataBlockSize() : Q_INT64_C(0)); }
76 inline const char *readPointer() const { return (m_buf ? m_buf->readPointer() : nullptr); }
77 inline const char *readPointerAtPosition(qint64 pos, qint64 &length) const { Q_ASSERT(m_buf); return m_buf->readPointerAtPosition(pos, length); }
78 inline void free(qint64 bytes) { Q_ASSERT(m_buf); m_buf->free(bytes); }
79 inline char *reserve(qint64 bytes) { Q_ASSERT(m_buf); return m_buf->reserve(bytes); }
80 inline char *reserveFront(qint64 bytes) { Q_ASSERT(m_buf); return m_buf->reserveFront(bytes); }
81 inline void truncate(qint64 pos) { Q_ASSERT(m_buf); m_buf->truncate(pos); }
82 inline void chop(qint64 bytes) { Q_ASSERT(m_buf); m_buf->chop(bytes); }
83 inline bool isEmpty() const { return !m_buf || m_buf->isEmpty(); }
84 inline int getChar() { return (m_buf ? m_buf->getChar() : -1); }
85 inline void putChar(char c) { Q_ASSERT(m_buf); m_buf->putChar(c); }
86 inline void ungetChar(char c) { Q_ASSERT(m_buf); m_buf->ungetChar(c); }
87 inline qint64 size() const { return (m_buf ? m_buf->size() : Q_INT64_C(0)); }
88 inline void clear() { if (m_buf) m_buf->clear(); }
89 inline qint64 indexOf(char c) const { return (m_buf ? m_buf->indexOf(c, m_buf->size()) : Q_INT64_C(-1)); }
90 inline qint64 indexOf(char c, qint64 maxLength, qint64 pos = 0) const { return (m_buf ? m_buf->indexOf(c, maxLength, pos) : Q_INT64_C(-1)); }
91 inline qint64 read(char *data, qint64 maxLength) { return (m_buf ? m_buf->read(data, maxLength) : Q_INT64_C(0)); }
92 inline QByteArray read() { return (m_buf ? m_buf->read() : QByteArray()); }
93 inline qint64 peek(char *data, qint64 maxLength, qint64 pos = 0) const { return (m_buf ? m_buf->peek(data, maxLength, pos) : Q_INT64_C(0)); }
94 inline void append(const char *data, qint64 size) { Q_ASSERT(m_buf); m_buf->append(data, size); }
95 inline void append(const QByteArray &qba) { Q_ASSERT(m_buf); m_buf->append(qba); }
96 inline qint64 skip(qint64 length) { return (m_buf ? m_buf->skip(length) : Q_INT64_C(0)); }
97 qint64 readLine(char *data, qint64 maxLength,
98 ReadLineOptions option = ReadLineOption::NullTerminated)
99 {
100 const auto appendNullByte = option & ReadLineOption::NullTerminated;
101 return !m_buf ? Q_INT64_C(-1) :
102 appendNullByte ? m_buf->readLine(data, maxLength) :
103 m_buf->readLineWithoutTerminatingNull(data, maxLength);
104 }
105 inline bool canReadLine() const { return m_buf && m_buf->canReadLine(); }
106 };
107
108 QRingBufferRef buffer;
109 QRingBufferRef writeBuffer;
110 const QByteArray *currentWriteChunk = nullptr;
111 int readChannelCount = 0;
112 int writeChannelCount = 0;
113 int currentReadChannel = 0;
114 int currentWriteChannel = 0;
115 int readBufferChunkSize = QIODEVICE_BUFFERSIZE;
116 int writeBufferChunkSize = 0;
117
118 QVarLengthArray<QRingBuffer, 2> readBuffers;
119 QVarLengthArray<QRingBuffer, 1> writeBuffers;
120 QString errorString;
121 QIODevice::OpenMode openMode = QIODevice::NotOpen;
122
123 bool transactionStarted = false;
124 bool baseReadLineDataCalled = false;
125
126 virtual bool putCharHelper(char c);
127
128 enum AccessMode : quint8 {
129 Unset,
130 Sequential,
131 RandomAccess
132 };
133 mutable AccessMode accessMode = Unset;
134 inline bool isSequential() const
135 {
136 if (accessMode == Unset)
137 accessMode = q_func()->isSequential() ? Sequential : RandomAccess;
138 return accessMode == Sequential;
139 }
140
141 inline bool isBufferEmpty() const
142 {
143 return buffer.isEmpty() || (transactionStarted && isSequential()
144 && transactionPos == buffer.size());
145 }
146 bool allWriteBuffersEmpty() const;
147
148 void seekBuffer(qint64 newPos);
149
150 inline void setCurrentReadChannel(int channel)
151 {
152 buffer.m_buf = (channel < readBuffers.size() ? &readBuffers[channel] : nullptr);
153 currentReadChannel = channel;
154 }
155 inline void setCurrentWriteChannel(int channel)
156 {
157 writeBuffer.m_buf = (channel < writeBuffers.size() ? &writeBuffers[channel] : nullptr);
158 currentWriteChannel = channel;
159 }
160 void setReadChannelCount(int count);
161 void setWriteChannelCount(int count);
162
163 qint64 read(char *data, qint64 maxSize, bool peeking = false);
164 qint64 readLine(char *data, qint64 maxSize,
165 ReadLineOption option = ReadLineOption::NullTerminated);
166
167 virtual qint64 peek(char *data, qint64 maxSize);
168 virtual QByteArray peek(qint64 maxSize);
169 qint64 skipByReading(qint64 maxSize);
170 qint64 skipLine();
171 void write(const char *data, qint64 size);
172
173 inline bool isWriteChunkCached(const char *data, qint64 size) const
174 {
175 return currentWriteChunk != nullptr
176 && currentWriteChunk->constData() == data
177 && currentWriteChunk->size() == size;
178 }
179
180#ifdef QT_NO_QOBJECT
181 QIODevice *q_ptr = nullptr;
182#endif
183};
184
185Q_DECLARE_OPERATORS_FOR_FLAGS(QIODevicePrivate::ReadLineOptions)
186
187QT_END_NAMESPACE
188
189#endif // QIODEVICE_P_H
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2568
int qt_subtract_from_timeout(int timeout, int elapsed)
#define CHECK_WRITABLE(function, returnType)
static Q_DECL_COLD_FUNCTION void checkWarnMessage(const QIODevice *device, const char *function, const char *what)
Definition qiodevice.cpp:49
#define CHECK_MAXLEN(function, returnType)
Definition qiodevice.cpp:74
#define CHECK_LINEMAXLEN_1(function, returnType)
Definition qiodevice.cpp:90
static void debugBinaryString(const char *input, qint64 maxlen)
Definition qiodevice.cpp:23
QDebug operator<<(QDebug debug, QIODevice::OpenMode modes)
#define CHECK_MAXBYTEARRAYSIZE(function)
Definition qiodevice.cpp:98
#define Q_VOID
Definition qiodevice.cpp:46
#define CHECK_READABLE(function, returnType)
#define CHECK_LINEMAXLEN(function, returnType)
Definition qiodevice.cpp:82
#define QIODEVICE_BUFFERSIZE
Definition qiodevice_p.h:34