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