Qt 4.8
qbytedata_p.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
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.
16 **
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.
24 **
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.
28 **
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.
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 
59 
60 // this class handles a list of QByteArrays. It is a variant of QRingBuffer
61 // that avoid malloc/realloc/memcpy.
63 {
64 private:
67 public:
68  QByteDataBuffer() : bufferCompleteSize(0)
69  {
70  }
71 
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 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.
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  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 
219 
220 #endif // QBYTEDATA_H
QByteArray read()
Definition: qbytedata_p.h:107
unsigned char c[8]
Definition: qnumeric_p.h:62
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
void append(QByteDataBuffer &other)
Definition: qbytedata_p.h:77
QByteArray read(qint64 amount)
Definition: qbytedata_p.h:122
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
void append(const QByteArray &bd)
Definition: qbytedata_p.h:87
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
T takeFirst()
Removes the first item in the list and returns it.
Definition: qlist.h:489
void prepend(const T &t)
Inserts value at the beginning of the list.
Definition: qlist.h:541
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
void clear()
Removes all items from the list.
Definition: qlist.h:764
__int64 qint64
Definition: qglobal.h:942
bool isEmpty() const
Definition: qbytedata_p.h:192
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
int length() const
This function is identical to count().
Definition: qlist.h:281
void resize(int size)
Sets the size of the byte array to size bytes.
qint64 bufferCompleteSize
Definition: qbytedata_p.h:66
void prepend(QByteArray &bd)
Definition: qbytedata_p.h:96
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
qint64 read(char *dst, qint64 amount)
Definition: qbytedata_p.h:133
QByteArray & operator[](int i)
Definition: qbytedata_p.h:205
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
QList< QByteArray > buffers
Definition: qbytedata_p.h:65
QByteArray readAll()
Definition: qbytedata_p.h:115
qint64 bufferCount() const
Definition: qbytedata_p.h:187
bool canReadLine() const
Definition: qbytedata_p.h:210
qint64 byteAmount() const
Definition: qbytedata_p.h:181
void clear()
Clears the contents of the byte array and makes it empty.
QBool contains(char c) const
Returns true if the byte array contains the character ch; otherwise returns false.
Definition: qbytearray.h:525
qint64 sizeNextBlock() const
Definition: qbytedata_p.h:197