Fix QIODevice::getChar optimization
[qt-netbsd.git] / src / corelib / io / qiodevice.cpp
blob7ee65e17218f8f1121d7c08e7ee6afe8588e1e55
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtCore module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 //#define QIODEVICE_DEBUG
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>
51 #ifdef QIODEVICE_DEBUG
52 # include <ctype.h>
53 #endif
55 QT_BEGIN_NAMESPACE
57 #ifdef QIODEVICE_DEBUG
58 void debugBinaryString(const QByteArray &input)
60 QByteArray tmp;
61 int startOffset = 0;
62 for (int i = 0; i < input.size(); ++i) {
63 tmp += input[i];
65 if ((i % 16) == 15 || i == (input.size() - 1)) {
66 printf("\n%15d:", startOffset);
67 startOffset += tmp.size();
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();
78 printf("\n\n");
81 void debugBinaryString(const char *data, qint64 maxlen)
83 debugBinaryString(QByteArray(data, maxlen));
85 #endif
87 #ifndef QIODEVICE_BUFFERSIZE
88 #define QIODEVICE_BUFFERSIZE Q_INT64_C(16384)
89 #endif
91 #define Q_VOID
93 #define CHECK_MAXLEN(function, returnType) \
94 do { \
95 if (maxSize < 0) { \
96 qWarning("QIODevice::"#function": Called with maxSize < 0"); \
97 return returnType; \
98 } \
99 } while (0)
101 #define CHECK_WRITABLE(function, returnType) \
102 do { \
103 if ((d->openMode & WriteOnly) == 0) { \
104 if (d->openMode == NotOpen) \
105 return returnType; \
106 qWarning("QIODevice::"#function": ReadOnly device"); \
107 return returnType; \
109 } while (0)
111 #define CHECK_READABLE(function, returnType) \
112 do { \
113 if ((d->openMode & ReadOnly) == 0) { \
114 if (d->openMode == NotOpen) \
115 return returnType; \
116 qWarning("QIODevice::"#function": WriteOnly device"); \
117 return returnType; \
119 } while (0)
121 /*! \internal
123 QIODevicePrivate::QIODevicePrivate()
124 : openMode(QIODevice::NotOpen), buffer(QIODEVICE_BUFFERSIZE),
125 pos(0), devicePos(0)
126 , baseReadLineDataCalled(false)
127 , accessMode(Unset)
128 #ifdef QT_NO_QOBJECT
129 , q_ptr(0)
130 #endif
134 /*! \internal
136 QIODevicePrivate::~QIODevicePrivate()
141 \class QIODevice
142 \reentrant
144 \brief The QIODevice class is the base interface class of all I/O
145 devices in Qt.
147 \ingroup io
149 QIODevice provides both a common implementation and an abstract
150 interface for devices that support reading and writing of blocks
151 of data, such as QFile, QBuffer and QTcpSocket. QIODevice is
152 abstract and can not be instantiated, but it is common to use the
153 interface it defines to provide device-independent I/O features.
154 For example, Qt's XML classes operate on a QIODevice pointer,
155 allowing them to be used with various devices (such as files and
156 buffers).
158 Before accessing the device, open() must be called to set the
159 correct OpenMode (such as ReadOnly or ReadWrite). You can then
160 write to the device with write() or putChar(), and read by calling
161 either read(), readLine(), or readAll(). Call close() when you are
162 done with the device.
164 QIODevice distinguishes between two types of devices:
165 random-access devices and sequential devices.
167 \list
168 \o Random-access devices support seeking to arbitrary
169 positions using seek(). The current position in the file is
170 available by calling pos(). QFile and QBuffer are examples of
171 random-access devices.
173 \o Sequential devices don't support seeking to arbitrary
174 positions. The data must be read in one pass. The functions
175 pos() and size() don't work for sequential devices.
176 QTcpSocket and QProcess are examples of sequential devices.
177 \endlist
179 You can use isSequential() to determine the type of device.
181 QIODevice emits readyRead() when new data is available for
182 reading; for example, if new data has arrived on the network or if
183 additional data is appended to a file that you are reading
184 from. You can call bytesAvailable() to determine the number of
185 bytes that are currently available for reading. It's common to use
186 bytesAvailable() together with the readyRead() signal when
187 programming with asynchronous devices such as QTcpSocket, where
188 fragments of data can arrive at arbitrary points in
189 time. QIODevice emits the bytesWritten() signal every time a
190 payload of data has been written to the device. Use bytesToWrite()
191 to determine the current amount of data waiting to be written.
193 Certain subclasses of QIODevice, such as QTcpSocket and QProcess,
194 are asynchronous. This means that I/O functions such as write()
195 or read() always return immediately, while communication with the
196 device itself may happen when control goes back to the event loop.
197 QIODevice provides functions that allow you to force these
198 operations to be performed immediately, while blocking the
199 calling thread and without entering the event loop. This allows
200 QIODevice subclasses to be used without an event loop, or in
201 a separate thread:
203 \list
204 \o waitForReadyRead() - This function suspends operation in the
205 calling thread until new data is available for reading.
207 \o waitForBytesWritten() - This function suspends operation in the
208 calling thread until one payload of data has been written to the
209 device.
211 \o waitFor....() - Subclasses of QIODevice implement blocking
212 functions for device-specific operations. For example, QProcess
213 has a function called waitForStarted() which suspends operation in
214 the calling thread until the process has started.
215 \endlist
217 Calling these functions from the main, GUI thread, may cause your
218 user interface to freeze. Example:
220 \snippet doc/src/snippets/code/src_corelib_io_qiodevice.cpp 0
222 By subclassing QIODevice, you can provide the same interface to
223 your own I/O devices. Subclasses of QIODevice are only required to
224 implement the protected readData() and writeData() functions.
225 QIODevice uses these functions to implement all its convenience
226 functions, such as getChar(), readLine() and write(). QIODevice
227 also handles access control for you, so you can safely assume that
228 the device is opened in write mode if writeData() is called.
230 Some subclasses, such as QFile and QTcpSocket, are implemented
231 using a memory buffer for intermediate storing of data. This
232 reduces the number of required device accessing calls, which are
233 often very slow. Buffering makes functions like getChar() and
234 putChar() fast, as they can operate on the memory buffer instead
235 of directly on the device itself. Certain I/O operations, however,
236 don't work well with a buffer. For example, if several users open
237 the same device and read it character by character, they may end
238 up reading the same data when they meant to read a separate chunk
239 each. For this reason, QIODevice allows you to bypass any
240 buffering by passing the Unbuffered flag to open(). When
241 subclassing QIODevice, remember to bypass any buffer you may use
242 when the device is open in Unbuffered mode.
244 \sa QBuffer QFile QTcpSocket
248 \typedef QIODevice::Offset
249 \compat
251 Use \c qint64 instead.
255 \typedef QIODevice::Status
256 \compat
258 Use QIODevice::OpenMode instead, or see the documentation for
259 specific devices.
263 \enum QIODevice::OpenModeFlag
265 This enum is used with open() to describe the mode in which a device
266 is opened. It is also returned by openMode().
268 \value NotOpen The device is not open.
269 \value ReadOnly The device is open for reading.
270 \value WriteOnly The device is open for writing.
271 \value ReadWrite The device is open for reading and writing.
272 \value Append The device is opened in append mode, so that all data is
273 written to the end of the file.
274 \value Truncate If possible, the device is truncated before it is opened.
275 All earlier contents of the device are lost.
276 \value Text When reading, the end-of-line terminators are
277 translated to '\n'. When writing, the end-of-line
278 terminators are translated to the local encoding, for
279 example '\r\n' for Win32.
280 \value Unbuffered Any buffer in the device is bypassed.
282 Certain flags, such as \c Unbuffered and \c Truncate, are
283 meaningless when used with some subclasses. Some of these
284 restrictions are implied by the type of device that is represented
285 by a subclass; for example, access to a QBuffer is always
286 unbuffered. In other cases, the restriction may be due to the
287 implementation, or may be imposed by the underlying platform; for
288 example, QTcpSocket does not support \c Unbuffered mode, and
289 limitations in the native API prevent QFile from supporting \c
290 Unbuffered on Windows.
293 /*! \fn QIODevice::bytesWritten(qint64 bytes)
295 This signal is emitted every time a payload of data has been
296 written to the device. The \a bytes argument is set to the number
297 of bytes that were written in this payload.
299 bytesWritten() is not emitted recursively; if you reenter the event loop
300 or call waitForBytesWritten() inside a slot connected to the
301 bytesWritten() signal, the signal will not be reemitted (although
302 waitForBytesWritten() may still return true).
304 \sa readyRead()
308 \fn QIODevice::readyRead()
310 This signal is emitted once every time new data is available for
311 reading from the device. It will only be emitted again once new
312 data is available, such as when a new payload of network data has
313 arrived on your network socket, or when a new block of data has
314 been appended to your device.
316 readyRead() is not emitted recursively; if you reenter the event loop or
317 call waitForReadyRead() inside a slot connected to the readyRead() signal,
318 the signal will not be reemitted (although waitForReadyRead() may still
319 return true).
321 Note for developers implementing classes derived from QIODevice:
322 you should always emit readyRead() when new data has arrived (do not
323 emit it only because there's data still to be read in your
324 buffers). Do not emit readyRead() in other conditions.
326 \sa bytesWritten()
329 /*! \fn QIODevice::aboutToClose()
331 This signal is emitted when the device is about to close. Connect
332 this signal if you have operations that need to be performed
333 before the device closes (e.g., if you have data in a separate
334 buffer that needs to be written to the device).
338 \fn QIODevice::readChannelFinished()
339 \since 4.4
341 This signal is emitted when the input (reading) stream is closed
342 in this device. It is emitted as soon as the closing is detected,
343 which means that there might still be data available for reading
344 with read().
346 \sa atEnd(), read()
349 #ifdef QT_NO_QOBJECT
350 QIODevice::QIODevice()
351 : d_ptr(new QIODevicePrivate)
353 d_ptr->q_ptr = this;
356 /*! \internal
358 QIODevice::QIODevice(QIODevicePrivate &dd)
359 : d_ptr(&dd)
361 d_ptr->q_ptr = this;
363 #else
366 Constructs a QIODevice object.
369 QIODevice::QIODevice()
370 : QObject(*new QIODevicePrivate, 0)
372 #if defined QIODEVICE_DEBUG
373 QFile *file = qobject_cast<QFile *>(this);
374 printf("%p QIODevice::QIODevice(\"%s\") %s\n", this, metaObject()->className(),
375 qPrintable(file ? file->fileName() : QString()));
376 #endif
380 Constructs a QIODevice object with the given \a parent.
383 QIODevice::QIODevice(QObject *parent)
384 : QObject(*new QIODevicePrivate, parent)
386 #if defined QIODEVICE_DEBUG
387 printf("%p QIODevice::QIODevice(%p \"%s\")\n", this, parent, metaObject()->className());
388 #endif
391 /*! \internal
393 QIODevice::QIODevice(QIODevicePrivate &dd, QObject *parent)
394 : QObject(dd, parent)
397 #endif
401 Destructs the QIODevice object.
403 QIODevice::~QIODevice()
405 #if defined QIODEVICE_DEBUG
406 printf("%p QIODevice::~QIODevice()\n", this);
407 #endif
411 Returns true if this device is sequential; otherwise returns
412 false.
414 Sequential devices, as opposed to a random-access devices, have no
415 concept of a start, an end, a size, or a current position, and they
416 do not support seeking. You can only read from the device when it
417 reports that data is available. The most common example of a
418 sequential device is a network socket. On Unix, special files such
419 as /dev/zero and fifo pipes are sequential.
421 Regular files, on the other hand, do support random access. They
422 have both a size and a current position, and they also support
423 seeking backwards and forwards in the data stream. Regular files
424 are non-sequential.
426 \sa bytesAvailable()
428 bool QIODevice::isSequential() const
430 return false;
434 Returns the mode in which the device has been opened;
435 i.e. ReadOnly or WriteOnly.
437 \sa OpenMode
439 QIODevice::OpenMode QIODevice::openMode() const
441 return d_func()->openMode;
445 Sets the OpenMode of the device to \a openMode. Call this
446 function to set the open mode if the flags change after the device
447 has been opened.
449 \sa openMode() OpenMode
451 void QIODevice::setOpenMode(OpenMode openMode)
453 #if defined QIODEVICE_DEBUG
454 printf("%p QIODevice::setOpenMode(0x%x)\n", this, int(openMode));
455 #endif
456 d_func()->openMode = openMode;
457 d_func()->accessMode = QIODevicePrivate::Unset;
461 If \a enabled is true, this function sets the \l Text flag on the device;
462 otherwise the \l Text flag is removed. This feature is useful for classes
463 that provide custom end-of-line handling on a QIODevice.
465 \sa open(), setOpenMode()
467 void QIODevice::setTextModeEnabled(bool enabled)
469 Q_D(QIODevice);
470 if (enabled)
471 d->openMode |= Text;
472 else
473 d->openMode &= ~Text;
477 Returns true if the \l Text flag is enabled; otherwise returns false.
479 \sa setTextModeEnabled()
481 bool QIODevice::isTextModeEnabled() const
483 return d_func()->openMode & Text;
487 Returns true if the device is open; otherwise returns false. A
488 device is open if it can be read from and/or written to. By
489 default, this function returns false if openMode() returns
490 \c NotOpen.
492 \sa openMode() OpenMode
494 bool QIODevice::isOpen() const
496 return d_func()->openMode != NotOpen;
500 Returns true if data can be read from the device; otherwise returns
501 false. Use bytesAvailable() to determine how many bytes can be read.
503 This is a convenience function which checks if the OpenMode of the
504 device contains the ReadOnly flag.
506 \sa openMode() OpenMode
508 bool QIODevice::isReadable() const
510 return (openMode() & ReadOnly) != 0;
514 Returns true if data can be written to the device; otherwise returns
515 false.
517 This is a convenience function which checks if the OpenMode of the
518 device contains the WriteOnly flag.
520 \sa openMode() OpenMode
522 bool QIODevice::isWritable() const
524 return (openMode() & WriteOnly) != 0;
528 Opens the device and sets its OpenMode to \a mode. Returns true if successful;
529 otherwise returns false. This function should be called from any
530 reimplementations of open() or other functions that open the device.
532 \sa openMode() OpenMode
534 bool QIODevice::open(OpenMode mode)
536 Q_D(QIODevice);
537 d->openMode = mode;
538 d->pos = (mode & Append) ? size() : qint64(0);
539 d->buffer.clear();
540 d->accessMode = QIODevicePrivate::Unset;
541 #if defined QIODEVICE_DEBUG
542 printf("%p QIODevice::open(0x%x)\n", this, quint32(mode));
543 #endif
544 return true;
548 First emits aboutToClose(), then closes the device and sets its
549 OpenMode to NotOpen. The error string is also reset.
551 \sa setOpenMode() OpenMode
553 void QIODevice::close()
555 Q_D(QIODevice);
556 if (d->openMode == NotOpen)
557 return;
559 #if defined QIODEVICE_DEBUG
560 printf("%p QIODevice::close()\n", this);
561 #endif
563 #ifndef QT_NO_QOBJECT
564 emit aboutToClose();
565 #endif
566 d->openMode = NotOpen;
567 d->errorString.clear();
568 d->pos = 0;
569 d->buffer.clear();
573 For random-access devices, this function returns the position that
574 data is written to or read from. For sequential devices or closed
575 devices, where there is no concept of a "current position", 0 is
576 returned.
578 The current read/write position of the device is maintained internally by
579 QIODevice, so reimplementing this function is not necessary. When
580 subclassing QIODevice, use QIODevice::seek() to notify QIODevice about
581 changes in the device position.
583 \sa isSequential(), seek()
585 qint64 QIODevice::pos() const
587 Q_D(const QIODevice);
588 #if defined QIODEVICE_DEBUG
589 printf("%p QIODevice::pos() == %d\n", this, int(d->pos));
590 #endif
591 return d->pos;
595 For open random-access devices, this function returns the size of the
596 device. For open sequential devices, bytesAvailable() is returned.
598 If the device is closed, the size returned will not reflect the actual
599 size of the device.
601 \sa isSequential(), pos()
603 qint64 QIODevice::size() const
605 return d_func()->isSequential() ? bytesAvailable() : qint64(0);
609 For random-access devices, this function sets the current position
610 to \a pos, returning true on success, or false if an error occurred.
611 For sequential devices, the default behavior is to do nothing and
612 return false.
614 When subclassing QIODevice, you must call QIODevice::seek() at the
615 start of your function to ensure integrity with QIODevice's
616 built-in buffer. The base implementation always returns true.
618 \sa pos(), isSequential()
620 bool QIODevice::seek(qint64 pos)
622 Q_D(QIODevice);
623 if (d->openMode == NotOpen) {
624 qWarning("QIODevice::seek: The device is not open");
625 return false;
627 if (pos < 0) {
628 qWarning("QIODevice::seek: Invalid pos: %d", int(pos));
629 return false;
632 #if defined QIODEVICE_DEBUG
633 printf("%p QIODevice::seek(%d), before: d->pos = %d, d->buffer.size() = %d\n",
634 this, int(pos), int(d->pos), d->buffer.size());
635 #endif
637 qint64 offset = pos - d->pos;
638 if (!d->isSequential()) {
639 d->pos = pos;
640 d->devicePos = pos;
643 if (offset < 0
644 || offset >= qint64(d->buffer.size()))
645 // When seeking backwards, an operation that is only allowed for
646 // random-access devices, the buffer is cleared. The next read
647 // operation will then refill the buffer. We can optimize this, if we
648 // find that seeking backwards becomes a significant performance hit.
649 d->buffer.clear();
650 else if (!d->buffer.isEmpty())
651 d->buffer.skip(int(offset));
653 #if defined QIODEVICE_DEBUG
654 printf("%p \tafter: d->pos == %d, d->buffer.size() == %d\n", this, int(d->pos),
655 d->buffer.size());
656 #endif
657 return true;
661 Returns true if the current read and write position is at the end
662 of the device (i.e. there is no more data available for reading on
663 the device); otherwise returns false.
665 For some devices, atEnd() can return true even though there is more data
666 to read. This special case only applies to devices that generate data in
667 direct response to you calling read() (e.g., \c /dev or \c /proc files on
668 Unix and Mac OS X, or console input / \c stdin on all platforms).
670 \sa bytesAvailable(), read(), isSequential()
672 bool QIODevice::atEnd() const
674 Q_D(const QIODevice);
675 #if defined QIODEVICE_DEBUG
676 printf("%p QIODevice::atEnd() returns %s, d->openMode == %d, d->pos == %d\n", this, (d->openMode == NotOpen || d->pos == size()) ? "true" : "false",
677 int(d->openMode), int(d->pos));
678 #endif
679 return d->openMode == NotOpen || (d->buffer.isEmpty() && bytesAvailable() == 0);
683 Seeks to the start of input for random-access devices. Returns
684 true on success; otherwise returns false (for example, if the
685 device is not open).
687 Note that when using a QTextStream on a QFile, calling reset() on
688 the QFile will not have the expected result because QTextStream
689 buffers the file. Use the QTextStream::seek() function instead.
691 \sa seek()
693 bool QIODevice::reset()
695 #if defined QIODEVICE_DEBUG
696 printf("%p QIODevice::reset()\n", this);
697 #endif
698 return seek(0);
702 Returns the number of bytes that are available for reading. This
703 function is commonly used with sequential devices to determine the
704 number of bytes to allocate in a buffer before reading.
706 Subclasses that reimplement this function must call the base
707 implementation in order to include the size of QIODevices' buffer. Example:
709 \snippet doc/src/snippets/code/src_corelib_io_qiodevice.cpp 1
711 \sa bytesToWrite(), readyRead(), isSequential()
713 qint64 QIODevice::bytesAvailable() const
715 Q_D(const QIODevice);
716 if (!d->isSequential())
717 return qMax(size() - d->pos, qint64(0));
718 return d->buffer.size();
722 For buffered devices, this function returns the number of bytes
723 waiting to be written. For devices with no buffer, this function
724 returns 0.
726 \sa bytesAvailable(), bytesWritten(), isSequential()
728 qint64 QIODevice::bytesToWrite() const
730 return qint64(0);
734 Reads at most \a maxSize bytes from the device into \a data, and
735 returns the number of bytes read. If an error occurs, such as when
736 attempting to read from a device opened in WriteOnly mode, this
737 function returns -1.
739 0 is returned when no more data is available for reading. However,
740 reading past the end of the stream is considered an error, so this
741 function returns -1 in those cases (that is, reading on a closed
742 socket or after a process has died).
744 \sa readData() readLine() write()
746 qint64 QIODevice::read(char *data, qint64 maxSize)
748 Q_D(QIODevice);
749 CHECK_READABLE(read, qint64(-1));
750 CHECK_MAXLEN(read, qint64(-1));
752 #if defined QIODEVICE_DEBUG
753 printf("%p QIODevice::read(%p, %d), d->pos = %d, d->buffer.size() = %d\n",
754 this, data, int(maxSize), int(d->pos), int(d->buffer.size()));
755 #endif
756 const bool sequential = d->isSequential();
758 // Short circuit for getChar()
759 if (maxSize == 1) {
760 int chint;
761 while ((chint = d->buffer.getChar()) != -1) {
762 if (!sequential)
763 ++d->pos;
765 char c = char(uchar(chint));
766 if (c == '\r' && (d->openMode & Text))
767 continue;
768 *data = c;
769 #if defined QIODEVICE_DEBUG
770 printf("%p \tread 0x%hhx (%c) returning 1 (shortcut)\n", this,
771 int(c), isprint(c) ? c : '?');
772 #endif
773 return qint64(1);
777 qint64 readSoFar = 0;
778 bool moreToRead = true;
779 do {
780 int lastReadChunkSize = 0;
782 // Try reading from the buffer.
783 if (!d->buffer.isEmpty()) {
784 lastReadChunkSize = d->buffer.read(data + readSoFar, maxSize - readSoFar);
785 readSoFar += lastReadChunkSize;
786 if (!sequential)
787 d->pos += lastReadChunkSize;
788 #if defined QIODEVICE_DEBUG
789 printf("%p \treading %d bytes from buffer into position %d\n", this, lastReadChunkSize,
790 int(readSoFar) - lastReadChunkSize);
791 #endif
792 } else if ((d->openMode & Unbuffered) == 0 && maxSize < QIODEVICE_BUFFERSIZE) {
793 // In buffered mode, we try to fill up the QIODevice buffer before
794 // we do anything else.
795 int bytesToBuffer = qMax(maxSize - readSoFar, QIODEVICE_BUFFERSIZE);
796 char *writePointer = d->buffer.reserve(bytesToBuffer);
798 // Make sure the device is positioned correctly.
799 if (d->pos != d->devicePos && !sequential && !seek(d->pos))
800 return qint64(-1);
801 qint64 readFromDevice = readData(writePointer, bytesToBuffer);
802 d->buffer.chop(bytesToBuffer - (readFromDevice < 0 ? 0 : int(readFromDevice)));
804 if (readFromDevice > 0) {
805 if (!sequential)
806 d->devicePos += readFromDevice;
807 #if defined QIODEVICE_DEBUG
808 printf("%p \treading %d from device into buffer\n", this, int(readFromDevice));
809 #endif
811 if (readFromDevice < bytesToBuffer)
812 d->buffer.truncate(int(readFromDevice));
813 if (!d->buffer.isEmpty()) {
814 lastReadChunkSize = d->buffer.read(data + readSoFar, maxSize - readSoFar);
815 readSoFar += lastReadChunkSize;
816 if (!sequential)
817 d->pos += lastReadChunkSize;
818 #if defined QIODEVICE_DEBUG
819 printf("%p \treading %d bytes from buffer at position %d\n", this,
820 lastReadChunkSize, int(readSoFar));
821 #endif
826 // If we need more, try reading from the device.
827 if (readSoFar < maxSize) {
828 // Make sure the device is positioned correctly.
829 if (d->pos != d->devicePos && !sequential && !seek(d->pos))
830 return qint64(-1);
831 qint64 readFromDevice = readData(data + readSoFar, maxSize - readSoFar);
832 #if defined QIODEVICE_DEBUG
833 printf("%p \treading %d bytes from device (total %d)\n", this, int(readFromDevice), int(readSoFar));
834 #endif
835 if (readFromDevice == -1 && readSoFar == 0) {
836 // error and we haven't read anything: return immediately
837 return -1;
839 if (readFromDevice <= 0) {
840 moreToRead = false;
841 } else {
842 // see if we read as much data as we asked for
843 if (readFromDevice < maxSize - readSoFar)
844 moreToRead = false;
846 lastReadChunkSize += int(readFromDevice);
847 readSoFar += readFromDevice;
848 if (!sequential) {
849 d->pos += readFromDevice;
850 d->devicePos += readFromDevice;
853 } else {
854 moreToRead = false;
857 if (readSoFar && d->openMode & Text) {
858 char *readPtr = data + readSoFar - lastReadChunkSize;
859 const char *endPtr = data + readSoFar;
861 if (readPtr < endPtr) {
862 // optimization to avoid initial self-assignment
863 while (*readPtr != '\r') {
864 if (++readPtr == endPtr)
865 return readSoFar;
868 char *writePtr = readPtr;
870 while (readPtr < endPtr) {
871 char ch = *readPtr++;
872 if (ch != '\r')
873 *writePtr++ = ch;
874 else
875 --readSoFar;
878 // Make sure we get more data if there is room for more. This
879 // is very important for when someone seeks to the start of a
880 // '\r\n' and reads one character - they should get the '\n'.
881 moreToRead = (readPtr != writePtr);
884 } while (moreToRead);
886 #if defined QIODEVICE_DEBUG
887 printf("%p \treturning %d, d->pos == %d, d->buffer.size() == %d\n", this,
888 int(readSoFar), int(d->pos), d->buffer.size());
889 debugBinaryString(data, readSoFar);
890 #endif
891 return readSoFar;
895 \overload
897 Reads at most \a maxSize bytes from the device, and returns the
898 data read as a QByteArray.
900 This function has no way of reporting errors; returning an empty
901 QByteArray() can mean either that no data was currently available
902 for reading, or that an error occurred.
904 QByteArray QIODevice::read(qint64 maxSize)
906 Q_D(QIODevice);
907 CHECK_MAXLEN(read, QByteArray());
908 QByteArray tmp;
909 qint64 readSoFar = 0;
910 char buffer[4096];
911 #if defined QIODEVICE_DEBUG
912 printf("%p QIODevice::read(%d), d->pos = %d, d->buffer.size() = %d\n",
913 this, int(maxSize), int(d->pos), int(d->buffer.size()));
914 #else
915 Q_UNUSED(d);
916 #endif
918 do {
919 qint64 bytesToRead = qMin(int(maxSize - readSoFar), int(sizeof(buffer)));
920 qint64 readBytes = read(buffer, bytesToRead);
921 if (readBytes <= 0)
922 break;
923 tmp.append(buffer, (int) readBytes);
924 readSoFar += readBytes;
925 } while (readSoFar < maxSize && bytesAvailable() > 0);
927 return tmp;
931 \overload
933 Reads all available data from the device, and returns it as a
934 QByteArray.
936 This function has no way of reporting errors; returning an empty
937 QByteArray() can mean either that no data was currently available
938 for reading, or that an error occurred.
940 QByteArray QIODevice::readAll()
942 Q_D(QIODevice);
943 #if defined QIODEVICE_DEBUG
944 printf("%p QIODevice::readAll(), d->pos = %d, d->buffer.size() = %d\n",
945 this, int(d->pos), int(d->buffer.size()));
946 #endif
948 QByteArray tmp;
949 if (d->isSequential() || size() == 0) {
950 // Read it in chunks. Use bytesAvailable() as an unreliable hint for
951 // sequential devices, but try to read 4K as a minimum.
952 int chunkSize = qMax(qint64(4096), bytesAvailable());
953 qint64 totalRead = 0;
954 forever {
955 tmp.resize(tmp.size() + chunkSize);
956 qint64 readBytes = read(tmp.data() + totalRead, chunkSize);
957 tmp.chop(chunkSize - (readBytes < 0 ? 0 : readBytes));
958 if (readBytes <= 0)
959 return tmp;
960 totalRead += readBytes;
961 chunkSize = qMax(qint64(4096), bytesAvailable());
963 } else {
964 // Read it all in one go.
965 tmp.resize(int(bytesAvailable()));
966 qint64 readBytes = read(tmp.data(), tmp.size());
967 tmp.resize(readBytes < 0 ? 0 : int(readBytes));
969 return tmp;
973 This function reads a line of ASCII characters from the device, up
974 to a maximum of \a maxSize - 1 bytes, stores the characters in \a
975 data, and returns the number of bytes read. If a line could not be
976 read but no error ocurred, this function returns 0. If an error
977 occurs, this function returns what it could the length of what
978 could be read, or -1 if nothing was read.
980 A terminating '\0' byte is always appended to \a data, so \a
981 maxSize must be larger than 1.
983 Data is read until either of the following conditions are met:
985 \list
986 \o The first '\n' character is read.
987 \o \a maxSize - 1 bytes are read.
988 \o The end of the device data is detected.
989 \endlist
991 For example, the following code reads a line of characters from a
992 file:
994 \snippet doc/src/snippets/code/src_corelib_io_qiodevice.cpp 2
996 The newline character ('\n') is included in the buffer. If a
997 newline is not encountered before maxSize - 1 bytes are read, a
998 newline will not be inserted into the buffer. On windows newline
999 characters are replaced with '\n'.
1001 This function calls readLineData(), which is implemented using
1002 repeated calls to getChar(). You can provide a more efficient
1003 implementation by reimplementing readLineData() in your own
1004 subclass.
1006 \sa getChar(), read(), write()
1008 qint64 QIODevice::readLine(char *data, qint64 maxSize)
1010 Q_D(QIODevice);
1011 if (maxSize < 2) {
1012 qWarning("QIODevice::readLine: Called with maxSize < 2");
1013 return qint64(-1);
1016 #if defined QIODEVICE_DEBUG
1017 printf("%p QIODevice::readLine(%p, %d), d->pos = %d, d->buffer.size() = %d\n",
1018 this, data, int(maxSize), int(d->pos), int(d->buffer.size()));
1019 #endif
1021 // Leave room for a '\0'
1022 --maxSize;
1024 const bool sequential = d->isSequential();
1026 qint64 readSoFar = 0;
1027 if (!d->buffer.isEmpty()) {
1028 readSoFar = d->buffer.readLine(data, maxSize);
1029 if (!sequential)
1030 d->pos += readSoFar;
1031 #if defined QIODEVICE_DEBUG
1032 printf("%p \tread from buffer: %d bytes, last character read: %hhx\n", this,
1033 int(readSoFar), data[int(readSoFar) - 1]);
1034 if (readSoFar)
1035 debugBinaryString(data, int(readSoFar));
1036 #endif
1037 #if defined(Q_OS_SYMBIAN)
1038 // Open C fgets strips '\r' but readSoFar gets returned as if it was still there
1039 if ((d->openMode & Text) &&
1040 readSoFar > 1 &&
1041 data[readSoFar - 1] == '\0' &&
1042 data[readSoFar - 2] == '\n') {
1043 --readSoFar;
1045 #endif
1046 if (readSoFar && data[readSoFar - 1] == '\n') {
1047 if (d->openMode & Text) {
1048 // QRingBuffer::readLine() isn't Text aware.
1049 if (readSoFar > 1 && data[readSoFar - 2] == '\r') {
1050 --readSoFar;
1051 data[readSoFar - 1] = '\n';
1054 data[readSoFar] = '\0';
1055 return readSoFar;
1059 if (d->pos != d->devicePos && !sequential && !seek(d->pos))
1060 return qint64(-1);
1061 d->baseReadLineDataCalled = false;
1062 qint64 readBytes = readLineData(data + readSoFar, maxSize - readSoFar);
1063 #if defined QIODEVICE_DEBUG
1064 printf("%p \tread from readLineData: %d bytes, readSoFar = %d bytes\n", this,
1065 int(readBytes), int(readSoFar));
1066 if (readBytes > 0) {
1067 debugBinaryString(data, int(readSoFar + readBytes));
1069 #endif
1070 if (readBytes < 0) {
1071 data[readSoFar] = '\0';
1072 return readSoFar ? readSoFar : -1;
1074 readSoFar += readBytes;
1075 if (!d->baseReadLineDataCalled && !sequential) {
1076 d->pos += readBytes;
1077 // If the base implementation was not called, then we must
1078 // assume the device position is invalid and force a seek.
1079 d->devicePos = qint64(-1);
1081 data[readSoFar] = '\0';
1083 if (d->openMode & Text) {
1084 #if defined(Q_OS_SYMBIAN)
1085 // Open C fgets strips '\r' but readSoFar gets returned as if it was still there
1086 if (readSoFar > 1 && data[readSoFar - 1] == '\0' && data[readSoFar - 2] == '\n') {
1087 --readSoFar;
1089 #endif
1090 if (readSoFar > 1 && data[readSoFar - 1] == '\n' && data[readSoFar - 2] == '\r') {
1091 data[readSoFar - 2] = '\n';
1092 data[readSoFar - 1] = '\0';
1093 --readSoFar;
1097 #if defined QIODEVICE_DEBUG
1098 printf("%p \treturning %d, d->pos = %d, d->buffer.size() = %d, size() = %d\n",
1099 this, int(readSoFar), int(d->pos), d->buffer.size(), int(size()));
1100 debugBinaryString(data, int(readSoFar));
1101 #endif
1102 return readSoFar;
1106 \overload
1108 Reads a line from the device, but no more than \a maxSize characters,
1109 and returns the result as a QByteArray.
1111 This function has no way of reporting errors; returning an empty
1112 QByteArray() can mean either that no data was currently available
1113 for reading, or that an error occurred.
1115 QByteArray QIODevice::readLine(qint64 maxSize)
1117 Q_D(QIODevice);
1118 CHECK_MAXLEN(readLine, QByteArray());
1119 QByteArray tmp;
1120 const int BufferGrowth = 4096;
1121 qint64 readSoFar = 0;
1122 qint64 readBytes = 0;
1124 #if defined QIODEVICE_DEBUG
1125 printf("%p QIODevice::readLine(%d), d->pos = %d, d->buffer.size() = %d\n",
1126 this, int(maxSize), int(d->pos), int(d->buffer.size()));
1127 #else
1128 Q_UNUSED(d);
1129 #endif
1131 do {
1132 if (maxSize != 0)
1133 tmp.resize(int(readSoFar + qMin(int(maxSize), BufferGrowth)));
1134 else
1135 tmp.resize(int(readSoFar + BufferGrowth));
1136 readBytes = readLine(tmp.data() + readSoFar, tmp.size() - readSoFar);
1137 if (readBytes <= 0)
1138 break;
1140 readSoFar += readBytes;
1141 } while ((!maxSize || readSoFar < maxSize) &&
1142 readSoFar + 1 == tmp.size() && // +1 due to the ending null
1143 tmp.at(readSoFar - 1) != '\n');
1145 if (readSoFar == 0 && readBytes == -1)
1146 tmp.clear(); // return Null if we found an error
1147 else
1148 tmp.resize(int(readSoFar));
1149 return tmp;
1153 Reads up to \a maxSize characters into \a data and returns the
1154 number of characters read.
1156 This function is called by readLine(), and provides its base
1157 implementation, using getChar(). Buffered devices can improve the
1158 performance of readLine() by reimplementing this function.
1160 readLine() appends a '\0' byte to \a data; readLineData() does not
1161 need to do this.
1163 If you reimplement this function, be careful to return the correct
1164 value: it should return the number of bytes read in this line,
1165 including the terminating newline, or 0 if there is no line to be
1166 read at this point. If an error occurs, it should return -1 if and
1167 only if no bytes were read. Reading past EOF is considered an error.
1169 qint64 QIODevice::readLineData(char *data, qint64 maxSize)
1171 Q_D(QIODevice);
1172 qint64 readSoFar = 0;
1173 char c;
1174 int lastReadReturn = 0;
1175 d->baseReadLineDataCalled = true;
1177 while (readSoFar < maxSize && (lastReadReturn = read(&c, 1)) == 1) {
1178 *data++ = c;
1179 ++readSoFar;
1180 if (c == '\n')
1181 break;
1184 #if defined QIODEVICE_DEBUG
1185 printf("%p QIODevice::readLineData(%p, %d), d->pos = %d, d->buffer.size() = %d, returns %d\n",
1186 this, data, int(maxSize), int(d->pos), int(d->buffer.size()), int(readSoFar));
1187 #endif
1188 if (lastReadReturn != 1 && readSoFar == 0)
1189 return isSequential() ? lastReadReturn : -1;
1190 return readSoFar;
1194 Returns true if a complete line of data can be read from the device;
1195 otherwise returns false.
1197 Note that unbuffered devices, which have no way of determining what
1198 can be read, always return false.
1200 This function is often called in conjunction with the readyRead()
1201 signal.
1203 Subclasses that reimplement this function must call the base
1204 implementation in order to include the contents of the QIODevice's buffer. Example:
1206 \snippet doc/src/snippets/code/src_corelib_io_qiodevice.cpp 3
1208 \sa readyRead(), readLine()
1210 bool QIODevice::canReadLine() const
1212 return d_func()->buffer.canReadLine();
1216 Writes at most \a maxSize bytes of data from \a data to the
1217 device. Returns the number of bytes that were actually written, or
1218 -1 if an error occurred.
1220 \sa read() writeData()
1222 qint64 QIODevice::write(const char *data, qint64 maxSize)
1224 Q_D(QIODevice);
1225 CHECK_WRITABLE(write, qint64(-1));
1226 CHECK_MAXLEN(write, qint64(-1));
1228 const bool sequential = d->isSequential();
1229 // Make sure the device is positioned correctly.
1230 if (d->pos != d->devicePos && !sequential && !seek(d->pos))
1231 return qint64(-1);
1233 #ifdef Q_OS_WIN
1234 if (d->openMode & Text) {
1235 const char *endOfData = data + maxSize;
1236 const char *startOfBlock = data;
1238 qint64 writtenSoFar = 0;
1240 forever {
1241 const char *endOfBlock = startOfBlock;
1242 while (endOfBlock < endOfData && *endOfBlock != '\n')
1243 ++endOfBlock;
1245 qint64 blockSize = endOfBlock - startOfBlock;
1246 if (blockSize > 0) {
1247 qint64 ret = writeData(startOfBlock, blockSize);
1248 if (ret <= 0) {
1249 if (writtenSoFar && !sequential)
1250 d->buffer.skip(writtenSoFar);
1251 return writtenSoFar ? writtenSoFar : ret;
1253 if (!sequential) {
1254 d->pos += ret;
1255 d->devicePos += ret;
1257 writtenSoFar += ret;
1260 if (endOfBlock == endOfData)
1261 break;
1263 qint64 ret = writeData("\r\n", 2);
1264 if (ret <= 0) {
1265 if (writtenSoFar && !sequential)
1266 d->buffer.skip(writtenSoFar);
1267 return writtenSoFar ? writtenSoFar : ret;
1269 if (!sequential) {
1270 d->pos += ret;
1271 d->devicePos += ret;
1273 ++writtenSoFar;
1275 startOfBlock = endOfBlock + 1;
1278 if (writtenSoFar && !sequential)
1279 d->buffer.skip(writtenSoFar);
1280 return writtenSoFar;
1282 #endif
1284 qint64 written = writeData(data, maxSize);
1285 if (written > 0) {
1286 if (!sequential) {
1287 d->pos += written;
1288 d->devicePos += written;
1290 if (!d->buffer.isEmpty() && !sequential)
1291 d->buffer.skip(written);
1293 return written;
1297 \since 4.5
1299 \overload
1301 Writes data from a zero-terminated string of 8-bit characters to the
1302 device. Returns the number of bytes that were actually written, or
1303 -1 if an error occurred. This is equivalent to
1304 \code
1306 QIODevice::write(data, qstrlen(data));
1308 \endcode
1310 \sa read() writeData()
1312 qint64 QIODevice::write(const char *data)
1314 return write(data, qstrlen(data));
1317 /*! \fn qint64 QIODevice::write(const QByteArray &byteArray)
1319 \overload
1321 Writes the content of \a byteArray to the device. Returns the number of
1322 bytes that were actually written, or -1 if an error occurred.
1324 \sa read() writeData()
1328 Puts the character \a c back into the device, and decrements the
1329 current position unless the position is 0. This function is
1330 usually called to "undo" a getChar() operation, such as when
1331 writing a backtracking parser.
1333 If \a c was not previously read from the device, the behavior is
1334 undefined.
1336 void QIODevice::ungetChar(char c)
1338 Q_D(QIODevice);
1339 CHECK_READABLE(read, Q_VOID);
1341 #if defined QIODEVICE_DEBUG
1342 printf("%p QIODevice::ungetChar(0x%hhx '%c')\n", this, c, isprint(c) ? c : '?');
1343 #endif
1345 d->buffer.ungetChar(c);
1346 if (!d->isSequential())
1347 --d->pos;
1350 /*! \fn bool QIODevice::putChar(char c)
1352 Writes the character \a c to the device. Returns true on success;
1353 otherwise returns false.
1355 \sa write() getChar() ungetChar()
1357 bool QIODevice::putChar(char c)
1359 return d_func()->putCharHelper(c);
1363 \internal
1365 bool QIODevicePrivate::putCharHelper(char c)
1367 return q_func()->write(&c, 1) == 1;
1370 /*! \fn bool QIODevice::getChar(char *c)
1372 Reads one character from the device and stores it in \a c. If \a c
1373 is 0, the character is discarded. Returns true on success;
1374 otherwise returns false.
1376 \sa read() putChar() ungetChar()
1378 bool QIODevice::getChar(char *c)
1380 char ch;
1381 return (1 == read(c ? c : &ch, 1));
1385 \since 4.1
1387 Reads at most \a maxSize bytes from the device into \a data, without side
1388 effects (i.e., if you call read() after peek(), you will get the same
1389 data). Returns the number of bytes read. If an error occurs, such as
1390 when attempting to peek a device opened in WriteOnly mode, this function
1391 returns -1.
1393 0 is returned when no more data is available for reading.
1395 Example:
1397 \snippet doc/src/snippets/code/src_corelib_io_qiodevice.cpp 4
1399 \sa read()
1401 qint64 QIODevice::peek(char *data, qint64 maxSize)
1403 qint64 readBytes = read(data, maxSize);
1404 int i = readBytes;
1405 while (i > 0)
1406 ungetChar(data[i-- - 1]);
1407 return readBytes;
1411 \since 4.1
1412 \overload
1414 Peeks at most \a maxSize bytes from the device, returning the data peeked
1415 as a QByteArray.
1417 Example:
1419 \snippet doc/src/snippets/code/src_corelib_io_qiodevice.cpp 5
1421 This function has no way of reporting errors; returning an empty
1422 QByteArray() can mean either that no data was currently available
1423 for peeking, or that an error occurred.
1425 \sa read()
1427 QByteArray QIODevice::peek(qint64 maxSize)
1429 QByteArray result = read(maxSize);
1430 int i = result.size();
1431 const char *data = result.constData();
1432 while (i > 0)
1433 ungetChar(data[i-- - 1]);
1434 return result;
1438 Blocks until new data is available for reading and the readyRead()
1439 signal has been emitted, or until \a msecs milliseconds have
1440 passed. If msecs is -1, this function will not time out.
1442 Returns true if new data is available for reading; otherwise returns
1443 false (if the operation timed out or if an error occurred).
1445 This function can operate without an event loop. It is
1446 useful when writing non-GUI applications and when performing
1447 I/O operations in a non-GUI thread.
1449 If called from within a slot connected to the readyRead() signal,
1450 readyRead() will not be reemitted.
1452 Reimplement this function to provide a blocking API for a custom
1453 device. The default implementation does nothing, and returns false.
1455 \warning Calling this function from the main (GUI) thread
1456 might cause your user interface to freeze.
1458 \sa waitForBytesWritten()
1460 bool QIODevice::waitForReadyRead(int msecs)
1462 Q_UNUSED(msecs);
1463 return false;
1467 For buffered devices, this function waits until a payload of
1468 buffered written data has been written to the device and the
1469 bytesWritten() signal has been emitted, or until \a msecs
1470 milliseconds have passed. If msecs is -1, this function will
1471 not time out. For unbuffered devices, it returns immediately.
1473 Returns true if a payload of data was written to the device;
1474 otherwise returns false (i.e. if the operation timed out, or if an
1475 error occurred).
1477 This function can operate without an event loop. It is
1478 useful when writing non-GUI applications and when performing
1479 I/O operations in a non-GUI thread.
1481 If called from within a slot connected to the bytesWritten() signal,
1482 bytesWritten() will not be reemitted.
1484 Reimplement this function to provide a blocking API for a custom
1485 device. The default implementation does nothing, and returns false.
1487 \warning Calling this function from the main (GUI) thread
1488 might cause your user interface to freeze.
1490 \sa waitForReadyRead()
1492 bool QIODevice::waitForBytesWritten(int msecs)
1494 Q_UNUSED(msecs);
1495 return false;
1499 Sets the human readable description of the last device error that
1500 occurred to \a str.
1502 \sa errorString()
1504 void QIODevice::setErrorString(const QString &str)
1506 d_func()->errorString = str;
1510 Returns a human-readable description of the last device error that
1511 occurred.
1513 \sa setErrorString()
1515 QString QIODevice::errorString() const
1517 Q_D(const QIODevice);
1518 if (d->errorString.isEmpty()) {
1519 #ifdef QT_NO_QOBJECT
1520 return QLatin1String(QT_TRANSLATE_NOOP(QIODevice, "Unknown error"));
1521 #else
1522 return tr("Unknown error");
1523 #endif
1525 return d->errorString;
1529 \fn qint64 QIODevice::readData(char *data, qint64 maxSize)
1531 Reads up to \a maxSize bytes from the device into \a data, and
1532 returns the number of bytes read or -1 if an error occurred. If
1533 there are no bytes to be read, this function should return -1 if
1534 there can never be more bytes available (for example: socket
1535 closed, pipe closed, sub-process finished).
1537 This function is called by QIODevice. Reimplement this function
1538 when creating a subclass of QIODevice.
1540 \sa read() readLine() writeData()
1544 \fn qint64 QIODevice::writeData(const char *data, qint64 maxSize)
1546 Writes up to \a maxSize bytes from \a data to the device. Returns
1547 the number of bytes written, or -1 if an error occurred.
1549 This function is called by QIODevice. Reimplement this function
1550 when creating a subclass of QIODevice.
1552 \sa read() write()
1556 \fn QIODevice::Offset QIODevice::status() const
1558 For device specific error handling, please refer to the
1559 individual device documentation.
1561 \sa qobject_cast()
1565 \fn QIODevice::Offset QIODevice::at() const
1567 Use pos() instead.
1571 \fn bool QIODevice::at(Offset offset)
1573 Use seek(\a offset) instead.
1576 /*! \fn int QIODevice::flags() const
1578 Use openMode() instead.
1581 /*! \fn int QIODevice::getch()
1583 Use getChar() instead.
1587 \fn bool QIODevice::isAsynchronous() const
1589 This functionality is no longer available. This function always
1590 returns true.
1594 \fn bool QIODevice::isBuffered() const
1596 Use !(openMode() & QIODevice::Unbuffered) instead.
1600 \fn bool QIODevice::isCombinedAccess() const
1602 Use openMode() instead.
1606 \fn bool QIODevice::isDirectAccess() const
1608 Use !isSequential() instead.
1612 \fn bool QIODevice::isInactive() const
1614 Use isOpen(), isReadable(), or isWritable() instead.
1618 \fn bool QIODevice::isRaw() const
1620 Use openMode() instead.
1624 \fn bool QIODevice::isSequentialAccess() const
1626 Use isSequential() instead.
1630 \fn bool QIODevice::isSynchronous() const
1632 This functionality is no longer available. This function always
1633 returns false.
1637 \fn bool QIODevice::isTranslated() const
1639 Use openMode() instead.
1643 \fn bool QIODevice::mode() const
1645 Use openMode() instead.
1648 /*! \fn int QIODevice::putch(int ch)
1650 Use putChar(\a ch) instead.
1653 /*! \fn int QIODevice::ungetch(int ch)
1655 Use ungetChar(\a ch) instead.
1659 \fn quint64 QIODevice::readBlock(char *data, quint64 size)
1661 Use read(\a data, \a size) instead.
1664 /*! \fn int QIODevice::state() const
1666 Use isOpen() instead.
1670 \fn qint64 QIODevice::writeBlock(const char *data, quint64 size)
1672 Use write(\a data, \a size) instead.
1676 \fn qint64 QIODevice::writeBlock(const QByteArray &data)
1678 Use write(\a data) instead.
1681 #if defined QT3_SUPPORT
1682 QIODevice::Status QIODevice::status() const
1684 #if !defined(QT_NO_QOBJECT)
1685 const QFile *f = qobject_cast<const QFile *>(this);
1686 if (f) return (int) f->error();
1687 #endif
1688 return isOpen() ? 0 /* IO_Ok */ : 8 /* IO_UnspecifiedError */;
1692 For device specific error handling, please refer to the
1693 individual device documentation.
1695 \sa qobject_cast()
1697 void QIODevice::resetStatus()
1699 #if !defined(QT_NO_QOBJECT)
1700 QFile *f = qobject_cast<QFile *>(this);
1701 if (f) f->unsetError();
1702 #endif
1704 #endif
1706 #if !defined(QT_NO_DEBUG_STREAM)
1707 QDebug operator<<(QDebug debug, QIODevice::OpenMode modes)
1709 debug << "OpenMode(";
1710 QStringList modeList;
1711 if (modes == QIODevice::NotOpen) {
1712 modeList << QLatin1String("NotOpen");
1713 } else {
1714 if (modes & QIODevice::ReadOnly)
1715 modeList << QLatin1String("ReadOnly");
1716 if (modes & QIODevice::WriteOnly)
1717 modeList << QLatin1String("WriteOnly");
1718 if (modes & QIODevice::Append)
1719 modeList << QLatin1String("Append");
1720 if (modes & QIODevice::Truncate)
1721 modeList << QLatin1String("Truncate");
1722 if (modes & QIODevice::Text)
1723 modeList << QLatin1String("Text");
1724 if (modes & QIODevice::Unbuffered)
1725 modeList << QLatin1String("Unbuffered");
1727 qSort(modeList);
1728 debug << modeList.join(QLatin1String("|"));
1729 debug << ')';
1730 return debug;
1732 #endif
1734 QT_END_NAMESPACE