qdatastream.cpp

来自「Trolltech公司发布的图形界面操作系统。可在qt-embedded-2.3」· C++ 代码 · 共 1,123 行 · 第 1/2 页

CPP
1,123
字号
/************************************************************************ Copyright (C) 2000-2005 Trolltech AS.  All rights reserved.**** This file is part of the Qtopia Environment.** ** This program is free software; you can redistribute it and/or modify it** under the terms of the GNU General Public License as published by the** Free Software Foundation; either version 2 of the License, or (at your** option) any later version.** ** A copy of the GNU GPL license version 2 is included in this package as ** LICENSE.GPL.**** This program is distributed in the hope that it will be useful, but** WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ** See the GNU General Public License for more details.**** In addition, as a special exception Trolltech gives permission to link** the code of this program with Qtopia applications copyrighted, developed** and distributed by Trolltech under the terms of the Qtopia Personal Use** License Agreement. You must comply with the GNU General Public License** in all respects for all of the code used other than the applications** licensed under the Qtopia Personal Use License Agreement. If you modify** this file, you may extend this exception to your version of the file,** but you are not obligated to do so. If you do not wish to do so, delete** this exception statement from your version.** ** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#include "qdatastream.h"#ifndef QT_NO_DATASTREAM#include "qbuffer.h"#include <stdio.h>#include <ctype.h>#include <stdlib.h>#include <locale.h>/*!    \class QDataStream qdatastream.h    \reentrant    \brief The QDataStream class provides serialization of binary data    to a QIODevice.    \ingroup io    A data stream is a binary stream of encoded information which is    100% independent of the host computer's operating system, CPU or    byte order. For example, a data stream that is written by a PC    under Windows can be read by a Sun SPARC running Solaris.    You can also use a data stream to read/write \link #raw raw    unencoded binary data\endlink. If you want a "parsing" input    stream, see QTextStream.    The QDataStream class implements serialization of primitive types,    like \c char, \c short, \c int, \c char* etc. Serialization of    more complex data is accomplished by breaking up the data into    primitive units.    A data stream cooperates closely with a QIODevice. A QIODevice    represents an input/output medium one can read data from and write    data to. The QFile class is an example of an IO device.    Example (write binary data to a stream):    \code    QFile file( "file.dat" );    file.open( IO_WriteOnly );    QDataStream stream( &file ); // we will serialize the data into the file    stream << "the answer is";   // serialize a string    stream << (Q_INT32)42;       // serialize an integer    \endcode    Example (read binary data from a stream):    \code    QFile file( "file.dat" );    file.open( IO_ReadOnly );    QDataStream stream( &file );  // read the data serialized from the file    QString str;    Q_INT32 a;    stream >> str >> a;           // extract "the answer is" and 42    \endcode    Each item written to the stream is written in a predefined binary    format that varies depending on the item's type. Supported Qt    types include QBrush, QColor, QDateTime, QFont, QPixmap, QString,    QVariant and many others. For the complete list of all Qt types    supporting data streaming see the \link datastreamformat.html    Format of the QDataStream operators \endlink.    To take one example, a \c char* string is written as a 32-bit    integer equal to the length of the string including the NUL byte    ('\0'), followed by all the characters of the string including the    NUL byte. When reading a \c char* string, 4 bytes are read to    create the 32-bit length value, then that many characters for the    \c char* string including the NUL are read.    The initial IODevice is usually set in the constructor, but can be    changed with setDevice(). If you've reached the end of the data    (or if there is no IODevice set) atEnd() will return TRUE.    If you want the data to be compatible with an earlier version of    Qt use setVersion().    If you want the data to be human-readable, e.g. for debugging, you    can set the data stream into printable data mode with    setPrintableData(). The data is then written slower, in a bloated    but human readable format.    If you are producing a new binary data format, such as a file    format for documents created by your application, you could use a    QDataStream to write the data in a portable format. Typically, you    would write a brief header containing a magic string and a version    number to give yourself room for future expansion. For example:    \code    QFile file( "file.xxx" );    file.open( IO_WriteOnly );    QDataStream stream( &file );    // Write a header with a "magic number" and a version    stream << (Q_UINT32)0xA0B0C0D0;    stream << (Q_INT32)123;    // Write the data    stream << [lots of interesting data]    \endcode    Then read it in with:    \code    QFile file( "file.xxx" );    file.open( IO_ReadOnly );    QDataStream stream( &file );    // Read and check the header    Q_UINT32 magic;    stream >> magic;    if ( magic != 0xA0B0C0D0 )	return XXX_BAD_FILE_FORMAT;    // Read the version    Q_INT32 version;    stream >> version;    if ( version < 100 )	return XXX_BAD_FILE_TOO_OLD;    if ( version > 123 )	return XXX_BAD_FILE_TOO_NEW;    if ( version <= 110 )	stream.setVersion(1);    // Read the data    stream >> [lots of interesting data];    if ( version > 120 )	stream >> [data new in XXX version 1.2];    stream >> [other interesting data];    \endcode    You can select which byte order to use when serializing data. The    default setting is big endian (MSB first). Changing it to little    endian breaks the portability (unless the reader also changes to    little endian). We recommend keeping this setting unless you have    special requirements.    \target raw    \section1 Reading and writing raw binary data    You may wish to read/write your own raw binary data to/from the    data stream directly. Data may be read from the stream into a    preallocated char* using readRawBytes(). Similarly data can be    written to the stream using writeRawBytes(). Notice that any    encoding/decoding of the data must be done by you.    A similar pair of functions is readBytes() and writeBytes(). These    differ from their \e raw counterparts as follows: readBytes()    reads a Q_UINT32 which is taken to be the length of the data to be    read, then that number of bytes is read into the preallocated    char*; writeBytes() writes a Q_UINT32 containing the length of the    data, followed by the data. Notice that any encoding/decoding of    the data (apart from the length Q_UINT32) must be done by you.    \sa QTextStream QVariant*//*!    \enum QDataStream::ByteOrder    The byte order used for reading/writing the data.    \value BigEndian the default    \value LittleEndian*//*****************************************************************************  QDataStream member functions *****************************************************************************/#if defined(QT_CHECK_STATE)#undef  CHECK_STREAM_PRECOND#define CHECK_STREAM_PRECOND  if ( !dev ) {				\				qWarning( "QDataStream: No device" );	\				return *this; }#else#define CHECK_STREAM_PRECOND#endifstatic int  systemWordSize = 0;static bool systemBigEndian;static const int DefaultStreamVersion = 5;// ### On next version bump, QPen::width() should not be restricted to 8-bit values.// ### On next version bump, when streaming invalid QVariants, just the type should// be written, no "data" after it// 5 is default in Qt 3.1// 4 is default in Qt 3.0// 3 is default in Qt 2.1// 2 is the Qt 2.0.x format// 1 is the Qt 1.x format/*!    Constructs a data stream that has no IO device.    \sa setDevice()*/QDataStream::QDataStream(){    if ( systemWordSize == 0 )			// get system features	qSysInfo( &systemWordSize, &systemBigEndian );    dev	      = 0;				// no device set    owndev    = FALSE;    byteorder = BigEndian;			// default byte order    printable = FALSE;    ver	      = DefaultStreamVersion;    noswap    = systemBigEndian;}/*!    Constructs a data stream that uses the IO device \a d.    \warning If you use QSocket or QSocketDevice as the IO device \a d    for reading data, you must make sure that enough data is available    on the socket for the operation to successfully proceed;    QDataStream does not have any means to handle or recover from    short-reads.    \sa setDevice(), device()*/QDataStream::QDataStream( QIODevice *d ){    if ( systemWordSize == 0 )			// get system features	qSysInfo( &systemWordSize, &systemBigEndian );    dev	      = d;				// set device    owndev    = FALSE;    byteorder = BigEndian;			// default byte order    printable = FALSE;    ver	      = DefaultStreamVersion;    noswap    = systemBigEndian;}/*!    Constructs a data stream that operates on a byte array, \a a,    through an internal QBuffer device. The \a mode is a    QIODevice::mode(), usually either \c IO_ReadOnly or \c    IO_WriteOnly.    Example:    \code    static char bindata[] = { 231, 1, 44, ... };    QByteArray a;    a.setRawData( bindata, sizeof(bindata) );	// a points to bindata    QDataStream stream( a, IO_ReadOnly );	// open on a's data    stream >> [something];			// read raw bindata    a.resetRawData( bindata, sizeof(bindata) ); // finished    \endcode    The QByteArray::setRawData() function is not for the inexperienced.*/QDataStream::QDataStream( QByteArray a, int mode ){    if ( systemWordSize == 0 )			// get system features	qSysInfo( &systemWordSize, &systemBigEndian );    dev	      = new QBuffer( a );		// create device    ((QBuffer *)dev)->open( mode );		// open device    owndev    = TRUE;    byteorder = BigEndian;			// default byte order    printable = FALSE;    ver	      = DefaultStreamVersion;    noswap    = systemBigEndian;}/*!    Destroys the data stream.    The destructor will not affect the current IO device, unless it is    an internal IO device processing a QByteArray passed in the \e    constructor, in which case the internal IO device is destroyed.*/QDataStream::~QDataStream(){    if ( owndev )	delete dev;}/*!    \fn QIODevice *QDataStream::device() const    Returns the IO device currently set.    \sa setDevice(), unsetDevice()*//*!    void QDataStream::setDevice(QIODevice *d )    Sets the IO device to \a d.    \sa device(), unsetDevice()*/void QDataStream::setDevice(QIODevice *d ){    if ( owndev ) {	delete dev;	owndev = FALSE;    }    dev = d;}/*!    Unsets the IO device. This is the same as calling setDevice( 0 ).    \sa device(), setDevice()*/void QDataStream::unsetDevice(){    setDevice( 0 );}/*!    \fn bool QDataStream::atEnd() const    Returns TRUE if the IO device has reached the end position (end of    the stream or file) or if there is no IO device set; otherwise    returns FALSE, i.e. if the current position of the IO device is    before the end position.    \sa QIODevice::atEnd()*//*!\fn bool QDataStream::eof() const  \obsolete  Returns TRUE if the IO device has reached the end position (end of  stream or file) or if there is no IO device set.  Returns FALSE if the current position of the read/write head of the IO  device is somewhere before the end position.  \sa QIODevice::atEnd()*//*!    \fn int QDataStream::byteOrder() const    Returns the current byte order setting -- either \c BigEndian or    \c LittleEndian.    \sa setByteOrder()*//*!    Sets the serialization byte order to \a bo.    The \a bo parameter can be \c QDataStream::BigEndian or \c    QDataStream::LittleEndian.    The default setting is big endian. We recommend leaving this    setting unless you have special requirements.    \sa byteOrder()*/void QDataStream::setByteOrder( int bo ){    byteorder = bo;    if ( systemBigEndian )	noswap = byteorder == BigEndian;    else	noswap = byteorder == LittleEndian;}/*!    \fn bool QDataStream::isPrintableData() const    Returns TRUE if the printable data flag has been set; otherwise    returns FALSE.    \sa setPrintableData()*//*!    \fn void QDataStream::setPrintableData( bool enable )    If \a enable is TRUE, data will be output in a human readable    format. If \a enable is FALSE, data will be output in a binary    format.    If \a enable is TRUE, the write functions will generate output    that consists of printable characters (7 bit ASCII). This output    will typically be a lot larger than the default binary output, and    consequently slower to write.    We recommend only enabling printable data for debugging purposes.*//*!    \fn int QDataStream::version() const    Returns the version number of the data serialization format. In Qt    3.1, this number is 5.    \sa setVersion()*//*!    \fn void QDataStream::setVersion( int v )    Sets the version number of the data serialization format to \a v.    You don't need to set a version if you are using the current    version of Qt.    In order to accommodate new functionality, the datastream    serialization format of some Qt classes has changed in some    versions of Qt. If you want to read data that was created by an    earlier version of Qt, or write data that can be read by a program    that was compiled with an earlier version of Qt, use this function    to modify the serialization format of QDataStream.    \table    \header \i Qt Version	    \i QDataStream Version    \row \i Qt 3.1		    \i11 5    \row \i Qt 3.0		    \i11 4    \row \i Qt 2.1.x and Qt 2.2.x   \i11 3    \row \i Qt 2.0.x		    \i11 2    \row \i Qt 1.x		    \i11 1    \endtable    \sa version()*//*****************************************************************************  QDataStream read functions *****************************************************************************/static Q_INT64 read_int_ascii( QDataStream *s ){    register int n = 0;    char buf[40];    for ( ;; ) {	buf[n] = s->device()->getch();	if ( buf[n] == '\n' || n > 38 )		// $-terminator	    break;	n++;    }    buf[n] = '\0';#if defined(Q_OS_TEMP)    return strtol( buf, (char**)0, 10 );#elif defined(Q_OS_WIN)    return _atoi64( buf );#elif defined(Q_OS_HPUX)#  if defined(__LP64__)    return strtol(buf, (char**)0, 10);#  else    extern "C" long long __strtoll( const char *, char**, int );    return __strtoll( buf, (char**)0, 10 );#  endif#elif defined(Q_OS_MACX) && defined(QT_MACOSX_VERSION) && QT_MACOSX_VERSION < 0x1020    return strtoq( buf, (char**)0, 10 );#elif defined(Q_OS_OSF) && defined(Q_CC_DEC)    return strtol( buf, (char**)0, 10 );#else    return strtoll( buf, (char**)0, 10 );	// C99 function#endif}/*!    \overload QDataStream &QDataStream::operator>>( Q_UINT8 &i )    Reads an unsigned byte from the stream into \a i, and returns a    reference to the stream.*//*!    Reads a signed byte from the stream into \a i, and returns a    reference to the stream.*/QDataStream &QDataStream::operator>>( Q_INT8 &i ){    CHECK_STREAM_PRECOND    if ( printable ) {				// printable data	i = (Q_INT8)dev->getch();	if ( i == '\\' ) {			// read octal code	    char buf[4];	    dev->readBlock( buf, 3 );	    i = (buf[2] & 0x07)+((buf[1] & 0x07) << 3)+((buf[0] & 0x07) << 6);	}    } else {					// data or text	i = (Q_INT8)dev->getch();    }    return *this;}/*!    \overload QDataStream &QDataStream::operator>>( Q_UINT16 &i )    Reads an unsigned 16-bit integer from the stream into \a i, and    returns a reference to the stream.*//*!    \overload    Reads a signed 16-bit integer from the stream into \a i, and    returns a reference to the stream.*/QDataStream &QDataStream::operator>>( Q_INT16 &i ){    CHECK_STREAM_PRECOND    if ( printable ) {				// printable data	i = (Q_INT16)read_int_ascii( this );    } else if ( noswap ) {			// no conversion needed	dev->readBlock( (char *)&i, sizeof(Q_INT16) );    } else {					// swap bytes	register uchar *p = (uchar *)(&i);	char b[2];	dev->readBlock( b, 2 );	*p++ = b[1];	*p   = b[0];    }    return *this;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?