1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtCore module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
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 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | //#define QIODEVICE_DEBUG
|
---|
43 |
|
---|
44 | #include "qbytearray.h"
|
---|
45 | #include "qdebug.h"
|
---|
46 | #include "qiodevice_p.h"
|
---|
47 | #include "qfile.h"
|
---|
48 | #include "qstringlist.h"
|
---|
49 | #include <limits.h>
|
---|
50 |
|
---|
51 | #ifdef QIODEVICE_DEBUG
|
---|
52 | # include <ctype.h>
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | QT_BEGIN_NAMESPACE
|
---|
56 |
|
---|
57 | #ifdef QIODEVICE_DEBUG
|
---|
58 | void debugBinaryString(const QByteArray &input)
|
---|
59 | {
|
---|
60 | QByteArray tmp;
|
---|
61 | int startOffset = 0;
|
---|
62 | for (int i = 0; i < input.size(); ++i) {
|
---|
63 | tmp += input[i];
|
---|
64 |
|
---|
65 | if ((i % 16) == 15 || i == (input.size() - 1)) {
|
---|
66 | printf("\n%15d:", startOffset);
|
---|
67 | startOffset += tmp.size();
|
---|
68 |
|
---|
69 | for (int j = 0; j < tmp.size(); ++j)
|
---|
70 | printf(" %02x", int(uchar(tmp[j])));
|
---|
71 | for (int j = tmp.size(); j < 16 + 1; ++j)
|
---|
72 | printf(" ");
|
---|
73 | for (int j = 0; j < tmp.size(); ++j)
|
---|
74 | printf("%c", isprint(int(uchar(tmp[j]))) ? tmp[j] : '.');
|
---|
75 | tmp.clear();
|
---|
76 | }
|
---|
77 | }
|
---|
78 | printf("\n\n");
|
---|
79 | }
|
---|
80 |
|
---|
81 | void debugBinaryString(const char *data, qint64 maxlen)
|
---|
82 | {
|
---|
83 | debugBinaryString(QByteArray(data, maxlen));
|
---|
84 | }
|
---|
85 | #endif
|
---|
86 |
|
---|
87 | #define Q_VOID
|
---|
88 |
|
---|
89 | #define CHECK_MAXLEN(function, returnType) \
|
---|
90 | do { \
|
---|
91 | if (maxSize < 0) { \
|
---|
92 | qWarning("QIODevice::"#function": Called with maxSize < 0"); \
|
---|
93 | return returnType; \
|
---|
94 | } \
|
---|
95 | } while (0)
|
---|
96 |
|
---|
97 | #define CHECK_WRITABLE(function, returnType) \
|
---|
98 | do { \
|
---|
99 | if ((d->openMode & WriteOnly) == 0) { \
|
---|
100 | if (d->openMode == NotOpen) \
|
---|
101 | return returnType; \
|
---|
102 | qWarning("QIODevice::"#function": ReadOnly device"); \
|
---|
103 | return returnType; \
|
---|
104 | } \
|
---|
105 | } while (0)
|
---|
106 |
|
---|
107 | #define CHECK_READABLE(function, returnType) \
|
---|
108 | do { \
|
---|
109 | if ((d->openMode & ReadOnly) == 0) { \
|
---|
110 | if (d->openMode == NotOpen) \
|
---|
111 | return returnType; \
|
---|
112 | qWarning("QIODevice::"#function": WriteOnly device"); \
|
---|
113 | return returnType; \
|
---|
114 | } \
|
---|
115 | } while (0)
|
---|
116 |
|
---|
117 | /*! \internal
|
---|
118 | */
|
---|
119 | QIODevicePrivate::QIODevicePrivate()
|
---|
120 | : openMode(QIODevice::NotOpen), buffer(QIODEVICE_BUFFERSIZE),
|
---|
121 | pos(0), devicePos(0)
|
---|
122 | , pPos(&pos), pDevicePos(&devicePos)
|
---|
123 | , baseReadLineDataCalled(false)
|
---|
124 | , firstRead(true)
|
---|
125 | , accessMode(Unset)
|
---|
126 | #ifdef QT_NO_QOBJECT
|
---|
127 | , q_ptr(0)
|
---|
128 | #endif
|
---|
129 | {
|
---|
130 | }
|
---|
131 |
|
---|
132 | /*! \internal
|
---|
133 | */
|
---|
134 | QIODevicePrivate::~QIODevicePrivate()
|
---|
135 | {
|
---|
136 | }
|
---|
137 |
|
---|
138 | /*!
|
---|
139 | \class QIODevice
|
---|
140 | \reentrant
|
---|
141 |
|
---|
142 | \brief The QIODevice class is the base interface class of all I/O
|
---|
143 | devices in Qt.
|
---|
144 |
|
---|
145 | \ingroup io
|
---|
146 |
|
---|
147 | QIODevice provides both a common implementation and an abstract
|
---|
148 | interface for devices that support reading and writing of blocks
|
---|
149 | of data, such as QFile, QBuffer and QTcpSocket. QIODevice is
|
---|
150 | abstract and can not be instantiated, but it is common to use the
|
---|
151 | interface it defines to provide device-independent I/O features.
|
---|
152 | For example, Qt's XML classes operate on a QIODevice pointer,
|
---|
153 | allowing them to be used with various devices (such as files and
|
---|
154 | buffers).
|
---|
155 |
|
---|
156 | Before accessing the device, open() must be called to set the
|
---|
157 | correct OpenMode (such as ReadOnly or ReadWrite). You can then
|
---|
158 | write to the device with write() or putChar(), and read by calling
|
---|
159 | either read(), readLine(), or readAll(). Call close() when you are
|
---|
160 | done with the device.
|
---|
161 |
|
---|
162 | QIODevice distinguishes between two types of devices:
|
---|
163 | random-access devices and sequential devices.
|
---|
164 |
|
---|
165 | \list
|
---|
166 | \o Random-access devices support seeking to arbitrary
|
---|
167 | positions using seek(). The current position in the file is
|
---|
168 | available by calling pos(). QFile and QBuffer are examples of
|
---|
169 | random-access devices.
|
---|
170 |
|
---|
171 | \o Sequential devices don't support seeking to arbitrary
|
---|
172 | positions. The data must be read in one pass. The functions
|
---|
173 | pos() and size() don't work for sequential devices.
|
---|
174 | QTcpSocket and QProcess are examples of sequential devices.
|
---|
175 | \endlist
|
---|
176 |
|
---|
177 | You can use isSequential() to determine the type of device.
|
---|
178 |
|
---|
179 | QIODevice emits readyRead() when new data is available for
|
---|
180 | reading; for example, if new data has arrived on the network or if
|
---|
181 | additional data is appended to a file that you are reading
|
---|
182 | from. You can call bytesAvailable() to determine the number of
|
---|
183 | bytes that are currently available for reading. It's common to use
|
---|
184 | bytesAvailable() together with the readyRead() signal when
|
---|
185 | programming with asynchronous devices such as QTcpSocket, where
|
---|
186 | fragments of data can arrive at arbitrary points in
|
---|
187 | time. QIODevice emits the bytesWritten() signal every time a
|
---|
188 | payload of data has been written to the device. Use bytesToWrite()
|
---|
189 | to determine the current amount of data waiting to be written.
|
---|
190 |
|
---|
191 | Certain subclasses of QIODevice, such as QTcpSocket and QProcess,
|
---|
192 | are asynchronous. This means that I/O functions such as write()
|
---|
193 | or read() always return immediately, while communication with the
|
---|
194 | device itself may happen when control goes back to the event loop.
|
---|
195 | QIODevice provides functions that allow you to force these
|
---|
196 | operations to be performed immediately, while blocking the
|
---|
197 | calling thread and without entering the event loop. This allows
|
---|
198 | QIODevice subclasses to be used without an event loop, or in
|
---|
199 | a separate thread:
|
---|
200 |
|
---|
201 | \list
|
---|
202 | \o waitForReadyRead() - This function suspends operation in the
|
---|
203 | calling thread until new data is available for reading.
|
---|
204 |
|
---|
205 | \o waitForBytesWritten() - This function suspends operation in the
|
---|
206 | calling thread until one payload of data has been written to the
|
---|
207 | device.
|
---|
208 |
|
---|
209 | \o waitFor....() - Subclasses of QIODevice implement blocking
|
---|
210 | functions for device-specific operations. For example, QProcess
|
---|
211 | has a function called waitForStarted() which suspends operation in
|
---|
212 | the calling thread until the process has started.
|
---|
213 | \endlist
|
---|
214 |
|
---|
215 | Calling these functions from the main, GUI thread, may cause your
|
---|
216 | user interface to freeze. Example:
|
---|
217 |
|
---|
218 | \snippet doc/src/snippets/code/src_corelib_io_qiodevice.cpp 0
|
---|
219 |
|
---|
220 | By subclassing QIODevice, you can provide the same interface to
|
---|
221 | your own I/O devices. Subclasses of QIODevice are only required to
|
---|
222 | implement the protected readData() and writeData() functions.
|
---|
223 | QIODevice uses these functions to implement all its convenience
|
---|
224 | functions, such as getChar(), readLine() and write(). QIODevice
|
---|
225 | also handles access control for you, so you can safely assume that
|
---|
226 | the device is opened in write mode if writeData() is called.
|
---|
227 |
|
---|
228 | Some subclasses, such as QFile and QTcpSocket, are implemented
|
---|
229 | using a memory buffer for intermediate storing of data. This
|
---|
230 | reduces the number of required device accessing calls, which are
|
---|
231 | often very slow. Buffering makes functions like getChar() and
|
---|
232 | putChar() fast, as they can operate on the memory buffer instead
|
---|
233 | of directly on the device itself. Certain I/O operations, however,
|
---|
234 | don't work well with a buffer. For example, if several users open
|
---|
235 | the same device and read it character by character, they may end
|
---|
236 | up reading the same data when they meant to read a separate chunk
|
---|
237 | each. For this reason, QIODevice allows you to bypass any
|
---|
238 | buffering by passing the Unbuffered flag to open(). When
|
---|
239 | subclassing QIODevice, remember to bypass any buffer you may use
|
---|
240 | when the device is open in Unbuffered mode.
|
---|
241 |
|
---|
242 | \sa QBuffer QFile QTcpSocket
|
---|
243 | */
|
---|
244 |
|
---|
245 | /*!
|
---|
246 | \typedef QIODevice::Offset
|
---|
247 | \compat
|
---|
248 |
|
---|
249 | Use \c qint64 instead.
|
---|
250 | */
|
---|
251 |
|
---|
252 | /*!
|
---|
253 | \typedef QIODevice::Status
|
---|
254 | \compat
|
---|
255 |
|
---|
256 | Use QIODevice::OpenMode instead, or see the documentation for
|
---|
257 | specific devices.
|
---|
258 | */
|
---|
259 |
|
---|
260 | /*!
|
---|
261 | \enum QIODevice::OpenModeFlag
|
---|
262 |
|
---|
263 | This enum is used with open() to describe the mode in which a device
|
---|
264 | is opened. It is also returned by openMode().
|
---|
265 |
|
---|
266 | \value NotOpen The device is not open.
|
---|
267 | \value ReadOnly The device is open for reading.
|
---|
268 | \value WriteOnly The device is open for writing.
|
---|
269 | \value ReadWrite The device is open for reading and writing.
|
---|
270 | \value Append The device is opened in append mode, so that all data is
|
---|
271 | written to the end of the file.
|
---|
272 | \value Truncate If possible, the device is truncated before it is opened.
|
---|
273 | All earlier contents of the device are lost.
|
---|
274 | \value Text When reading, the end-of-line terminators are
|
---|
275 | translated to '\n'. When writing, the end-of-line
|
---|
276 | terminators are translated to the local encoding, for
|
---|
277 | example '\r\n' for Win32.
|
---|
278 | \value Unbuffered Any buffer in the device is bypassed.
|
---|
279 |
|
---|
280 | Certain flags, such as \c Unbuffered and \c Truncate, are
|
---|
281 | meaningless when used with some subclasses. Some of these
|
---|
282 | restrictions are implied by the type of device that is represented
|
---|
283 | by a subclass. In other cases, the restriction may be due to the
|
---|
284 | implementation, or may be imposed by the underlying platform; for
|
---|
285 | example, QTcpSocket does not support \c Unbuffered mode, and
|
---|
286 | limitations in the native API prevent QFile from supporting \c
|
---|
287 | Unbuffered on Windows.
|
---|
288 | */
|
---|
289 |
|
---|
290 | /*! \fn QIODevice::bytesWritten(qint64 bytes)
|
---|
291 |
|
---|
292 | This signal is emitted every time a payload of data has been
|
---|
293 | written to the device. The \a bytes argument is set to the number
|
---|
294 | of bytes that were written in this payload.
|
---|
295 |
|
---|
296 | bytesWritten() is not emitted recursively; if you reenter the event loop
|
---|
297 | or call waitForBytesWritten() inside a slot connected to the
|
---|
298 | bytesWritten() signal, the signal will not be reemitted (although
|
---|
299 | waitForBytesWritten() may still return true).
|
---|
300 |
|
---|
301 | \sa readyRead()
|
---|
302 | */
|
---|
303 |
|
---|
304 | /*!
|
---|
305 | \fn QIODevice::readyRead()
|
---|
306 |
|
---|
307 | This signal is emitted once every time new data is available for
|
---|
308 | reading from the device. It will only be emitted again once new
|
---|
309 | data is available, such as when a new payload of network data has
|
---|
310 | arrived on your network socket, or when a new block of data has
|
---|
311 | been appended to your device.
|
---|
312 |
|
---|
313 | readyRead() is not emitted recursively; if you reenter the event loop or
|
---|
314 | call waitForReadyRead() inside a slot connected to the readyRead() signal,
|
---|
315 | the signal will not be reemitted (although waitForReadyRead() may still
|
---|
316 | return true).
|
---|
317 |
|
---|
318 | Note for developers implementing classes derived from QIODevice:
|
---|
319 | you should always emit readyRead() when new data has arrived (do not
|
---|
320 | emit it only because there's data still to be read in your
|
---|
321 | buffers). Do not emit readyRead() in other conditions.
|
---|
322 |
|
---|
323 | \sa bytesWritten()
|
---|
324 | */
|
---|
325 |
|
---|
326 | /*! \fn QIODevice::aboutToClose()
|
---|
327 |
|
---|
328 | This signal is emitted when the device is about to close. Connect
|
---|
329 | this signal if you have operations that need to be performed
|
---|
330 | before the device closes (e.g., if you have data in a separate
|
---|
331 | buffer that needs to be written to the device).
|
---|
332 | */
|
---|
333 |
|
---|
334 | /*!
|
---|
335 | \fn QIODevice::readChannelFinished()
|
---|
336 | \since 4.4
|
---|
337 |
|
---|
338 | This signal is emitted when the input (reading) stream is closed
|
---|
339 | in this device. It is emitted as soon as the closing is detected,
|
---|
340 | which means that there might still be data available for reading
|
---|
341 | with read().
|
---|
342 |
|
---|
343 | \sa atEnd(), read()
|
---|
344 | */
|
---|
345 |
|
---|
346 | #ifdef QT_NO_QOBJECT
|
---|
347 | QIODevice::QIODevice()
|
---|
348 | : d_ptr(new QIODevicePrivate)
|
---|
349 | {
|
---|
350 | d_ptr->q_ptr = this;
|
---|
351 | }
|
---|
352 |
|
---|
353 | /*! \internal
|
---|
354 | */
|
---|
355 | QIODevice::QIODevice(QIODevicePrivate &dd)
|
---|
356 | : d_ptr(&dd)
|
---|
357 | {
|
---|
358 | d_ptr->q_ptr = this;
|
---|
359 | }
|
---|
360 | #else
|
---|
361 |
|
---|
362 | /*!
|
---|
363 | Constructs a QIODevice object.
|
---|
364 | */
|
---|
365 |
|
---|
366 | QIODevice::QIODevice()
|
---|
367 | : QObject(*new QIODevicePrivate, 0)
|
---|
368 | {
|
---|
369 | #if defined QIODEVICE_DEBUG
|
---|
370 | QFile *file = qobject_cast<QFile *>(this);
|
---|
371 | printf("%p QIODevice::QIODevice(\"%s\") %s\n", this, metaObject()->className(),
|
---|
372 | qPrintable(file ? file->fileName() : QString()));
|
---|
373 | #endif
|
---|
374 | }
|
---|
375 |
|
---|
376 | /*!
|
---|
377 | Constructs a QIODevice object with the given \a parent.
|
---|
378 | */
|
---|
379 |
|
---|
380 | QIODevice::QIODevice(QObject *parent)
|
---|
381 | : QObject(*new QIODevicePrivate, parent)
|
---|
382 | {
|
---|
383 | #if defined QIODEVICE_DEBUG
|
---|
384 | printf("%p QIODevice::QIODevice(%p \"%s\")\n", this, parent, metaObject()->className());
|
---|
385 | #endif
|
---|
386 | }
|
---|
387 |
|
---|
388 | /*! \internal
|
---|
389 | */
|
---|
390 | QIODevice::QIODevice(QIODevicePrivate &dd, QObject *parent)
|
---|
391 | : QObject(dd, parent)
|
---|
392 | {
|
---|
393 | }
|
---|
394 | #endif
|
---|
395 |
|
---|
396 |
|
---|
397 | /*!
|
---|
398 | Destructs the QIODevice object.
|
---|
399 | */
|
---|
400 | QIODevice::~QIODevice()
|
---|
401 | {
|
---|
402 | #if defined QIODEVICE_DEBUG
|
---|
403 | printf("%p QIODevice::~QIODevice()\n", this);
|
---|
404 | #endif
|
---|
405 | }
|
---|
406 |
|
---|
407 | /*!
|
---|
408 | Returns true if this device is sequential; otherwise returns
|
---|
409 | false.
|
---|
410 |
|
---|
411 | Sequential devices, as opposed to a random-access devices, have no
|
---|
412 | concept of a start, an end, a size, or a current position, and they
|
---|
413 | do not support seeking. You can only read from the device when it
|
---|
414 | reports that data is available. The most common example of a
|
---|
415 | sequential device is a network socket. On Unix, special files such
|
---|
416 | as /dev/zero and fifo pipes are sequential.
|
---|
417 |
|
---|
418 | Regular files, on the other hand, do support random access. They
|
---|
419 | have both a size and a current position, and they also support
|
---|
420 | seeking backwards and forwards in the data stream. Regular files
|
---|
421 | are non-sequential.
|
---|
422 |
|
---|
423 | \sa bytesAvailable()
|
---|
424 | */
|
---|
425 | bool QIODevice::isSequential() const
|
---|
426 | {
|
---|
427 | return false;
|
---|
428 | }
|
---|
429 |
|
---|
430 | /*!
|
---|
431 | Returns the mode in which the device has been opened;
|
---|
432 | i.e. ReadOnly or WriteOnly.
|
---|
433 |
|
---|
434 | \sa OpenMode
|
---|
435 | */
|
---|
436 | QIODevice::OpenMode QIODevice::openMode() const
|
---|
437 | {
|
---|
438 | return d_func()->openMode;
|
---|
439 | }
|
---|
440 |
|
---|
441 | /*!
|
---|
442 | Sets the OpenMode of the device to \a openMode. Call this
|
---|
443 | function to set the open mode if the flags change after the device
|
---|
444 | has been opened.
|
---|
445 |
|
---|
446 | \sa openMode() OpenMode
|
---|
447 | */
|
---|
448 | void QIODevice::setOpenMode(OpenMode openMode)
|
---|
449 | {
|
---|
450 | Q_D(QIODevice);
|
---|
451 | #if defined QIODEVICE_DEBUG
|
---|
452 | printf("%p QIODevice::setOpenMode(0x%x)\n", this, int(openMode));
|
---|
453 | #endif
|
---|
454 | d->openMode = openMode;
|
---|
455 | d->accessMode = QIODevicePrivate::Unset;
|
---|
456 | d->firstRead = true;
|
---|
457 | if (!isReadable())
|
---|
458 | d->buffer.clear();
|
---|
459 | }
|
---|
460 |
|
---|
461 | /*!
|
---|
462 | If \a enabled is true, this function sets the \l Text flag on the device;
|
---|
463 | otherwise the \l Text flag is removed. This feature is useful for classes
|
---|
464 | that provide custom end-of-line handling on a QIODevice.
|
---|
465 |
|
---|
466 | \sa open(), setOpenMode()
|
---|
467 | */
|
---|
468 | void QIODevice::setTextModeEnabled(bool enabled)
|
---|
469 | {
|
---|
470 | Q_D(QIODevice);
|
---|
471 | if (enabled)
|
---|
472 | d->openMode |= Text;
|
---|
473 | else
|
---|
474 | d->openMode &= ~Text;
|
---|
475 | }
|
---|
476 |
|
---|
477 | /*!
|
---|
478 | Returns true if the \l Text flag is enabled; otherwise returns false.
|
---|
479 |
|
---|
480 | \sa setTextModeEnabled()
|
---|
481 | */
|
---|
482 | bool QIODevice::isTextModeEnabled() const
|
---|
483 | {
|
---|
484 | return d_func()->openMode & Text;
|
---|
485 | }
|
---|
486 |
|
---|
487 | /*!
|
---|
488 | Returns true if the device is open; otherwise returns false. A
|
---|
489 | device is open if it can be read from and/or written to. By
|
---|
490 | default, this function returns false if openMode() returns
|
---|
491 | \c NotOpen.
|
---|
492 |
|
---|
493 | \sa openMode() OpenMode
|
---|
494 | */
|
---|
495 | bool QIODevice::isOpen() const
|
---|
496 | {
|
---|
497 | return d_func()->openMode != NotOpen;
|
---|
498 | }
|
---|
499 |
|
---|
500 | /*!
|
---|
501 | Returns true if data can be read from the device; otherwise returns
|
---|
502 | false. Use bytesAvailable() to determine how many bytes can be read.
|
---|
503 |
|
---|
504 | This is a convenience function which checks if the OpenMode of the
|
---|
505 | device contains the ReadOnly flag.
|
---|
506 |
|
---|
507 | \sa openMode() OpenMode
|
---|
508 | */
|
---|
509 | bool QIODevice::isReadable() const
|
---|
510 | {
|
---|
511 | return (openMode() & ReadOnly) != 0;
|
---|
512 | }
|
---|
513 |
|
---|
514 | /*!
|
---|
515 | Returns true if data can be written to the device; otherwise returns
|
---|
516 | false.
|
---|
517 |
|
---|
518 | This is a convenience function which checks if the OpenMode of the
|
---|
519 | device contains the WriteOnly flag.
|
---|
520 |
|
---|
521 | \sa openMode() OpenMode
|
---|
522 | */
|
---|
523 | bool QIODevice::isWritable() const
|
---|
524 | {
|
---|
525 | return (openMode() & WriteOnly) != 0;
|
---|
526 | }
|
---|
527 |
|
---|
528 | /*!
|
---|
529 | Opens the device and sets its OpenMode to \a mode. Returns true if successful;
|
---|
530 | otherwise returns false. This function should be called from any
|
---|
531 | reimplementations of open() or other functions that open the device.
|
---|
532 |
|
---|
533 | \sa openMode() OpenMode
|
---|
534 | */
|
---|
535 | bool QIODevice::open(OpenMode mode)
|
---|
536 | {
|
---|
537 | Q_D(QIODevice);
|
---|
538 | d->openMode = mode;
|
---|
539 | d->pos = (mode & Append) ? size() : qint64(0);
|
---|
540 | d->buffer.clear();
|
---|
541 | d->accessMode = QIODevicePrivate::Unset;
|
---|
542 | d->firstRead = true;
|
---|
543 | #if defined QIODEVICE_DEBUG
|
---|
544 | printf("%p QIODevice::open(0x%x)\n", this, quint32(mode));
|
---|
545 | #endif
|
---|
546 | return true;
|
---|
547 | }
|
---|
548 |
|
---|
549 | /*!
|
---|
550 | First emits aboutToClose(), then closes the device and sets its
|
---|
551 | OpenMode to NotOpen. The error string is also reset.
|
---|
552 |
|
---|
553 | \sa setOpenMode() OpenMode
|
---|
554 | */
|
---|
555 | void QIODevice::close()
|
---|
556 | {
|
---|
557 | Q_D(QIODevice);
|
---|
558 | if (d->openMode == NotOpen)
|
---|
559 | return;
|
---|
560 |
|
---|
561 | #if defined QIODEVICE_DEBUG
|
---|
562 | printf("%p QIODevice::close()\n", this);
|
---|
563 | #endif
|
---|
564 |
|
---|
565 | #ifndef QT_NO_QOBJECT
|
---|
566 | emit aboutToClose();
|
---|
567 | #endif
|
---|
568 | d->openMode = NotOpen;
|
---|
569 | d->errorString.clear();
|
---|
570 | d->pos = 0;
|
---|
571 | d->buffer.clear();
|
---|
572 | d->firstRead = true;
|
---|
573 | }
|
---|
574 |
|
---|
575 | /*!
|
---|
576 | For random-access devices, this function returns the position that
|
---|
577 | data is written to or read from. For sequential devices or closed
|
---|
578 | devices, where there is no concept of a "current position", 0 is
|
---|
579 | returned.
|
---|
580 |
|
---|
581 | The current read/write position of the device is maintained internally by
|
---|
582 | QIODevice, so reimplementing this function is not necessary. When
|
---|
583 | subclassing QIODevice, use QIODevice::seek() to notify QIODevice about
|
---|
584 | changes in the device position.
|
---|
585 |
|
---|
586 | \sa isSequential(), seek()
|
---|
587 | */
|
---|
588 | qint64 QIODevice::pos() const
|
---|
589 | {
|
---|
590 | Q_D(const QIODevice);
|
---|
591 | #if defined QIODEVICE_DEBUG
|
---|
592 | printf("%p QIODevice::pos() == %d\n", this, int(d->pos));
|
---|
593 | #endif
|
---|
594 | return d->pos;
|
---|
595 | }
|
---|
596 |
|
---|
597 | /*!
|
---|
598 | For open random-access devices, this function returns the size of the
|
---|
599 | device. For open sequential devices, bytesAvailable() is returned.
|
---|
600 |
|
---|
601 | If the device is closed, the size returned will not reflect the actual
|
---|
602 | size of the device.
|
---|
603 |
|
---|
604 | \sa isSequential(), pos()
|
---|
605 | */
|
---|
606 | qint64 QIODevice::size() const
|
---|
607 | {
|
---|
608 | return d_func()->isSequential() ? bytesAvailable() : qint64(0);
|
---|
609 | }
|
---|
610 |
|
---|
611 | /*!
|
---|
612 | For random-access devices, this function sets the current position
|
---|
613 | to \a pos, returning true on success, or false if an error occurred.
|
---|
614 | For sequential devices, the default behavior is to do nothing and
|
---|
615 | return false.
|
---|
616 |
|
---|
617 | When subclassing QIODevice, you must call QIODevice::seek() at the
|
---|
618 | start of your function to ensure integrity with QIODevice's
|
---|
619 | built-in buffer. The base implementation always returns true.
|
---|
620 |
|
---|
621 | \sa pos(), isSequential()
|
---|
622 | */
|
---|
623 | bool QIODevice::seek(qint64 pos)
|
---|
624 | {
|
---|
625 | Q_D(QIODevice);
|
---|
626 | if (d->openMode == NotOpen) {
|
---|
627 | qWarning("QIODevice::seek: The device is not open");
|
---|
628 | return false;
|
---|
629 | }
|
---|
630 | if (pos < 0) {
|
---|
631 | qWarning("QIODevice::seek: Invalid pos: %d", int(pos));
|
---|
632 | return false;
|
---|
633 | }
|
---|
634 |
|
---|
635 | #if defined QIODEVICE_DEBUG
|
---|
636 | printf("%p QIODevice::seek(%d), before: d->pos = %d, d->buffer.size() = %d\n",
|
---|
637 | this, int(pos), int(d->pos), d->buffer.size());
|
---|
638 | #endif
|
---|
639 |
|
---|
640 | qint64 offset = pos - d->pos;
|
---|
641 | if (!d->isSequential()) {
|
---|
642 | d->pos = pos;
|
---|
643 | d->devicePos = pos;
|
---|
644 | }
|
---|
645 |
|
---|
646 | if (offset < 0
|
---|
647 | || offset >= qint64(d->buffer.size()))
|
---|
648 | // When seeking backwards, an operation that is only allowed for
|
---|
649 | // random-access devices, the buffer is cleared. The next read
|
---|
650 | // operation will then refill the buffer. We can optimize this, if we
|
---|
651 | // find that seeking backwards becomes a significant performance hit.
|
---|
652 | d->buffer.clear();
|
---|
653 | else if (!d->buffer.isEmpty())
|
---|
654 | d->buffer.skip(int(offset));
|
---|
655 |
|
---|
656 | #if defined QIODEVICE_DEBUG
|
---|
657 | printf("%p \tafter: d->pos == %d, d->buffer.size() == %d\n", this, int(d->pos),
|
---|
658 | d->buffer.size());
|
---|
659 | #endif
|
---|
660 | return true;
|
---|
661 | }
|
---|
662 |
|
---|
663 | /*!
|
---|
664 | Returns true if the current read and write position is at the end
|
---|
665 | of the device (i.e. there is no more data available for reading on
|
---|
666 | the device); otherwise returns false.
|
---|
667 |
|
---|
668 | For some devices, atEnd() can return true even though there is more data
|
---|
669 | to read. This special case only applies to devices that generate data in
|
---|
670 | direct response to you calling read() (e.g., \c /dev or \c /proc files on
|
---|
671 | Unix and Mac OS X, or console input / \c stdin on all platforms).
|
---|
672 |
|
---|
673 | \sa bytesAvailable(), read(), isSequential()
|
---|
674 | */
|
---|
675 | bool QIODevice::atEnd() const
|
---|
676 | {
|
---|
677 | Q_D(const QIODevice);
|
---|
678 | #if defined QIODEVICE_DEBUG
|
---|
679 | printf("%p QIODevice::atEnd() returns %s, d->openMode == %d, d->pos == %d\n", this, (d->openMode == NotOpen || d->pos == size()) ? "true" : "false",
|
---|
680 | int(d->openMode), int(d->pos));
|
---|
681 | #endif
|
---|
682 | return d->openMode == NotOpen || (d->buffer.isEmpty() && bytesAvailable() == 0);
|
---|
683 | }
|
---|
684 |
|
---|
685 | /*!
|
---|
686 | Seeks to the start of input for random-access devices. Returns
|
---|
687 | true on success; otherwise returns false (for example, if the
|
---|
688 | device is not open).
|
---|
689 |
|
---|
690 | Note that when using a QTextStream on a QFile, calling reset() on
|
---|
691 | the QFile will not have the expected result because QTextStream
|
---|
692 | buffers the file. Use the QTextStream::seek() function instead.
|
---|
693 |
|
---|
694 | \sa seek()
|
---|
695 | */
|
---|
696 | bool QIODevice::reset()
|
---|
697 | {
|
---|
698 | #if defined QIODEVICE_DEBUG
|
---|
699 | printf("%p QIODevice::reset()\n", this);
|
---|
700 | #endif
|
---|
701 | return seek(0);
|
---|
702 | }
|
---|
703 |
|
---|
704 | /*!
|
---|
705 | Returns the number of bytes that are available for reading. This
|
---|
706 | function is commonly used with sequential devices to determine the
|
---|
707 | number of bytes to allocate in a buffer before reading.
|
---|
708 |
|
---|
709 | Subclasses that reimplement this function must call the base
|
---|
710 | implementation in order to include the size of QIODevices' buffer. Example:
|
---|
711 |
|
---|
712 | \snippet doc/src/snippets/code/src_corelib_io_qiodevice.cpp 1
|
---|
713 |
|
---|
714 | \sa bytesToWrite(), readyRead(), isSequential()
|
---|
715 | */
|
---|
716 | qint64 QIODevice::bytesAvailable() const
|
---|
717 | {
|
---|
718 | Q_D(const QIODevice);
|
---|
719 | if (!d->isSequential())
|
---|
720 | return qMax(size() - d->pos, qint64(0));
|
---|
721 | return d->buffer.size();
|
---|
722 | }
|
---|
723 |
|
---|
724 | /*!
|
---|
725 | For buffered devices, this function returns the number of bytes
|
---|
726 | waiting to be written. For devices with no buffer, this function
|
---|
727 | returns 0.
|
---|
728 |
|
---|
729 | \sa bytesAvailable(), bytesWritten(), isSequential()
|
---|
730 | */
|
---|
731 | qint64 QIODevice::bytesToWrite() const
|
---|
732 | {
|
---|
733 | return qint64(0);
|
---|
734 | }
|
---|
735 |
|
---|
736 | #ifdef Q_CC_RVCT
|
---|
737 | // arm mode makes the 64-bit integer operations much faster in RVCT 2.2
|
---|
738 | #pragma push
|
---|
739 | #pragma arm
|
---|
740 | #endif
|
---|
741 |
|
---|
742 | /*!
|
---|
743 | Reads at most \a maxSize bytes from the device into \a data, and
|
---|
744 | returns the number of bytes read. If an error occurs, such as when
|
---|
745 | attempting to read from a device opened in WriteOnly mode, this
|
---|
746 | function returns -1.
|
---|
747 |
|
---|
748 | 0 is returned when no more data is available for reading. However,
|
---|
749 | reading past the end of the stream is considered an error, so this
|
---|
750 | function returns -1 in those cases (that is, reading on a closed
|
---|
751 | socket or after a process has died).
|
---|
752 |
|
---|
753 | \sa readData() readLine() write()
|
---|
754 | */
|
---|
755 | qint64 QIODevice::read(char *data, qint64 maxSize)
|
---|
756 | {
|
---|
757 | Q_D(QIODevice);
|
---|
758 |
|
---|
759 | #if defined QIODEVICE_DEBUG
|
---|
760 | printf("%p QIODevice::read(%p, %d), d->pos = %d, d->buffer.size() = %d\n",
|
---|
761 | this, data, int(maxSize), int(d->pos), int(d->buffer.size()));
|
---|
762 | #endif
|
---|
763 |
|
---|
764 | // Short circuit for getChar()
|
---|
765 | if (maxSize == 1) {
|
---|
766 | int chint;
|
---|
767 | while ((chint = d->buffer.getChar()) != -1) {
|
---|
768 | ++(*d->pPos);
|
---|
769 |
|
---|
770 | char c = char(uchar(chint));
|
---|
771 | if (c == '\r' && (d->openMode & Text))
|
---|
772 | continue;
|
---|
773 | *data = c;
|
---|
774 | #if defined QIODEVICE_DEBUG
|
---|
775 | printf("%p \tread 0x%hhx (%c) returning 1 (shortcut)\n", this,
|
---|
776 | int(c), isprint(c) ? c : '?');
|
---|
777 | #endif
|
---|
778 | return qint64(1);
|
---|
779 | }
|
---|
780 | }
|
---|
781 |
|
---|
782 | CHECK_MAXLEN(read, qint64(-1));
|
---|
783 | qint64 readSoFar = 0;
|
---|
784 | bool moreToRead = true;
|
---|
785 | do {
|
---|
786 | // Try reading from the buffer.
|
---|
787 | int lastReadChunkSize = d->buffer.read(data, maxSize);
|
---|
788 | if (lastReadChunkSize > 0) {
|
---|
789 | *d->pPos += lastReadChunkSize;
|
---|
790 | readSoFar += lastReadChunkSize;
|
---|
791 | // fast exit when satisfied by buffer
|
---|
792 | if (lastReadChunkSize == maxSize && !(d->openMode & Text))
|
---|
793 | return readSoFar;
|
---|
794 |
|
---|
795 | data += lastReadChunkSize;
|
---|
796 | maxSize -= lastReadChunkSize;
|
---|
797 | #if defined QIODEVICE_DEBUG
|
---|
798 | printf("%p \treading %d bytes from buffer into position %d\n", this, lastReadChunkSize,
|
---|
799 | int(readSoFar) - lastReadChunkSize);
|
---|
800 | #endif
|
---|
801 | } else {
|
---|
802 | if (d->firstRead) {
|
---|
803 | // this is the first time the file has been read, check it's valid and set up pos pointers
|
---|
804 | // for fast pos updates.
|
---|
805 | CHECK_READABLE(read, qint64(-1));
|
---|
806 | d->firstRead = false;
|
---|
807 | if (d->isSequential()) {
|
---|
808 | d->pPos = &d->seqDumpPos;
|
---|
809 | d->pDevicePos = &d->seqDumpPos;
|
---|
810 | }
|
---|
811 | }
|
---|
812 |
|
---|
813 | if (!maxSize)
|
---|
814 | return readSoFar;
|
---|
815 |
|
---|
816 | if ((d->openMode & Unbuffered) == 0 && maxSize < QIODEVICE_BUFFERSIZE) {
|
---|
817 | // In buffered mode, we try to fill up the QIODevice buffer before
|
---|
818 | // we do anything else.
|
---|
819 | // buffer is empty at this point, try to fill it
|
---|
820 | int bytesToBuffer = QIODEVICE_BUFFERSIZE;
|
---|
821 | char *writePointer = d->buffer.reserve(bytesToBuffer);
|
---|
822 |
|
---|
823 | // Make sure the device is positioned correctly.
|
---|
824 | if (d->pos != d->devicePos && !d->isSequential() && !seek(d->pos))
|
---|
825 | return readSoFar ? readSoFar : qint64(-1);
|
---|
826 | qint64 readFromDevice = readData(writePointer, bytesToBuffer);
|
---|
827 | d->buffer.chop(bytesToBuffer - (readFromDevice < 0 ? 0 : int(readFromDevice)));
|
---|
828 |
|
---|
829 | if (readFromDevice > 0) {
|
---|
830 | *d->pDevicePos += readFromDevice;
|
---|
831 | #if defined QIODEVICE_DEBUG
|
---|
832 | printf("%p \treading %d from device into buffer\n", this, int(readFromDevice));
|
---|
833 | #endif
|
---|
834 |
|
---|
835 | if (!d->buffer.isEmpty()) {
|
---|
836 | lastReadChunkSize = d->buffer.read(data, maxSize);
|
---|
837 | readSoFar += lastReadChunkSize;
|
---|
838 | data += lastReadChunkSize;
|
---|
839 | maxSize -= lastReadChunkSize;
|
---|
840 | *d->pPos += lastReadChunkSize;
|
---|
841 | #if defined QIODEVICE_DEBUG
|
---|
842 | printf("%p \treading %d bytes from buffer at position %d\n", this,
|
---|
843 | lastReadChunkSize, int(readSoFar));
|
---|
844 | #endif
|
---|
845 | }
|
---|
846 | }
|
---|
847 | }
|
---|
848 | }
|
---|
849 |
|
---|
850 | // If we need more, try reading from the device.
|
---|
851 | if (maxSize > 0) {
|
---|
852 | // Make sure the device is positioned correctly.
|
---|
853 | if (d->pos != d->devicePos && !d->isSequential() && !seek(d->pos))
|
---|
854 | return readSoFar ? readSoFar : qint64(-1);
|
---|
855 | qint64 readFromDevice = readData(data, maxSize);
|
---|
856 | #if defined QIODEVICE_DEBUG
|
---|
857 | printf("%p \treading %d bytes from device (total %d)\n", this, int(readFromDevice), int(readSoFar));
|
---|
858 | #endif
|
---|
859 | if (readFromDevice == -1 && readSoFar == 0) {
|
---|
860 | // error and we haven't read anything: return immediately
|
---|
861 | return -1;
|
---|
862 | }
|
---|
863 | if (readFromDevice > 0) {
|
---|
864 | lastReadChunkSize += int(readFromDevice);
|
---|
865 | readSoFar += readFromDevice;
|
---|
866 | data += readFromDevice;
|
---|
867 | maxSize -= readFromDevice;
|
---|
868 | *d->pPos += readFromDevice;
|
---|
869 | *d->pDevicePos += readFromDevice;
|
---|
870 | }
|
---|
871 | }
|
---|
872 | // Best attempt has been made to read data, don't try again except for text mode adjustment below
|
---|
873 | moreToRead = false;
|
---|
874 |
|
---|
875 | if (readSoFar && d->openMode & Text) {
|
---|
876 | char *readPtr = data - lastReadChunkSize;
|
---|
877 | const char *endPtr = data;
|
---|
878 |
|
---|
879 | if (readPtr < endPtr) {
|
---|
880 | // optimization to avoid initial self-assignment
|
---|
881 | while (*readPtr != '\r') {
|
---|
882 | if (++readPtr == endPtr)
|
---|
883 | return readSoFar;
|
---|
884 | }
|
---|
885 |
|
---|
886 | char *writePtr = readPtr;
|
---|
887 |
|
---|
888 | while (readPtr < endPtr) {
|
---|
889 | char ch = *readPtr++;
|
---|
890 | if (ch != '\r')
|
---|
891 | *writePtr++ = ch;
|
---|
892 | else {
|
---|
893 | --readSoFar;
|
---|
894 | --data;
|
---|
895 | ++maxSize;
|
---|
896 | }
|
---|
897 | }
|
---|
898 |
|
---|
899 | // Make sure we get more data if there is room for more. This
|
---|
900 | // is very important for when someone seeks to the start of a
|
---|
901 | // '\r\n' and reads one character - they should get the '\n'.
|
---|
902 | moreToRead = (readPtr != writePtr);
|
---|
903 | }
|
---|
904 | }
|
---|
905 | } while (moreToRead);
|
---|
906 |
|
---|
907 | #if defined QIODEVICE_DEBUG
|
---|
908 | printf("%p \treturning %d, d->pos == %d, d->buffer.size() == %d\n", this,
|
---|
909 | int(readSoFar), int(d->pos), d->buffer.size());
|
---|
910 | debugBinaryString(data - readSoFar, readSoFar);
|
---|
911 | #endif
|
---|
912 | return readSoFar;
|
---|
913 | }
|
---|
914 |
|
---|
915 | #ifdef Q_CC_RVCT
|
---|
916 | #pragma pop
|
---|
917 | #endif
|
---|
918 |
|
---|
919 | /*!
|
---|
920 | \overload
|
---|
921 |
|
---|
922 | Reads at most \a maxSize bytes from the device, and returns the
|
---|
923 | data read as a QByteArray.
|
---|
924 |
|
---|
925 | This function has no way of reporting errors; returning an empty
|
---|
926 | QByteArray() can mean either that no data was currently available
|
---|
927 | for reading, or that an error occurred.
|
---|
928 | */
|
---|
929 | QByteArray QIODevice::read(qint64 maxSize)
|
---|
930 | {
|
---|
931 | Q_D(QIODevice);
|
---|
932 | QByteArray result;
|
---|
933 |
|
---|
934 | CHECK_MAXLEN(read, result);
|
---|
935 |
|
---|
936 | #if defined QIODEVICE_DEBUG
|
---|
937 | printf("%p QIODevice::read(%d), d->pos = %d, d->buffer.size() = %d\n",
|
---|
938 | this, int(maxSize), int(d->pos), int(d->buffer.size()));
|
---|
939 | #else
|
---|
940 | Q_UNUSED(d);
|
---|
941 | #endif
|
---|
942 |
|
---|
943 | if (maxSize != qint64(int(maxSize))) {
|
---|
944 | qWarning("QIODevice::read: maxSize argument exceeds QByteArray size limit");
|
---|
945 | maxSize = INT_MAX;
|
---|
946 | }
|
---|
947 |
|
---|
948 | qint64 readBytes = 0;
|
---|
949 | if (maxSize) {
|
---|
950 | result.resize(int(maxSize));
|
---|
951 | if (!result.size()) {
|
---|
952 | // If resize fails, read incrementally.
|
---|
953 | qint64 readResult;
|
---|
954 | do {
|
---|
955 | result.resize(int(qMin(maxSize, result.size() + QIODEVICE_BUFFERSIZE)));
|
---|
956 | readResult = read(result.data() + readBytes, result.size() - readBytes);
|
---|
957 | if (readResult > 0 || readBytes == 0)
|
---|
958 | readBytes += readResult;
|
---|
959 | } while (readResult == QIODEVICE_BUFFERSIZE);
|
---|
960 | } else {
|
---|
961 | readBytes = read(result.data(), result.size());
|
---|
962 | }
|
---|
963 | }
|
---|
964 |
|
---|
965 | if (readBytes <= 0)
|
---|
966 | result.clear();
|
---|
967 | else
|
---|
968 | result.resize(int(readBytes));
|
---|
969 |
|
---|
970 | return result;
|
---|
971 | }
|
---|
972 |
|
---|
973 | /*!
|
---|
974 | \overload
|
---|
975 |
|
---|
976 | Reads all available data from the device, and returns it as a
|
---|
977 | QByteArray.
|
---|
978 |
|
---|
979 | This function has no way of reporting errors; returning an empty
|
---|
980 | QByteArray() can mean either that no data was currently available
|
---|
981 | for reading, or that an error occurred.
|
---|
982 | */
|
---|
983 | QByteArray QIODevice::readAll()
|
---|
984 | {
|
---|
985 | Q_D(QIODevice);
|
---|
986 | #if defined QIODEVICE_DEBUG
|
---|
987 | printf("%p QIODevice::readAll(), d->pos = %d, d->buffer.size() = %d\n",
|
---|
988 | this, int(d->pos), int(d->buffer.size()));
|
---|
989 | #endif
|
---|
990 |
|
---|
991 | QByteArray result;
|
---|
992 | qint64 readBytes = 0;
|
---|
993 |
|
---|
994 | // flush internal read buffer
|
---|
995 | if (!(d->openMode & Text) && !d->buffer.isEmpty()) {
|
---|
996 | result = d->buffer.readAll();
|
---|
997 | readBytes = result.size();
|
---|
998 | d->pos += readBytes;
|
---|
999 | }
|
---|
1000 |
|
---|
1001 | qint64 theSize;
|
---|
1002 | if (d->isSequential() || (theSize = size()) == 0) {
|
---|
1003 | // Size is unknown, read incrementally.
|
---|
1004 | qint64 readResult;
|
---|
1005 | do {
|
---|
1006 | result.resize(result.size() + QIODEVICE_BUFFERSIZE);
|
---|
1007 | readResult = read(result.data() + readBytes, result.size() - readBytes);
|
---|
1008 | if (readResult > 0 || readBytes == 0)
|
---|
1009 | readBytes += readResult;
|
---|
1010 | } while (readResult > 0);
|
---|
1011 | } else {
|
---|
1012 | // Read it all in one go.
|
---|
1013 | // If resize fails, don't read anything.
|
---|
1014 | result.resize(int(readBytes + theSize - d->pos));
|
---|
1015 | readBytes += read(result.data() + readBytes, result.size() - readBytes);
|
---|
1016 | }
|
---|
1017 |
|
---|
1018 | if (readBytes <= 0)
|
---|
1019 | result.clear();
|
---|
1020 | else
|
---|
1021 | result.resize(int(readBytes));
|
---|
1022 |
|
---|
1023 | return result;
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | #ifdef Q_CC_RVCT
|
---|
1027 | // arm mode makes the 64-bit integer operations much faster in RVCT 2.2
|
---|
1028 | #pragma push
|
---|
1029 | #pragma arm
|
---|
1030 | #endif
|
---|
1031 |
|
---|
1032 | /*!
|
---|
1033 | This function reads a line of ASCII characters from the device, up
|
---|
1034 | to a maximum of \a maxSize - 1 bytes, stores the characters in \a
|
---|
1035 | data, and returns the number of bytes read. If a line could not be
|
---|
1036 | read but no error ocurred, this function returns 0. If an error
|
---|
1037 | occurs, this function returns the length of what could be read, or
|
---|
1038 | -1 if nothing was read.
|
---|
1039 |
|
---|
1040 | A terminating '\0' byte is always appended to \a data, so \a
|
---|
1041 | maxSize must be larger than 1.
|
---|
1042 |
|
---|
1043 | Data is read until either of the following conditions are met:
|
---|
1044 |
|
---|
1045 | \list
|
---|
1046 | \o The first '\n' character is read.
|
---|
1047 | \o \a maxSize - 1 bytes are read.
|
---|
1048 | \o The end of the device data is detected.
|
---|
1049 | \endlist
|
---|
1050 |
|
---|
1051 | For example, the following code reads a line of characters from a
|
---|
1052 | file:
|
---|
1053 |
|
---|
1054 | \snippet doc/src/snippets/code/src_corelib_io_qiodevice.cpp 2
|
---|
1055 |
|
---|
1056 | The newline character ('\n') is included in the buffer. If a
|
---|
1057 | newline is not encountered before maxSize - 1 bytes are read, a
|
---|
1058 | newline will not be inserted into the buffer. On windows newline
|
---|
1059 | characters are replaced with '\n'.
|
---|
1060 |
|
---|
1061 | This function calls readLineData(), which is implemented using
|
---|
1062 | repeated calls to getChar(). You can provide a more efficient
|
---|
1063 | implementation by reimplementing readLineData() in your own
|
---|
1064 | subclass.
|
---|
1065 |
|
---|
1066 | \sa getChar(), read(), write()
|
---|
1067 | */
|
---|
1068 | qint64 QIODevice::readLine(char *data, qint64 maxSize)
|
---|
1069 | {
|
---|
1070 | Q_D(QIODevice);
|
---|
1071 | if (maxSize < 2) {
|
---|
1072 | qWarning("QIODevice::readLine: Called with maxSize < 2");
|
---|
1073 | return qint64(-1);
|
---|
1074 | }
|
---|
1075 |
|
---|
1076 | #if defined QIODEVICE_DEBUG
|
---|
1077 | printf("%p QIODevice::readLine(%p, %d), d->pos = %d, d->buffer.size() = %d\n",
|
---|
1078 | this, data, int(maxSize), int(d->pos), int(d->buffer.size()));
|
---|
1079 | #endif
|
---|
1080 |
|
---|
1081 | // Leave room for a '\0'
|
---|
1082 | --maxSize;
|
---|
1083 |
|
---|
1084 | const bool sequential = d->isSequential();
|
---|
1085 |
|
---|
1086 | qint64 readSoFar = 0;
|
---|
1087 | if (!d->buffer.isEmpty()) {
|
---|
1088 | readSoFar = d->buffer.readLine(data, maxSize);
|
---|
1089 | if (!sequential)
|
---|
1090 | d->pos += readSoFar;
|
---|
1091 | #if defined QIODEVICE_DEBUG
|
---|
1092 | printf("%p \tread from buffer: %d bytes, last character read: %hhx\n", this,
|
---|
1093 | int(readSoFar), data[int(readSoFar) - 1]);
|
---|
1094 | if (readSoFar)
|
---|
1095 | debugBinaryString(data, int(readSoFar));
|
---|
1096 | #endif
|
---|
1097 | #if defined(Q_OS_SYMBIAN)
|
---|
1098 | // Open C fgets strips '\r' but readSoFar gets returned as if it was still there
|
---|
1099 | if ((d->openMode & Text) &&
|
---|
1100 | readSoFar > 1 &&
|
---|
1101 | data[readSoFar - 1] == '\0' &&
|
---|
1102 | data[readSoFar - 2] == '\n') {
|
---|
1103 | --readSoFar;
|
---|
1104 | }
|
---|
1105 | #endif
|
---|
1106 | if (readSoFar && data[readSoFar - 1] == '\n') {
|
---|
1107 | if (d->openMode & Text) {
|
---|
1108 | // QRingBuffer::readLine() isn't Text aware.
|
---|
1109 | if (readSoFar > 1 && data[readSoFar - 2] == '\r') {
|
---|
1110 | --readSoFar;
|
---|
1111 | data[readSoFar - 1] = '\n';
|
---|
1112 | }
|
---|
1113 | }
|
---|
1114 | data[readSoFar] = '\0';
|
---|
1115 | return readSoFar;
|
---|
1116 | }
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 | if (d->pos != d->devicePos && !sequential && !seek(d->pos))
|
---|
1120 | return qint64(-1);
|
---|
1121 | d->baseReadLineDataCalled = false;
|
---|
1122 | qint64 readBytes = readLineData(data + readSoFar, maxSize - readSoFar);
|
---|
1123 | #if defined QIODEVICE_DEBUG
|
---|
1124 | printf("%p \tread from readLineData: %d bytes, readSoFar = %d bytes\n", this,
|
---|
1125 | int(readBytes), int(readSoFar));
|
---|
1126 | if (readBytes > 0) {
|
---|
1127 | debugBinaryString(data, int(readSoFar + readBytes));
|
---|
1128 | }
|
---|
1129 | #endif
|
---|
1130 | if (readBytes < 0) {
|
---|
1131 | data[readSoFar] = '\0';
|
---|
1132 | return readSoFar ? readSoFar : -1;
|
---|
1133 | }
|
---|
1134 | readSoFar += readBytes;
|
---|
1135 | if (!d->baseReadLineDataCalled && !sequential) {
|
---|
1136 | d->pos += readBytes;
|
---|
1137 | // If the base implementation was not called, then we must
|
---|
1138 | // assume the device position is invalid and force a seek.
|
---|
1139 | d->devicePos = qint64(-1);
|
---|
1140 | }
|
---|
1141 | data[readSoFar] = '\0';
|
---|
1142 |
|
---|
1143 | if (d->openMode & Text) {
|
---|
1144 | #if defined(Q_OS_SYMBIAN)
|
---|
1145 | // Open C fgets strips '\r' but readSoFar gets returned as if it was still there
|
---|
1146 | if (readSoFar > 1 && data[readSoFar - 1] == '\0' && data[readSoFar - 2] == '\n') {
|
---|
1147 | --readSoFar;
|
---|
1148 | }
|
---|
1149 | #endif
|
---|
1150 | if (readSoFar > 1 && data[readSoFar - 1] == '\n' && data[readSoFar - 2] == '\r') {
|
---|
1151 | data[readSoFar - 2] = '\n';
|
---|
1152 | data[readSoFar - 1] = '\0';
|
---|
1153 | --readSoFar;
|
---|
1154 | }
|
---|
1155 | }
|
---|
1156 |
|
---|
1157 | #if defined QIODEVICE_DEBUG
|
---|
1158 | printf("%p \treturning %d, d->pos = %d, d->buffer.size() = %d, size() = %d\n",
|
---|
1159 | this, int(readSoFar), int(d->pos), d->buffer.size(), int(size()));
|
---|
1160 | debugBinaryString(data, int(readSoFar));
|
---|
1161 | #endif
|
---|
1162 | return readSoFar;
|
---|
1163 | }
|
---|
1164 |
|
---|
1165 | /*!
|
---|
1166 | \overload
|
---|
1167 |
|
---|
1168 | Reads a line from the device, but no more than \a maxSize characters,
|
---|
1169 | and returns the result as a QByteArray.
|
---|
1170 |
|
---|
1171 | This function has no way of reporting errors; returning an empty
|
---|
1172 | QByteArray() can mean either that no data was currently available
|
---|
1173 | for reading, or that an error occurred.
|
---|
1174 | */
|
---|
1175 | QByteArray QIODevice::readLine(qint64 maxSize)
|
---|
1176 | {
|
---|
1177 | Q_D(QIODevice);
|
---|
1178 | QByteArray result;
|
---|
1179 |
|
---|
1180 | CHECK_MAXLEN(readLine, result);
|
---|
1181 |
|
---|
1182 | #if defined QIODEVICE_DEBUG
|
---|
1183 | printf("%p QIODevice::readLine(%d), d->pos = %d, d->buffer.size() = %d\n",
|
---|
1184 | this, int(maxSize), int(d->pos), int(d->buffer.size()));
|
---|
1185 | #else
|
---|
1186 | Q_UNUSED(d);
|
---|
1187 | #endif
|
---|
1188 |
|
---|
1189 | if (maxSize > INT_MAX) {
|
---|
1190 | qWarning("QIODevice::read: maxSize argument exceeds QByteArray size limit");
|
---|
1191 | maxSize = INT_MAX;
|
---|
1192 | }
|
---|
1193 |
|
---|
1194 | result.resize(int(maxSize));
|
---|
1195 | qint64 readBytes = 0;
|
---|
1196 | if (!result.size()) {
|
---|
1197 | // If resize fails or maxSize == 0, read incrementally
|
---|
1198 | if (maxSize == 0)
|
---|
1199 | maxSize = INT_MAX;
|
---|
1200 |
|
---|
1201 | // The first iteration needs to leave an extra byte for the terminating null
|
---|
1202 | result.resize(1);
|
---|
1203 |
|
---|
1204 | qint64 readResult;
|
---|
1205 | do {
|
---|
1206 | result.resize(int(qMin(maxSize, result.size() + QIODEVICE_BUFFERSIZE)));
|
---|
1207 | readResult = readLine(result.data() + readBytes, result.size() - readBytes);
|
---|
1208 | if (readResult > 0 || readBytes == 0)
|
---|
1209 | readBytes += readResult;
|
---|
1210 | } while (readResult == QIODEVICE_BUFFERSIZE
|
---|
1211 | && result[int(readBytes - 1)] != '\n');
|
---|
1212 | } else
|
---|
1213 | readBytes = readLine(result.data(), result.size());
|
---|
1214 |
|
---|
1215 | if (readBytes <= 0)
|
---|
1216 | result.clear();
|
---|
1217 | else
|
---|
1218 | result.resize(readBytes);
|
---|
1219 |
|
---|
1220 | return result;
|
---|
1221 | }
|
---|
1222 |
|
---|
1223 | /*!
|
---|
1224 | Reads up to \a maxSize characters into \a data and returns the
|
---|
1225 | number of characters read.
|
---|
1226 |
|
---|
1227 | This function is called by readLine(), and provides its base
|
---|
1228 | implementation, using getChar(). Buffered devices can improve the
|
---|
1229 | performance of readLine() by reimplementing this function.
|
---|
1230 |
|
---|
1231 | readLine() appends a '\0' byte to \a data; readLineData() does not
|
---|
1232 | need to do this.
|
---|
1233 |
|
---|
1234 | If you reimplement this function, be careful to return the correct
|
---|
1235 | value: it should return the number of bytes read in this line,
|
---|
1236 | including the terminating newline, or 0 if there is no line to be
|
---|
1237 | read at this point. If an error occurs, it should return -1 if and
|
---|
1238 | only if no bytes were read. Reading past EOF is considered an error.
|
---|
1239 | */
|
---|
1240 | qint64 QIODevice::readLineData(char *data, qint64 maxSize)
|
---|
1241 | {
|
---|
1242 | Q_D(QIODevice);
|
---|
1243 | qint64 readSoFar = 0;
|
---|
1244 | char c;
|
---|
1245 | int lastReadReturn = 0;
|
---|
1246 | d->baseReadLineDataCalled = true;
|
---|
1247 |
|
---|
1248 | while (readSoFar < maxSize && (lastReadReturn = read(&c, 1)) == 1) {
|
---|
1249 | *data++ = c;
|
---|
1250 | ++readSoFar;
|
---|
1251 | if (c == '\n')
|
---|
1252 | break;
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 | #if defined QIODEVICE_DEBUG
|
---|
1256 | printf("%p QIODevice::readLineData(%p, %d), d->pos = %d, d->buffer.size() = %d, returns %d\n",
|
---|
1257 | this, data, int(maxSize), int(d->pos), int(d->buffer.size()), int(readSoFar));
|
---|
1258 | #endif
|
---|
1259 | if (lastReadReturn != 1 && readSoFar == 0)
|
---|
1260 | return isSequential() ? lastReadReturn : -1;
|
---|
1261 | return readSoFar;
|
---|
1262 | }
|
---|
1263 |
|
---|
1264 | #ifdef Q_CC_RVCT
|
---|
1265 | #pragma pop
|
---|
1266 | #endif
|
---|
1267 |
|
---|
1268 | /*!
|
---|
1269 | Returns true if a complete line of data can be read from the device;
|
---|
1270 | otherwise returns false.
|
---|
1271 |
|
---|
1272 | Note that unbuffered devices, which have no way of determining what
|
---|
1273 | can be read, always return false.
|
---|
1274 |
|
---|
1275 | This function is often called in conjunction with the readyRead()
|
---|
1276 | signal.
|
---|
1277 |
|
---|
1278 | Subclasses that reimplement this function must call the base
|
---|
1279 | implementation in order to include the contents of the QIODevice's buffer. Example:
|
---|
1280 |
|
---|
1281 | \snippet doc/src/snippets/code/src_corelib_io_qiodevice.cpp 3
|
---|
1282 |
|
---|
1283 | \sa readyRead(), readLine()
|
---|
1284 | */
|
---|
1285 | bool QIODevice::canReadLine() const
|
---|
1286 | {
|
---|
1287 | return d_func()->buffer.canReadLine();
|
---|
1288 | }
|
---|
1289 |
|
---|
1290 | /*!
|
---|
1291 | Writes at most \a maxSize bytes of data from \a data to the
|
---|
1292 | device. Returns the number of bytes that were actually written, or
|
---|
1293 | -1 if an error occurred.
|
---|
1294 |
|
---|
1295 | \sa read() writeData()
|
---|
1296 | */
|
---|
1297 | qint64 QIODevice::write(const char *data, qint64 maxSize)
|
---|
1298 | {
|
---|
1299 | Q_D(QIODevice);
|
---|
1300 | CHECK_WRITABLE(write, qint64(-1));
|
---|
1301 | CHECK_MAXLEN(write, qint64(-1));
|
---|
1302 |
|
---|
1303 | const bool sequential = d->isSequential();
|
---|
1304 | // Make sure the device is positioned correctly.
|
---|
1305 | if (d->pos != d->devicePos && !sequential && !seek(d->pos))
|
---|
1306 | return qint64(-1);
|
---|
1307 |
|
---|
1308 | #if defined(Q_OS_WIN) || defined(Q_OS_OS2)
|
---|
1309 | if (d->openMode & Text) {
|
---|
1310 | const char *endOfData = data + maxSize;
|
---|
1311 | const char *startOfBlock = data;
|
---|
1312 |
|
---|
1313 | qint64 writtenSoFar = 0;
|
---|
1314 |
|
---|
1315 | forever {
|
---|
1316 | const char *endOfBlock = startOfBlock;
|
---|
1317 | while (endOfBlock < endOfData && *endOfBlock != '\n')
|
---|
1318 | ++endOfBlock;
|
---|
1319 |
|
---|
1320 | qint64 blockSize = endOfBlock - startOfBlock;
|
---|
1321 | if (blockSize > 0) {
|
---|
1322 | qint64 ret = writeData(startOfBlock, blockSize);
|
---|
1323 | if (ret <= 0) {
|
---|
1324 | if (writtenSoFar && !sequential)
|
---|
1325 | d->buffer.skip(writtenSoFar);
|
---|
1326 | return writtenSoFar ? writtenSoFar : ret;
|
---|
1327 | }
|
---|
1328 | if (!sequential) {
|
---|
1329 | d->pos += ret;
|
---|
1330 | d->devicePos += ret;
|
---|
1331 | }
|
---|
1332 | writtenSoFar += ret;
|
---|
1333 | }
|
---|
1334 |
|
---|
1335 | if (endOfBlock == endOfData)
|
---|
1336 | break;
|
---|
1337 |
|
---|
1338 | qint64 ret = writeData("\r\n", 2);
|
---|
1339 | if (ret <= 0) {
|
---|
1340 | if (writtenSoFar && !sequential)
|
---|
1341 | d->buffer.skip(writtenSoFar);
|
---|
1342 | return writtenSoFar ? writtenSoFar : ret;
|
---|
1343 | }
|
---|
1344 | if (!sequential) {
|
---|
1345 | d->pos += ret;
|
---|
1346 | d->devicePos += ret;
|
---|
1347 | }
|
---|
1348 | ++writtenSoFar;
|
---|
1349 |
|
---|
1350 | startOfBlock = endOfBlock + 1;
|
---|
1351 | }
|
---|
1352 |
|
---|
1353 | if (writtenSoFar && !sequential)
|
---|
1354 | d->buffer.skip(writtenSoFar);
|
---|
1355 | return writtenSoFar;
|
---|
1356 | }
|
---|
1357 | #endif
|
---|
1358 |
|
---|
1359 | qint64 written = writeData(data, maxSize);
|
---|
1360 | if (written > 0) {
|
---|
1361 | if (!sequential) {
|
---|
1362 | d->pos += written;
|
---|
1363 | d->devicePos += written;
|
---|
1364 | }
|
---|
1365 | if (!d->buffer.isEmpty() && !sequential)
|
---|
1366 | d->buffer.skip(written);
|
---|
1367 | }
|
---|
1368 | return written;
|
---|
1369 | }
|
---|
1370 |
|
---|
1371 | /*!
|
---|
1372 | \since 4.5
|
---|
1373 |
|
---|
1374 | \overload
|
---|
1375 |
|
---|
1376 | Writes data from a zero-terminated string of 8-bit characters to the
|
---|
1377 | device. Returns the number of bytes that were actually written, or
|
---|
1378 | -1 if an error occurred. This is equivalent to
|
---|
1379 | \code
|
---|
1380 | ...
|
---|
1381 | QIODevice::write(data, qstrlen(data));
|
---|
1382 | ...
|
---|
1383 | \endcode
|
---|
1384 |
|
---|
1385 | \sa read() writeData()
|
---|
1386 | */
|
---|
1387 | qint64 QIODevice::write(const char *data)
|
---|
1388 | {
|
---|
1389 | return write(data, qstrlen(data));
|
---|
1390 | }
|
---|
1391 |
|
---|
1392 | /*! \fn qint64 QIODevice::write(const QByteArray &byteArray)
|
---|
1393 |
|
---|
1394 | \overload
|
---|
1395 |
|
---|
1396 | Writes the content of \a byteArray to the device. Returns the number of
|
---|
1397 | bytes that were actually written, or -1 if an error occurred.
|
---|
1398 |
|
---|
1399 | \sa read() writeData()
|
---|
1400 | */
|
---|
1401 |
|
---|
1402 | /*!
|
---|
1403 | Puts the character \a c back into the device, and decrements the
|
---|
1404 | current position unless the position is 0. This function is
|
---|
1405 | usually called to "undo" a getChar() operation, such as when
|
---|
1406 | writing a backtracking parser.
|
---|
1407 |
|
---|
1408 | If \a c was not previously read from the device, the behavior is
|
---|
1409 | undefined.
|
---|
1410 | */
|
---|
1411 | void QIODevice::ungetChar(char c)
|
---|
1412 | {
|
---|
1413 | Q_D(QIODevice);
|
---|
1414 | CHECK_READABLE(read, Q_VOID);
|
---|
1415 |
|
---|
1416 | #if defined QIODEVICE_DEBUG
|
---|
1417 | printf("%p QIODevice::ungetChar(0x%hhx '%c')\n", this, c, isprint(c) ? c : '?');
|
---|
1418 | #endif
|
---|
1419 |
|
---|
1420 | d->buffer.ungetChar(c);
|
---|
1421 | if (!d->isSequential())
|
---|
1422 | --d->pos;
|
---|
1423 | }
|
---|
1424 |
|
---|
1425 | /*! \fn bool QIODevice::putChar(char c)
|
---|
1426 |
|
---|
1427 | Writes the character \a c to the device. Returns true on success;
|
---|
1428 | otherwise returns false.
|
---|
1429 |
|
---|
1430 | \sa write() getChar() ungetChar()
|
---|
1431 | */
|
---|
1432 | bool QIODevice::putChar(char c)
|
---|
1433 | {
|
---|
1434 | return d_func()->putCharHelper(c);
|
---|
1435 | }
|
---|
1436 |
|
---|
1437 | /*!
|
---|
1438 | \internal
|
---|
1439 | */
|
---|
1440 | bool QIODevicePrivate::putCharHelper(char c)
|
---|
1441 | {
|
---|
1442 | return q_func()->write(&c, 1) == 1;
|
---|
1443 | }
|
---|
1444 |
|
---|
1445 | /*! \fn bool QIODevice::getChar(char *c)
|
---|
1446 |
|
---|
1447 | Reads one character from the device and stores it in \a c. If \a c
|
---|
1448 | is 0, the character is discarded. Returns true on success;
|
---|
1449 | otherwise returns false.
|
---|
1450 |
|
---|
1451 | \sa read() putChar() ungetChar()
|
---|
1452 | */
|
---|
1453 | bool QIODevice::getChar(char *c)
|
---|
1454 | {
|
---|
1455 | // readability checked in read()
|
---|
1456 | char ch;
|
---|
1457 | return (1 == read(c ? c : &ch, 1));
|
---|
1458 | }
|
---|
1459 |
|
---|
1460 | /*!
|
---|
1461 | \since 4.1
|
---|
1462 |
|
---|
1463 | Reads at most \a maxSize bytes from the device into \a data, without side
|
---|
1464 | effects (i.e., if you call read() after peek(), you will get the same
|
---|
1465 | data). Returns the number of bytes read. If an error occurs, such as
|
---|
1466 | when attempting to peek a device opened in WriteOnly mode, this function
|
---|
1467 | returns -1.
|
---|
1468 |
|
---|
1469 | 0 is returned when no more data is available for reading.
|
---|
1470 |
|
---|
1471 | Example:
|
---|
1472 |
|
---|
1473 | \snippet doc/src/snippets/code/src_corelib_io_qiodevice.cpp 4
|
---|
1474 |
|
---|
1475 | \sa read()
|
---|
1476 | */
|
---|
1477 | qint64 QIODevice::peek(char *data, qint64 maxSize)
|
---|
1478 | {
|
---|
1479 | qint64 readBytes = read(data, maxSize);
|
---|
1480 | int i = readBytes;
|
---|
1481 | while (i > 0)
|
---|
1482 | ungetChar(data[i-- - 1]);
|
---|
1483 | return readBytes;
|
---|
1484 | }
|
---|
1485 |
|
---|
1486 | /*!
|
---|
1487 | \since 4.1
|
---|
1488 | \overload
|
---|
1489 |
|
---|
1490 | Peeks at most \a maxSize bytes from the device, returning the data peeked
|
---|
1491 | as a QByteArray.
|
---|
1492 |
|
---|
1493 | Example:
|
---|
1494 |
|
---|
1495 | \snippet doc/src/snippets/code/src_corelib_io_qiodevice.cpp 5
|
---|
1496 |
|
---|
1497 | This function has no way of reporting errors; returning an empty
|
---|
1498 | QByteArray() can mean either that no data was currently available
|
---|
1499 | for peeking, or that an error occurred.
|
---|
1500 |
|
---|
1501 | \sa read()
|
---|
1502 | */
|
---|
1503 | QByteArray QIODevice::peek(qint64 maxSize)
|
---|
1504 | {
|
---|
1505 | QByteArray result = read(maxSize);
|
---|
1506 | int i = result.size();
|
---|
1507 | const char *data = result.constData();
|
---|
1508 | while (i > 0)
|
---|
1509 | ungetChar(data[i-- - 1]);
|
---|
1510 | return result;
|
---|
1511 | }
|
---|
1512 |
|
---|
1513 | /*!
|
---|
1514 | Blocks until new data is available for reading and the readyRead()
|
---|
1515 | signal has been emitted, or until \a msecs milliseconds have
|
---|
1516 | passed. If msecs is -1, this function will not time out.
|
---|
1517 |
|
---|
1518 | Returns true if new data is available for reading; otherwise returns
|
---|
1519 | false (if the operation timed out or if an error occurred).
|
---|
1520 |
|
---|
1521 | This function can operate without an event loop. It is
|
---|
1522 | useful when writing non-GUI applications and when performing
|
---|
1523 | I/O operations in a non-GUI thread.
|
---|
1524 |
|
---|
1525 | If called from within a slot connected to the readyRead() signal,
|
---|
1526 | readyRead() will not be reemitted.
|
---|
1527 |
|
---|
1528 | Reimplement this function to provide a blocking API for a custom
|
---|
1529 | device. The default implementation does nothing, and returns false.
|
---|
1530 |
|
---|
1531 | \warning Calling this function from the main (GUI) thread
|
---|
1532 | might cause your user interface to freeze.
|
---|
1533 |
|
---|
1534 | \sa waitForBytesWritten()
|
---|
1535 | */
|
---|
1536 | bool QIODevice::waitForReadyRead(int msecs)
|
---|
1537 | {
|
---|
1538 | Q_UNUSED(msecs);
|
---|
1539 | return false;
|
---|
1540 | }
|
---|
1541 |
|
---|
1542 | /*!
|
---|
1543 | For buffered devices, this function waits until a payload of
|
---|
1544 | buffered written data has been written to the device and the
|
---|
1545 | bytesWritten() signal has been emitted, or until \a msecs
|
---|
1546 | milliseconds have passed. If msecs is -1, this function will
|
---|
1547 | not time out. For unbuffered devices, it returns immediately.
|
---|
1548 |
|
---|
1549 | Returns true if a payload of data was written to the device;
|
---|
1550 | otherwise returns false (i.e. if the operation timed out, or if an
|
---|
1551 | error occurred).
|
---|
1552 |
|
---|
1553 | This function can operate without an event loop. It is
|
---|
1554 | useful when writing non-GUI applications and when performing
|
---|
1555 | I/O operations in a non-GUI thread.
|
---|
1556 |
|
---|
1557 | If called from within a slot connected to the bytesWritten() signal,
|
---|
1558 | bytesWritten() will not be reemitted.
|
---|
1559 |
|
---|
1560 | Reimplement this function to provide a blocking API for a custom
|
---|
1561 | device. The default implementation does nothing, and returns false.
|
---|
1562 |
|
---|
1563 | \warning Calling this function from the main (GUI) thread
|
---|
1564 | might cause your user interface to freeze.
|
---|
1565 |
|
---|
1566 | \sa waitForReadyRead()
|
---|
1567 | */
|
---|
1568 | bool QIODevice::waitForBytesWritten(int msecs)
|
---|
1569 | {
|
---|
1570 | Q_UNUSED(msecs);
|
---|
1571 | return false;
|
---|
1572 | }
|
---|
1573 |
|
---|
1574 | /*!
|
---|
1575 | Sets the human readable description of the last device error that
|
---|
1576 | occurred to \a str.
|
---|
1577 |
|
---|
1578 | \sa errorString()
|
---|
1579 | */
|
---|
1580 | void QIODevice::setErrorString(const QString &str)
|
---|
1581 | {
|
---|
1582 | d_func()->errorString = str;
|
---|
1583 | }
|
---|
1584 |
|
---|
1585 | /*!
|
---|
1586 | Returns a human-readable description of the last device error that
|
---|
1587 | occurred.
|
---|
1588 |
|
---|
1589 | \sa setErrorString()
|
---|
1590 | */
|
---|
1591 | QString QIODevice::errorString() const
|
---|
1592 | {
|
---|
1593 | Q_D(const QIODevice);
|
---|
1594 | if (d->errorString.isEmpty()) {
|
---|
1595 | #ifdef QT_NO_QOBJECT
|
---|
1596 | return QLatin1String(QT_TRANSLATE_NOOP(QIODevice, "Unknown error"));
|
---|
1597 | #else
|
---|
1598 | return tr("Unknown error");
|
---|
1599 | #endif
|
---|
1600 | }
|
---|
1601 | return d->errorString;
|
---|
1602 | }
|
---|
1603 |
|
---|
1604 | /*!
|
---|
1605 | \fn qint64 QIODevice::readData(char *data, qint64 maxSize)
|
---|
1606 |
|
---|
1607 | Reads up to \a maxSize bytes from the device into \a data, and
|
---|
1608 | returns the number of bytes read or -1 if an error occurred. If
|
---|
1609 | there are no bytes to be read, this function should return -1 if
|
---|
1610 | there can never be more bytes available (for example: socket
|
---|
1611 | closed, pipe closed, sub-process finished).
|
---|
1612 |
|
---|
1613 | This function is called by QIODevice. Reimplement this function
|
---|
1614 | when creating a subclass of QIODevice.
|
---|
1615 |
|
---|
1616 | \sa read() readLine() writeData()
|
---|
1617 | */
|
---|
1618 |
|
---|
1619 | /*!
|
---|
1620 | \fn qint64 QIODevice::writeData(const char *data, qint64 maxSize)
|
---|
1621 |
|
---|
1622 | Writes up to \a maxSize bytes from \a data to the device. Returns
|
---|
1623 | the number of bytes written, or -1 if an error occurred.
|
---|
1624 |
|
---|
1625 | This function is called by QIODevice. Reimplement this function
|
---|
1626 | when creating a subclass of QIODevice.
|
---|
1627 |
|
---|
1628 | \sa read() write()
|
---|
1629 | */
|
---|
1630 |
|
---|
1631 | /*!
|
---|
1632 | \fn QIODevice::Offset QIODevice::status() const
|
---|
1633 |
|
---|
1634 | For device specific error handling, please refer to the
|
---|
1635 | individual device documentation.
|
---|
1636 |
|
---|
1637 | \sa qobject_cast()
|
---|
1638 | */
|
---|
1639 |
|
---|
1640 | /*!
|
---|
1641 | \fn QIODevice::Offset QIODevice::at() const
|
---|
1642 |
|
---|
1643 | Use pos() instead.
|
---|
1644 | */
|
---|
1645 |
|
---|
1646 | /*!
|
---|
1647 | \fn bool QIODevice::at(Offset offset)
|
---|
1648 |
|
---|
1649 | Use seek(\a offset) instead.
|
---|
1650 | */
|
---|
1651 |
|
---|
1652 | /*! \fn int QIODevice::flags() const
|
---|
1653 |
|
---|
1654 | Use openMode() instead.
|
---|
1655 | */
|
---|
1656 |
|
---|
1657 | /*! \fn int QIODevice::getch()
|
---|
1658 |
|
---|
1659 | Use getChar() instead.
|
---|
1660 | */
|
---|
1661 |
|
---|
1662 | /*!
|
---|
1663 | \fn bool QIODevice::isAsynchronous() const
|
---|
1664 |
|
---|
1665 | This functionality is no longer available. This function always
|
---|
1666 | returns true.
|
---|
1667 | */
|
---|
1668 |
|
---|
1669 | /*!
|
---|
1670 | \fn bool QIODevice::isBuffered() const
|
---|
1671 |
|
---|
1672 | Use !(openMode() & QIODevice::Unbuffered) instead.
|
---|
1673 | */
|
---|
1674 |
|
---|
1675 | /*!
|
---|
1676 | \fn bool QIODevice::isCombinedAccess() const
|
---|
1677 |
|
---|
1678 | Use openMode() instead.
|
---|
1679 | */
|
---|
1680 |
|
---|
1681 | /*!
|
---|
1682 | \fn bool QIODevice::isDirectAccess() const
|
---|
1683 |
|
---|
1684 | Use !isSequential() instead.
|
---|
1685 | */
|
---|
1686 |
|
---|
1687 | /*!
|
---|
1688 | \fn bool QIODevice::isInactive() const
|
---|
1689 |
|
---|
1690 | Use isOpen(), isReadable(), or isWritable() instead.
|
---|
1691 | */
|
---|
1692 |
|
---|
1693 | /*!
|
---|
1694 | \fn bool QIODevice::isRaw() const
|
---|
1695 |
|
---|
1696 | Use openMode() instead.
|
---|
1697 | */
|
---|
1698 |
|
---|
1699 | /*!
|
---|
1700 | \fn bool QIODevice::isSequentialAccess() const
|
---|
1701 |
|
---|
1702 | Use isSequential() instead.
|
---|
1703 | */
|
---|
1704 |
|
---|
1705 | /*!
|
---|
1706 | \fn bool QIODevice::isSynchronous() const
|
---|
1707 |
|
---|
1708 | This functionality is no longer available. This function always
|
---|
1709 | returns false.
|
---|
1710 | */
|
---|
1711 |
|
---|
1712 | /*!
|
---|
1713 | \fn bool QIODevice::isTranslated() const
|
---|
1714 |
|
---|
1715 | Use openMode() instead.
|
---|
1716 | */
|
---|
1717 |
|
---|
1718 | /*!
|
---|
1719 | \fn bool QIODevice::mode() const
|
---|
1720 |
|
---|
1721 | Use openMode() instead.
|
---|
1722 | */
|
---|
1723 |
|
---|
1724 | /*! \fn int QIODevice::putch(int ch)
|
---|
1725 |
|
---|
1726 | Use putChar(\a ch) instead.
|
---|
1727 | */
|
---|
1728 |
|
---|
1729 | /*! \fn int QIODevice::ungetch(int ch)
|
---|
1730 |
|
---|
1731 | Use ungetChar(\a ch) instead.
|
---|
1732 | */
|
---|
1733 |
|
---|
1734 | /*!
|
---|
1735 | \fn quint64 QIODevice::readBlock(char *data, quint64 size)
|
---|
1736 |
|
---|
1737 | Use read(\a data, \a size) instead.
|
---|
1738 | */
|
---|
1739 |
|
---|
1740 | /*! \fn int QIODevice::state() const
|
---|
1741 |
|
---|
1742 | Use isOpen() instead.
|
---|
1743 | */
|
---|
1744 |
|
---|
1745 | /*!
|
---|
1746 | \fn qint64 QIODevice::writeBlock(const char *data, quint64 size)
|
---|
1747 |
|
---|
1748 | Use write(\a data, \a size) instead.
|
---|
1749 | */
|
---|
1750 |
|
---|
1751 | /*!
|
---|
1752 | \fn qint64 QIODevice::writeBlock(const QByteArray &data)
|
---|
1753 |
|
---|
1754 | Use write(\a data) instead.
|
---|
1755 | */
|
---|
1756 |
|
---|
1757 | #if defined QT3_SUPPORT
|
---|
1758 | QIODevice::Status QIODevice::status() const
|
---|
1759 | {
|
---|
1760 | #if !defined(QT_NO_QOBJECT)
|
---|
1761 | const QFile *f = qobject_cast<const QFile *>(this);
|
---|
1762 | if (f) return (int) f->error();
|
---|
1763 | #endif
|
---|
1764 | return isOpen() ? 0 /* IO_Ok */ : 8 /* IO_UnspecifiedError */;
|
---|
1765 | }
|
---|
1766 |
|
---|
1767 | /*!
|
---|
1768 | For device specific error handling, please refer to the
|
---|
1769 | individual device documentation.
|
---|
1770 |
|
---|
1771 | \sa qobject_cast()
|
---|
1772 | */
|
---|
1773 | void QIODevice::resetStatus()
|
---|
1774 | {
|
---|
1775 | #if !defined(QT_NO_QOBJECT)
|
---|
1776 | QFile *f = qobject_cast<QFile *>(this);
|
---|
1777 | if (f) f->unsetError();
|
---|
1778 | #endif
|
---|
1779 | }
|
---|
1780 | #endif
|
---|
1781 |
|
---|
1782 | #if !defined(QT_NO_DEBUG_STREAM)
|
---|
1783 | QDebug operator<<(QDebug debug, QIODevice::OpenMode modes)
|
---|
1784 | {
|
---|
1785 | debug << "OpenMode(";
|
---|
1786 | QStringList modeList;
|
---|
1787 | if (modes == QIODevice::NotOpen) {
|
---|
1788 | modeList << QLatin1String("NotOpen");
|
---|
1789 | } else {
|
---|
1790 | if (modes & QIODevice::ReadOnly)
|
---|
1791 | modeList << QLatin1String("ReadOnly");
|
---|
1792 | if (modes & QIODevice::WriteOnly)
|
---|
1793 | modeList << QLatin1String("WriteOnly");
|
---|
1794 | if (modes & QIODevice::Append)
|
---|
1795 | modeList << QLatin1String("Append");
|
---|
1796 | if (modes & QIODevice::Truncate)
|
---|
1797 | modeList << QLatin1String("Truncate");
|
---|
1798 | if (modes & QIODevice::Text)
|
---|
1799 | modeList << QLatin1String("Text");
|
---|
1800 | if (modes & QIODevice::Unbuffered)
|
---|
1801 | modeList << QLatin1String("Unbuffered");
|
---|
1802 | }
|
---|
1803 | qSort(modeList);
|
---|
1804 | debug << modeList.join(QLatin1String("|"));
|
---|
1805 | debug << ')';
|
---|
1806 | return debug;
|
---|
1807 | }
|
---|
1808 | #endif
|
---|
1809 |
|
---|
1810 | QT_END_NAMESPACE
|
---|