Qt 4.8
qiodevice_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 QIODEVICE_P_H
43 #define QIODEVICE_P_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 for the convenience
50 // of QIODevice. 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 "QtCore/qiodevice.h"
57 #include "QtCore/qbytearray.h"
58 #include "QtCore/qobjectdefs.h"
59 #include "QtCore/qstring.h"
60 #include "private/qringbuffer_p.h"
61 #ifndef QT_NO_QOBJECT
62 #include "private/qobject_p.h"
63 #endif
64 
66 
67 #ifndef QIODEVICE_BUFFERSIZE
68 #define QIODEVICE_BUFFERSIZE Q_INT64_C(16384)
69 #endif
70 
71 // This is QIODevice's read buffer, optimized for read(), isEmpty() and getChar()
73 {
74 public:
76  }
78  delete [] buf;
79  }
80  void clear() {
81  first = buf;
82  len = 0;
83  }
84  int size() const {
85  return len;
86  }
87  bool isEmpty() const {
88  return len == 0;
89  }
90  void skip(int n) {
91  if (n >= len) {
92  clear();
93  } else {
94  len -= n;
95  first += n;
96  }
97  }
98  int getChar() {
99  if (len == 0)
100  return -1;
101  int ch = uchar(*first);
102  len--;
103  first++;
104  return ch;
105  }
106  int read(char* target, int size) {
107  int r = qMin(size, len);
108  memcpy(target, first, r);
109  len -= r;
110  first += r;
111  return r;
112  }
113  int peek(char* target, int size) {
114  int r = qMin(size, len);
115  memcpy(target, first, r);
116  return r;
117  }
118  char* reserve(int size) {
119  makeSpace(size + len, freeSpaceAtEnd);
120  char* writePtr = first + len;
121  len += size;
122  return writePtr;
123  }
124  void chop(int size) {
125  if (size >= len) {
126  clear();
127  } else {
128  len -= size;
129  }
130  }
132  char* f = first;
133  int l = len;
134  clear();
135  return QByteArray(f, l);
136  }
137  int readLine(char* target, int size) {
138  int r = qMin(size, len);
139  char* eol = static_cast<char*>(memchr(first, '\n', r));
140  if (eol)
141  r = 1+(eol-first);
142  memcpy(target, first, r);
143  len -= r;
144  first += r;
145  return int(r);
146  }
147  bool canReadLine() const {
148  return memchr(first, '\n', len);
149  }
150  void ungetChar(char c) {
151  if (first == buf) {
152  // underflow, the existing valid data needs to move to the end of the (potentially bigger) buffer
154  }
155  first--;
156  len++;
157  *first = c;
158  }
159  void ungetBlock(const char* block, int size) {
160  if ((first - buf) < size) {
161  // underflow, the existing valid data needs to move to the end of the (potentially bigger) buffer
162  makeSpace(len + size, freeSpaceAtStart);
163  }
164  first -= size;
165  len += size;
166  memcpy(first, block, size);
167  }
168 
169 private:
171  void makeSpace(size_t required, FreeSpacePos where) {
172  size_t newCapacity = qMax(capacity, size_t(QIODEVICE_BUFFERSIZE));
173  while (newCapacity < required)
174  newCapacity *= 2;
175  int moveOffset = (where == freeSpaceAtEnd) ? 0 : newCapacity - len;
176  if (newCapacity > capacity) {
177  // allocate more space
178  char* newBuf = new char[newCapacity];
179  memmove(newBuf + moveOffset, first, len);
180  delete [] buf;
181  buf = newBuf;
182  capacity = newCapacity;
183  } else {
184  // shift any existing data to make space
185  memmove(buf + moveOffset, first, len);
186  }
187  first = buf + moveOffset;
188  }
189 
190 private:
191  // length of the unread data
192  int len;
193  // start of the unread data
194  char* first;
195  // the allocated buffer
196  char* buf;
197  // allocated buffer size
198  size_t capacity;
199 };
200 
202 #ifndef QT_NO_QOBJECT
203  : public QObjectPrivate
204 #endif
205 {
207 
208 public:
210  virtual ~QIODevicePrivate();
211 
212  QIODevice::OpenMode openMode;
214 
218  // these three are for fast position updates during read, avoiding isSequential test
223  bool firstRead;
224 
225  virtual bool putCharHelper(char c);
226 
227  enum AccessMode {
230  RandomAccess
231  };
233  inline bool isSequential() const
234  {
235  if (accessMode == Unset)
236  accessMode = q_func()->isSequential() ? Sequential : RandomAccess;
237  return accessMode == Sequential;
238  }
239 
240  virtual qint64 peek(char *data, qint64 maxSize);
241  virtual QByteArray peek(qint64 maxSize);
242 
243 #ifdef QT_NO_QOBJECT
244  QIODevice *q_ptr;
245 #endif
246 };
247 
249 
250 #endif // QIODEVICE_P_H
qint64 * pDevicePos
Definition: qiodevice_p.h:221
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
QIODevice::OpenMode openMode
Definition: qiodevice_p.h:212
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
void makeSpace(size_t required, FreeSpacePos where)
Definition: qiodevice_p.h:171
QIODevicePrivateLinearBuffer buffer
Definition: qiodevice_p.h:215
int read(char *target, int size)
Definition: qiodevice_p.h:106
#define QIODEVICE_BUFFERSIZE
Definition: qiodevice_p.h:68
The QString class provides a Unicode character string.
Definition: qstring.h:83
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
unsigned char uchar
Definition: qglobal.h:994
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static const char * data(const QByteArray &arr)
int peek(char *target, int size)
Definition: qiodevice_p.h:113
QString errorString
Definition: qiodevice_p.h:213
AccessMode accessMode
Definition: qiodevice_p.h:232
__int64 qint64
Definition: qglobal.h:942
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
#define Q_CORE_EXPORT
Definition: qglobal.h:1449
void ungetBlock(const char *block, int size)
Definition: qiodevice_p.h:159
char * reserve(int size)
Definition: qiodevice_p.h:118
QFactoryLoader * l
bool isSequential() const
Definition: qiodevice_p.h:233
bool baseReadLineDataCalled
Definition: qiodevice_p.h:222
int readLine(char *target, int size)
Definition: qiodevice_p.h:137
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66