1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the Qt 3 compatibility classes of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "q3membuf_p.h"
31
// *******************************************************************
32
// QMembuf declaration and implementation
33
// *******************************************************************
36
This class implements an efficient buffering of data that is often used by
37
asynchronous IO classes like QSocket, QHttp and QProcess.
40
Q3Membuf::Q3Membuf() : _size(0), _index(0)
46
while (!buf.isEmpty())
47
delete buf.takeFirst();
51
This function consumes \a nbytes bytes of data from the
52
buffer and copies it into \a sink. If \a sink is a 0 pointer
53
the data goes into the nirvana.
55
bool Q3Membuf::consumeBytes(Q_ULONG nbytes, char *sink)
57
if (nbytes <= 0 || (qint64)nbytes > _size)
60
while (!buf.isEmpty()) {
61
QByteArray *a = buf.first();
62
if ((int)(_index + nbytes) >= a->size()) {
63
// Here we skip the whole byte array and get the next later
64
int len = a->size() - _index;
66
memcpy(sink, a->constData()+_index, len);
76
// Here we skip only a part of the first byte array
78
memcpy(sink, a->constData()+_index, nbytes);
87
Scans for any occurrence of '\n' in the buffer. If \a store
88
is not 0 the text up to the first '\n' (or terminating 0) is
89
written to \a store, and a terminating 0 is appended to \a store
90
if necessary. Returns true if a '\n' was found; otherwise returns
93
bool Q3Membuf::scanNewline(QByteArray *store)
97
int i = 0; // index into 'store'
102
for (int j = 0; j < buf.size(); ++j) {
113
*(store->data()+i) = *p;
114
if (++i == (int)store->size())
115
store->resize(store->size() < 256
116
? 1024 : store->size()*4);
137
int Q3Membuf::ungetch(int ch)
139
if (buf.isEmpty() || _index==0) {
140
// we need a new QByteArray
141
QByteArray *ba = new QByteArray;
147
// we can reuse a place in the buffer
148
QByteArray *ba = buf.first();
151
(*ba)[(int)_index] = ch;