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