author | eckhart.koppen@nokia.com |
Wed, 31 Mar 2010 11:06:36 +0300 | |
changeset 7 | f7bc934e204c |
parent 0 | 1918ee327afb |
child 23 | 89e065397ea6 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
7
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtCore module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#ifndef QBYTEDATA_H |
|
43 |
#define QBYTEDATA_H |
|
44 |
||
45 |
// |
|
46 |
// W A R N I N G |
|
47 |
// ------------- |
|
48 |
// |
|
49 |
// This file is not part of the Qt API. It exists purely as an |
|
50 |
// implementation detail. This header file may change from version to |
|
51 |
// version without notice, or even be removed. |
|
52 |
// |
|
53 |
// We mean it. |
|
54 |
// |
|
55 |
||
56 |
#include <qbytearray.h> |
|
57 |
||
58 |
QT_BEGIN_NAMESPACE |
|
59 |
||
60 |
// this class handles a list of QByteArrays. It is a variant of QRingBuffer |
|
61 |
// that avoid malloc/realloc/memcpy. |
|
62 |
class QByteDataBuffer |
|
63 |
{ |
|
64 |
private: |
|
65 |
QList<QByteArray> buffers; |
|
66 |
qint64 bufferCompleteSize; |
|
67 |
public: |
|
68 |
QByteDataBuffer() : bufferCompleteSize(0) |
|
69 |
{ |
|
70 |
} |
|
71 |
||
72 |
~QByteDataBuffer() |
|
73 |
{ |
|
74 |
clear(); |
|
75 |
} |
|
76 |
||
77 |
inline void append(QByteDataBuffer& other) |
|
78 |
{ |
|
79 |
if (other.isEmpty()) |
|
80 |
return; |
|
81 |
||
82 |
buffers.append(other.buffers); |
|
83 |
bufferCompleteSize += other.byteAmount(); |
|
84 |
} |
|
85 |
||
86 |
||
87 |
inline void append(QByteArray& bd) |
|
88 |
{ |
|
89 |
if (bd.isEmpty()) |
|
90 |
return; |
|
91 |
||
92 |
buffers.append(bd); |
|
93 |
bufferCompleteSize += bd.size(); |
|
94 |
} |
|
95 |
||
96 |
inline void prepend(QByteArray& bd) |
|
97 |
{ |
|
98 |
if (bd.isEmpty()) |
|
99 |
return; |
|
100 |
||
101 |
buffers.prepend(bd); |
|
102 |
bufferCompleteSize += bd.size(); |
|
103 |
} |
|
104 |
||
105 |
// return the first QByteData. User of this function has to qFree() its .data! |
|
106 |
// preferably use this function to read data. |
|
107 |
inline QByteArray read() |
|
108 |
{ |
|
109 |
bufferCompleteSize -= buffers.first().size(); |
|
110 |
return buffers.takeFirst(); |
|
111 |
} |
|
112 |
||
113 |
// return everything. User of this function has to qFree() its .data! |
|
114 |
// avoid to use this, it might malloc and memcpy. |
|
115 |
inline QByteArray readAll() |
|
116 |
{ |
|
117 |
return read(byteAmount()); |
|
118 |
} |
|
119 |
||
120 |
// return amount. User of this function has to qFree() its .data! |
|
121 |
// avoid to use this, it might malloc and memcpy. |
|
122 |
inline QByteArray read(qint64 amount) |
|
123 |
{ |
|
124 |
amount = qMin(byteAmount(), amount); |
|
125 |
QByteArray byteData; |
|
126 |
byteData.resize(amount); |
|
127 |
read(byteData.data(), byteData.size()); |
|
128 |
return byteData; |
|
129 |
} |
|
130 |
||
131 |
// return amount bytes. User of this function has to qFree() its .data! |
|
132 |
// avoid to use this, it will memcpy. |
|
133 |
qint64 read(char* dst, qint64 amount) |
|
134 |
{ |
|
135 |
amount = qMin(amount, byteAmount()); |
|
136 |
qint64 originalAmount = amount; |
|
137 |
char *writeDst = dst; |
|
138 |
||
139 |
while (amount > 0) { |
|
140 |
QByteArray first = buffers.takeFirst(); |
|
141 |
if (amount >= first.size()) { |
|
142 |
// take it completely |
|
143 |
bufferCompleteSize -= first.size(); |
|
144 |
amount -= first.size(); |
|
145 |
memcpy(writeDst, first.constData(), first.size()); |
|
146 |
writeDst += first.size(); |
|
147 |
first.clear(); |
|
148 |
} else { |
|
149 |
// take a part of it & it is the last one to take |
|
150 |
bufferCompleteSize -= amount; |
|
151 |
memcpy(writeDst, first.constData(), amount); |
|
152 |
||
153 |
qint64 newFirstSize = first.size() - amount; |
|
154 |
QByteArray newFirstData; |
|
155 |
newFirstData.resize(newFirstSize); |
|
156 |
memcpy(newFirstData.data(), first.constData() + amount, newFirstSize); |
|
157 |
buffers.prepend(newFirstData); |
|
158 |
||
159 |
amount = 0; |
|
160 |
first.clear(); |
|
161 |
} |
|
162 |
} |
|
163 |
||
164 |
return originalAmount; |
|
165 |
} |
|
166 |
||
167 |
inline char getChar() |
|
168 |
{ |
|
169 |
char c; |
|
170 |
read(&c, 1); |
|
171 |
return c; |
|
172 |
} |
|
173 |
||
174 |
inline void clear() |
|
175 |
{ |
|
176 |
buffers.clear(); |
|
177 |
bufferCompleteSize = 0; |
|
178 |
} |
|
179 |
||
180 |
// The byte count of all QByteArrays |
|
181 |
inline qint64 byteAmount() const |
|
182 |
{ |
|
183 |
return bufferCompleteSize; |
|
184 |
} |
|
185 |
||
186 |
// the number of QByteArrays |
|
187 |
inline qint64 bufferCount() const |
|
188 |
{ |
|
189 |
return buffers.length(); |
|
190 |
} |
|
191 |
||
192 |
inline bool isEmpty() const |
|
193 |
{ |
|
194 |
return byteAmount() == 0; |
|
195 |
} |
|
196 |
||
197 |
inline qint64 sizeNextBlock() const |
|
198 |
{ |
|
199 |
if(buffers.isEmpty()) |
|
200 |
return 0; |
|
201 |
else |
|
202 |
return buffers.first().size(); |
|
203 |
} |
|
204 |
||
205 |
inline QByteArray& operator[](int i) |
|
206 |
{ |
|
207 |
return buffers[i]; |
|
208 |
} |
|
209 |
}; |
|
210 |
||
211 |
QT_END_NAMESPACE |
|
212 |
||
213 |
#endif // QBYTEDATA_H |