~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/corelib/io/qdatastream.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

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