Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
QDataStream Class Reference

The QDataStream class provides serialization of binary data to a QIODevice. More...

#include <qdatastream.h>

Public Types

enum  ByteOrder { BigEndian, LittleEndian }
 

Public Member Functions

 QDataStream ()
 
 QDataStream (QIODevice *)
 
 QDataStream (QByteArray, int mode)
 
virtual ~QDataStream ()
 
QIODevicedevice () const
 
void setDevice (QIODevice *)
 
void unsetDevice ()
 
bool atEnd () const
 
bool eof () const
 
int byteOrder () const
 
void setByteOrder (int)
 
bool isPrintableData () const
 
void setPrintableData (bool)
 
int version () const
 
void setVersion (int)
 
QDataStreamoperator>> (Q_INT8 &i)
 
QDataStreamoperator>> (Q_UINT8 &i)
 
QDataStreamoperator>> (Q_INT16 &i)
 
QDataStreamoperator>> (Q_UINT16 &i)
 
QDataStreamoperator>> (Q_INT32 &i)
 
QDataStreamoperator>> (Q_UINT32 &i)
 
QDataStreamoperator>> (Q_INT64 &i)
 
QDataStreamoperator>> (Q_UINT64 &i)
 
QDataStreamoperator>> (float &f)
 
QDataStreamoperator>> (double &f)
 
QDataStreamoperator>> (char *&str)
 
QDataStreamoperator<< (Q_INT8 i)
 
QDataStreamoperator<< (Q_UINT8 i)
 
QDataStreamoperator<< (Q_INT16 i)
 
QDataStreamoperator<< (Q_UINT16 i)
 
QDataStreamoperator<< (Q_INT32 i)
 
QDataStreamoperator<< (Q_UINT32 i)
 
QDataStreamoperator<< (Q_INT64 i)
 
QDataStreamoperator<< (Q_UINT64 i)
 
QDataStreamoperator<< (float f)
 
QDataStreamoperator<< (double f)
 
QDataStreamoperator<< (const char *str)
 
QDataStreamreadBytes (char *&, uint &len)
 
QDataStreamreadRawBytes (char *, uint len)
 
QDataStreamwriteBytes (const char *, uint len)
 
QDataStreamwriteRawBytes (const char *, uint len)
 

Private Member Functions

 QDataStream (const QDataStream &)
 
QDataStreamoperator= (const QDataStream &)
 

Private Attributes

QIODevicedev
 
bool owndev
 
int byteorder
 
bool printable
 
bool noswap
 
int ver
 

Detailed Description

The QDataStream class provides serialization of binary data to a QIODevice.

A data stream is a binary stream of encoded information which is 100% independent of the host computer operation system, CPU or byte order. A stream that is written by a PC under DOS/Windows can be read by a Sun SPARC running Solaris.

The QDataStream class implements serialization of primitive types, like char, short, int, char* etc. Serialization of more complex data is accomplished by breaking up the data into primitive units.

The programmer 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.

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 data to a stream):

QFile f( "file.dta" );
f.open( IO_WriteOnly ); // open file for writing
QDataStream s( &f ); // serialize using f
s << "the answer is"; // serialize string
s << (Q_INT32)42; // serialize integer

Example (read data from a stream):

QFile f( "file.dta" );
f.open( IO_ReadOnly ); // open file for reading
QDataStream s( &f ); // serialize using f
char *str;
s >> str >> a; // "the answer is" and 42
delete str; // delete string

In the last example, if you read into a QString instead of a char* you do not have to delete it.

Normally, each item written to the stream is written in a fixed binary format. For example, a char* is written as a 32-bit integer equal to the length of the string including the NUL byte, followed by all the characters of the string including the NUL byte. Similarly when reading a string, 4 bytes are read to create the 32-bit length value, then that many characters for the string including the NUL. For a complete description of all Qt types supporting data streaming see Format of the QDataStream operators .

If you want a "parsing" input stream, see QTextStream. If you just want the data to be human-readable to aid in debugging, you can set the data stream into printable data mode with setPrintableData(). The data is then written slower, in a human readable bloated form that is sufficient for debugging.

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:

// Open the file.
QFile f( "file.xxx" );
f.open( IO_WriteOnly );
// Write a header with a "magic number" and a version
s << 0xa0b0c0d0;
s << 123;
// Write the data
s << [lots of interesting data]

Then read it in with:

// Open the file.
QFile f( "file.xxx" );
f.open( IO_ReadOnly );
// Read and check the header
s >> magic;
if ( magic != 0xa0b0c0d0 )
return XXX_BAD_FILE_FORMAT;
// Read the version
if ( version < 100 )
return XXX_BAD_FILE_TOO_OLD;
if ( version > 123 )
return XXX_BAD_FILE_TOO_NEW;
if ( version <= 110 )
s.setVersion(1);
// Read the data
s >> [lots of interesting data];
if ( version > 120 )
s >> [data new in XXX version 1.2];
s >> [other interesting data];
See also
QTextStream QVariant

Definition at line 47 of file qdatastream.h.

Member Enumeration Documentation

Enumerator
BigEndian 
LittleEndian 

Definition at line 62 of file qdatastream.h.

Constructor & Destructor Documentation

QDataStream::QDataStream ( )

Constructs a data stream that has no IO device.

See also
setDevice()

Definition at line 195 of file qdatastream.cpp.

196 {
197  if ( systemWordSize == 0 ) // get system features
199  dev = 0; // no device set
200  owndev = FALSE;
201  byteorder = BigEndian; // default byte order
202  printable = FALSE;
205 }
Q_EXPORT bool qSysInfo(int *wordSize, bool *bigEndian)
static bool systemBigEndian
QIODevice * dev
Definition: qdatastream.h:104
const bool FALSE
Definition: qglobal.h:370
static const int DefaultStreamVersion
bool printable
Definition: qdatastream.h:107
static int systemWordSize
QDataStream::QDataStream ( QIODevice d)

Constructs a data stream that uses the IO device d.

See also
setDevice(), device()

Definition at line 213 of file qdatastream.cpp.

214 {
215  if ( systemWordSize == 0 ) // get system features
217  dev = d; // set device
218  owndev = FALSE;
219  byteorder = BigEndian; // default byte order
220  printable = FALSE;
223 }
Q_EXPORT bool qSysInfo(int *wordSize, bool *bigEndian)
static bool systemBigEndian
QIODevice * dev
Definition: qdatastream.h:104
const bool FALSE
Definition: qglobal.h:370
static const int DefaultStreamVersion
bool printable
Definition: qdatastream.h:107
static int systemWordSize
QDataStream::QDataStream ( QByteArray  a,
int  mode 
)

Constructs a data stream that operates on a byte array through an internal QBuffer device.

Example:

static char bindata[] = { 231, 1, 44, ... };
a.setRawData( bindata, sizeof(bindata) ); // a points to bindata
QDataStream s( a, IO_ReadOnly ); // open on a's data
s >> [something]; // read raw bindata
a.resetRawData( bindata, sizeof(bindata) ); // finished

The QArray::setRawData() function is not for the inexperienced.

Definition at line 242 of file qdatastream.cpp.

243 {
244  if ( systemWordSize == 0 ) // get system features
246  dev = new QBuffer( a ); // create device
247  ((QBuffer *)dev)->open( mode ); // open device
248  owndev = TRUE;
249  byteorder = BigEndian; // default byte order
250  printable = FALSE;
253 }
Q_EXPORT bool qSysInfo(int *wordSize, bool *bigEndian)
static bool systemBigEndian
QIODevice * dev
Definition: qdatastream.h:104
const bool FALSE
Definition: qglobal.h:370
The QBuffer class is an I/O device that operates on a QByteArray.
Definition: qbuffer.h:47
static const int DefaultStreamVersion
bool printable
Definition: qdatastream.h:107
static int systemWordSize
const bool TRUE
Definition: qglobal.h:371
QDataStream::~QDataStream ( )
virtual

Destructs 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 constructor.

Definition at line 262 of file qdatastream.cpp.

263 {
264  if ( owndev )
265  delete dev;
266 }
QIODevice * dev
Definition: qdatastream.h:104
QDataStream::QDataStream ( const QDataStream )
private

Member Function Documentation

bool QDataStream::atEnd ( ) const
inline

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.

See also
QIODevice::atEnd()

Definition at line 126 of file qdatastream.h.

127 { return dev ? dev->atEnd() : TRUE; }
virtual bool atEnd() const
Definition: qiodevice.cpp:498
QIODevice * dev
Definition: qdatastream.h:104
const bool TRUE
Definition: qglobal.h:371
int QDataStream::byteOrder ( ) const
inline

Returns the current byte order setting - either BigEndian or LittleEndian.

See also
setByteOrder()

Definition at line 132 of file qdatastream.h.

133 { return byteorder; }
QIODevice * QDataStream::device ( ) const
inline

Returns the IO device currently set.

See also
setDevice(), unsetDevice()

Definition at line 123 of file qdatastream.h.

124 { return dev; }
QIODevice * dev
Definition: qdatastream.h:104
bool QDataStream::eof ( ) const
inline

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.

See also
QIODevice::atEnd()

Definition at line 129 of file qdatastream.h.

130 { return atEnd(); }
bool atEnd() const
Definition: qdatastream.h:126
bool QDataStream::isPrintableData ( ) const
inline

Returns TRUE if the printable data flag has been set.

See also
setPrintableData()

Definition at line 135 of file qdatastream.h.

136 { return printable; }
bool printable
Definition: qdatastream.h:107
QDataStream & QDataStream::operator<< ( Q_INT8  i)

Writes a signed byte to the stream.

Definition at line 698 of file qdatastream.cpp.

699 {
701  if ( printable && (i == '\\' || !isprint(i)) ) {
702  char buf[6]; // write octal code
703  buf[0] = '\\';
704  buf[1] = '0' + ((i >> 6) & 0x07);
705  buf[2] = '0' + ((i >> 3) & 0x07);
706  buf[3] = '0' + (i & 0x07);
707  buf[4] = '\0';
708  dev->writeBlock( buf, 4 );
709  } else {
710  dev->putch( i );
711  }
712  return *this;
713 }
QIODevice * dev
Definition: qdatastream.h:104
virtual int writeBlock(const char *data, uint len)=0
#define CHECK_STREAM_PRECOND
bool printable
Definition: qdatastream.h:107
virtual int putch(int)=0
QDataStream & QDataStream::operator<< ( Q_UINT8  i)
inline

Writes an unsigned byte to the stream and returns a reference to the stream.

Definition at line 159 of file qdatastream.h.

160 { return *this << (Q_INT8)i; }
signed char Q_INT8
Definition: qglobal.h:415
QDataStream & QDataStream::operator<< ( Q_INT16  i)

Writes a signed 16-bit integer to the stream and returns a reference to the stream.

Definition at line 727 of file qdatastream.cpp.

728 {
730  if ( printable ) { // printable data
731  char buf[16];
732  sprintf( buf, "%d\n", i );
733  dev->writeBlock( buf, strlen(buf) );
734  } else if ( noswap ) { // no conversion needed
735  dev->writeBlock( (char *)&i, sizeof(Q_INT16) );
736  } else { // swap bytes
737  register uchar *p = (uchar *)(&i);
738  char b[2];
739  b[1] = *p++;
740  b[0] = *p;
741  dev->writeBlock( b, 2 );
742  }
743  return *this;
744 }
QIODevice * dev
Definition: qdatastream.h:104
virtual int writeBlock(const char *data, uint len)=0
unsigned char uchar
Definition: nybbler.cc:11
#define CHECK_STREAM_PRECOND
p
Definition: test.py:223
static bool * b
Definition: config.cpp:1043
short Q_INT16
Definition: qglobal.h:417
bool printable
Definition: qdatastream.h:107
QDataStream & QDataStream::operator<< ( Q_UINT16  i)
inline

Writes an unsigned 16-bit integer to the stream and returns a reference to the stream.

Definition at line 162 of file qdatastream.h.

163 { return *this << (Q_INT16)i; }
short Q_INT16
Definition: qglobal.h:417
QDataStream & QDataStream::operator<< ( Q_INT32  i)

Writes a signed 32-bit integer to the stream and returns a reference to the stream.

Writes a signed integer to the stream as a 32-bit signed integer (Q_INT32). Returns a reference to the stream.

Definition at line 758 of file qdatastream.cpp.

759 {
761  if ( printable ) { // printable data
762  char buf[16];
763  sprintf( buf, "%d\n", i );
764  dev->writeBlock( buf, strlen(buf) );
765  } else if ( noswap ) { // no conversion needed
766  dev->writeBlock( (char *)&i, sizeof(Q_INT32) );
767  } else { // swap bytes
768  register uchar *p = (uchar *)(&i);
769  char b[4];
770  b[3] = *p++;
771  b[2] = *p++;
772  b[1] = *p++;
773  b[0] = *p;
774  dev->writeBlock( b, 4 );
775  }
776  return *this;
777 }
QIODevice * dev
Definition: qdatastream.h:104
virtual int writeBlock(const char *data, uint len)=0
unsigned char uchar
Definition: nybbler.cc:11
int Q_INT32
Definition: qglobal.h:419
#define CHECK_STREAM_PRECOND
p
Definition: test.py:223
static bool * b
Definition: config.cpp:1043
bool printable
Definition: qdatastream.h:107
QDataStream & QDataStream::operator<< ( Q_UINT32  i)
inline

Writes an unsigned 32-bit integer to the stream and returns a reference to the stream.

Definition at line 165 of file qdatastream.h.

166 { return *this << (Q_INT32)i; }
int Q_INT32
Definition: qglobal.h:419
QDataStream & QDataStream::operator<< ( Q_INT64  i)

Writes a signed 64-bit integer to the stream and returns a reference to the stream, or calls the Q_INT32-operator if 64 bit is not available.

Definition at line 790 of file qdatastream.cpp.

791 {
793  if ( printable ) { // printable data
794  char buf[20];
795  sprintf( buf, "%ld\n", i );
796  dev->writeBlock( buf, strlen(buf) );
797  } else if ( noswap ) { // no conversion needed
798  dev->writeBlock( (char *)&i, sizeof(Q_INT64) );
799  } else { // swap bytes
800  register uchar *p = (uchar *)(&i);
801  char b[sizeof(Q_INT64)];
802  if ( sizeof(Q_INT64) == 8 ) {
803  b[7] = *p++;
804  b[6] = *p++;
805  b[5] = *p++;
806  b[4] = *p++;
807  }
808  b[3] = *p++;
809  b[2] = *p++;
810  b[1] = *p++;
811  b[0] = *p;
812  dev->writeBlock( b, sizeof(Q_INT64) );
813  }
814  return *this;
815 }
QIODevice * dev
Definition: qdatastream.h:104
virtual int writeBlock(const char *data, uint len)=0
unsigned char uchar
Definition: nybbler.cc:11
#define CHECK_STREAM_PRECOND
p
Definition: test.py:223
static bool * b
Definition: config.cpp:1043
bool printable
Definition: qdatastream.h:107
long Q_INT64
Definition: qglobal.h:421
QDataStream & QDataStream::operator<< ( Q_UINT64  i)
inline

Writes an unsigned 64-bit integer to the stream and returns a reference to the stream, or uses the Q_UINT32-operator if 64 bit is not available.

Definition at line 168 of file qdatastream.h.

169 { return *this << (Q_INT64)i; }
long Q_INT64
Definition: qglobal.h:421
QDataStream & QDataStream::operator<< ( float  f)

Writes a 32-bit floating point number to the stream using the standard IEEE754 format. Returns a reference to the stream.

Definition at line 836 of file qdatastream.cpp.

837 {
839  if ( printable ) { // printable data
840  char buf[32];
841  sprintf( buf, "%g\n", (double)f );
842  dev->writeBlock( buf, strlen(buf) );
843  } else {
844  float g = f; // fixes float-on-stack problem
845  if ( noswap ) { // no conversion needed
846  dev->writeBlock( (char *)&g, sizeof(float) );
847  } else { // swap bytes
848  register uchar *p = (uchar *)(&g);
849  char b[4];
850  b[3] = *p++;
851  b[2] = *p++;
852  b[1] = *p++;
853  b[0] = *p;
854  dev->writeBlock( b, 4 );
855  }
856  }
857  return *this;
858 }
static constexpr double g
Definition: Units.h:144
QIODevice * dev
Definition: qdatastream.h:104
virtual int writeBlock(const char *data, uint len)=0
unsigned char uchar
Definition: nybbler.cc:11
#define CHECK_STREAM_PRECOND
p
Definition: test.py:223
static bool * b
Definition: config.cpp:1043
bool printable
Definition: qdatastream.h:107
QDataStream & QDataStream::operator<< ( double  f)

Writes a 64-bit floating point number to the stream using the standard IEEE754 format. Returns a reference to the stream.

Definition at line 866 of file qdatastream.cpp.

867 {
869  if ( printable ) { // printable data
870  char buf[32];
871  sprintf( buf, "%g\n", f );
872  dev->writeBlock( buf, strlen(buf) );
873  } else if ( noswap ) { // no conversion needed
874  dev->writeBlock( (char *)&f, sizeof(double) );
875  } else { // swap bytes
876  register uchar *p = (uchar *)(&f);
877  char b[8];
878  b[7] = *p++;
879  b[6] = *p++;
880  b[5] = *p++;
881  b[4] = *p++;
882  b[3] = *p++;
883  b[2] = *p++;
884  b[1] = *p++;
885  b[0] = *p;
886  dev->writeBlock( b, 8 );
887  }
888  return *this;
889 }
QIODevice * dev
Definition: qdatastream.h:104
virtual int writeBlock(const char *data, uint len)=0
unsigned char uchar
Definition: nybbler.cc:11
#define CHECK_STREAM_PRECOND
p
Definition: test.py:223
static bool * b
Definition: config.cpp:1043
bool printable
Definition: qdatastream.h:107
QDataStream & QDataStream::operator<< ( const char *  s)

Writes the '\0'-terminated string s to the stream and returns a reference to the stream.

The string is serialized using writeBytes().

Definition at line 899 of file qdatastream.cpp.

900 {
901  if ( !s ) {
902  *this << (Q_UINT32)0;
903  return *this;
904  }
905  uint len = qstrlen( s ) + 1; // also write null terminator
906  *this << (Q_UINT32)len; // write length specifier
907  return writeRawBytes( s, len );
908 }
Q_EXPORT uint qstrlen(const char *str)
Definition: qcstring.h:81
unsigned int Q_UINT32
Definition: qglobal.h:420
unsigned uint
Definition: qglobal.h:351
QDataStream & writeRawBytes(const char *, uint len)
static QCString * s
Definition: config.cpp:1042
QDataStream& QDataStream::operator= ( const QDataStream )
private
QDataStream & QDataStream::operator>> ( Q_INT8 i)

Reads a signed byte from the stream.

Definition at line 429 of file qdatastream.cpp.

430 {
432  if ( printable ) { // printable data
433  i = (Q_INT8)dev->getch();
434  if ( i == '\\' ) { // read octal code
435  char buf[4];
436  dev->readBlock( buf, 3 );
437  i = (buf[2] & 0x07)+((buf[1] & 0x07) << 3)+((buf[0] & 0x07) << 6);
438  }
439  } else { // data or text
440  i = (Q_INT8)dev->getch();
441  }
442  return *this;
443 }
virtual int readBlock(char *data, uint maxlen)=0
QIODevice * dev
Definition: qdatastream.h:104
virtual int getch()=0
signed char Q_INT8
Definition: qglobal.h:415
#define CHECK_STREAM_PRECOND
bool printable
Definition: qdatastream.h:107
QDataStream & QDataStream::operator>> ( Q_UINT8 i)
inline

Reads an unsigned byte from the stream and returns a reference to the stream.

Definition at line 147 of file qdatastream.h.

148 { return *this >> (Q_INT8&)i; }
signed char Q_INT8
Definition: qglobal.h:415
QDataStream & QDataStream::operator>> ( Q_INT16 i)

Reads a signed 16-bit integer from the stream and returns a reference to the stream.

Definition at line 457 of file qdatastream.cpp.

458 {
460  if ( printable ) { // printable data
461  i = (Q_INT16)read_int_ascii( this );
462  } else if ( noswap ) { // no conversion needed
463  dev->readBlock( (char *)&i, sizeof(Q_INT16) );
464  } else { // swap bytes
465  register uchar *p = (uchar *)(&i);
466  char b[2];
467  dev->readBlock( b, 2 );
468  *p++ = b[1];
469  *p = b[0];
470  }
471  return *this;
472 }
virtual int readBlock(char *data, uint maxlen)=0
QIODevice * dev
Definition: qdatastream.h:104
unsigned char uchar
Definition: nybbler.cc:11
#define CHECK_STREAM_PRECOND
p
Definition: test.py:223
static Q_INT32 read_int_ascii(QDataStream *s)
static bool * b
Definition: config.cpp:1043
short Q_INT16
Definition: qglobal.h:417
bool printable
Definition: qdatastream.h:107
QDataStream & QDataStream::operator>> ( Q_UINT16 i)
inline

Reads an unsigned 16-bit integer from the stream and returns a reference to the stream.

Definition at line 150 of file qdatastream.h.

151 { return *this >> (Q_INT16&)i; }
short Q_INT16
Definition: qglobal.h:417
QDataStream & QDataStream::operator>> ( Q_INT32 i)

Reads a signed 32-bit integer from the stream and returns a reference to the stream.

Definition at line 486 of file qdatastream.cpp.

487 {
489  if ( printable ) { // printable data
490  i = read_int_ascii( this );
491  } else if ( noswap ) { // no conversion needed
492  dev->readBlock( (char *)&i, sizeof(Q_INT32) );
493  } else { // swap bytes
494  register uchar *p = (uchar *)(&i);
495  char b[4];
496  dev->readBlock( b, 4 );
497  *p++ = b[3];
498  *p++ = b[2];
499  *p++ = b[1];
500  *p = b[0];
501  }
502  return *this;
503 }
virtual int readBlock(char *data, uint maxlen)=0
QIODevice * dev
Definition: qdatastream.h:104
unsigned char uchar
Definition: nybbler.cc:11
int Q_INT32
Definition: qglobal.h:419
#define CHECK_STREAM_PRECOND
p
Definition: test.py:223
static Q_INT32 read_int_ascii(QDataStream *s)
static bool * b
Definition: config.cpp:1043
bool printable
Definition: qdatastream.h:107
QDataStream & QDataStream::operator>> ( Q_UINT32 i)
inline

Reads an unsigned 32-bit integer from the stream and returns a reference to the stream.

Definition at line 153 of file qdatastream.h.

154 { return *this >> (Q_INT32&)i; }
int Q_INT32
Definition: qglobal.h:419
QDataStream & QDataStream::operator>> ( Q_INT64 i)

Reads a signed 64-bit integer from the stream and returns a reference to the stream, or uses the Q_UINT32 operator if 64 bit is not available.

Definition at line 516 of file qdatastream.cpp.

517 {
519  if ( printable ) { // printable data
520  i = read_int_ascii( this );
521  } else if ( noswap ) { // no conversion needed
522  dev->readBlock( (char *)&i, sizeof(Q_INT64) );
523  } else { // swap bytes
524  register uchar *p = (uchar *)(&i);
525  char b[sizeof(Q_INT64)];
526  dev->readBlock( b, sizeof(Q_INT64) );
527  if ( sizeof(Q_INT64) == 8 ) {
528  *p++ = b[7];
529  *p++ = b[6];
530  *p++ = b[5];
531  *p++ = b[4];
532  }
533  *p++ = b[3];
534  *p++ = b[2];
535  *p++ = b[1];
536  *p = b[0];
537  }
538  return *this;
539 }
virtual int readBlock(char *data, uint maxlen)=0
QIODevice * dev
Definition: qdatastream.h:104
unsigned char uchar
Definition: nybbler.cc:11
#define CHECK_STREAM_PRECOND
p
Definition: test.py:223
static Q_INT32 read_int_ascii(QDataStream *s)
static bool * b
Definition: config.cpp:1043
bool printable
Definition: qdatastream.h:107
long Q_INT64
Definition: qglobal.h:421
QDataStream & QDataStream::operator>> ( Q_UINT64 i)
inline

Reads an unsigned 64-bit integer from the stream and returns a reference to the stream, or uses the Q_UINT32 operator if 64 bit is not available.

Definition at line 156 of file qdatastream.h.

157 { return *this >> (Q_INT64&)i; }
long Q_INT64
Definition: qglobal.h:421
QDataStream & QDataStream::operator>> ( float &  f)

Reads a 32-bit floating point number from the stream using the standard IEEE754 format. Returns a reference to the stream.

Definition at line 561 of file qdatastream.cpp.

562 {
564  if ( printable ) { // printable data
565  f = (float)read_double_ascii( this );
566  } else if ( noswap ) { // no conversion needed
567  dev->readBlock( (char *)&f, sizeof(float) );
568  } else { // swap bytes
569  register uchar *p = (uchar *)(&f);
570  char b[4];
571  dev->readBlock( b, 4 );
572  *p++ = b[3];
573  *p++ = b[2];
574  *p++ = b[1];
575  *p = b[0];
576  }
577  return *this;
578 }
virtual int readBlock(char *data, uint maxlen)=0
QIODevice * dev
Definition: qdatastream.h:104
unsigned char uchar
Definition: nybbler.cc:11
#define CHECK_STREAM_PRECOND
p
Definition: test.py:223
static double read_double_ascii(QDataStream *s)
static bool * b
Definition: config.cpp:1043
bool printable
Definition: qdatastream.h:107
QDataStream & QDataStream::operator>> ( double &  f)

Reads a 64-bit floating point number from the stream using the standard IEEE754 format. Returns a reference to the stream.

Definition at line 586 of file qdatastream.cpp.

587 {
589  if ( printable ) { // printable data
590  f = read_double_ascii( this );
591  } else if ( noswap ) { // no conversion needed
592  dev->readBlock( (char *)&f, sizeof(double) );
593  } else { // swap bytes
594  register uchar *p = (uchar *)(&f);
595  char b[8];
596  dev->readBlock( b, 8 );
597  *p++ = b[7];
598  *p++ = b[6];
599  *p++ = b[5];
600  *p++ = b[4];
601  *p++ = b[3];
602  *p++ = b[2];
603  *p++ = b[1];
604  *p = b[0];
605  }
606  return *this;
607 }
virtual int readBlock(char *data, uint maxlen)=0
QIODevice * dev
Definition: qdatastream.h:104
unsigned char uchar
Definition: nybbler.cc:11
#define CHECK_STREAM_PRECOND
p
Definition: test.py:223
static double read_double_ascii(QDataStream *s)
static bool * b
Definition: config.cpp:1043
bool printable
Definition: qdatastream.h:107
QDataStream & QDataStream::operator>> ( char *&  s)

Reads the '\0'-terminated string s from the stream and returns a reference to the stream.

Space for the string is allocated using new - the caller must eventually call delete[] on the value.

Definition at line 618 of file qdatastream.cpp.

619 {
620  uint len = 0;
621  return readBytes( s, len );
622 }
QDataStream & readBytes(char *&, uint &len)
unsigned uint
Definition: qglobal.h:351
static QCString * s
Definition: config.cpp:1042
QDataStream & QDataStream::readBytes ( char *&  s,
uint l 
)

Reads the buffer s from the stream and returns a reference to the stream.

The buffer s is allocated using new. Destroy it with the delete[] operator. If the length is zero or s cannot be allocated, s is set to 0.

The l parameter will be set to the length of the buffer.

The serialization format is an Q_UINT32 length specifier first, then the data (l bytes).

See also
readRawBytes(), writeBytes()

Definition at line 641 of file qdatastream.cpp.

642 {
644  Q_UINT32 len;
645  *this >> len; // first read length spec
646  l = (uint)len;
647  if ( len == 0 || eof() ) {
648  s = 0;
649  return *this;
650  } else {
651  s = new char[len]; // create char array
652  CHECK_PTR( s );
653  if ( !s ) // no memory
654  return *this;
655  return readRawBytes( s, (uint)len );
656  }
657 }
static QStrList * l
Definition: config.cpp:1044
#define CHECK_STREAM_PRECOND
bool eof() const
Definition: qdatastream.h:129
QDataStream & readRawBytes(char *, uint len)
unsigned int Q_UINT32
Definition: qglobal.h:420
#define CHECK_PTR(p)
Definition: qglobal.h:601
unsigned uint
Definition: qglobal.h:351
static QCString * s
Definition: config.cpp:1042
QDataStream & QDataStream::readRawBytes ( char *  s,
uint  len 
)

Reads len bytes from the stream into s and returns a reference to the stream.

The buffer s must be preallocated.

See also
readBytes(), QIODevice::readBlock(), writeRawBytes()

Definition at line 669 of file qdatastream.cpp.

670 {
672  if ( printable ) { // printable data
673  register Q_INT8 *p = (Q_INT8*)s;
674  while ( len-- )
675  *this >> *p++;
676  } else { // read data char array
677  dev->readBlock( s, len );
678  }
679  return *this;
680 }
virtual int readBlock(char *data, uint maxlen)=0
QIODevice * dev
Definition: qdatastream.h:104
signed char Q_INT8
Definition: qglobal.h:415
#define CHECK_STREAM_PRECOND
p
Definition: test.py:223
bool printable
Definition: qdatastream.h:107
static QCString * s
Definition: config.cpp:1042
void QDataStream::setByteOrder ( int  bo)

Sets the serialization byte order to bo.

The bo parameter can be QDataStream::BigEndian or QDataStream::LittleEndian.

The default setting is big endian. We recommend leaving this setting unless you have special requirements.

See also
byteOrder()

Definition at line 345 of file qdatastream.cpp.

346 {
347  byteorder = bo;
348  if ( systemBigEndian )
350  else
352 }
static bool systemBigEndian
void QDataStream::setDevice ( QIODevice d)

void QDataStream::setDevice(QIODevice *d ) Sets the IO device to d.

See also
device(), unsetDevice()

Definition at line 281 of file qdatastream.cpp.

282 {
283  if ( owndev ) {
284  delete dev;
285  owndev = FALSE;
286  }
287  dev = d;
288 }
QIODevice * dev
Definition: qdatastream.h:104
const bool FALSE
Definition: qglobal.h:370
void QDataStream::setPrintableData ( bool  enable)
inline

Sets or clears the printable data flag.

If this flag is set, the write functions will generate output that consists of printable characters (7 bit ASCII).

We recommend enabling printable data only for debugging purposes (it is slower and creates larger output).

Definition at line 138 of file qdatastream.h.

139 { printable = p; }
p
Definition: test.py:223
bool printable
Definition: qdatastream.h:107
void QDataStream::setVersion ( int  v)
inline

Sets the version number of the data serialization format.

In order to accomodate for 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.

For Qt 1.x compatibility, use v == 1.

For Qt 2.0.x compatibility, use v == 2 (Not required for reading in Qt 2.1).

See also
version()

Definition at line 144 of file qdatastream.h.

145 { ver = v; }
void QDataStream::unsetDevice ( )

Unsets the IO device. This is the same as calling setDevice( 0 ).

See also
device(), setDevice()

Definition at line 295 of file qdatastream.cpp.

296 {
297  setDevice( 0 );
298 }
void setDevice(QIODevice *)
int QDataStream::version ( ) const
inline

Returns the version number of the data serialization format. In Qt 2.1, this number is by default 3.

See also
setVersion()

Definition at line 141 of file qdatastream.h.

142 { return ver; }
QDataStream & QDataStream::writeBytes ( const char *  s,
uint  len 
)

Writes the length specifier len and the buffer s to the stream and returns a reference to the stream.

The len is serialized as an Q_UINT32, followed by len bytes from s.

See also
writeRawBytes(), readBytes()

Definition at line 921 of file qdatastream.cpp.

922 {
924  *this << (Q_UINT32)len; // write length specifier
925  if ( len )
926  writeRawBytes( s, len );
927  return *this;
928 }
#define CHECK_STREAM_PRECOND
unsigned int Q_UINT32
Definition: qglobal.h:420
QDataStream & writeRawBytes(const char *, uint len)
static QCString * s
Definition: config.cpp:1042
QDataStream & QDataStream::writeRawBytes ( const char *  s,
uint  len 
)

Writes len bytes from s to the stream and returns a reference to the stream.

See also
writeBytes(), QIODevice::writeBlock(), readRawBytes()

Definition at line 938 of file qdatastream.cpp.

939 {
941  if ( printable ) { // write printable
942  register char *p = (char *)s;
943  while ( len-- )
944  *this << *p++;
945  } else { // write data char array
946  dev->writeBlock( s, len );
947  }
948  return *this;
949 }
QIODevice * dev
Definition: qdatastream.h:104
virtual int writeBlock(const char *data, uint len)=0
#define CHECK_STREAM_PRECOND
p
Definition: test.py:223
bool printable
Definition: qdatastream.h:107
static QCString * s
Definition: config.cpp:1042

Member Data Documentation

int QDataStream::byteorder
private

Definition at line 106 of file qdatastream.h.

QIODevice* QDataStream::dev
private

Definition at line 104 of file qdatastream.h.

bool QDataStream::noswap
private

Definition at line 108 of file qdatastream.h.

bool QDataStream::owndev
private

Definition at line 105 of file qdatastream.h.

bool QDataStream::printable
private

Definition at line 107 of file qdatastream.h.

int QDataStream::ver
private

Definition at line 109 of file qdatastream.h.


The documentation for this class was generated from the following files: