~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/qt3support/other/q3membuf.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the Qt 3 compatibility classes of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "q3membuf_p.h"
 
30
 
 
31
// *******************************************************************
 
32
// QMembuf declaration and implementation
 
33
// *******************************************************************
 
34
 
 
35
/*  \internal
 
36
    This class implements an efficient buffering of data that is often used by
 
37
    asynchronous IO classes like QSocket, QHttp and QProcess.
 
38
*/
 
39
 
 
40
Q3Membuf::Q3Membuf() : _size(0), _index(0)
 
41
{
 
42
}
 
43
 
 
44
Q3Membuf::~Q3Membuf()
 
45
{
 
46
    while (!buf.isEmpty())
 
47
        delete buf.takeFirst();
 
48
}
 
49
 
 
50
/*! \internal
 
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.
 
54
*/
 
55
bool Q3Membuf::consumeBytes(Q_ULONG nbytes, char *sink)
 
56
{
 
57
    if (nbytes <= 0 || (qint64)nbytes > _size)
 
58
        return false;
 
59
    _size -= nbytes;
 
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;
 
65
            if (sink) {
 
66
                memcpy(sink, a->constData()+_index, len);
 
67
                sink += len;
 
68
            }
 
69
            nbytes -= len;
 
70
            buf.removeFirst();
 
71
            delete a;
 
72
            _index = 0;
 
73
            if (nbytes == 0)
 
74
                break;
 
75
        } else {
 
76
            // Here we skip only a part of the first byte array
 
77
            if (sink)
 
78
                memcpy(sink, a->constData()+_index, nbytes);
 
79
            _index += nbytes;
 
80
            break;
 
81
        }
 
82
    }
 
83
    return true;
 
84
}
 
85
 
 
86
/*! \internal
 
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
 
91
    false.
 
92
*/
 
93
bool Q3Membuf::scanNewline(QByteArray *store)
 
94
{
 
95
    if (_size == 0)
 
96
        return false;
 
97
    int i = 0; // index into 'store'
 
98
    QByteArray *a = 0;
 
99
    char *p;
 
100
    int n;
 
101
    bool retval = false;
 
102
    for (int j = 0; j < buf.size(); ++j) {
 
103
        a = buf.at(j);
 
104
        p = a->data();
 
105
        n = a->size();
 
106
        if (!j) {
 
107
            // first buffer
 
108
            p += _index;
 
109
            n -= _index;
 
110
        }
 
111
        if (store) {
 
112
            while (n-- > 0) {
 
113
                *(store->data()+i) = *p;
 
114
                if (++i == (int)store->size())
 
115
                    store->resize(store->size() < 256
 
116
                                   ? 1024 : store->size()*4);
 
117
                if (*p == '\n') {
 
118
                    retval = true;
 
119
                    goto end;
 
120
                }
 
121
                p++;
 
122
            }
 
123
        } else {
 
124
            while (n-- > 0) {
 
125
                if(*p == '\n')
 
126
                    return true;
 
127
                p++;
 
128
            }
 
129
        }
 
130
    }
 
131
 end:
 
132
    if (store)
 
133
        store->resize(i);
 
134
    return retval;
 
135
}
 
136
 
 
137
int Q3Membuf::ungetch(int ch)
 
138
{
 
139
    if (buf.isEmpty() || _index==0) {
 
140
        // we need a new QByteArray
 
141
        QByteArray *ba = new QByteArray;
 
142
        ba->resize(1);
 
143
        buf.prepend(ba);
 
144
        _size++;
 
145
        (*ba)[0] = ch;
 
146
    } else {
 
147
        // we can reuse a place in the buffer
 
148
        QByteArray *ba = buf.first();
 
149
        _index--;
 
150
        _size++;
 
151
        (*ba)[(int)_index] = ch;
 
152
    }
 
153
    return ch;
 
154
}