Define QT_OPEN_LARGEFILE on Symbian + WinCE
[qt-netbsd.git] / src / corelib / io / qdatastream.cpp
blob19e86a6e1466c6f8520023116ec1993d88578877
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtCore module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 #include "qdatastream.h"
43 #include "qdatastream_p.h"
45 #ifndef QT_NO_DATASTREAM
46 #include "qbuffer.h"
47 #include "qstring.h"
48 #include <stdio.h>
49 #include <ctype.h>
50 #include <stdlib.h>
52 QT_BEGIN_NAMESPACE
54 /*!
55 \class QDataStream
56 \reentrant
57 \brief The QDataStream class provides serialization of binary data
58 to a QIODevice.
60 \ingroup io
63 A data stream is a binary stream of encoded information which is
64 100% independent of the host computer's operating system, CPU or
65 byte order. For example, a data stream that is written by a PC
66 under Windows can be read by a Sun SPARC running Solaris.
68 You can also use a data stream to read/write \l{raw}{raw
69 unencoded binary data}. If you want a "parsing" input stream, see
70 QTextStream.
72 The QDataStream class implements the serialization of C++'s basic
73 data types, like \c char, \c short, \c int, \c{char *}, etc.
74 Serialization of more complex data is accomplished by breaking up
75 the data into primitive units.
77 A data stream cooperates closely with a QIODevice. A QIODevice
78 represents an input/output medium one can read data from and write
79 data to. The QFile class is an example of an I/O device.
81 Example (write binary data to a stream):
83 \snippet doc/src/snippets/code/src_corelib_io_qdatastream.cpp 0
85 Example (read binary data from a stream):
87 \snippet doc/src/snippets/code/src_corelib_io_qdatastream.cpp 1
89 Each item written to the stream is written in a predefined binary
90 format that varies depending on the item's type. Supported Qt
91 types include QBrush, QColor, QDateTime, QFont, QPixmap, QString,
92 QVariant and many others. For the complete list of all Qt types
93 supporting data streaming see the \l{Format of the QDataStream
94 operators}.
96 For integers it is best to always cast to a Qt integer type for
97 writing, and to read back into the same Qt integer type. This
98 ensures that you get integers of the size you want and insulates
99 you from compiler and platform differences.
101 To take one example, a \c{char *} string is written as a 32-bit
102 integer equal to the length of the string including the '\\0' byte,
103 followed by all the characters of the string including the
104 '\\0' byte. When reading a \c{char *} string, 4 bytes are read to
105 create the 32-bit length value, then that many characters for the
106 \c {char *} string including the '\\0' terminator are read.
108 The initial I/O device is usually set in the constructor, but can be
109 changed with setDevice(). If you've reached the end of the data
110 (or if there is no I/O device set) atEnd() will return true.
112 \section1 Versioning
114 QDataStream's binary format has evolved since Qt 1.0, and is
115 likely to continue evolving to reflect changes done in Qt. When
116 inputting or outputting complex types, it's very important to
117 make sure that the same version of the stream (version()) is used
118 for reading and writing. If you need both forward and backward
119 compatibility, you can hardcode the version number in the
120 application:
122 \snippet doc/src/snippets/code/src_corelib_io_qdatastream.cpp 2
124 If you are producing a new binary data format, such as a file
125 format for documents created by your application, you could use a
126 QDataStream to write the data in a portable format. Typically, you
127 would write a brief header containing a magic string and a version
128 number to give yourself room for future expansion. For example:
130 \snippet doc/src/snippets/code/src_corelib_io_qdatastream.cpp 3
132 Then read it in with:
134 \snippet doc/src/snippets/code/src_corelib_io_qdatastream.cpp 4
136 You can select which byte order to use when serializing data. The
137 default setting is big endian (MSB first). Changing it to little
138 endian breaks the portability (unless the reader also changes to
139 little endian). We recommend keeping this setting unless you have
140 special requirements.
142 \target raw
143 \section1 Reading and writing raw binary data
145 You may wish to read/write your own raw binary data to/from the
146 data stream directly. Data may be read from the stream into a
147 preallocated \c{char *} using readRawData(). Similarly data can be
148 written to the stream using writeRawData(). Note that any
149 encoding/decoding of the data must be done by you.
151 A similar pair of functions is readBytes() and writeBytes(). These
152 differ from their \e raw counterparts as follows: readBytes()
153 reads a quint32 which is taken to be the length of the data to be
154 read, then that number of bytes is read into the preallocated
155 \c{char *}; writeBytes() writes a quint32 containing the length of the
156 data, followed by the data. Note that any encoding/decoding of
157 the data (apart from the length quint32) must be done by you.
159 \target Serializing Qt Classes
160 \section1 Reading and writing other Qt classes.
162 In addition to the overloaded stream operators documented here,
163 any Qt classes that you might want to serialize to a QDataStream
164 will have appropriate stream operators declared as non-member of
165 the class:
167 \code
168 QDataStream &operator<<(QDataStream &, const QXxx &);
169 QDataStream &operator>>(QDataStream &, QXxx &);
170 \endcode
172 For example, here are the stream operators declared as non-members
173 of the QImage class:
175 \code
176 QDataStream & operator<< (QDataStream& stream, const QImage& image);
177 QDataStream & operator>> (QDataStream& stream, QImage& image);
178 \endcode
180 To see if your favorite Qt class has similar stream operators
181 defined, check the \bold {Related Non-Members} section of the
182 class's documentation page.
184 \sa QTextStream QVariant
188 \enum QDataStream::ByteOrder
190 The byte order used for reading/writing the data.
192 \value BigEndian Most significant byte first (the default)
193 \value LittleEndian Least significant byte first
197 \enum QDataStream::FloatingPointPrecision
199 The precision of floating point numbers used for reading/writing the data. This will only have
200 an effect if the version of the data stream is Qt_4_6 or higher.
202 \warning The floating point precision must be set to the same value on the object that writes
203 and the object that reads the data stream.
205 \value SinglePrecision All floating point numbers in the data stream have 32-bit precision.
206 \value DoublePrecision All floating point numbers in the data stream have 64-bit precision.
208 \sa setFloatingPointPrecision(), floatingPointPrecision()
212 \enum QDataStream::Status
214 This enum describes the current status of the data stream.
216 \value Ok The data stream is operating normally.
217 \value ReadPastEnd The data stream has read past the end of the
218 data in the underlying device.
219 \value ReadCorruptData The data stream has read corrupt data.
222 /*****************************************************************************
223 QDataStream member functions
224 *****************************************************************************/
226 #undef CHECK_STREAM_PRECOND
227 #ifndef QT_NO_DEBUG
228 #define CHECK_STREAM_PRECOND(retVal) \
229 if (!dev) { \
230 qWarning("QDataStream: No device"); \
231 return retVal; \
233 #else
234 #define CHECK_STREAM_PRECOND(retVal) \
235 if (!dev) { \
236 return retVal; \
238 #endif
240 enum {
241 DefaultStreamVersion = QDataStream::Qt_4_6
244 // ### 5.0: when streaming invalid QVariants, just the type should
245 // be written, no "data" after it
248 Constructs a data stream that has no I/O device.
250 \sa setDevice()
253 QDataStream::QDataStream()
255 dev = 0;
256 owndev = false;
257 byteorder = BigEndian;
258 ver = DefaultStreamVersion;
259 noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian;
260 q_status = Ok;
264 Constructs a data stream that uses the I/O device \a d.
266 \warning If you use QSocket or QSocketDevice as the I/O device \a d
267 for reading data, you must make sure that enough data is available
268 on the socket for the operation to successfully proceed;
269 QDataStream does not have any means to handle or recover from
270 short-reads.
272 \sa setDevice(), device()
275 QDataStream::QDataStream(QIODevice *d)
277 dev = d; // set device
278 owndev = false;
279 byteorder = BigEndian; // default byte order
280 ver = DefaultStreamVersion;
281 noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian;
282 q_status = Ok;
285 #ifdef QT3_SUPPORT
287 \fn QDataStream::QDataStream(QByteArray *array, int mode)
288 \compat
290 Constructs a data stream that operates on the given \a array. The
291 \a mode specifies how the byte array is to be used, and is
292 usually either QIODevice::ReadOnly or QIODevice::WriteOnly.
294 QDataStream::QDataStream(QByteArray *a, int mode)
296 QBuffer *buf = new QBuffer(a);
297 #ifndef QT_NO_QOBJECT
298 buf->blockSignals(true);
299 #endif
300 buf->open(QIODevice::OpenMode(mode));
301 dev = buf;
302 owndev = true;
303 byteorder = BigEndian;
304 ver = DefaultStreamVersion;
305 noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian;
306 q_status = Ok;
308 #endif
311 \fn QDataStream::QDataStream(QByteArray *a, QIODevice::OpenMode mode)
313 Constructs a data stream that operates on a byte array, \a a. The
314 \a mode describes how the device is to be used.
316 Alternatively, you can use QDataStream(const QByteArray &) if you
317 just want to read from a byte array.
319 Since QByteArray is not a QIODevice subclass, internally a QBuffer
320 is created to wrap the byte array.
323 QDataStream::QDataStream(QByteArray *a, QIODevice::OpenMode flags)
325 QBuffer *buf = new QBuffer(a);
326 #ifndef QT_NO_QOBJECT
327 buf->blockSignals(true);
328 #endif
329 buf->open(flags);
330 dev = buf;
331 owndev = true;
332 byteorder = BigEndian;
333 ver = DefaultStreamVersion;
334 noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian;
335 q_status = Ok;
339 Constructs a read-only data stream that operates on byte array \a a.
340 Use QDataStream(QByteArray*, int) if you want to write to a byte
341 array.
343 Since QByteArray is not a QIODevice subclass, internally a QBuffer
344 is created to wrap the byte array.
346 QDataStream::QDataStream(const QByteArray &a)
348 QBuffer *buf = new QBuffer;
349 #ifndef QT_NO_QOBJECT
350 buf->blockSignals(true);
351 #endif
352 buf->setData(a);
353 buf->open(QIODevice::ReadOnly);
354 dev = buf;
355 owndev = true;
356 byteorder = BigEndian;
357 ver = DefaultStreamVersion;
358 noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian;
359 q_status = Ok;
363 Destroys the data stream.
365 The destructor will not affect the current I/O device, unless it is
366 an internal I/O device (e.g. a QBuffer) processing a QByteArray
367 passed in the \e constructor, in which case the internal I/O device
368 is destroyed.
371 QDataStream::~QDataStream()
373 if (owndev)
374 delete dev;
379 \fn QIODevice *QDataStream::device() const
381 Returns the I/O device currently set, or 0 if no
382 device is currently set.
384 \sa setDevice()
388 void QDataStream::setDevice(QIODevice *d)
390 Sets the I/O device to \a d, which can be 0
391 to unset to current I/O device.
393 \sa device()
396 void QDataStream::setDevice(QIODevice *d)
398 if (owndev) {
399 delete dev;
400 owndev = false;
402 dev = d;
406 \obsolete
407 Unsets the I/O device.
408 Use setDevice(0) instead.
411 void QDataStream::unsetDevice()
413 setDevice(0);
418 \fn bool QDataStream::atEnd() const
420 Returns true if the I/O device has reached the end position (end of
421 the stream or file) or if there is no I/O device set; otherwise
422 returns false.
424 \sa QIODevice::atEnd()
427 bool QDataStream::atEnd() const
429 return dev ? dev->atEnd() : true;
433 Returns the floating point precision of the data stream.
435 \since 4.6
437 \sa FloatingPointPrecision setFloatingPointPrecision()
439 QDataStream::FloatingPointPrecision QDataStream::floatingPointPrecision() const
441 return d == 0 ? QDataStream::DoublePrecision : d->floatingPointPrecision;
445 Sets the floating point precision of the data stream to \a precision. If the floating point precision is
446 DoublePrecision and the version of the data stream is Qt_4_6 or higher, all floating point
447 numbers will be written and read with 64-bit precision. If the floating point precision is
448 SinglePrecision and the version is Qt_4_6 or higher, all floating point numbers will be written
449 and read with 32-bit precision.
451 For versions prior to Qt_4_6, the precision of floating point numbers in the data stream depends
452 on the stream operator called.
454 The default is DoublePrecision.
456 \warning This property must be set to the same value on the object that writes and the object
457 that reads the data stream.
459 \since 4.6
461 void QDataStream::setFloatingPointPrecision(QDataStream::FloatingPointPrecision precision)
463 if (d == 0)
464 d.reset(new QDataStreamPrivate());
465 d->floatingPointPrecision = precision;
469 Returns the status of the data stream.
471 \sa Status setStatus() resetStatus()
474 QDataStream::Status QDataStream::status() const
476 return q_status;
480 Resets the status of the data stream.
482 \sa Status status() setStatus()
484 void QDataStream::resetStatus()
486 q_status = Ok;
490 Sets the status of the data stream to the \a status given.
492 \sa Status status() resetStatus()
494 void QDataStream::setStatus(Status status)
496 if (q_status == Ok)
497 q_status = status;
500 /*!\fn bool QDataStream::eof() const
502 Use atEnd() instead.
506 \fn int QDataStream::byteOrder() const
508 Returns the current byte order setting -- either BigEndian or
509 LittleEndian.
511 \sa setByteOrder()
515 Sets the serialization byte order to \a bo.
517 The \a bo parameter can be QDataStream::BigEndian or
518 QDataStream::LittleEndian.
520 The default setting is big endian. We recommend leaving this
521 setting unless you have special requirements.
523 \sa byteOrder()
526 void QDataStream::setByteOrder(ByteOrder bo)
528 byteorder = bo;
529 if (QSysInfo::ByteOrder == QSysInfo::BigEndian)
530 noswap = (byteorder == BigEndian);
531 else
532 noswap = (byteorder == LittleEndian);
537 \fn bool QDataStream::isPrintableData() const
539 In Qt 4, this function always returns false.
541 \sa setPrintableData()
545 \fn void QDataStream::setPrintableData(bool enable)
547 In Qt 3, this function enabled output in a human-readable
548 format if \a enable was false.
550 In Qt 4, QDataStream no longer provides a human-readable output.
551 This function does nothing.
555 \enum QDataStream::Version
557 This enum provides symbolic synonyms for the data serialization
558 format version numbers.
560 \value Qt_1_0 Version 1 (Qt 1.x)
561 \value Qt_2_0 Version 2 (Qt 2.0)
562 \value Qt_2_1 Version 3 (Qt 2.1, 2.2, 2.3)
563 \value Qt_3_0 Version 4 (Qt 3.0)
564 \value Qt_3_1 Version 5 (Qt 3.1, 3.2)
565 \value Qt_3_3 Version 6 (Qt 3.3)
566 \value Qt_4_0 Version 7 (Qt 4.0, Qt 4.1)
567 \value Qt_4_1 Version 7 (Qt 4.0, Qt 4.1)
568 \value Qt_4_2 Version 8 (Qt 4.2)
569 \value Qt_4_3 Version 9 (Qt 4.3)
570 \value Qt_4_4 Version 10 (Qt 4.4)
571 \value Qt_4_5 Version 11 (Qt 4.5)
572 \value Qt_4_6 Version 12 (Qt 4.6)
574 \sa setVersion(), version()
578 \fn int QDataStream::version() const
580 Returns the version number of the data serialization format.
582 \sa setVersion(), Version
586 \fn void QDataStream::setVersion(int v)
588 Sets the version number of the data serialization format to \a v.
590 You don't \e have to set a version if you are using the current
591 version of Qt, but for your own custom binary formats we
592 recommend that you do; see \l{Versioning} in the Detailed
593 Description.
595 In order to accommodate new functionality, the datastream
596 serialization format of some Qt classes has changed in some
597 versions of Qt. If you want to read data that was created by an
598 earlier version of Qt, or write data that can be read by a
599 program that was compiled with an earlier version of Qt, use this
600 function to modify the serialization format used by QDataStream.
602 \table
603 \header \i Qt Version \i QDataStream Version
604 \row \i Qt 4.4 \i 10
605 \row \i Qt 4.3 \i 9
606 \row \i Qt 4.2 \i 8
607 \row \i Qt 4.0, 4.1 \i 7
608 \row \i Qt 3.3 \i 6
609 \row \i Qt 3.1, 3.2 \i 5
610 \row \i Qt 3.0 \i 4
611 \row \i Qt 2.1, 2.2, 2.3 \i 3
612 \row \i Qt 2.0 \i 2
613 \row \i Qt 1.x \i 1
614 \endtable
616 The \l Version enum provides symbolic constants for the different
617 versions of Qt. For example:
619 \snippet doc/src/snippets/code/src_corelib_io_qdatastream.cpp 5
621 \sa version(), Version
624 /*****************************************************************************
625 QDataStream read functions
626 *****************************************************************************/
629 \fn QDataStream &QDataStream::operator>>(quint8 &i)
630 \overload
632 Reads an unsigned byte from the stream into \a i, and returns a
633 reference to the stream.
637 Reads a signed byte from the stream into \a i, and returns a
638 reference to the stream.
641 QDataStream &QDataStream::operator>>(qint8 &i)
643 i = 0;
644 CHECK_STREAM_PRECOND(*this)
645 char c;
646 if (!dev->getChar(&c))
647 setStatus(ReadPastEnd);
648 else
649 i = qint8(c);
650 return *this;
655 \fn QDataStream &QDataStream::operator>>(quint16 &i)
656 \overload
658 Reads an unsigned 16-bit integer from the stream into \a i, and
659 returns a reference to the stream.
663 \overload
665 Reads a signed 16-bit integer from the stream into \a i, and
666 returns a reference to the stream.
669 QDataStream &QDataStream::operator>>(qint16 &i)
671 i = 0;
672 CHECK_STREAM_PRECOND(*this)
673 if (noswap) {
674 if (dev->read((char *)&i, 2) != 2) {
675 i = 0;
676 setStatus(ReadPastEnd);
678 } else {
679 union {
680 qint16 val1;
681 char val2[2];
682 } x;
683 char *p = x.val2;
684 char b[2];
685 if (dev->read(b, 2) == 2) {
686 *p++ = b[1];
687 *p = b[0];
688 i = x.val1;
689 } else {
690 setStatus(ReadPastEnd);
693 return *this;
698 \fn QDataStream &QDataStream::operator>>(quint32 &i)
699 \overload
701 Reads an unsigned 32-bit integer from the stream into \a i, and
702 returns a reference to the stream.
706 \overload
708 Reads a signed 32-bit integer from the stream into \a i, and
709 returns a reference to the stream.
712 QDataStream &QDataStream::operator>>(qint32 &i)
714 i = 0;
715 CHECK_STREAM_PRECOND(*this)
716 if (noswap) {
717 if (dev->read((char *)&i, 4) != 4) {
718 i = 0;
719 setStatus(ReadPastEnd);
721 } else { // swap bytes
722 union {
723 qint32 val1;
724 char val2[4];
725 } x;
726 char *p = x.val2;
727 char b[4];
728 if (dev->read(b, 4) == 4) {
729 *p++ = b[3];
730 *p++ = b[2];
731 *p++ = b[1];
732 *p = b[0];
733 i = x.val1;
734 } else {
735 setStatus(ReadPastEnd);
738 return *this;
742 \fn QDataStream &QDataStream::operator>>(quint64 &i)
743 \overload
745 Reads an unsigned 64-bit integer from the stream, into \a i, and
746 returns a reference to the stream.
750 \overload
752 Reads a signed 64-bit integer from the stream into \a i, and
753 returns a reference to the stream.
756 QDataStream &QDataStream::operator>>(qint64 &i)
758 i = qint64(0);
759 CHECK_STREAM_PRECOND(*this)
760 if (version() < 6) {
761 quint32 i1, i2;
762 *this >> i2 >> i1;
763 i = ((quint64)i1 << 32) + i2;
764 } else if (noswap) { // no conversion needed
765 if (dev->read((char *)&i, 8) != 8) {
766 i = qint64(0);
767 setStatus(ReadPastEnd);
769 } else { // swap bytes
770 union {
771 qint64 val1;
772 char val2[8];
773 } x;
775 char *p = x.val2;
776 char b[8];
777 if (dev->read(b, 8) == 8) {
778 *p++ = b[7];
779 *p++ = b[6];
780 *p++ = b[5];
781 *p++ = b[4];
782 *p++ = b[3];
783 *p++ = b[2];
784 *p++ = b[1];
785 *p = b[0];
786 i = x.val1;
787 } else {
788 setStatus(ReadPastEnd);
791 return *this;
795 Reads a boolean value from the stream into \a i. Returns a
796 reference to the stream.
798 QDataStream &QDataStream::operator>>(bool &i)
800 qint8 v;
801 *this >> v;
802 i = !!v;
803 return *this;
807 \overload
809 Reads a floating point number from the stream into \a f,
810 using the standard IEEE 754 format. Returns a reference to the
811 stream.
813 \sa setFloatingPointPrecision()
816 QDataStream &QDataStream::operator>>(float &f)
818 if (version() >= QDataStream::Qt_4_6
819 && floatingPointPrecision() == QDataStream::DoublePrecision) {
820 double d;
821 *this >> d;
822 f = d;
823 return *this;
826 f = 0.0f;
827 CHECK_STREAM_PRECOND(*this)
828 if (noswap) {
829 if (dev->read((char *)&f, 4) != 4) {
830 f = 0.0f;
831 setStatus(ReadPastEnd);
833 } else { // swap bytes
834 union {
835 float val1;
836 char val2[4];
837 } x;
839 char *p = x.val2;
840 char b[4];
841 if (dev->read(b, 4) == 4) {
842 *p++ = b[3];
843 *p++ = b[2];
844 *p++ = b[1];
845 *p = b[0];
846 f = x.val1;
847 } else {
848 setStatus(ReadPastEnd);
851 return *this;
854 #if defined(Q_DOUBLE_FORMAT)
855 #define Q_DF(x) Q_DOUBLE_FORMAT[(x)] - '0'
856 #endif
859 \overload
861 Reads a floating point number from the stream into \a f,
862 using the standard IEEE 754 format. Returns a reference to the
863 stream.
865 \sa setFloatingPointPrecision()
868 QDataStream &QDataStream::operator>>(double &f)
870 if (version() >= QDataStream::Qt_4_6
871 && floatingPointPrecision() == QDataStream::SinglePrecision) {
872 float d;
873 *this >> d;
874 f = d;
875 return *this;
878 f = 0.0;
879 CHECK_STREAM_PRECOND(*this)
880 #ifndef Q_DOUBLE_FORMAT
881 if (noswap) {
882 if (dev->read((char *)&f, 8) != 8) {
883 f = 0.0;
884 setStatus(ReadPastEnd);
886 } else { // swap bytes
887 union {
888 double val1;
889 char val2[8];
890 } x;
891 char *p = x.val2;
892 char b[8];
893 if (dev->read(b, 8) == 8) {
894 *p++ = b[7];
895 *p++ = b[6];
896 *p++ = b[5];
897 *p++ = b[4];
898 *p++ = b[3];
899 *p++ = b[2];
900 *p++ = b[1];
901 *p = b[0];
902 f = x.val1;
903 } else {
904 setStatus(ReadPastEnd);
907 #else
908 //non-standard floating point format
909 union {
910 double val1;
911 char val2[8];
912 } x;
913 char *p = x.val2;
914 char b[8];
915 if (dev->read(b, 8) == 8) {
916 if (noswap) {
917 *p++ = b[Q_DF(0)];
918 *p++ = b[Q_DF(1)];
919 *p++ = b[Q_DF(2)];
920 *p++ = b[Q_DF(3)];
921 *p++ = b[Q_DF(4)];
922 *p++ = b[Q_DF(5)];
923 *p++ = b[Q_DF(6)];
924 *p = b[Q_DF(7)];
925 } else {
926 *p++ = b[Q_DF(7)];
927 *p++ = b[Q_DF(6)];
928 *p++ = b[Q_DF(5)];
929 *p++ = b[Q_DF(4)];
930 *p++ = b[Q_DF(3)];
931 *p++ = b[Q_DF(2)];
932 *p++ = b[Q_DF(1)];
933 *p = b[Q_DF(0)];
935 f = x.val1;
936 } else {
937 setStatus(ReadPastEnd);
939 #endif
940 return *this;
945 \overload
947 Reads the '\0'-terminated string \a s from the stream and returns
948 a reference to the stream.
950 Space for the string is allocated using \c new -- the caller must
951 destroy it with \c{delete[]}.
954 QDataStream &QDataStream::operator>>(char *&s)
956 uint len = 0;
957 return readBytes(s, len);
962 Reads the buffer \a s from the stream and returns a reference to
963 the stream.
965 The buffer \a s is allocated using \c new. Destroy it with the \c
966 delete[] operator.
968 The \a l parameter is set to the length of the buffer. If the
969 string read is empty, \a l is set to 0 and \a s is set to
970 a null pointer.
972 The serialization format is a quint32 length specifier first,
973 then \a l bytes of data.
975 \sa readRawData(), writeBytes()
978 QDataStream &QDataStream::readBytes(char *&s, uint &l)
980 s = 0;
981 l = 0;
982 CHECK_STREAM_PRECOND(*this)
984 quint32 len;
985 *this >> len;
986 if (len == 0)
987 return *this;
989 const quint32 Step = 1024 * 1024;
990 quint32 allocated = 0;
991 char *prevBuf = 0;
992 char *curBuf = 0;
994 do {
995 int blockSize = qMin(Step, len - allocated);
996 prevBuf = curBuf;
997 curBuf = new char[allocated + blockSize + 1];
998 if (prevBuf) {
999 memcpy(curBuf, prevBuf, allocated);
1000 delete [] prevBuf;
1002 if (dev->read(curBuf + allocated, blockSize) != blockSize) {
1003 delete [] curBuf;
1004 setStatus(ReadPastEnd);
1005 return *this;
1007 allocated += blockSize;
1008 } while (allocated < len);
1010 s = curBuf;
1011 s[len] = '\0';
1012 l = (uint)len;
1013 return *this;
1017 Reads at most \a len bytes from the stream into \a s and returns the number of
1018 bytes read. If an error occurs, this function returns -1.
1020 The buffer \a s must be preallocated. The data is \e not encoded.
1022 \sa readBytes(), QIODevice::read(), writeRawData()
1025 int QDataStream::readRawData(char *s, int len)
1027 CHECK_STREAM_PRECOND(-1)
1028 return dev->read(s, len);
1032 /*****************************************************************************
1033 QDataStream write functions
1034 *****************************************************************************/
1038 \fn QDataStream &QDataStream::operator<<(quint8 i)
1039 \overload
1041 Writes an unsigned byte, \a i, to the stream and returns a
1042 reference to the stream.
1046 Writes a signed byte, \a i, to the stream and returns a reference
1047 to the stream.
1050 QDataStream &QDataStream::operator<<(qint8 i)
1052 CHECK_STREAM_PRECOND(*this)
1053 dev->putChar(i);
1054 return *this;
1059 \fn QDataStream &QDataStream::operator<<(quint16 i)
1060 \overload
1062 Writes an unsigned 16-bit integer, \a i, to the stream and returns
1063 a reference to the stream.
1067 \overload
1069 Writes a signed 16-bit integer, \a i, to the stream and returns a
1070 reference to the stream.
1073 QDataStream &QDataStream::operator<<(qint16 i)
1075 CHECK_STREAM_PRECOND(*this)
1076 if (noswap) {
1077 dev->write((char *)&i, sizeof(qint16));
1078 } else { // swap bytes
1079 union {
1080 qint16 val1;
1081 char val2[2];
1082 } x;
1083 x.val1 = i;
1084 char *p = x.val2;
1085 char b[2];
1086 b[1] = *p++;
1087 b[0] = *p;
1088 dev->write(b, 2);
1090 return *this;
1094 \overload
1096 Writes a signed 32-bit integer, \a i, to the stream and returns a
1097 reference to the stream.
1100 QDataStream &QDataStream::operator<<(qint32 i)
1102 CHECK_STREAM_PRECOND(*this)
1103 if (noswap) {
1104 dev->write((char *)&i, sizeof(qint32));
1105 } else { // swap bytes
1106 union {
1107 qint32 val1;
1108 char val2[4];
1109 } x;
1110 x.val1 = i;
1111 char *p = x.val2;
1112 char b[4];
1113 b[3] = *p++;
1114 b[2] = *p++;
1115 b[1] = *p++;
1116 b[0] = *p;
1117 dev->write(b, 4);
1119 return *this;
1123 \fn QDataStream &QDataStream::operator<<(quint64 i)
1124 \overload
1126 Writes an unsigned 64-bit integer, \a i, to the stream and returns a
1127 reference to the stream.
1131 \overload
1133 Writes a signed 64-bit integer, \a i, to the stream and returns a
1134 reference to the stream.
1137 QDataStream &QDataStream::operator<<(qint64 i)
1139 CHECK_STREAM_PRECOND(*this)
1140 if (version() < 6) {
1141 quint32 i1 = i & 0xffffffff;
1142 quint32 i2 = i >> 32;
1143 *this << i2 << i1;
1144 } else if (noswap) { // no conversion needed
1145 dev->write((char *)&i, sizeof(qint64));
1146 } else { // swap bytes
1147 union {
1148 qint64 val1;
1149 char val2[8];
1150 } x;
1151 x.val1 = i;
1152 char *p = x.val2;
1153 char b[8];
1154 b[7] = *p++;
1155 b[6] = *p++;
1156 b[5] = *p++;
1157 b[4] = *p++;
1158 b[3] = *p++;
1159 b[2] = *p++;
1160 b[1] = *p++;
1161 b[0] = *p;
1162 dev->write(b, 8);
1164 return *this;
1168 \fn QDataStream &QDataStream::operator<<(quint32 i)
1169 \overload
1171 Writes an unsigned integer, \a i, to the stream as a 32-bit
1172 unsigned integer (quint32). Returns a reference to the stream.
1176 Writes a boolean value, \a i, to the stream. Returns a reference
1177 to the stream.
1180 QDataStream &QDataStream::operator<<(bool i)
1182 CHECK_STREAM_PRECOND(*this)
1183 dev->putChar(qint8(i));
1184 return *this;
1188 \overload
1190 Writes a floating point number, \a f, to the stream using
1191 the standard IEEE 754 format. Returns a reference to the stream.
1193 \sa setFloatingPointPrecision()
1196 QDataStream &QDataStream::operator<<(float f)
1198 if (version() >= QDataStream::Qt_4_6
1199 && floatingPointPrecision() == QDataStream::DoublePrecision) {
1200 *this << double(f);
1201 return *this;
1204 CHECK_STREAM_PRECOND(*this)
1205 float g = f; // fixes float-on-stack problem
1206 if (noswap) { // no conversion needed
1207 dev->write((char *)&g, sizeof(float));
1208 } else { // swap bytes
1209 union {
1210 float val1;
1211 char val2[4];
1212 } x;
1213 x.val1 = f;
1214 char *p = x.val2;
1215 char b[4];
1216 b[3] = *p++;
1217 b[2] = *p++;
1218 b[1] = *p++;
1219 b[0] = *p;
1220 dev->write(b, 4);
1222 return *this;
1227 \overload
1229 Writes a floating point number, \a f, to the stream using
1230 the standard IEEE 754 format. Returns a reference to the stream.
1232 \sa setFloatingPointPrecision()
1235 QDataStream &QDataStream::operator<<(double f)
1237 if (version() >= QDataStream::Qt_4_6
1238 && floatingPointPrecision() == QDataStream::SinglePrecision) {
1239 *this << float(f);
1240 return *this;
1243 CHECK_STREAM_PRECOND(*this)
1244 #ifndef Q_DOUBLE_FORMAT
1245 if (noswap) {
1246 dev->write((char *)&f, sizeof(double));
1247 } else {
1248 union {
1249 double val1;
1250 char val2[8];
1251 } x;
1252 x.val1 = f;
1253 char *p = x.val2;
1254 char b[8];
1255 b[7] = *p++;
1256 b[6] = *p++;
1257 b[5] = *p++;
1258 b[4] = *p++;
1259 b[3] = *p++;
1260 b[2] = *p++;
1261 b[1] = *p++;
1262 b[0] = *p;
1263 dev->write(b, 8);
1265 #else
1266 union {
1267 double val1;
1268 char val2[8];
1269 } x;
1270 x.val1 = f;
1271 char *p = x.val2;
1272 char b[8];
1273 if (noswap) {
1274 b[Q_DF(0)] = *p++;
1275 b[Q_DF(1)] = *p++;
1276 b[Q_DF(2)] = *p++;
1277 b[Q_DF(3)] = *p++;
1278 b[Q_DF(4)] = *p++;
1279 b[Q_DF(5)] = *p++;
1280 b[Q_DF(6)] = *p++;
1281 b[Q_DF(7)] = *p;
1282 } else {
1283 b[Q_DF(7)] = *p++;
1284 b[Q_DF(6)] = *p++;
1285 b[Q_DF(5)] = *p++;
1286 b[Q_DF(4)] = *p++;
1287 b[Q_DF(3)] = *p++;
1288 b[Q_DF(2)] = *p++;
1289 b[Q_DF(1)] = *p++;
1290 b[Q_DF(0)] = *p;
1292 dev->write(b, 8);
1293 #endif
1294 return *this;
1299 \overload
1301 Writes the '\0'-terminated string \a s to the stream and returns a
1302 reference to the stream.
1304 The string is serialized using writeBytes().
1307 QDataStream &QDataStream::operator<<(const char *s)
1309 if (!s) {
1310 *this << (quint32)0;
1311 return *this;
1313 uint len = qstrlen(s) + 1; // also write null terminator
1314 *this << (quint32)len; // write length specifier
1315 writeRawData(s, len);
1316 return *this;
1321 Writes the length specifier \a len and the buffer \a s to the
1322 stream and returns a reference to the stream.
1324 The \a len is serialized as a quint32, followed by \a len bytes
1325 from \a s. Note that the data is \e not encoded.
1327 \sa writeRawData(), readBytes()
1330 QDataStream &QDataStream::writeBytes(const char *s, uint len)
1332 CHECK_STREAM_PRECOND(*this)
1333 *this << (quint32)len; // write length specifier
1334 if (len)
1335 writeRawData(s, len);
1336 return *this;
1341 Writes \a len bytes from \a s to the stream. Returns the
1342 number of bytes actually written, or -1 on error.
1343 The data is \e not encoded.
1345 \sa writeBytes(), QIODevice::write(), readRawData()
1348 int QDataStream::writeRawData(const char *s, int len)
1350 CHECK_STREAM_PRECOND(-1)
1351 return dev->write(s, len);
1355 \since 4.1
1357 Skips \a len bytes from the device. Returns the number of bytes
1358 actually skipped, or -1 on error.
1360 This is equivalent to calling readRawData() on a buffer of length
1361 \a len and ignoring the buffer.
1363 \sa QIODevice::seek()
1365 int QDataStream::skipRawData(int len)
1367 CHECK_STREAM_PRECOND(-1)
1369 if (dev->isSequential()) {
1370 char buf[4096];
1371 int sumRead = 0;
1373 while (len > 0) {
1374 int blockSize = qMin(len, (int)sizeof(buf));
1375 int n = dev->read(buf, blockSize);
1376 if (n == -1)
1377 return -1;
1378 if (n == 0)
1379 return sumRead;
1381 sumRead += n;
1382 len -= blockSize;
1384 return sumRead;
1385 } else {
1386 qint64 pos = dev->pos();
1387 qint64 size = dev->size();
1388 if (pos + len > size)
1389 len = size - pos;
1390 if (!dev->seek(pos + len))
1391 return -1;
1392 return len;
1396 #ifdef QT3_SUPPORT
1398 \fn QDataStream &QDataStream::readRawBytes(char *str, uint len)
1400 Use readRawData() instead.
1404 \fn QDataStream &QDataStream::writeRawBytes(const char *str, uint len)
1406 Use writeRawData() instead.
1408 #endif
1410 QT_END_NAMESPACE
1412 #endif // QT_NO_DATASTREAM