qdatastream.cpp

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

CPP
1,123
字号
}/*!    \overload QDataStream &QDataStream::operator>>( Q_UINT32 &i )    Reads an unsigned 32-bit integer from the stream into \a i, and    returns a reference to the stream.*//*!    \overload    Reads a signed 32-bit integer from the stream into \a i, and    returns a reference to the stream.*/QDataStream &QDataStream::operator>>( Q_INT32 &i ){    CHECK_STREAM_PRECOND    if ( printable ) {				// printable data	i = (Q_INT32)read_int_ascii( this );    } else if ( noswap ) {			// no conversion needed	dev->readBlock( (char *)&i, sizeof(Q_INT32) );    } else {					// swap bytes	uchar *p = (uchar *)(&i);	char b[4];	dev->readBlock( b, 4 );	*p++ = b[3];	*p++ = b[2];	*p++ = b[1];	*p   = b[0];    }    return *this;}/*!    \overload QDataStream &QDataStream::operator>>( Q_UINT64 &i )    Reads an unsigned 64-bit integer from the stream, into \a i, and    returns a reference to the stream.*//*!    \overload    Reads a signed 64-bit integer from the stream into \a i, and    returns a reference to the stream.*/QDataStream &QDataStream::operator>>( Q_INT64 &i ){    CHECK_STREAM_PRECOND    if ( printable ) {				// printable data	i = read_int_ascii( this );    } else {	Q_UINT32 i1, i2;	*this >> i2 >> i1;	i = ((Q_UINT64)i1 << 32) + i2;    }    return *this;}/*!    \overload QDataStream &QDataStream::operator>>( Q_ULONG &i )    Reads an unsigned integer of the system's word length from the    stream, into \a i, and returns a reference to the stream.*//*!    \overload    Reads a signed integer of the system's word length from the stream    into \a i, and returns a reference to the stream.*/QDataStream &QDataStream::operator>>( Q_LONG &i ){    CHECK_STREAM_PRECOND    if ( printable ) {				// printable data	i = (Q_LONG)read_int_ascii( this );    } else if ( noswap ) {			// no conversion needed	dev->readBlock( (char *)&i, sizeof(Q_LONG) );    } else {					// swap bytes	register uchar *p = (uchar *)(&i);	char b[sizeof(Q_LONG)];	dev->readBlock( b, sizeof(Q_LONG) );	for ( int j = sizeof(Q_LONG); j;  )	    *p++ = b[--j];    }    return *this;}static double read_double_ascii( QDataStream *s ){    register int n = 0;    char buf[80];    for ( ;; ) {	buf[n] = s->device()->getch();	if ( buf[n] == '\n' || n > 78 )		// $-terminator	    break;	n++;    }    buf[n] = '\0';    return atof( buf );}/*!    \overload    Reads a 32-bit floating point number from the stream into \a f,    using the standard IEEE754 format. Returns a reference to the    stream.*/QDataStream &QDataStream::operator>>( float &f ){    CHECK_STREAM_PRECOND    if ( printable ) {				// printable data	f = (float)read_double_ascii( this );    } else if ( noswap ) {			// no conversion needed	dev->readBlock( (char *)&f, sizeof(float) );    } else {					// swap bytes	uchar *p = (uchar *)(&f);	char b[4];	dev->readBlock( b, 4 );	*p++ = b[3];	*p++ = b[2];	*p++ = b[1];	*p   = b[0];    }    return *this;}/*!    \overload    Reads a 64-bit floating point number from the stream into \a f,    using the standard IEEE754 format. Returns a reference to the    stream.*/QDataStream &QDataStream::operator>>( double &f ){    CHECK_STREAM_PRECOND    if ( printable ) {				// printable data	f = read_double_ascii( this );    } else if ( noswap ) {			// no conversion needed	dev->readBlock( (char *)&f, sizeof(double) );    } else {					// swap bytes	register uchar *p = (uchar *)(&f);	char b[8];	dev->readBlock( b, 8 );	*p++ = b[7];	*p++ = b[6];	*p++ = b[5];	*p++ = b[4];	*p++ = b[3];	*p++ = b[2];	*p++ = b[1];	*p   = b[0];    }    return *this;}/*!    \overload    Reads the '\0'-terminated string \a s from the stream and returns    a reference to the stream.    Space for the string is allocated using \c new -- the caller must    destroy it with delete[].*/QDataStream &QDataStream::operator>>( char *&s ){    uint len = 0;    return readBytes( s, len );}/*!    Reads the buffer \a s from the stream and returns a reference to    the stream.    The buffer \a s is allocated using \c new. Destroy it with the \c    delete[] operator. If the length is zero or \a s cannot be    allocated, \a s is set to 0.    The \a l parameter will be set to the length of the buffer.    The serialization format is a Q_UINT32 length specifier first,    then \a l bytes of data. Note that the data is \e not encoded.    \sa readRawBytes(), writeBytes()*/QDataStream &QDataStream::readBytes( char *&s, uint &l ){    CHECK_STREAM_PRECOND    Q_UINT32 len;    *this >> len;				// first read length spec    l = (uint)len;    if ( len == 0 || eof() ) {	s = 0;	return *this;    } else {	s = new char[len];			// create char array	Q_CHECK_PTR( s );	if ( !s )				// no memory	    return *this;	return readRawBytes( s, (uint)len );    }}/*!    Reads \a len bytes from the stream into \a s and returns a    reference to the stream.    The buffer \a s must be preallocated. The data is \e not encoded.    \sa readBytes(), QIODevice::readBlock(), writeRawBytes()*/QDataStream &QDataStream::readRawBytes( char *s, uint len ){    CHECK_STREAM_PRECOND    if ( printable ) {				// printable data	register Q_INT8 *p = (Q_INT8*)s;	if ( version() < 4 ) {	    while ( len-- ) {		Q_INT32 tmp;		*this >> tmp;		*p++ = tmp;	    }	} else {	    while ( len-- )		*this >> *p++;	}    } else {					// read data char array	dev->readBlock( s, len );    }    return *this;}/*****************************************************************************  QDataStream write functions *****************************************************************************//*!    \overload QDataStream &QDataStream::operator<<( Q_UINT8 i )    Writes an unsigned byte, \a i, to the stream and returns a    reference to the stream.*//*!    Writes a signed byte, \a i, to the stream and returns a reference    to the stream.*/QDataStream &QDataStream::operator<<( Q_INT8 i ){    CHECK_STREAM_PRECOND    if ( printable && (i == '\\' || !isprint((uchar) i)) ) {	char buf[6];				// write octal code	buf[0] = '\\';	buf[1] = '0' + ((i >> 6) & 0x07);	buf[2] = '0' + ((i >> 3) & 0x07);	buf[3] = '0' + (i & 0x07);	buf[4] = '\0';	dev->writeBlock( buf, 4 );    } else {	dev->putch( i );    }    return *this;}/*!    \overload QDataStream &QDataStream::operator<<( Q_UINT16 i )    Writes an unsigned 16-bit integer, \a i, to the stream and returns    a reference to the stream.*//*!    \overload    Writes a signed 16-bit integer, \a i, to the stream and returns a    reference to the stream.*/QDataStream &QDataStream::operator<<( Q_INT16 i ){    CHECK_STREAM_PRECOND    if ( printable ) {				// printable data	char buf[16];	sprintf( buf, "%d\n", i );	dev->writeBlock( buf, strlen(buf) );    } else if ( noswap ) {			// no conversion needed	dev->writeBlock( (char *)&i, sizeof(Q_INT16) );    } else {					// swap bytes	register uchar *p = (uchar *)(&i);	char b[2];	b[1] = *p++;	b[0] = *p;	dev->writeBlock( b, 2 );    }    return *this;}/*!    \overload    Writes a signed 32-bit integer, \a i, to the stream and returns a    reference to the stream.*/QDataStream &QDataStream::operator<<( Q_INT32 i ){    CHECK_STREAM_PRECOND    if ( printable ) {				// printable data	char buf[16];	sprintf( buf, "%d\n", i );	dev->writeBlock( buf, strlen(buf) );    } else if ( noswap ) {			// no conversion needed	dev->writeBlock( (char *)&i, sizeof(Q_INT32) );    } else {					// swap bytes	register uchar *p = (uchar *)(&i);	char b[4];	b[3] = *p++;	b[2] = *p++;	b[1] = *p++;	b[0] = *p;	dev->writeBlock( b, 4 );    }    return *this;}/*!    \overload QDataStream &QDataStream::operator<<( Q_UINT64 i )    Writes an unsigned 64-bit integer, \a i, to the stream and returns a    reference to the stream.*//*!    \overload    Writes a signed 64-bit integer, \a i, to the stream and returns a    reference to the stream.*/QDataStream &QDataStream::operator<<( Q_INT64 i ){    CHECK_STREAM_PRECOND    if ( printable ) {				// printable data	char buf[16];#ifdef Q_OS_WIN	sprintf( buf, "%I64d\n", i );#else	sprintf( buf, "%lld\n", i );#endif	dev->writeBlock( buf, strlen(buf) );    } else {	Q_UINT32 i1 = i & 0xffffffff;	Q_UINT32 i2 = i >> 32;	*this << i1 << i2;    }    return *this;}/*!    \overload QDataStream &QDataStream::operator<<( Q_ULONG i )    Writes an unsigned integer \a i, of the system's word length, to    the stream and returns a reference to the stream.*//*!    \overload    Writes a signed integer \a i, of the system's word length, to the    stream and returns a reference to the stream.*/QDataStream &QDataStream::operator<<( Q_LONG i ){    CHECK_STREAM_PRECOND    if ( printable ) {				// printable data	char buf[20];	sprintf( buf, "%ld\n", i );	dev->writeBlock( buf, strlen(buf) );    } else if ( noswap ) {			// no conversion needed	dev->writeBlock( (char *)&i, sizeof(Q_LONG) );    } else {					// swap bytes	register uchar *p = (uchar *)(&i);	char b[sizeof(Q_LONG)];	for ( int j = sizeof(Q_LONG); j;  )	    b[--j] = *p++;	dev->writeBlock( b, sizeof(Q_LONG) );    }    return *this;}extern void qt_fix_double(char *);/*!    \overload QDataStream &QDataStream::operator<<( Q_UINT32 i )    Writes an unsigned integer, \a i, to the stream as a 32-bit    unsigned integer (Q_UINT32). Returns a reference to the stream.*//*!    \overload    Writes a 32-bit floating point number, \a f, to the stream using    the standard IEEE754 format. Returns a reference to the stream.*/QDataStream &QDataStream::operator<<( float f ){    CHECK_STREAM_PRECOND    if ( printable ) {				// printable data	char buf[64];	sprintf( buf, "%g\n", (double)f );	qt_fix_double(buf);	dev->writeBlock( buf, strlen(buf) );    } else {	float g = f;				// fixes float-on-stack problem	if ( noswap ) {				// no conversion needed	    dev->writeBlock( (char *)&g, sizeof(float) );	} else {				// swap bytes	    register uchar *p = (uchar *)(&g);	    char b[4];	    b[3] = *p++;	    b[2] = *p++;	    b[1] = *p++;	    b[0] = *p;	    dev->writeBlock( b, 4 );	}    }    return *this;}/*!    \overload    Writes a 64-bit floating point number, \a f, to the stream using    the standard IEEE754 format. Returns a reference to the stream.*/QDataStream &QDataStream::operator<<( double f ){    CHECK_STREAM_PRECOND    if ( printable ) {				// printable data	char buf[64];	sprintf( buf, "%g\n", (double)f );	qt_fix_double(buf);	dev->writeBlock( buf, strlen(buf) );    } else if ( noswap ) {			// no conversion needed	dev->writeBlock( (char *)&f, sizeof(double) );    } else {					// swap bytes	register uchar *p = (uchar *)(&f);	char b[8];	b[7] = *p++;	b[6] = *p++;	b[5] = *p++;	b[4] = *p++;	b[3] = *p++;	b[2] = *p++;	b[1] = *p++;	b[0] = *p;	dev->writeBlock( b, 8 );    }    return *this;}/*!    \overload    Writes the '\0'-terminated string \a s to the stream and returns a    reference to the stream.    The string is serialized using writeBytes().*/QDataStream &QDataStream::operator<<( const char *s ){    if ( !s ) {	*this << (Q_UINT32)0;	return *this;    }    uint len = qstrlen( s ) + 1;			// also write null terminator    *this << (Q_UINT32)len;			// write length specifier    return writeRawBytes( s, len );}/*!    Writes the length specifier \a len and the buffer \a s to the    stream and returns a reference to the stream.    The \a len is serialized as a Q_UINT32, followed by \a len bytes    from \a s. Note that the data is \e not encoded.    \sa writeRawBytes(), readBytes()*/QDataStream &QDataStream::writeBytes(const char *s, uint len){    CHECK_STREAM_PRECOND    *this << (Q_UINT32)len;			// write length specifier    if ( len )	writeRawBytes( s, len );    return *this;}/*!    Writes \a len bytes from \a s to the stream and returns a    reference to the stream. The data is \e not encoded.    \sa writeBytes(), QIODevice::writeBlock(), readRawBytes()*/QDataStream &QDataStream::writeRawBytes( const char *s, uint len ){    CHECK_STREAM_PRECOND    if ( printable ) {				// write printable	if ( version() < 4 ) {	    register char *p = (char *)s;	    while ( len-- )		*this << *p++;	} else {	    register Q_INT8 *p = (Q_INT8*)s;	    while ( len-- )		*this << *p++;	}    } else {					// write data char array	dev->writeBlock( s, len );    }    return *this;}#endif // QT_NO_DATASTREAM

⌨️ 快捷键说明

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