| 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 |
#include "qdatastream.h" |
| 43 |
|
| 44 |
#ifndef QT_NO_DATASTREAM |
| 45 |
#include "qbuffer.h" |
| 46 |
#include "qstring.h" |
| 47 |
#include <stdio.h> |
| 48 |
#include <ctype.h> |
| 49 |
#include <stdlib.h> |
| 50 |
|
| 51 |
QT_BEGIN_NAMESPACE |
| 52 |
|
| 53 |
/*! |
| 54 |
\class QDataStream |
| 55 |
\reentrant |
| 56 |
\brief The QDataStream class provides serialization of binary data |
| 57 |
to a QIODevice. |
| 58 |
|
| 59 |
\ingroup io |
| 60 |
\mainclass |
| 61 |
|
| 62 |
A data stream is a binary stream of encoded information which is |
| 63 |
100% independent of the host computer's operating system, CPU or |
| 64 |
byte order. For example, a data stream that is written by a PC |
| 65 |
under Windows can be read by a Sun SPARC running Solaris. |
| 66 |
|
| 67 |
You can also use a data stream to read/write \l{raw}{raw |
| 68 |
unencoded binary data}. If you want a "parsing" input stream, see |
| 69 |
QTextStream. |
| 70 |
|
| 71 |
The QDataStream class implements the serialization of C++'s basic |
| 72 |
data types, like \c char, \c short, \c int, \c{char *}, etc. |
| 73 |
Serialization of more complex data is accomplished by breaking up |
| 74 |
the data into primitive units. |
| 75 |
|
| 76 |
A data stream cooperates closely with a QIODevice. A QIODevice |
| 77 |
represents an input/output medium one can read data from and write |
| 78 |
data to. The QFile class is an example of an I/O device. |
| 79 |
|
| 80 |
Example (write binary data to a stream): |
| 81 |
|
| 82 |
\snippet doc/src/snippets/code/src_corelib_io_qdatastream.cpp 0 |
| 83 |
|
| 84 |
Example (read binary data from a stream): |
| 85 |
|
| 86 |
\snippet doc/src/snippets/code/src_corelib_io_qdatastream.cpp 1 |
| 87 |
|
| 88 |
Each item written to the stream is written in a predefined binary |
| 89 |
format that varies depending on the item's type. Supported Qt |
| 90 |
types include QBrush, QColor, QDateTime, QFont, QPixmap, QString, |
| 91 |
QVariant and many others. For the complete list of all Qt types |
| 92 |
supporting data streaming see the \l{Format of the QDataStream |
| 93 |
operators}. |
| 94 |
|
| 95 |
For integers it is best to always cast to a Qt integer type for |
| 96 |
writing, and to read back into the same Qt integer type. This |
| 97 |
ensures that you get integers of the size you want and insulates |
| 98 |
you from compiler and platform differences. |
| 99 |
|
| 100 |
To take one example, a \c{char *} string is written as a 32-bit |
| 101 |
integer equal to the length of the string including the '\\0' byte, |
| 102 |
followed by all the characters of the string including the |
| 103 |
'\\0' byte. When reading a \c{char *} string, 4 bytes are read to |
| 104 |
create the 32-bit length value, then that many characters for the |
| 105 |
\c {char *} string including the '\\0' terminator are read. |
| 106 |
|
| 107 |
The initial I/O device is usually set in the constructor, but can be |
| 108 |
changed with setDevice(). If you've reached the end of the data |
| 109 |
(or if there is no I/O device set) atEnd() will return true. |
| 110 |
|
| 111 |
\section1 Versioning |
| 112 |
|
| 113 |
QDataStream's binary format has evolved since Qt 1.0, and is |
| 114 |
likely to continue evolving to reflect changes done in Qt. When |
| 115 |
inputting or outputting complex types, it's very important to |
| 116 |
make sure that the same version of the stream (version()) is used |
| 117 |
for reading and writing. If you need both forward and backward |
| 118 |
compatibility, you can hardcode the version number in the |
| 119 |
application: |
| 120 |
|
| 121 |
\snippet doc/src/snippets/code/src_corelib_io_qdatastream.cpp 2 |
| 122 |
|
| 123 |
If you are producing a new binary data format, such as a file |
| 124 |
format for documents created by your application, you could use a |
| 125 |
QDataStream to write the data in a portable format. Typically, you |
| 126 |
would write a brief header containing a magic string and a version |
| 127 |
number to give yourself room for future expansion. For example: |
| 128 |
|
| 129 |
\snippet doc/src/snippets/code/src_corelib_io_qdatastream.cpp 3 |
| 130 |
|
| 131 |
Then read it in with: |
| 132 |
|
| 133 |
\snippet doc/src/snippets/code/src_corelib_io_qdatastream.cpp 4 |
| 134 |
|
| 135 |
You can select which byte order to use when serializing data. The |
| 136 |
default setting is big endian (MSB first). Changing it to little |
| 137 |
endian breaks the portability (unless the reader also changes to |
| 138 |
little endian). We recommend keeping this setting unless you have |
| 139 |
special requirements. |
| 140 |
|
| 141 |
\target raw |
| 142 |
\section1 Reading and writing raw binary data |
| 143 |
|
| 144 |
You may wish to read/write your own raw binary data to/from the |
| 145 |
data stream directly. Data may be read from the stream into a |
| 146 |
preallocated \c{char *} using readRawData(). Similarly data can be |
| 147 |
written to the stream using writeRawData(). Note that any |
| 148 |
encoding/decoding of the data must be done by you. |
| 149 |
|
| 150 |
A similar pair of functions is readBytes() and writeBytes(). These |
| 151 |
differ from their \e raw counterparts as follows: readBytes() |
| 152 |
reads a quint32 which is taken to be the length of the data to be |
| 153 |
read, then that number of bytes is read into the preallocated |
| 154 |
\c{char *}; writeBytes() writes a quint32 containing the length of the |
| 155 |
data, followed by the data. Note that any encoding/decoding of |
| 156 |
the data (apart from the length quint32) must be done by you. |
| 157 |
|
| 158 |
\target Serializing Qt Classes |
| 159 |
\section1 Reading and writing other Qt classes. |
| 160 |
|
| 161 |
In addition to the overloaded stream operators documented here, |
| 162 |
any Qt classes that you might want to serialize to a QDataStream |
| 163 |
will have appropriate stream operators declared as non-member of |
| 164 |
the class: |
| 165 |
|
| 166 |
\code |
| 167 |
QDataStream &operator<<(QDataStream &, const QXxx &); |
| 168 |
QDataStream &operator>>(QDataStream &, QXxx &); |
| 169 |
\endcode |
| 170 |
|
| 171 |
For example, here are the stream operators declared as non-members |
| 172 |
of the QImage class: |
| 173 |
|
| 174 |
\code |
| 175 |
QDataStream & operator<< (QDataStream& stream, const QImage& image); |
| 176 |
QDataStream & operator>> (QDataStream& stream, QImage& image); |
| 177 |
\endcode |
| 178 |
|
| 179 |
To see if your favorite Qt class has similar stream operators |
| 180 |
defined, check the \bold {Related Non-Members} section of the |
| 181 |
class's documentation page. |
| 182 |
|
| 183 |
\sa QTextStream QVariant |
| 184 |
*/ |
| 185 |
|
| 186 |
/*! |
| 187 |
\enum QDataStream::ByteOrder |
| 188 |
|
| 189 |
The byte order used for reading/writing the data. |
| 190 |
|
| 191 |
\value BigEndian Most significant byte first (the default) |
| 192 |
\value LittleEndian Least significant byte first |
| 193 |
*/ |
| 194 |
|
| 195 |
/*! |
| 196 |
\enum QDataStream::Status |
| 197 |
|
| 198 |
This enum describes the current status of the data stream. |
| 199 |
|
| 200 |
\value Ok The data stream is operating normally. |
| 201 |
\value ReadPastEnd The data stream has read past the end of the |
| 202 |
data in the underlying device. |
| 203 |
\value ReadCorruptData The data stream has read corrupt data. |
| 204 |
*/ |
| 205 |
|
| 206 |
/***************************************************************************** |
| 207 |
QDataStream member functions |
| 208 |
*****************************************************************************/ |
| 209 |
|
| 210 |
#undef CHECK_STREAM_PRECOND |
| 211 |
#ifndef QT_NO_DEBUG |
| 212 |
#define CHECK_STREAM_PRECOND(retVal) \ |
| 213 |
if (!dev) { \ |
| 214 |
qWarning("QDataStream: No device"); \ |
| 215 |
return retVal; \ |
| 216 |
} |
| 217 |
#else |
| 218 |
#define CHECK_STREAM_PRECOND(retVal) \ |
| 219 |
if (!dev) { \ |
| 220 |
return retVal; \ |
| 221 |
} |
| 222 |
#endif |
| 223 |
|
| 224 |
enum { |
| 225 |
DefaultStreamVersion = QDataStream::Qt_4_5 |
| 226 |
}; |
| 227 |
|
| 228 |
// ### 5.0: when streaming invalid QVariants, just the type should |
| 229 |
// be written, no "data" after it |
| 230 |
|
| 231 |
/*! |
| 232 |
Constructs a data stream that has no I/O device. |
| 233 |
|
| 234 |
\sa setDevice() |
| 235 |
*/ |
| 236 |
|
| 237 |
QDataStream::QDataStream() |
| 238 |
{ |
| 239 |
dev = 0; |
| 240 |
owndev = false; |
| 241 |
byteorder = BigEndian; |
| 242 |
ver = DefaultStreamVersion; |
| 243 |
noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian; |
| 244 |
q_status = Ok; |
| 245 |
} |
| 246 |
|
| 247 |
/*! |
| 248 |
Constructs a data stream that uses the I/O device \a d. |
| 249 |
|
| 250 |
\warning If you use QSocket or QSocketDevice as the I/O device \a d |
| 251 |
for reading data, you must make sure that enough data is available |
| 252 |
on the socket for the operation to successfully proceed; |
| 253 |
QDataStream does not have any means to handle or recover from |
| 254 |
short-reads. |
| 255 |
|
| 256 |
\sa setDevice(), device() |
| 257 |
*/ |
| 258 |
|
| 259 |
QDataStream::QDataStream(QIODevice *d) |
| 260 |
{ |
| 261 |
dev = d; // set device |
| 262 |
owndev = false; |
| 263 |
byteorder = BigEndian; // default byte order |
| 264 |
ver = DefaultStreamVersion; |
| 265 |
noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian; |
| 266 |
q_status = Ok; |
| 267 |
} |
| 268 |
|
| 269 |
#ifdef QT3_SUPPORT |
| 270 |
/*! |
| 271 |
\fn QDataStream::QDataStream(QByteArray *array, int mode) |
| 272 |
\compat |
| 273 |
|
| 274 |
Constructs a data stream that operates on the given \a array. The |
| 275 |
\a mode specifies how the byte array is to be used, and is |
| 276 |
usually either QIODevice::ReadOnly or QIODevice::WriteOnly. |
| 277 |
*/ |
| 278 |
QDataStream::QDataStream(QByteArray *a, int mode) |
| 279 |
{ |
| 280 |
QBuffer *buf = new QBuffer(a); |
| 281 |
#ifndef QT_NO_QOBJECT |
| 282 |
buf->blockSignals(true); |
| 283 |
#endif |
| 284 |
buf->open(QIODevice::OpenMode(mode)); |
| 285 |
dev = buf; |
| 286 |
owndev = true; |
| 287 |
byteorder = BigEndian; |
| 288 |
ver = DefaultStreamVersion; |
| 289 |
noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian; |
| 290 |
q_status = Ok; |
| 291 |
} |
| 292 |
#endif |
| 293 |
|
| 294 |
/*! |
| 295 |
\fn QDataStream::QDataStream(QByteArray *a, QIODevice::OpenMode mode) |
| 296 |
|
| 297 |
Constructs a data stream that operates on a byte array, \a a. The |
| 298 |
\a mode describes how the device is to be used. |
| 299 |
|
| 300 |
Alternatively, you can use QDataStream(const QByteArray &) if you |
| 301 |
just want to read from a byte array. |
| 302 |
|
| 303 |
Since QByteArray is not a QIODevice subclass, internally a QBuffer |
| 304 |
is created to wrap the byte array. |
| 305 |
*/ |
| 306 |
|
| 307 |
QDataStream::QDataStream(QByteArray *a, QIODevice::OpenMode flags) |
| 308 |
{ |
| 309 |
QBuffer *buf = new QBuffer(a); |
| 310 |
#ifndef QT_NO_QOBJECT |
| 311 |
buf->blockSignals(true); |
| 312 |
#endif |
| 313 |
buf->open(flags); |
| 314 |
dev = buf; |
| 315 |
owndev = true; |
| 316 |
byteorder = BigEndian; |
| 317 |
ver = DefaultStreamVersion; |
| 318 |
noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian; |
| 319 |
q_status = Ok; |
| 320 |
} |
| 321 |
|
| 322 |
/*! |
| 323 |
Constructs a read-only data stream that operates on byte array \a a. |
| 324 |
Use QDataStream(QByteArray*, int) if you want to write to a byte |
| 325 |
array. |
| 326 |
|
| 327 |
Since QByteArray is not a QIODevice subclass, internally a QBuffer |
| 328 |
is created to wrap the byte array. |
| 329 |
*/ |
| 330 |
QDataStream::QDataStream(const QByteArray &a) |
| 331 |
{ |
| 332 |
QBuffer *buf = new QBuffer; |
| 333 |
#ifndef QT_NO_QOBJECT |
| 334 |
buf->blockSignals(true); |
| 335 |
#endif |
| 336 |
buf->setData(a); |
| 337 |
buf->open(QIODevice::ReadOnly); |
| 338 |
dev = buf; |
| 339 |
owndev = true; |
| 340 |
byteorder = BigEndian; |
| 341 |
ver = DefaultStreamVersion; |
| 342 |
noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian; |
| 343 |
q_status = Ok; |
| 344 |
} |
| 345 |
|
| 346 |
/*! |
| 347 |
Destroys the data stream. |
| 348 |
|
| 349 |
The destructor will not affect the current I/O device, unless it is |
| 350 |
an internal I/O device (e.g. a QBuffer) processing a QByteArray |
| 351 |
passed in the \e constructor, in which case the internal I/O device |
| 352 |
is destroyed. |
| 353 |
*/ |
| 354 |
|
| 355 |
QDataStream::~QDataStream() |
| 356 |
{ |
| 357 |
if (owndev) |
| 358 |
delete dev; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
/*! |
| 363 |
\fn QIODevice *QDataStream::device() const |
| 364 |
|
| 365 |
Returns the I/O device currently set. |
| 366 |
|
| 367 |
\sa setDevice(), unsetDevice() |
| 368 |
*/ |
| 369 |
|
| 370 |
/*! |
| 371 |
void QDataStream::setDevice(QIODevice *d) |
| 372 |
|
| 373 |
Sets the I/O device to \a d. |
| 374 |
|
| 375 |
\sa device(), unsetDevice() |
| 376 |
*/ |
| 377 |
|
| 378 |
void QDataStream::setDevice(QIODevice *d) |
| 379 |
{ |
| 380 |
if (owndev) { |
| 381 |
delete dev; |
| 382 |
owndev = false; |
| 383 |
} |
| 384 |
dev = d; |
| 385 |
} |
| 386 |
|
| 387 |
/*! |
| 388 |
\obsolete |
| 389 |
Unsets the I/O device. |
| 390 |
Use setDevice(0) instead. |
| 391 |
*/ |
| 392 |
|
| 393 |
void QDataStream::unsetDevice() |
| 394 |
{ |
| 395 |
setDevice(0); |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
/*! |
| 400 |
\fn bool QDataStream::atEnd() const |
| 401 |
|
| 402 |
Returns true if the I/O device has reached the end position (end of |
| 403 |
the stream or file) or if there is no I/O device set; otherwise |
| 404 |
returns false. |
| 405 |
|
| 406 |
\sa QIODevice::atEnd() |
| 407 |
*/ |
| 408 |
|
| 409 |
bool QDataStream::atEnd() const |
| 410 |
{ |
| 411 |
return dev ? dev->atEnd() : true; |
| 412 |
} |
| 413 |
|
| 414 |
/*! |
| 415 |
Returns the status of the data stream. |
| 416 |
|
| 417 |
\sa Status setStatus() resetStatus() |
| 418 |
*/ |
| 419 |
|
| 420 |
QDataStream::Status QDataStream::status() const |
| 421 |
{ |
| 422 |
return q_status; |
| 423 |
} |
| 424 |
|
| 425 |
/*! |
| 426 |
Resets the status of the data stream. |
| 427 |
|
| 428 |
\sa Status status() setStatus() |
| 429 |
*/ |
| 430 |
void QDataStream::resetStatus() |
| 431 |
{ |
| 432 |
q_status = Ok; |
| 433 |
} |
| 434 |
|
| 435 |
/*! |
| 436 |
Sets the status of the data stream to the \a status given. |
| 437 |
|
| 438 |
\sa Status status() resetStatus() |
| 439 |
*/ |
| 440 |
void QDataStream::setStatus(Status status) |
| 441 |
{ |
| 442 |
if (q_status == Ok) |
| 443 |
q_status = status; |
| 444 |
} |
| 445 |
|
| 446 |
/*!\fn bool QDataStream::eof() const |
| 447 |
|
| 448 |
Use atEnd() instead. |
| 449 |
*/ |
| 450 |
|
| 451 |
/*! |
| 452 |
\fn int QDataStream::byteOrder() const |
| 453 |
|
| 454 |
Returns the current byte order setting -- either BigEndian or |
| 455 |
LittleEndian. |
| 456 |
|
| 457 |
\sa setByteOrder() |
| 458 |
*/ |
| 459 |
|
| 460 |
/*! |
| 461 |
Sets the serialization byte order to \a bo. |
| 462 |
|
| 463 |
The \a bo parameter can be QDataStream::BigEndian or |
| 464 |
QDataStream::LittleEndian. |
| 465 |
|
| 466 |
The default setting is big endian. We recommend leaving this |
| 467 |
setting unless you have special requirements. |
| 468 |
|
| 469 |
\sa byteOrder() |
| 470 |
*/ |
| 471 |
|
| 472 |
void QDataStream::setByteOrder(ByteOrder bo) |
| 473 |
{ |
| 474 |
byteorder = bo; |
| 475 |
if (QSysInfo::ByteOrder == QSysInfo::BigEndian) |
| 476 |
noswap = (byteorder == BigEndian); |
| 477 |
else |
| 478 |
noswap = (byteorder == LittleEndian); |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
/*! |
| 483 |
\fn bool QDataStream::isPrintableData() const |
| 484 |
|
| 485 |
In Qt 4, this function always returns false. |
| 486 |
|
| 487 |
\sa setPrintableData() |
| 488 |
*/ |
| 489 |
|
| 490 |
/*! |
| 491 |
\fn void QDataStream::setPrintableData(bool enable) |
| 492 |
|
| 493 |
In Qt 3, this function enabled output in a human-readable |
| 494 |
format if \a enable was false. |
| 495 |
|
| 496 |
In Qt 4, QDataStream no longer provides a human-readable output. |
| 497 |
This function does nothing. |
| 498 |
*/ |
| 499 |
|
| 500 |
/*! |
| 501 |
\enum QDataStream::Version |
| 502 |
|
| 503 |
This enum provides symbolic synonyms for the data serialization |
| 504 |
format version numbers. |
| 505 |
|
| 506 |
\value Qt_1_0 Version 1 (Qt 1.x) |
| 507 |
\value Qt_2_0 Version 2 (Qt 2.0) |
| 508 |
\value Qt_2_1 Version 3 (Qt 2.1, 2.2, 2.3) |
| 509 |
\value Qt_3_0 Version 4 (Qt 3.0) |
| 510 |
\value Qt_3_1 Version 5 (Qt 3.1, 3.2) |
| 511 |
\value Qt_3_3 Version 6 (Qt 3.3) |
| 512 |
\value Qt_4_0 Version 7 (Qt 4.0, Qt 4.1) |
| 513 |
\value Qt_4_1 Version 7 (Qt 4.0, Qt 4.1) |
| 514 |
\value Qt_4_2 Version 8 (Qt 4.2) |
| 515 |
\value Qt_4_3 Version 9 (Qt 4.3) |
| 516 |
\value Qt_4_4 Version 10 (Qt 4.4) |
| 517 |
\value Qt_4_5 Version 11 (Qt 4.5) |
| 518 |
\omitvalue Qt_4_6 |
| 519 |
|
| 520 |
\sa setVersion(), version() |
| 521 |
*/ |
| 522 |
|
| 523 |
/*! |
| 524 |
\fn int QDataStream::version() const |
| 525 |
|
| 526 |
Returns the version number of the data serialization format. |
| 527 |
|
| 528 |
\sa setVersion(), Version |
| 529 |
*/ |
| 530 |
|
| 531 |
/*! |
| 532 |
\fn void QDataStream::setVersion(int v) |
| 533 |
|
| 534 |
Sets the version number of the data serialization format to \a v. |
| 535 |
|
| 536 |
You don't \e have to set a version if you are using the current |
| 537 |
version of Qt, but for your own custom binary formats we |
| 538 |
recommend that you do; see \l{Versioning} in the Detailed |
| 539 |
Description. |
| 540 |
|
| 541 |
In order to accommodate new functionality, the datastream |
| 542 |
serialization format of some Qt classes has changed in some |
| 543 |
versions of Qt. If you want to read data that was created by an |
| 544 |
earlier version of Qt, or write data that can be read by a |
| 545 |
program that was compiled with an earlier version of Qt, use this |
| 546 |
function to modify the serialization format used by QDataStream. |
| 547 |
|
| 548 |
\table |
| 549 |
\header \i Qt Version \i QDataStream Version |
| 550 |
\row \i Qt 4.4 \i 10 |
| 551 |
\row \i Qt 4.3 \i 9 |
| 552 |
\row \i Qt 4.2 \i 8 |
| 553 |
\row \i Qt 4.0, 4.1 \i 7 |
| 554 |
\row \i Qt 3.3 \i 6 |
| 555 |
\row \i Qt 3.1, 3.2 \i 5 |
| 556 |
\row \i Qt 3.0 \i 4 |
| 557 |
\row \i Qt 2.1, 2.2, 2.3 \i 3 |
| 558 |
\row \i Qt 2.0 \i 2 |
| 559 |
\row \i Qt 1.x \i 1 |
| 560 |
\endtable |
| 561 |
|
| 562 |
The \l Version enum provides symbolic constants for the different |
| 563 |
versions of Qt. For example: |
| 564 |
|
| 565 |
\snippet doc/src/snippets/code/src_corelib_io_qdatastream.cpp 5 |
| 566 |
|
| 567 |
\sa version(), Version |
| 568 |
*/ |
| 569 |
|
| 570 |
/***************************************************************************** |
| 571 |
QDataStream read functions |
| 572 |
*****************************************************************************/ |
| 573 |
|
| 574 |
/*! |
| 575 |
\fn QDataStream &QDataStream::operator>>(quint8 &i) |
| 576 |
\overload |
| 577 |
|
| 578 |
Reads an unsigned byte from the stream into \a i, and returns a |
| 579 |
reference to the stream. |
| 580 |
*/ |
| 581 |
|
| 582 |
/*! |
| 583 |
Reads a signed byte from the stream into \a i, and returns a |
| 584 |
reference to the stream. |
| 585 |
*/ |
| 586 |
|
| 587 |
QDataStream &QDataStream::operator>>(qint8 &i) |
| 588 |
{ |
| 589 |
i = 0; |
| 590 |
CHECK_STREAM_PRECOND(*this) |
| 591 |
char c; |
| 592 |
if (!dev->getChar(&c)) |
| 593 |
setStatus(ReadPastEnd); |
| 594 |
else |
| 595 |
i = qint8(c); |
| 596 |
return *this; |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
/*! |
| 601 |
\fn QDataStream &QDataStream::operator>>(quint16 &i) |
| 602 |
\overload |
| 603 |
|
| 604 |
Reads an unsigned 16-bit integer from the stream into \a i, and |
| 605 |
returns a reference to the stream. |
| 606 |
*/ |
| 607 |
|
| 608 |
/*! |
| 609 |
\overload |
| 610 |
|
| 611 |
Reads a signed 16-bit integer from the stream into \a i, and |
| 612 |
returns a reference to the stream. |
| 613 |
*/ |
| 614 |
|
| 615 |
QDataStream &QDataStream::operator>>(qint16 &i) |
| 616 |
{ |
| 617 |
i = 0; |
| 618 |
CHECK_STREAM_PRECOND(*this) |
| 619 |
if (noswap) { |
| 620 |
if (dev->read((char *)&i, 2) != 2) { |
| 621 |
i = 0; |
| 622 |
setStatus(ReadPastEnd); |
| 623 |
} |
| 624 |
} else { |
| 625 |
union { |
| 626 |
qint16 val1; |
| 627 |
char val2[2]; |
| 628 |
} x; |
| 629 |
char *p = x.val2; |
| 630 |
char b[2]; |
| 631 |
if (dev->read(b, 2) == 2) { |
| 632 |
*p++ = b[1]; |
| 633 |
*p = b[0]; |
| 634 |
i = x.val1; |
| 635 |
} else { |
| 636 |
setStatus(ReadPastEnd); |
| 637 |
} |
| 638 |
} |
| 639 |
return *this; |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
/*! |
| 644 |
\fn QDataStream &QDataStream::operator>>(quint32 &i) |
| 645 |
\overload |
| 646 |
|
| 647 |
Reads an unsigned 32-bit integer from the stream into \a i, and |
| 648 |
returns a reference to the stream. |
| 649 |
*/ |
| 650 |
|
| 651 |
/*! |
| 652 |
\overload |
| 653 |
|
| 654 |
Reads a signed 32-bit integer from the stream into \a i, and |
| 655 |
returns a reference to the stream. |
| 656 |
*/ |
| 657 |
|
| 658 |
QDataStream &QDataStream::operator>>(qint32 &i) |
| 659 |
{ |
| 660 |
i = 0; |
| 661 |
CHECK_STREAM_PRECOND(*this) |
| 662 |
if (noswap) { |
| 663 |
if (dev->read((char *)&i, 4) != 4) { |
| 664 |
i = 0; |
| 665 |
setStatus(ReadPastEnd); |
| 666 |
} |
| 667 |
} else { // swap bytes |
| 668 |
union { |
| 669 |
qint32 val1; |
| 670 |
char val2[4]; |
| 671 |
} x; |
| 672 |
char *p = x.val2; |
| 673 |
char b[4]; |
| 674 |
if (dev->read(b, 4) == 4) { |
| 675 |
*p++ = b[3]; |
| 676 |
*p++ = b[2]; |
| 677 |
*p++ = b[1]; |
| 678 |
*p = b[0]; |
| 679 |
i = x.val1; |
| 680 |
} else { |
| 681 |
setStatus(ReadPastEnd); |
| 682 |
} |
| 683 |
} |
| 684 |
return *this; |
| 685 |
} |
| 686 |
|
| 687 |
/*! |
| 688 |
\fn QDataStream &QDataStream::operator>>(quint64 &i) |
| 689 |
\overload |
| 690 |
|
| 691 |
Reads an unsigned 64-bit integer from the stream, into \a i, and |
| 692 |
returns a reference to the stream. |
| 693 |
*/ |
| 694 |
|
| 695 |
/*! |
| 696 |
\overload |
| 697 |
|
| 698 |
Reads a signed 64-bit integer from the stream into \a i, and |
| 699 |
returns a reference to the stream. |
| 700 |
*/ |
| 701 |
|
| 702 |
QDataStream &QDataStream::operator>>(qint64 &i) |
| 703 |
{ |
| 704 |
i = qint64(0); |
| 705 |
CHECK_STREAM_PRECOND(*this) |
| 706 |
if (version() < 6) { |
| 707 |
quint32 i1, i2; |
| 708 |
*this >> i2 >> i1; |
| 709 |
i = ((quint64)i1 << 32) + i2; |
| 710 |
} else if (noswap) { // no conversion needed |
| 711 |
if (dev->read((char *)&i, 8) != 8) { |
| 712 |
i = qint64(0); |
| 713 |
setStatus(ReadPastEnd); |
| 714 |
} |
| 715 |
} else { // swap bytes |
| 716 |
union { |
| 717 |
qint64 val1; |
| 718 |
char val2[8]; |
| 719 |
} x; |
| 720 |
|
| 721 |
char *p = x.val2; |
| 722 |
char b[8]; |
| 723 |
if (dev->read(b, 8) == 8) { |
| 724 |
*p++ = b[7]; |
| 725 |
*p++ = b[6]; |
| 726 |
*p++ = b[5]; |
| 727 |
*p++ = b[4]; |
| 728 |
*p++ = b[3]; |
| 729 |
*p++ = b[2]; |
| 730 |
*p++ = b[1]; |
| 731 |
*p = b[0]; |
| 732 |
i = x.val1; |
| 733 |
} else { |
| 734 |
setStatus(ReadPastEnd); |
| 735 |
} |
| 736 |
} |
| 737 |
return *this; |
| 738 |
} |
| 739 |
|
| 740 |
/*! |
| 741 |
Reads a boolean value from the stream into \a i. Returns a |
| 742 |
reference to the stream. |
| 743 |
*/ |
| 744 |
QDataStream &QDataStream::operator>>(bool &i) |
| 745 |
{ |
| 746 |
qint8 v; |
| 747 |
*this >> v; |
| 748 |
i = !!v; |
| 749 |
return *this; |
| 750 |
} |
| 751 |
|
| 752 |
/*! |
| 753 |
\overload |
| 754 |
|
| 755 |
Reads a 32-bit floating point number from the stream into \a f, |
| 756 |
using the standard IEEE 754 format. Returns a reference to the |
| 757 |
stream. |
| 758 |
*/ |
| 759 |
|
| 760 |
QDataStream &QDataStream::operator>>(float &f) |
| 761 |
{ |
| 762 |
f = 0.0f; |
| 763 |
CHECK_STREAM_PRECOND(*this) |
| 764 |
if (noswap) { |
| 765 |
if (dev->read((char *)&f, 4) != 4) { |
| 766 |
f = 0.0f; |
| 767 |
setStatus(ReadPastEnd); |
| 768 |
} |
| 769 |
} else { // swap bytes |
| 770 |
union { |
| 771 |
float val1; |
| 772 |
char val2[4]; |
| 773 |
} x; |
| 774 |
|
| 775 |
char *p = x.val2; |
| 776 |
char b[4]; |
| 777 |
if (dev->read(b, 4) == 4) { |
| 778 |
*p++ = b[3]; |
| 779 |
*p++ = b[2]; |
| 780 |
*p++ = b[1]; |
| 781 |
*p = b[0]; |
| 782 |
f = x.val1; |
| 783 |
} else { |
| 784 |
setStatus(ReadPastEnd); |
| 785 |
} |
| 786 |
} |
| 787 |
return *this; |
| 788 |
} |
| 789 |
|
| 790 |
#if defined(Q_DOUBLE_FORMAT) |
| 791 |
#define Q_DF(x) Q_DOUBLE_FORMAT[(x)] - '0' |
| 792 |
#endif |
| 793 |
|
| 794 |
/*! |
| 795 |
\overload |
| 796 |
|
| 797 |
Reads a 64-bit floating point number from the stream into \a f, |
| 798 |
using the standard IEEE 754 format. Returns a reference to the |
| 799 |
stream. |
| 800 |
*/ |
| 801 |
|
| 802 |
QDataStream &QDataStream::operator>>(double &f) |
| 803 |
{ |
| 804 |
f = 0.0; |
| 805 |
CHECK_STREAM_PRECOND(*this) |
| 806 |
#ifndef Q_DOUBLE_FORMAT |
| 807 |
if (noswap) { |
| 808 |
if (dev->read((char *)&f, 8) != 8) { |
| 809 |
f = 0.0; |
| 810 |
setStatus(ReadPastEnd); |
| 811 |
} |
| 812 |
} else { // swap bytes |
| 813 |
union { |
| 814 |
double val1; |
| 815 |
char val2[8]; |
| 816 |
} x; |
| 817 |
char *p = x.val2; |
| 818 |
char b[8]; |
| 819 |
if (dev->read(b, 8) == 8) { |
| 820 |
*p++ = b[7]; |
| 821 |
*p++ = b[6]; |
| 822 |
*p++ = b[5]; |
| 823 |
*p++ = b[4]; |
| 824 |
*p++ = b[3]; |
| 825 |
*p++ = b[2]; |
| 826 |
*p++ = b[1]; |
| 827 |
*p = b[0]; |
| 828 |
f = x.val1; |
| 829 |
} else { |
| 830 |
setStatus(ReadPastEnd); |
| 831 |
} |
| 832 |
} |
| 833 |
#else |
| 834 |
//non-standard floating point format |
| 835 |
union { |
| 836 |
double val1; |
| 837 |
char val2[8]; |
| 838 |
} x; |
| 839 |
char *p = x.val2; |
| 840 |
char b[8]; |
| 841 |
if (dev->read(b, 8) == 8) { |
| 842 |
if (noswap) { |
| 843 |
*p++ = b[Q_DF(0)]; |
| 844 |
*p++ = b[Q_DF(1)]; |
| 845 |
*p++ = b[Q_DF(2)]; |
| 846 |
*p++ = b[Q_DF(3)]; |
| 847 |
*p++ = b[Q_DF(4)]; |
| 848 |
*p++ = b[Q_DF(5)]; |
| 849 |
*p++ = b[Q_DF(6)]; |
| 850 |
*p = b[Q_DF(7)]; |
| 851 |
} else { |
| 852 |
*p++ = b[Q_DF(7)]; |
| 853 |
*p++ = b[Q_DF(6)]; |
| 854 |
*p++ = b[Q_DF(5)]; |
| 855 |
*p++ = b[Q_DF(4)]; |
| 856 |
*p++ = b[Q_DF(3)]; |
| 857 |
*p++ = b[Q_DF(2)]; |
| 858 |
*p++ = b[Q_DF(1)]; |
| 859 |
*p = b[Q_DF(0)]; |
| 860 |
} |
| 861 |
f = x.val1; |
| 862 |
} else { |
| 863 |
setStatus(ReadPastEnd); |
| 864 |
} |
| 865 |
#endif |
| 866 |
return *this; |
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
/*! |
| 871 |
\overload |
| 872 |
|
| 873 |
Reads the '\0'-terminated string \a s from the stream and returns |
| 874 |
a reference to the stream. |
| 875 |
|
| 876 |
Space for the string is allocated using \c new -- the caller must |
| 877 |
destroy it with \c{delete[]}. |
| 878 |
*/ |
| 879 |
|
| 880 |
QDataStream &QDataStream::operator>>(char *&s) |
| 881 |
{ |
| 882 |
uint len = 0; |
| 883 |
return readBytes(s, len); |
| 884 |
} |
| 885 |
|
| 886 |
|
| 887 |
/*! |
| 888 |
Reads the buffer \a s from the stream and returns a reference to |
| 889 |
the stream. |
| 890 |
|
| 891 |
The buffer \a s is allocated using \c new. Destroy it with the \c |
| 892 |
delete[] operator. |
| 893 |
|
| 894 |
The \a l parameter is set to the length of the buffer. If the |
| 895 |
string read is empty, \a l is set to 0 and \a s is set to |
| 896 |
a null pointer. |
| 897 |
|
| 898 |
The serialization format is a quint32 length specifier first, |
| 899 |
then \a l bytes of data. |
| 900 |
|
| 901 |
\sa readRawData(), writeBytes() |
| 902 |
*/ |
| 903 |
|
| 904 |
QDataStream &QDataStream::readBytes(char *&s, uint &l) |
| 905 |
{ |
| 906 |
s = 0; |
| 907 |
l = 0; |
| 908 |
CHECK_STREAM_PRECOND(*this) |
| 909 |
|
| 910 |
quint32 len; |
| 911 |
*this >> len; |
| 912 |
if (len == 0) |
| 913 |
return *this; |
| 914 |
|
| 915 |
const quint32 Step = 1024 * 1024; |
| 916 |
quint32 allocated = 0; |
| 917 |
char *prevBuf = 0; |
| 918 |
char *curBuf = 0; |
| 919 |
|
| 920 |
do { |
| 921 |
int blockSize = qMin(Step, len - allocated); |
| 922 |
prevBuf = curBuf; |
| 923 |
curBuf = new char[allocated + blockSize + 1]; |
| 924 |
if (prevBuf) { |
| 925 |
memcpy(curBuf, prevBuf, allocated); |
| 926 |
delete [] prevBuf; |
| 927 |
} |
| 928 |
if (dev->read(curBuf + allocated, blockSize) != blockSize) { |
| 929 |
delete [] curBuf; |
| 930 |
setStatus(ReadPastEnd); |
| 931 |
return *this; |
| 932 |
} |
| 933 |
allocated += blockSize; |
| 934 |
} while (allocated < len); |
| 935 |
|
| 936 |
s = curBuf; |
| 937 |
s[len] = '\0'; |
| 938 |
l = (uint)len; |
| 939 |
return *this; |
| 940 |
} |
| 941 |
|
| 942 |
/*! |
| 943 |
Reads at most \a len bytes from the stream into \a s and returns the number of |
| 944 |
bytes read. If an error occurs, this function returns -1. |
| 945 |
|
| 946 |
The buffer \a s must be preallocated. The data is \e not encoded. |
| 947 |
|
| 948 |
\sa readBytes(), QIODevice::read(), writeRawData() |
| 949 |
*/ |
| 950 |
|
| 951 |
int QDataStream::readRawData(char *s, int len) |
| 952 |
{ |
| 953 |
CHECK_STREAM_PRECOND(-1) |
| 954 |
return dev->read(s, len); |
| 955 |
} |
| 956 |
|
| 957 |
|
| 958 |
/***************************************************************************** |
| 959 |
QDataStream write functions |
| 960 |
*****************************************************************************/ |
| 961 |
|
| 962 |
|
| 963 |
/*! |
| 964 |
\fn QDataStream &QDataStream::operator<<(quint8 i) |
| 965 |
\overload |
| 966 |
|
| 967 |
Writes an unsigned byte, \a i, to the stream and returns a |
| 968 |
reference to the stream. |
| 969 |
*/ |
| 970 |
|
| 971 |
/*! |
| 972 |
Writes a signed byte, \a i, to the stream and returns a reference |
| 973 |
to the stream. |
| 974 |
*/ |
| 975 |
|
| 976 |
QDataStream &QDataStream::operator<<(qint8 i) |
| 977 |
{ |
| 978 |
CHECK_STREAM_PRECOND(*this) |
| 979 |
dev->putChar(i); |
| 980 |
return *this; |
| 981 |
} |
| 982 |
|
| 983 |
|
| 984 |
/*! |
| 985 |
\fn QDataStream &QDataStream::operator<<(quint16 i) |
| 986 |
\overload |
| 987 |
|
| 988 |
Writes an unsigned 16-bit integer, \a i, to the stream and returns |
| 989 |
a reference to the stream. |
| 990 |
*/ |
| 991 |
|
| 992 |
/*! |
| 993 |
\overload |
| 994 |
|
| 995 |
Writes a signed 16-bit integer, \a i, to the stream and returns a |
| 996 |
reference to the stream. |
| 997 |
*/ |
| 998 |
|
| 999 |
QDataStream &QDataStream::operator<<(qint16 i) |
| 1000 |
{ |
| 1001 |
CHECK_STREAM_PRECOND(*this) |
| 1002 |
if (noswap) { |
| 1003 |
dev->write((char *)&i, sizeof(qint16)); |
| 1004 |
} else { // swap bytes |
| 1005 |
union { |
| 1006 |
qint16 val1; |
| 1007 |
char val2[2]; |
| 1008 |
} x; |
| 1009 |
x.val1 = i; |
| 1010 |
char *p = x.val2; |
| 1011 |
char b[2]; |
| 1012 |
b[1] = *p++; |
| 1013 |
b[0] = *p; |
| 1014 |
dev->write(b, 2); |
| 1015 |
} |
| 1016 |
return *this; |
| 1017 |
} |
| 1018 |
|
| 1019 |
/*! |
| 1020 |
\overload |
| 1021 |
|
| 1022 |
Writes a signed 32-bit integer, \a i, to the stream and returns a |
| 1023 |
reference to the stream. |
| 1024 |
*/ |
| 1025 |
|
| 1026 |
QDataStream &QDataStream::operator<<(qint32 i) |
| 1027 |
{ |
| 1028 |
CHECK_STREAM_PRECOND(*this) |
| 1029 |
if (noswap) { |
| 1030 |
dev->write((char *)&i, sizeof(qint32)); |
| 1031 |
} else { // swap bytes |
| 1032 |
union { |
| 1033 |
qint32 val1; |
| 1034 |
char val2[4]; |
| 1035 |
} x; |
| 1036 |
x.val1 = i; |
| 1037 |
char *p = x.val2; |
| 1038 |
char b[4]; |
| 1039 |
b[3] = *p++; |
| 1040 |
b[2] = *p++; |
| 1041 |
b[1] = *p++; |
| 1042 |
b[0] = *p; |
| 1043 |
dev->write(b, 4); |
| 1044 |
} |
| 1045 |
return *this; |
| 1046 |
} |
| 1047 |
|
| 1048 |
/*! |
| 1049 |
\fn QDataStream &QDataStream::operator<<(quint64 i) |
| 1050 |
\overload |
| 1051 |
|
| 1052 |
Writes an unsigned 64-bit integer, \a i, to the stream and returns a |
| 1053 |
reference to the stream. |
| 1054 |
*/ |
| 1055 |
|
| 1056 |
/*! |
| 1057 |
\overload |
| 1058 |
|
| 1059 |
Writes a signed 64-bit integer, \a i, to the stream and returns a |
| 1060 |
reference to the stream. |
| 1061 |
*/ |
| 1062 |
|
| 1063 |
QDataStream &QDataStream::operator<<(qint64 i) |
| 1064 |
{ |
| 1065 |
CHECK_STREAM_PRECOND(*this) |
| 1066 |
if (version() < 6) { |
| 1067 |
quint32 i1 = i & 0xffffffff; |
| 1068 |
quint32 i2 = i >> 32; |
| 1069 |
*this << i2 << i1; |
| 1070 |
} else if (noswap) { // no conversion needed |
| 1071 |
dev->write((char *)&i, sizeof(qint64)); |
| 1072 |
} else { // swap bytes |
| 1073 |
union { |
| 1074 |
qint64 val1; |
| 1075 |
char val2[8]; |
| 1076 |
} x; |
| 1077 |
x.val1 = i; |
| 1078 |
char *p = x.val2; |
| 1079 |
char b[8]; |
| 1080 |
b[7] = *p++; |
| 1081 |
b[6] = *p++; |
| 1082 |
b[5] = *p++; |
| 1083 |
b[4] = *p++; |
| 1084 |
b[3] = *p++; |
| 1085 |
b[2] = *p++; |
| 1086 |
b[1] = *p++; |
| 1087 |
b[0] = *p; |
| 1088 |
dev->write(b, 8); |
| 1089 |
} |
| 1090 |
return *this; |
| 1091 |
} |
| 1092 |
|
| 1093 |
/*! |
| 1094 |
\fn QDataStream &QDataStream::operator<<(quint32 i) |
| 1095 |
\overload |
| 1096 |
|
| 1097 |
Writes an unsigned integer, \a i, to the stream as a 32-bit |
| 1098 |
unsigned integer (quint32). Returns a reference to the stream. |
| 1099 |
*/ |
| 1100 |
|
| 1101 |
/*! |
| 1102 |
Writes a boolean value, \a i, to the stream. Returns a reference |
| 1103 |
to the stream. |
| 1104 |
*/ |
| 1105 |
|
| 1106 |
QDataStream &QDataStream::operator<<(bool i) |
| 1107 |
{ |
| 1108 |
CHECK_STREAM_PRECOND(*this) |
| 1109 |
dev->putChar(qint8(i)); |
| 1110 |
return *this; |
| 1111 |
} |
| 1112 |
|
| 1113 |
/*! |
| 1114 |
\overload |
| 1115 |
|
| 1116 |
Writes a 32-bit floating point number, \a f, to the stream using |
| 1117 |
the standard IEEE 754 format. Returns a reference to the stream. |
| 1118 |
*/ |
| 1119 |
|
| 1120 |
QDataStream &QDataStream::operator<<(float f) |
| 1121 |
{ |
| 1122 |
CHECK_STREAM_PRECOND(*this) |
| 1123 |
float g = f; // fixes float-on-stack problem |
| 1124 |
if (noswap) { // no conversion needed |
| 1125 |
dev->write((char *)&g, sizeof(float)); |
| 1126 |
} else { // swap bytes |
| 1127 |
union { |
| 1128 |
float val1; |
| 1129 |
char val2[4]; |
| 1130 |
} x; |
| 1131 |
x.val1 = f; |
| 1132 |
char *p = x.val2; |
| 1133 |
char b[4]; |
| 1134 |
b[3] = *p++; |
| 1135 |
b[2] = *p++; |
| 1136 |
b[1] = *p++; |
| 1137 |
b[0] = *p; |
| 1138 |
dev->write(b, 4); |
| 1139 |
} |
| 1140 |
return *this; |
| 1141 |
} |
| 1142 |
|
| 1143 |
|
| 1144 |
/*! |
| 1145 |
\overload |
| 1146 |
|
| 1147 |
Writes a 64-bit floating point number, \a f, to the stream using |
| 1148 |
the standard IEEE 754 format. Returns a reference to the stream. |
| 1149 |
*/ |
| 1150 |
|
| 1151 |
QDataStream &QDataStream::operator<<(double f) |
| 1152 |
{ |
| 1153 |
CHECK_STREAM_PRECOND(*this) |
| 1154 |
#ifndef Q_DOUBLE_FORMAT |
| 1155 |
if (noswap) { |
| 1156 |
dev->write((char *)&f, sizeof(double)); |
| 1157 |
} else { |
| 1158 |
union { |
| 1159 |
double val1; |
| 1160 |
char val2[8]; |
| 1161 |
} x; |
| 1162 |
x.val1 = f; |
| 1163 |
char *p = x.val2; |
| 1164 |
char b[8]; |
| 1165 |
b[7] = *p++; |
| 1166 |
b[6] = *p++; |
| 1167 |
b[5] = *p++; |
| 1168 |
b[4] = *p++; |
| 1169 |
b[3] = *p++; |
| 1170 |
b[2] = *p++; |
| 1171 |
b[1] = *p++; |
| 1172 |
b[0] = *p; |
| 1173 |
dev->write(b, 8); |
| 1174 |
} |
| 1175 |
#else |
| 1176 |
union { |
| 1177 |
double val1; |
| 1178 |
char val2[8]; |
| 1179 |
} x; |
| 1180 |
x.val1 = f; |
| 1181 |
char *p = x.val2; |
| 1182 |
char b[8]; |
| 1183 |
if (noswap) { |
| 1184 |
b[Q_DF(0)] = *p++; |
| 1185 |
b[Q_DF(1)] = *p++; |
| 1186 |
b[Q_DF(2)] = *p++; |
| 1187 |
b[Q_DF(3)] = *p++; |
| 1188 |
b[Q_DF(4)] = *p++; |
| 1189 |
b[Q_DF(5)] = *p++; |
| 1190 |
b[Q_DF(6)] = *p++; |
| 1191 |
b[Q_DF(7)] = *p; |
| 1192 |
} else { |
| 1193 |
b[Q_DF(7)] = *p++; |
| 1194 |
b[Q_DF(6)] = *p++; |
| 1195 |
b[Q_DF(5)] = *p++; |
| 1196 |
b[Q_DF(4)] = *p++; |
| 1197 |
b[Q_DF(3)] = *p++; |
| 1198 |
b[Q_DF(2)] = *p++; |
| 1199 |
b[Q_DF(1)] = *p++; |
| 1200 |
b[Q_DF(0)] = *p; |
| 1201 |
} |
| 1202 |
dev->write(b, 8); |
| 1203 |
#endif |
| 1204 |
return *this; |
| 1205 |
} |
| 1206 |
|
| 1207 |
|
| 1208 |
/*! |
| 1209 |
\overload |
| 1210 |
|
| 1211 |
Writes the '\0'-terminated string \a s to the stream and returns a |
| 1212 |
reference to the stream. |
| 1213 |
|
| 1214 |
The string is serialized using writeBytes(). |
| 1215 |
*/ |
| 1216 |
|
| 1217 |
QDataStream &QDataStream::operator<<(const char *s) |
| 1218 |
{ |
| 1219 |
if (!s) { |
| 1220 |
*this << (quint32)0; |
| 1221 |
return *this; |
| 1222 |
} |
| 1223 |
uint len = qstrlen(s) + 1; // also write null terminator |
| 1224 |
*this << (quint32)len; // write length specifier |
| 1225 |
writeRawData(s, len); |
| 1226 |
return *this; |
| 1227 |
} |
| 1228 |
|
| 1229 |
|
| 1230 |
/*! |
| 1231 |
Writes the length specifier \a len and the buffer \a s to the |
| 1232 |
stream and returns a reference to the stream. |
| 1233 |
|
| 1234 |
The \a len is serialized as a quint32, followed by \a len bytes |
| 1235 |
from \a s. Note that the data is \e not encoded. |
| 1236 |
|
| 1237 |
\sa writeRawData(), readBytes() |
| 1238 |
*/ |
| 1239 |
|
| 1240 |
QDataStream &QDataStream::writeBytes(const char *s, uint len) |
| 1241 |
{ |
| 1242 |
CHECK_STREAM_PRECOND(*this) |
| 1243 |
*this << (quint32)len; // write length specifier |
| 1244 |
if (len) |
| 1245 |
writeRawData(s, len); |
| 1246 |
return *this; |
| 1247 |
} |
| 1248 |
|
| 1249 |
|
| 1250 |
/*! |
| 1251 |
Writes \a len bytes from \a s to the stream. Returns the |
| 1252 |
number of bytes actually written, or -1 on error. |
| 1253 |
The data is \e not encoded. |
| 1254 |
|
| 1255 |
\sa writeBytes(), QIODevice::write(), readRawData() |
| 1256 |
*/ |
| 1257 |
|
| 1258 |
int QDataStream::writeRawData(const char *s, int len) |
| 1259 |
{ |
| 1260 |
CHECK_STREAM_PRECOND(-1) |
| 1261 |
return dev->write(s, len); |
| 1262 |
} |
| 1263 |
|
| 1264 |
/*! |
| 1265 |
\since 4.1 |
| 1266 |
|
| 1267 |
Skips \a len bytes from the device. Returns the number of bytes |
| 1268 |
actually skipped, or -1 on error. |
| 1269 |
|
| 1270 |
This is equivalent to calling readRawData() on a buffer of length |
| 1271 |
\a len and ignoring the buffer. |
| 1272 |
|
| 1273 |
\sa QIODevice::seek() |
| 1274 |
*/ |
| 1275 |
int QDataStream::skipRawData(int len) |
| 1276 |
{ |
| 1277 |
CHECK_STREAM_PRECOND(-1) |
| 1278 |
|
| 1279 |
if (dev->isSequential()) { |
| 1280 |
char buf[4096]; |
| 1281 |
int sumRead = 0; |
| 1282 |
|
| 1283 |
while (len > 0) { |
| 1284 |
int blockSize = qMin(len, (int)sizeof(buf)); |
| 1285 |
int n = dev->read(buf, blockSize); |
| 1286 |
if (n == -1) |
| 1287 |
return -1; |
| 1288 |
if (n == 0) |
| 1289 |
return sumRead; |
| 1290 |
|
| 1291 |
sumRead += n; |
| 1292 |
len -= blockSize; |
| 1293 |
} |
| 1294 |
return sumRead; |
| 1295 |
} else { |
| 1296 |
qint64 pos = dev->pos(); |
| 1297 |
qint64 size = dev->size(); |
| 1298 |
if (pos + len > size) |
| 1299 |
len = size - pos; |
| 1300 |
if (!dev->seek(pos + len)) |
| 1301 |
return -1; |
| 1302 |
return len; |
| 1303 |
} |
| 1304 |
} |
| 1305 |
|
| 1306 |
#ifdef QT3_SUPPORT |
| 1307 |
/*! |
| 1308 |
\fn QDataStream &QDataStream::readRawBytes(char *str, uint len) |
| 1309 |
|
| 1310 |
Use readRawData() instead. |
| 1311 |
*/ |
| 1312 |
|
| 1313 |
/*! |
| 1314 |
\fn QDataStream &QDataStream::writeRawBytes(const char *str, uint len) |
| 1315 |
|
| 1316 |
Use writeRawData() instead. |
| 1317 |
*/ |
| 1318 |
#endif |
| 1319 |
|
| 1320 |
QT_END_NAMESPACE |
| 1321 |
|
| 1322 |
#endif // QT_NO_DATASTREAM |