|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtCore module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "qdatastream.h" |
|
43 #include "qdatastream_p.h" |
|
44 |
|
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> |
|
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 |
|
159 \target Serializing Qt Classes |
|
160 \section1 Reading and writing other Qt classes. |
|
161 |
|
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: |
|
166 |
|
167 \code |
|
168 QDataStream &operator<<(QDataStream &, const QXxx &); |
|
169 QDataStream &operator>>(QDataStream &, QXxx &); |
|
170 \endcode |
|
171 |
|
172 For example, here are the stream operators declared as non-members |
|
173 of the QImage class: |
|
174 |
|
175 \code |
|
176 QDataStream & operator<< (QDataStream& stream, const QImage& image); |
|
177 QDataStream & operator>> (QDataStream& stream, QImage& image); |
|
178 \endcode |
|
179 |
|
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. |
|
183 |
|
184 \sa QTextStream QVariant |
|
185 */ |
|
186 |
|
187 /*! |
|
188 \enum QDataStream::ByteOrder |
|
189 |
|
190 The byte order used for reading/writing the data. |
|
191 |
|
192 \value BigEndian Most significant byte first (the default) |
|
193 \value LittleEndian Least significant byte first |
|
194 */ |
|
195 |
|
196 /*! |
|
197 \enum QDataStream::FloatingPointPrecision |
|
198 |
|
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. |
|
201 |
|
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. |
|
204 |
|
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. |
|
207 |
|
208 \sa setFloatingPointPrecision(), floatingPointPrecision() |
|
209 */ |
|
210 |
|
211 /*! |
|
212 \enum QDataStream::Status |
|
213 |
|
214 This enum describes the current status of the data stream. |
|
215 |
|
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. |
|
220 */ |
|
221 |
|
222 /***************************************************************************** |
|
223 QDataStream member functions |
|
224 *****************************************************************************/ |
|
225 |
|
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; \ |
|
232 } |
|
233 #else |
|
234 #define CHECK_STREAM_PRECOND(retVal) \ |
|
235 if (!dev) { \ |
|
236 return retVal; \ |
|
237 } |
|
238 #endif |
|
239 |
|
240 enum { |
|
241 DefaultStreamVersion = QDataStream::Qt_4_6 |
|
242 }; |
|
243 |
|
244 // ### 5.0: when streaming invalid QVariants, just the type should |
|
245 // be written, no "data" after it |
|
246 |
|
247 /*! |
|
248 Constructs a data stream that has no I/O device. |
|
249 |
|
250 \sa setDevice() |
|
251 */ |
|
252 |
|
253 QDataStream::QDataStream() |
|
254 { |
|
255 dev = 0; |
|
256 owndev = false; |
|
257 byteorder = BigEndian; |
|
258 ver = DefaultStreamVersion; |
|
259 noswap = QSysInfo::ByteOrder == QSysInfo::BigEndian; |
|
260 q_status = Ok; |
|
261 } |
|
262 |
|
263 /*! |
|
264 Constructs a data stream that uses the I/O device \a d. |
|
265 |
|
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. |
|
271 |
|
272 \sa setDevice(), device() |
|
273 */ |
|
274 |
|
275 QDataStream::QDataStream(QIODevice *d) |
|
276 { |
|
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; |
|
283 } |
|
284 |
|
285 #ifdef QT3_SUPPORT |
|
286 /*! |
|
287 \fn QDataStream::QDataStream(QByteArray *array, int mode) |
|
288 \compat |
|
289 |
|
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. |
|
293 */ |
|
294 QDataStream::QDataStream(QByteArray *a, int mode) |
|
295 { |
|
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; |
|
307 } |
|
308 #endif |
|
309 |
|
310 /*! |
|
311 \fn QDataStream::QDataStream(QByteArray *a, QIODevice::OpenMode mode) |
|
312 |
|
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. |
|
315 |
|
316 Alternatively, you can use QDataStream(const QByteArray &) if you |
|
317 just want to read from a byte array. |
|
318 |
|
319 Since QByteArray is not a QIODevice subclass, internally a QBuffer |
|
320 is created to wrap the byte array. |
|
321 */ |
|
322 |
|
323 QDataStream::QDataStream(QByteArray *a, QIODevice::OpenMode flags) |
|
324 { |
|
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; |
|
336 } |
|
337 |
|
338 /*! |
|
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. |
|
342 |
|
343 Since QByteArray is not a QIODevice subclass, internally a QBuffer |
|
344 is created to wrap the byte array. |
|
345 */ |
|
346 QDataStream::QDataStream(const QByteArray &a) |
|
347 { |
|
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; |
|
360 } |
|
361 |
|
362 /*! |
|
363 Destroys the data stream. |
|
364 |
|
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. |
|
369 */ |
|
370 |
|
371 QDataStream::~QDataStream() |
|
372 { |
|
373 if (owndev) |
|
374 delete dev; |
|
375 } |
|
376 |
|
377 |
|
378 /*! |
|
379 \fn QIODevice *QDataStream::device() const |
|
380 |
|
381 Returns the I/O device currently set, or 0 if no |
|
382 device is currently set. |
|
383 |
|
384 \sa setDevice() |
|
385 */ |
|
386 |
|
387 /*! |
|
388 void QDataStream::setDevice(QIODevice *d) |
|
389 |
|
390 Sets the I/O device to \a d, which can be 0 |
|
391 to unset to current I/O device. |
|
392 |
|
393 \sa device() |
|
394 */ |
|
395 |
|
396 void QDataStream::setDevice(QIODevice *d) |
|
397 { |
|
398 if (owndev) { |
|
399 delete dev; |
|
400 owndev = false; |
|
401 } |
|
402 dev = d; |
|
403 } |
|
404 |
|
405 /*! |
|
406 \obsolete |
|
407 Unsets the I/O device. |
|
408 Use setDevice(0) instead. |
|
409 */ |
|
410 |
|
411 void QDataStream::unsetDevice() |
|
412 { |
|
413 setDevice(0); |
|
414 } |
|
415 |
|
416 |
|
417 /*! |
|
418 \fn bool QDataStream::atEnd() const |
|
419 |
|
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. |
|
423 |
|
424 \sa QIODevice::atEnd() |
|
425 */ |
|
426 |
|
427 bool QDataStream::atEnd() const |
|
428 { |
|
429 return dev ? dev->atEnd() : true; |
|
430 } |
|
431 |
|
432 /*! |
|
433 Returns the floating point precision of the data stream. |
|
434 |
|
435 \since 4.6 |
|
436 |
|
437 \sa FloatingPointPrecision setFloatingPointPrecision() |
|
438 */ |
|
439 QDataStream::FloatingPointPrecision QDataStream::floatingPointPrecision() const |
|
440 { |
|
441 return d == 0 ? QDataStream::DoublePrecision : d->floatingPointPrecision; |
|
442 } |
|
443 |
|
444 /*! |
|
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. |
|
450 |
|
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. |
|
453 |
|
454 The default is DoublePrecision. |
|
455 |
|
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. |
|
458 |
|
459 \since 4.6 |
|
460 */ |
|
461 void QDataStream::setFloatingPointPrecision(QDataStream::FloatingPointPrecision precision) |
|
462 { |
|
463 if (d == 0) |
|
464 d.reset(new QDataStreamPrivate()); |
|
465 d->floatingPointPrecision = precision; |
|
466 } |
|
467 |
|
468 /*! |
|
469 Returns the status of the data stream. |
|
470 |
|
471 \sa Status setStatus() resetStatus() |
|
472 */ |
|
473 |
|
474 QDataStream::Status QDataStream::status() const |
|
475 { |
|
476 return q_status; |
|
477 } |
|
478 |
|
479 /*! |
|
480 Resets the status of the data stream. |
|
481 |
|
482 \sa Status status() setStatus() |
|
483 */ |
|
484 void QDataStream::resetStatus() |
|
485 { |
|
486 q_status = Ok; |
|
487 } |
|
488 |
|
489 /*! |
|
490 Sets the status of the data stream to the \a status given. |
|
491 |
|
492 \sa Status status() resetStatus() |
|
493 */ |
|
494 void QDataStream::setStatus(Status status) |
|
495 { |
|
496 if (q_status == Ok) |
|
497 q_status = status; |
|
498 } |
|
499 |
|
500 /*!\fn bool QDataStream::eof() const |
|
501 |
|
502 Use atEnd() instead. |
|
503 */ |
|
504 |
|
505 /*! |
|
506 \fn int QDataStream::byteOrder() const |
|
507 |
|
508 Returns the current byte order setting -- either BigEndian or |
|
509 LittleEndian. |
|
510 |
|
511 \sa setByteOrder() |
|
512 */ |
|
513 |
|
514 /*! |
|
515 Sets the serialization byte order to \a bo. |
|
516 |
|
517 The \a bo parameter can be QDataStream::BigEndian or |
|
518 QDataStream::LittleEndian. |
|
519 |
|
520 The default setting is big endian. We recommend leaving this |
|
521 setting unless you have special requirements. |
|
522 |
|
523 \sa byteOrder() |
|
524 */ |
|
525 |
|
526 void QDataStream::setByteOrder(ByteOrder bo) |
|
527 { |
|
528 byteorder = bo; |
|
529 if (QSysInfo::ByteOrder == QSysInfo::BigEndian) |
|
530 noswap = (byteorder == BigEndian); |
|
531 else |
|
532 noswap = (byteorder == LittleEndian); |
|
533 } |
|
534 |
|
535 |
|
536 /*! |
|
537 \fn bool QDataStream::isPrintableData() const |
|
538 |
|
539 In Qt 4, this function always returns false. |
|
540 |
|
541 \sa setPrintableData() |
|
542 */ |
|
543 |
|
544 /*! |
|
545 \fn void QDataStream::setPrintableData(bool enable) |
|
546 |
|
547 In Qt 3, this function enabled output in a human-readable |
|
548 format if \a enable was false. |
|
549 |
|
550 In Qt 4, QDataStream no longer provides a human-readable output. |
|
551 This function does nothing. |
|
552 */ |
|
553 |
|
554 /*! |
|
555 \enum QDataStream::Version |
|
556 |
|
557 This enum provides symbolic synonyms for the data serialization |
|
558 format version numbers. |
|
559 |
|
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) |
|
573 |
|
574 \sa setVersion(), version() |
|
575 */ |
|
576 |
|
577 /*! |
|
578 \fn int QDataStream::version() const |
|
579 |
|
580 Returns the version number of the data serialization format. |
|
581 |
|
582 \sa setVersion(), Version |
|
583 */ |
|
584 |
|
585 /*! |
|
586 \fn void QDataStream::setVersion(int v) |
|
587 |
|
588 Sets the version number of the data serialization format to \a v. |
|
589 |
|
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. |
|
594 |
|
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. |
|
601 |
|
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 |
|
615 |
|
616 The \l Version enum provides symbolic constants for the different |
|
617 versions of Qt. For example: |
|
618 |
|
619 \snippet doc/src/snippets/code/src_corelib_io_qdatastream.cpp 5 |
|
620 |
|
621 \sa version(), Version |
|
622 */ |
|
623 |
|
624 /***************************************************************************** |
|
625 QDataStream read functions |
|
626 *****************************************************************************/ |
|
627 |
|
628 /*! |
|
629 \fn QDataStream &QDataStream::operator>>(quint8 &i) |
|
630 \overload |
|
631 |
|
632 Reads an unsigned byte from the stream into \a i, and returns a |
|
633 reference to the stream. |
|
634 */ |
|
635 |
|
636 /*! |
|
637 Reads a signed byte from the stream into \a i, and returns a |
|
638 reference to the stream. |
|
639 */ |
|
640 |
|
641 QDataStream &QDataStream::operator>>(qint8 &i) |
|
642 { |
|
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; |
|
651 } |
|
652 |
|
653 |
|
654 /*! |
|
655 \fn QDataStream &QDataStream::operator>>(quint16 &i) |
|
656 \overload |
|
657 |
|
658 Reads an unsigned 16-bit integer from the stream into \a i, and |
|
659 returns a reference to the stream. |
|
660 */ |
|
661 |
|
662 /*! |
|
663 \overload |
|
664 |
|
665 Reads a signed 16-bit integer from the stream into \a i, and |
|
666 returns a reference to the stream. |
|
667 */ |
|
668 |
|
669 QDataStream &QDataStream::operator>>(qint16 &i) |
|
670 { |
|
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); |
|
677 } |
|
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); |
|
691 } |
|
692 } |
|
693 return *this; |
|
694 } |
|
695 |
|
696 |
|
697 /*! |
|
698 \fn QDataStream &QDataStream::operator>>(quint32 &i) |
|
699 \overload |
|
700 |
|
701 Reads an unsigned 32-bit integer from the stream into \a i, and |
|
702 returns a reference to the stream. |
|
703 */ |
|
704 |
|
705 /*! |
|
706 \overload |
|
707 |
|
708 Reads a signed 32-bit integer from the stream into \a i, and |
|
709 returns a reference to the stream. |
|
710 */ |
|
711 |
|
712 QDataStream &QDataStream::operator>>(qint32 &i) |
|
713 { |
|
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); |
|
720 } |
|
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); |
|
736 } |
|
737 } |
|
738 return *this; |
|
739 } |
|
740 |
|
741 /*! |
|
742 \fn QDataStream &QDataStream::operator>>(quint64 &i) |
|
743 \overload |
|
744 |
|
745 Reads an unsigned 64-bit integer from the stream, into \a i, and |
|
746 returns a reference to the stream. |
|
747 */ |
|
748 |
|
749 /*! |
|
750 \overload |
|
751 |
|
752 Reads a signed 64-bit integer from the stream into \a i, and |
|
753 returns a reference to the stream. |
|
754 */ |
|
755 |
|
756 QDataStream &QDataStream::operator>>(qint64 &i) |
|
757 { |
|
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); |
|
768 } |
|
769 } else { // swap bytes |
|
770 union { |
|
771 qint64 val1; |
|
772 char val2[8]; |
|
773 } x; |
|
774 |
|
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); |
|
789 } |
|
790 } |
|
791 return *this; |
|
792 } |
|
793 |
|
794 /*! |
|
795 Reads a boolean value from the stream into \a i. Returns a |
|
796 reference to the stream. |
|
797 */ |
|
798 QDataStream &QDataStream::operator>>(bool &i) |
|
799 { |
|
800 qint8 v; |
|
801 *this >> v; |
|
802 i = !!v; |
|
803 return *this; |
|
804 } |
|
805 |
|
806 /*! |
|
807 \overload |
|
808 |
|
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. |
|
812 |
|
813 \sa setFloatingPointPrecision() |
|
814 */ |
|
815 |
|
816 QDataStream &QDataStream::operator>>(float &f) |
|
817 { |
|
818 if (version() >= QDataStream::Qt_4_6 |
|
819 && floatingPointPrecision() == QDataStream::DoublePrecision) { |
|
820 double d; |
|
821 *this >> d; |
|
822 f = d; |
|
823 return *this; |
|
824 } |
|
825 |
|
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); |
|
832 } |
|
833 } else { // swap bytes |
|
834 union { |
|
835 float val1; |
|
836 char val2[4]; |
|
837 } x; |
|
838 |
|
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); |
|
849 } |
|
850 } |
|
851 return *this; |
|
852 } |
|
853 |
|
854 #if defined(Q_DOUBLE_FORMAT) |
|
855 #define Q_DF(x) Q_DOUBLE_FORMAT[(x)] - '0' |
|
856 #endif |
|
857 |
|
858 /*! |
|
859 \overload |
|
860 |
|
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. |
|
864 |
|
865 \sa setFloatingPointPrecision() |
|
866 */ |
|
867 |
|
868 QDataStream &QDataStream::operator>>(double &f) |
|
869 { |
|
870 if (version() >= QDataStream::Qt_4_6 |
|
871 && floatingPointPrecision() == QDataStream::SinglePrecision) { |
|
872 float d; |
|
873 *this >> d; |
|
874 f = d; |
|
875 return *this; |
|
876 } |
|
877 |
|
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); |
|
885 } |
|
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); |
|
905 } |
|
906 } |
|
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)]; |
|
934 } |
|
935 f = x.val1; |
|
936 } else { |
|
937 setStatus(ReadPastEnd); |
|
938 } |
|
939 #endif |
|
940 return *this; |
|
941 } |
|
942 |
|
943 |
|
944 /*! |
|
945 \overload |
|
946 |
|
947 Reads the '\0'-terminated string \a s from the stream and returns |
|
948 a reference to the stream. |
|
949 |
|
950 Space for the string is allocated using \c new -- the caller must |
|
951 destroy it with \c{delete[]}. |
|
952 */ |
|
953 |
|
954 QDataStream &QDataStream::operator>>(char *&s) |
|
955 { |
|
956 uint len = 0; |
|
957 return readBytes(s, len); |
|
958 } |
|
959 |
|
960 |
|
961 /*! |
|
962 Reads the buffer \a s from the stream and returns a reference to |
|
963 the stream. |
|
964 |
|
965 The buffer \a s is allocated using \c new. Destroy it with the \c |
|
966 delete[] operator. |
|
967 |
|
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. |
|
971 |
|
972 The serialization format is a quint32 length specifier first, |
|
973 then \a l bytes of data. |
|
974 |
|
975 \sa readRawData(), writeBytes() |
|
976 */ |
|
977 |
|
978 QDataStream &QDataStream::readBytes(char *&s, uint &l) |
|
979 { |
|
980 s = 0; |
|
981 l = 0; |
|
982 CHECK_STREAM_PRECOND(*this) |
|
983 |
|
984 quint32 len; |
|
985 *this >> len; |
|
986 if (len == 0) |
|
987 return *this; |
|
988 |
|
989 const quint32 Step = 1024 * 1024; |
|
990 quint32 allocated = 0; |
|
991 char *prevBuf = 0; |
|
992 char *curBuf = 0; |
|
993 |
|
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; |
|
1001 } |
|
1002 if (dev->read(curBuf + allocated, blockSize) != blockSize) { |
|
1003 delete [] curBuf; |
|
1004 setStatus(ReadPastEnd); |
|
1005 return *this; |
|
1006 } |
|
1007 allocated += blockSize; |
|
1008 } while (allocated < len); |
|
1009 |
|
1010 s = curBuf; |
|
1011 s[len] = '\0'; |
|
1012 l = (uint)len; |
|
1013 return *this; |
|
1014 } |
|
1015 |
|
1016 /*! |
|
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. |
|
1019 |
|
1020 The buffer \a s must be preallocated. The data is \e not encoded. |
|
1021 |
|
1022 \sa readBytes(), QIODevice::read(), writeRawData() |
|
1023 */ |
|
1024 |
|
1025 int QDataStream::readRawData(char *s, int len) |
|
1026 { |
|
1027 CHECK_STREAM_PRECOND(-1) |
|
1028 return dev->read(s, len); |
|
1029 } |
|
1030 |
|
1031 |
|
1032 /***************************************************************************** |
|
1033 QDataStream write functions |
|
1034 *****************************************************************************/ |
|
1035 |
|
1036 |
|
1037 /*! |
|
1038 \fn QDataStream &QDataStream::operator<<(quint8 i) |
|
1039 \overload |
|
1040 |
|
1041 Writes an unsigned byte, \a i, to the stream and returns a |
|
1042 reference to the stream. |
|
1043 */ |
|
1044 |
|
1045 /*! |
|
1046 Writes a signed byte, \a i, to the stream and returns a reference |
|
1047 to the stream. |
|
1048 */ |
|
1049 |
|
1050 QDataStream &QDataStream::operator<<(qint8 i) |
|
1051 { |
|
1052 CHECK_STREAM_PRECOND(*this) |
|
1053 dev->putChar(i); |
|
1054 return *this; |
|
1055 } |
|
1056 |
|
1057 |
|
1058 /*! |
|
1059 \fn QDataStream &QDataStream::operator<<(quint16 i) |
|
1060 \overload |
|
1061 |
|
1062 Writes an unsigned 16-bit integer, \a i, to the stream and returns |
|
1063 a reference to the stream. |
|
1064 */ |
|
1065 |
|
1066 /*! |
|
1067 \overload |
|
1068 |
|
1069 Writes a signed 16-bit integer, \a i, to the stream and returns a |
|
1070 reference to the stream. |
|
1071 */ |
|
1072 |
|
1073 QDataStream &QDataStream::operator<<(qint16 i) |
|
1074 { |
|
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); |
|
1089 } |
|
1090 return *this; |
|
1091 } |
|
1092 |
|
1093 /*! |
|
1094 \overload |
|
1095 |
|
1096 Writes a signed 32-bit integer, \a i, to the stream and returns a |
|
1097 reference to the stream. |
|
1098 */ |
|
1099 |
|
1100 QDataStream &QDataStream::operator<<(qint32 i) |
|
1101 { |
|
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); |
|
1118 } |
|
1119 return *this; |
|
1120 } |
|
1121 |
|
1122 /*! |
|
1123 \fn QDataStream &QDataStream::operator<<(quint64 i) |
|
1124 \overload |
|
1125 |
|
1126 Writes an unsigned 64-bit integer, \a i, to the stream and returns a |
|
1127 reference to the stream. |
|
1128 */ |
|
1129 |
|
1130 /*! |
|
1131 \overload |
|
1132 |
|
1133 Writes a signed 64-bit integer, \a i, to the stream and returns a |
|
1134 reference to the stream. |
|
1135 */ |
|
1136 |
|
1137 QDataStream &QDataStream::operator<<(qint64 i) |
|
1138 { |
|
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); |
|
1163 } |
|
1164 return *this; |
|
1165 } |
|
1166 |
|
1167 /*! |
|
1168 \fn QDataStream &QDataStream::operator<<(quint32 i) |
|
1169 \overload |
|
1170 |
|
1171 Writes an unsigned integer, \a i, to the stream as a 32-bit |
|
1172 unsigned integer (quint32). Returns a reference to the stream. |
|
1173 */ |
|
1174 |
|
1175 /*! |
|
1176 Writes a boolean value, \a i, to the stream. Returns a reference |
|
1177 to the stream. |
|
1178 */ |
|
1179 |
|
1180 QDataStream &QDataStream::operator<<(bool i) |
|
1181 { |
|
1182 CHECK_STREAM_PRECOND(*this) |
|
1183 dev->putChar(qint8(i)); |
|
1184 return *this; |
|
1185 } |
|
1186 |
|
1187 /*! |
|
1188 \overload |
|
1189 |
|
1190 Writes a floating point number, \a f, to the stream using |
|
1191 the standard IEEE 754 format. Returns a reference to the stream. |
|
1192 |
|
1193 \sa setFloatingPointPrecision() |
|
1194 */ |
|
1195 |
|
1196 QDataStream &QDataStream::operator<<(float f) |
|
1197 { |
|
1198 if (version() >= QDataStream::Qt_4_6 |
|
1199 && floatingPointPrecision() == QDataStream::DoublePrecision) { |
|
1200 *this << double(f); |
|
1201 return *this; |
|
1202 } |
|
1203 |
|
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); |
|
1221 } |
|
1222 return *this; |
|
1223 } |
|
1224 |
|
1225 |
|
1226 /*! |
|
1227 \overload |
|
1228 |
|
1229 Writes a floating point number, \a f, to the stream using |
|
1230 the standard IEEE 754 format. Returns a reference to the stream. |
|
1231 |
|
1232 \sa setFloatingPointPrecision() |
|
1233 */ |
|
1234 |
|
1235 QDataStream &QDataStream::operator<<(double f) |
|
1236 { |
|
1237 if (version() >= QDataStream::Qt_4_6 |
|
1238 && floatingPointPrecision() == QDataStream::SinglePrecision) { |
|
1239 *this << float(f); |
|
1240 return *this; |
|
1241 } |
|
1242 |
|
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); |
|
1264 } |
|
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; |
|
1291 } |
|
1292 dev->write(b, 8); |
|
1293 #endif |
|
1294 return *this; |
|
1295 } |
|
1296 |
|
1297 |
|
1298 /*! |
|
1299 \overload |
|
1300 |
|
1301 Writes the '\0'-terminated string \a s to the stream and returns a |
|
1302 reference to the stream. |
|
1303 |
|
1304 The string is serialized using writeBytes(). |
|
1305 */ |
|
1306 |
|
1307 QDataStream &QDataStream::operator<<(const char *s) |
|
1308 { |
|
1309 if (!s) { |
|
1310 *this << (quint32)0; |
|
1311 return *this; |
|
1312 } |
|
1313 uint len = qstrlen(s) + 1; // also write null terminator |
|
1314 *this << (quint32)len; // write length specifier |
|
1315 writeRawData(s, len); |
|
1316 return *this; |
|
1317 } |
|
1318 |
|
1319 |
|
1320 /*! |
|
1321 Writes the length specifier \a len and the buffer \a s to the |
|
1322 stream and returns a reference to the stream. |
|
1323 |
|
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. |
|
1326 |
|
1327 \sa writeRawData(), readBytes() |
|
1328 */ |
|
1329 |
|
1330 QDataStream &QDataStream::writeBytes(const char *s, uint len) |
|
1331 { |
|
1332 CHECK_STREAM_PRECOND(*this) |
|
1333 *this << (quint32)len; // write length specifier |
|
1334 if (len) |
|
1335 writeRawData(s, len); |
|
1336 return *this; |
|
1337 } |
|
1338 |
|
1339 |
|
1340 /*! |
|
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. |
|
1344 |
|
1345 \sa writeBytes(), QIODevice::write(), readRawData() |
|
1346 */ |
|
1347 |
|
1348 int QDataStream::writeRawData(const char *s, int len) |
|
1349 { |
|
1350 CHECK_STREAM_PRECOND(-1) |
|
1351 return dev->write(s, len); |
|
1352 } |
|
1353 |
|
1354 /*! |
|
1355 \since 4.1 |
|
1356 |
|
1357 Skips \a len bytes from the device. Returns the number of bytes |
|
1358 actually skipped, or -1 on error. |
|
1359 |
|
1360 This is equivalent to calling readRawData() on a buffer of length |
|
1361 \a len and ignoring the buffer. |
|
1362 |
|
1363 \sa QIODevice::seek() |
|
1364 */ |
|
1365 int QDataStream::skipRawData(int len) |
|
1366 { |
|
1367 CHECK_STREAM_PRECOND(-1) |
|
1368 |
|
1369 if (dev->isSequential()) { |
|
1370 char buf[4096]; |
|
1371 int sumRead = 0; |
|
1372 |
|
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; |
|
1380 |
|
1381 sumRead += n; |
|
1382 len -= blockSize; |
|
1383 } |
|
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; |
|
1393 } |
|
1394 } |
|
1395 |
|
1396 #ifdef QT3_SUPPORT |
|
1397 /*! |
|
1398 \fn QDataStream &QDataStream::readRawBytes(char *str, uint len) |
|
1399 |
|
1400 Use readRawData() instead. |
|
1401 */ |
|
1402 |
|
1403 /*! |
|
1404 \fn QDataStream &QDataStream::writeRawBytes(const char *str, uint len) |
|
1405 |
|
1406 Use writeRawData() instead. |
|
1407 */ |
|
1408 #endif |
|
1409 |
|
1410 QT_END_NAMESPACE |
|
1411 |
|
1412 #endif // QT_NO_DATASTREAM |