Merge remote-tracking branch 'origin/master' into api_changes
[profile/ivi/qtbase.git] / src / corelib / tools / qbytedata_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
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(const 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 free() 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 free() 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 free() 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 free() 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     inline bool canReadLine() const {
211         for (int i = 0; i < buffers.length(); i++)
212             if (buffers.at(i).contains('\n'))
213                 return true;
214         return false;
215     }
216 };
217
218 QT_END_NAMESPACE
219
220 #endif // QBYTEDATA_H