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