📄 qdatastream.cpp
字号:
bool QDataStream::atEnd() const{ return dev ? dev->atEnd() : true;}/*! Returns the status of the data stream. \sa Status setStatus() resetStatus()*/QDataStream::Status QDataStream::status() const{ return q_status;}/*! Resets the status of the data stream. \sa Status status() setStatus()*/void QDataStream::resetStatus(){ q_status = Ok;}/*! Sets the status of the data stream to the \a status given. \sa Status status() resetStatus()*/void QDataStream::setStatus(Status status){ if (q_status == Ok) q_status = status;}/*!\fn bool QDataStream::eof() const Use atEnd() instead.*//*! \fn int QDataStream::byteOrder() const Returns the current byte order setting -- either BigEndian or LittleEndian. \sa setByteOrder()*//*! Sets the serialization byte order to \a bo. The \a 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. \sa byteOrder()*/void QDataStream::setByteOrder(ByteOrder bo){ byteorder = bo; if (QSysInfo::ByteOrder == QSysInfo::BigEndian) noswap = (byteorder == BigEndian); else noswap = (byteorder == LittleEndian);}/*! \fn bool QDataStream::isPrintableData() const In Qt 4, this function always returns false. \sa setPrintableData()*//*! \fn void QDataStream::setPrintableData(bool enable) In Qt 3, this function enabled output in a human-readable format if \a enable was false. In Qt 4, QDataStream no longer provides a human-readable output. This function does nothing.*//*! \enum QDataStream::Version This enum provides symbolic synonyms for the data serialization format version numbers. \value Qt_1_0 Version 1 (Qt 1.x) \value Qt_2_0 Version 2 (Qt 2.0) \value Qt_2_1 Version 3 (Qt 2.1, 2.2, 2.3) \value Qt_3_0 Version 4 (Qt 3.0) \value Qt_3_1 Version 5 (Qt 3.1, 3.2) \value Qt_3_3 Version 6 (Qt 3.3) \value Qt_4_0 Version 7 (Qt 4.0, Qt 4.1) \value Qt_4_1 Version 7 (Qt 4.0, Qt 4.1) \value Qt_4_2 Version 8 (Qt 4.2) \value Qt_4_3 Version 9 (Qt 4.3) \sa setVersion(), version()*//*! \fn int QDataStream::version() const Returns the version number of the data serialization format. \sa setVersion(), Version*//*! \fn void QDataStream::setVersion(int v) Sets the version number of the data serialization format to \a v. You don't \e have to set a version if you are using the current version of Qt, but for your own custom binary formats we recommend that you do; see \l{Versioning} in the Detailed Description. 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 used by QDataStream. \table \header \i Qt Version \i QDataStream Version \row \i Qt 4.2 \i 8 \row \i Qt 4.0 \i 7 \row \i Qt 3.3 \i 6 \row \i Qt 3.1, 3.2 \i 5 \row \i Qt 3.0 \i 4 \row \i Qt 2.1, 2.2, 2.3 \i 3 \row \i Qt 2.0 \i 2 \row \i Qt 1.x \i 1 \endtable The \l Version enum provides symbolic constants for the different versions of Qt. For example: \code QDataStream out(file); out.setVersion(QDataStream::Qt_4_0); \endcode \sa version(), Version*//***************************************************************************** QDataStream read functions *****************************************************************************//*! \fn QDataStream &QDataStream::operator>>(quint8 &i) \overload 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>>(qint8 &i){ i = 0; CHECK_STREAM_PRECOND(*this) char c; if (!dev->getChar(&c)) setStatus(ReadPastEnd); else i = qint8(c); return *this;}/*! \fn QDataStream &QDataStream::operator>>(quint16 &i) \overload 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>>(qint16 &i){ i = 0; CHECK_STREAM_PRECOND(*this) if (noswap) { if (dev->read((char *)&i, 2) != 2) { i = 0; setStatus(ReadPastEnd); } } else { register uchar *p = (uchar *)(&i); char b[2]; if (dev->read(b, 2) == 2) { *p++ = b[1]; *p = b[0]; } else { setStatus(ReadPastEnd); } } return *this;}/*! \fn QDataStream &QDataStream::operator>>(quint32 &i) \overload 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>>(qint32 &i){ i = 0; CHECK_STREAM_PRECOND(*this) if (noswap) { if (dev->read((char *)&i, 4) != 4) { i = 0; setStatus(ReadPastEnd); } } else { // swap bytes uchar *p = (uchar *)(&i); char b[4]; if (dev->read(b, 4) == 4) { *p++ = b[3]; *p++ = b[2]; *p++ = b[1]; *p = b[0]; } else { setStatus(ReadPastEnd); } } return *this;}/*! \fn QDataStream &QDataStream::operator>>(quint64 &i) \overload 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>>(qint64 &i){ i = qint64(0); CHECK_STREAM_PRECOND(*this) if (version() < 6) { quint32 i1, i2; *this >> i2 >> i1; i = ((quint64)i1 << 32) + i2; } else if (noswap) { // no conversion needed if (dev->read((char *)&i, 8) != 8) { i = qint64(0); setStatus(ReadPastEnd); } } else { // swap bytes uchar *p = (uchar *)(&i); char b[8]; if (dev->read(b, 8) == 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]; } else { setStatus(ReadPastEnd); } } return *this;}/*! Reads a boolean value from the stream into \a i. Returns a reference to the stream.*/QDataStream &QDataStream::operator>>(bool &i){ qint8 v; *this >> v; i = !!v; return *this;}/*! \overload Reads a 32-bit floating point number from the stream into \a f, using the standard IEEE 754 format. Returns a reference to the stream.*/QDataStream &QDataStream::operator>>(float &f){ f = 0.0f; CHECK_STREAM_PRECOND(*this) if (noswap) { if (dev->read((char *)&f, 4) != 4) { f = 0.0f; setStatus(ReadPastEnd); } } else { // swap bytes uchar *p = (uchar *)(&f); char b[4]; if (dev->read(b, 4) == 4) { *p++ = b[3]; *p++ = b[2]; *p++ = b[1]; *p = b[0]; } else { setStatus(ReadPastEnd); } } return *this;}#if defined(Q_DOUBLE_FORMAT)#define Q_DF(x) Q_DOUBLE_FORMAT[(x)] - '0'#endif/*! \overload Reads a 64-bit floating point number from the stream into \a f, using the standard IEEE 754 format. Returns a reference to the stream.*/QDataStream &QDataStream::operator>>(double &f){ f = 0.0; CHECK_STREAM_PRECOND(*this)#ifndef Q_DOUBLE_FORMAT if (noswap) { if (dev->read((char *)&f, 8) != 8) { f = 0.0; setStatus(ReadPastEnd); } } else { // swap bytes register uchar *p = (uchar *)(&f); char b[8]; if (dev->read(b, 8) == 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]; } else { setStatus(ReadPastEnd); } }#else //non-standard floating point format register uchar *p = (uchar *)(&f); char b[8]; if (dev->read(b, 8) == 8) { if (noswap) { *p++ = b[Q_DF(0)]; *p++ = b[Q_DF(1)]; *p++ = b[Q_DF(2)]; *p++ = b[Q_DF(3)]; *p++ = b[Q_DF(4)]; *p++ = b[Q_DF(5)]; *p++ = b[Q_DF(6)]; *p = b[Q_DF(7)]; } else { *p++ = b[Q_DF(7)]; *p++ = b[Q_DF(6)]; *p++ = b[Q_DF(5)]; *p++ = b[Q_DF(4)]; *p++ = b[Q_DF(3)]; *p++ = b[Q_DF(2)]; *p++ = b[Q_DF(1)]; *p = b[Q_DF(0)]; } } else { setStatus(ReadPastEnd); }#endif return *this;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -