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