?? qdatastream.cpp
字號:
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;}/*! \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) 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); } } 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 \c{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. The \a l parameter is set to the length of the buffer. If the string read is empty, \a l is set to 0 and \a s is set to a null pointer. The serialization format is a quint32 length specifier first, then \a l bytes of data. \sa readRawData(), writeBytes()*/QDataStream &QDataStream::readBytes(char *&s, uint &l){ s = 0; l = 0; CHECK_STREAM_PRECOND(*this) quint32 len; *this >> len; if (len == 0) return *this; const quint32 Step = 1024 * 1024; quint32 allocated = 0; char *prevBuf = 0; char *curBuf = 0; do { int blockSize = qMin(Step, len - allocated); prevBuf = curBuf; curBuf = new char[allocated + blockSize + 1]; if (prevBuf) { memcpy(curBuf, prevBuf, allocated); delete [] prevBuf; } if (dev->read(curBuf + allocated, blockSize) != blockSize) { delete [] curBuf; setStatus(ReadPastEnd); return *this; } allocated += blockSize; } while (allocated < len); s = curBuf; s[len] = '\0'; l = (uint)len; return *this;}/*! Reads at most \a len bytes from the stream into \a s and returns the number of bytes read. If an error occurs, this function returns -1. The buffer \a s must be preallocated. The data is \e not encoded. \sa readBytes(), QIODevice::read(), writeRawData()*/int QDataStream::readRawData(char *s, int len){ CHECK_STREAM_PRECOND(-1) return dev->read(s, len);}/***************************************************************************** QDataStream write functions *****************************************************************************//*! \fn QDataStream &QDataStream::operator<<(quint8 i) \overload 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<<(qint8 i){ CHECK_STREAM_PRECOND(*this) dev->putChar(i); return *this;}/*! \fn QDataStream &QDataStream::operator<<(quint16 i) \overload 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<<(qint16 i){ CHECK_STREAM_PRECOND(*this) if (noswap) { dev->write((char *)&i, sizeof(qint16)); } else { // swap bytes register uchar *p = (uchar *)(&i); char b[2]; b[1] = *p++; b[0] = *p; dev->write(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<<(qint32 i){ CHECK_STREAM_PRECOND(*this) if (noswap) { dev->write((char *)&i, sizeof(qint32)); } else { // swap bytes register uchar *p = (uchar *)(&i); char b[4]; b[3] = *p++; b[2] = *p++; b[1] = *p++; b[0] = *p; dev->write(b, 4); } return *this;}/*! \fn QDataStream &QDataStream::operator<<(quint64 i) \overload 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<<(qint64 i){ CHECK_STREAM_PRECOND(*this) if (version() < 6) { quint32 i1 = i & 0xffffffff; quint32 i2 = i >> 32; *this << i2 << i1; } else if (noswap) { // no conversion needed dev->write((char *)&i, sizeof(qint64)); } else { // swap bytes register uchar *p = (uchar *)(&i); 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->write(b, 8); } return *this;}/*! \fn QDataStream &QDataStream::operator<<(quint32 i) \overload Writes an unsigned integer, \a i, to the stream as a 32-bit unsigned integer (quint32). Returns a reference to the stream.*//*! Writes a boolean value, \a i, to the stream. Returns a reference to the stream.*/QDataStream &QDataStream::operator<<(bool i){ CHECK_STREAM_PRECOND(*this) dev->putChar(qint8(i)); return *this;}/*! \overload Writes a 32-bit floating point number, \a f, to the stream using the standard IEEE 754 format. Returns a reference to the stream.*/QDataStream &QDataStream::operator<<(float f){ CHECK_STREAM_PRECOND(*this) float g = f; // fixes float-on-stack problem if (noswap) { // no conversion needed dev->write((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->write(b, 4); } return *this;}/*! \overload Writes a 64-bit floating point number, \a f, to the stream using the standard IEEE 754 format. Returns a reference to the stream.*/QDataStream &QDataStream::operator<<(double f){ CHECK_STREAM_PRECOND(*this) if (noswap) { dev->write((char *)&f, sizeof(double)); } else { 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->write(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 << (quint32)0; return *this; } uint len = qstrlen(s) + 1; // also write null terminator *this << (quint32)len; // write length specifier writeRawData(s, len); return *this;}/*! 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 quint32, followed by \a len bytes from \a s. Note that the data is \e not encoded. \sa writeRawData(), readBytes()*/QDataStream &QDataStream::writeBytes(const char *s, uint len){ CHECK_STREAM_PRECOND(*this) *this << (quint32)len; // write length specifier if (len) writeRawData(s, len); return *this;}/*! Writes \a len bytes from \a s to the stream. Returns the number of bytes actually written, or -1 on error. The data is \e not encoded. \sa writeBytes(), QIODevice::write(), readRawData()*/int QDataStream::writeRawData(const char *s, int len){ CHECK_STREAM_PRECOND(-1) return dev->write(s, len);}/*! \since 4.1 Skips \a len bytes from the device. Returns the number of bytes actually skipped, or -1 on error. This is equivalent to calling readRawData() on a buffer of length \a len and ignoring the buffer. \sa QIODevice::seek()*/int QDataStream::skipRawData(int len){ CHECK_STREAM_PRECOND(-1) if (dev->isSequential()) { char buf[4096]; int sumRead = 0; while (len > 0) { int blockSize = qMin(len, (int)sizeof(buf)); int n = dev->read(buf, blockSize); if (n == -1) return -1; if (n == 0) return sumRead; sumRead += n; len -= blockSize; } return sumRead; } else { quint64 pos = dev->pos(); len = qMin(int(dev->size() - pos), len); if (!dev->seek(pos + len)) return -1; return len; }}#ifdef QT3_SUPPORT/*! \fn QDataStream &QDataStream::readRawBytes(char *str, uint len) Use readRawData() instead.*//*! \fn QDataStream &QDataStream::writeRawBytes(const char *str, uint len) Use writeRawData() instead.*/#endif#endif // QT_NO_DATASTREAM
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -