1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the QtCore module of the Qt Toolkit.
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 Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
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 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
49
// This file is not part of the Qt API. It exists for the convenience
50
// of QIODevice. This header file may change from version to
51
// version without notice, or even be removed.
56
#include "QtCore/qiodevice.h"
57
#include "QtCore/qbytearray.h"
58
#include "QtCore/qobjectdefs.h"
59
#include "QtCore/qstring.h"
60
#include "private/qringbuffer_p.h"
62
#include "private/qobject_p.h"
67
#ifndef QIODEVICE_BUFFERSIZE
68
#define QIODEVICE_BUFFERSIZE Q_INT64_C(16384)
71
// This is QIODevice's read buffer, optimized for read(), isEmpty() and getChar()
72
class QIODevicePrivateLinearBuffer
75
QIODevicePrivateLinearBuffer(int) : len(0), first(0), buf(0), capacity(0) {
77
~QIODevicePrivateLinearBuffer() {
90
bool isEmpty() const {
104
int ch = uchar(*first);
109
int read(char* target, int size) {
110
int r = qMin(size, len);
111
memcpy(target, first, r);
116
int peek(char* target, int size) {
117
int r = qMin(size, len);
118
memcpy(target, first, r);
121
char* reserve(int size) {
122
makeSpace(size + len, freeSpaceAtEnd);
123
char* writePtr = first + len;
127
void chop(int size) {
134
QByteArray readAll() {
135
QByteArray retVal(first, len);
139
int readLine(char* target, int size) {
140
int r = qMin(size, len);
141
char* eol = static_cast<char*>(memchr(first, '\n', r));
144
memcpy(target, first, r);
149
bool canReadLine() const {
150
return memchr(first, '\n', len);
152
void ungetChar(char c) {
154
// underflow, the existing valid data needs to move to the end of the (potentially bigger) buffer
155
makeSpace(len+1, freeSpaceAtStart);
161
void ungetBlock(const char* block, int size) {
162
if ((first - buf) < size) {
163
// underflow, the existing valid data needs to move to the end of the (potentially bigger) buffer
164
makeSpace(len + size, freeSpaceAtStart);
168
memcpy(first, block, size);
172
enum FreeSpacePos {freeSpaceAtStart, freeSpaceAtEnd};
173
void makeSpace(size_t required, FreeSpacePos where) {
174
size_t newCapacity = qMax(capacity, size_t(QIODEVICE_BUFFERSIZE));
175
while (newCapacity < required)
177
const size_t moveOffset = (where == freeSpaceAtEnd) ? 0 : newCapacity - size_t(len);
178
if (newCapacity > capacity) {
179
// allocate more space
180
char* newBuf = new char[newCapacity];
181
memmove(newBuf + moveOffset, first, len);
184
capacity = newCapacity;
186
// shift any existing data to make space
187
memmove(buf + moveOffset, first, len);
189
first = buf + moveOffset;
193
// length of the unread data
195
// start of the unread data
197
// the allocated buffer
199
// allocated buffer size
203
class Q_CORE_EXPORT QIODevicePrivate
204
#ifndef QT_NO_QOBJECT
205
: public QObjectPrivate
208
Q_DECLARE_PUBLIC(QIODevice)
212
virtual ~QIODevicePrivate();
214
QIODevice::OpenMode openMode;
217
QIODevicePrivateLinearBuffer buffer;
220
// these three are for fast position updates during read, avoiding isSequential test
224
bool baseReadLineDataCalled;
227
virtual bool putCharHelper(char c);
234
mutable AccessMode accessMode;
235
inline bool isSequential() const
237
if (accessMode == Unset)
238
accessMode = q_func()->isSequential() ? Sequential : RandomAccess;
239
return accessMode == Sequential;
242
virtual qint64 peek(char *data, qint64 maxSize);
243
virtual QByteArray peek(qint64 maxSize);
252
#endif // QIODEVICE_P_H