⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qdatastream.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 3 页
字号:
}/*!    \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)#ifndef Q_DOUBLE_FORMAT    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);    }#else    register uchar *p = (uchar *)(&f);    char b[8];    if (noswap) {        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)] = *p;    } else {        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)] = *p;    }    dev->write(b, 8);#endif    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 + -