From d4c34ce65b0e2f4f2b93d798545a357411fe6655 Mon Sep 17 00:00:00 2001 From: alqualos Date: Tue, 29 Mar 2016 17:41:12 +0000 Subject: [PATCH] QuaZioDevice::atEnd() and bytesAvailable() (might break ABI, but probably safe) --- quazip/quazip/quaziodevice.cpp | 16 ++++++++++++++-- quazip/quazip/quaziodevice.h | 4 ++++ quazip/qztest/testquaziodevice.cpp | 26 ++++++++++++++++++++++++++ quazip/qztest/testquaziodevice.h | 1 + 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/quazip/quazip/quaziodevice.cpp b/quazip/quazip/quaziodevice.cpp index b736617..8580fcf 100644 --- a/quazip/quazip/quaziodevice.cpp +++ b/quazip/quazip/quaziodevice.cpp @@ -42,6 +42,7 @@ class QuaZIODevicePrivate { int outBufPos; int outBufSize; bool zBufError; + bool atEnd; int doFlush(QString &error); }; @@ -53,7 +54,8 @@ QuaZIODevicePrivate::QuaZIODevicePrivate(QIODevice *io): outBuf(NULL), outBufPos(0), outBufSize(0), - zBufError(false) + zBufError(false), + atEnd(false) { zins.zalloc = (alloc_func) NULL; zins.zfree = (free_func) NULL; @@ -316,5 +318,15 @@ bool QuaZIODevice::flush() bool QuaZIODevice::isSequential() const { - return true; + return true; +} + +bool QuaZIODevice::atEnd() const +{ + return (openMode() == NotOpen) || d->atEnd; +} + +qint64 QuaZIODevice::bytesAvailable() const +{ + return (atEnd() ? 0 : 1) + QIODevice::bytesAvailable(); } diff --git a/quazip/quazip/quaziodevice.h b/quazip/quazip/quaziodevice.h index 73952ef..63499c3 100644 --- a/quazip/quazip/quaziodevice.h +++ b/quazip/quazip/quaziodevice.h @@ -87,6 +87,10 @@ public: QIODevice *getIoDevice() const; /// Returns true. virtual bool isSequential() const; + /// Returns true iff the end of the compressed stream is reached. + virtual bool atEnd() const; + /// Returns the number of the bytes buffered. + virtual qint64 bytesAvailable() const; protected: /// Implementation of QIODevice::readData(). virtual qint64 readData(char *data, qint64 maxSize); diff --git a/quazip/qztest/testquaziodevice.cpp b/quazip/qztest/testquaziodevice.cpp index 7642f1f..56963b2 100644 --- a/quazip/qztest/testquaziodevice.cpp +++ b/quazip/qztest/testquaziodevice.cpp @@ -52,6 +52,32 @@ void TestQuaZIODevice::read() QVERIFY(!testDevice.isOpen()); } +void TestQuaZIODevice::readMany() +{ + QByteArray buf(256, 0); + z_stream zouts; + zouts.zalloc = (alloc_func) NULL; + zouts.zfree = (free_func) NULL; + zouts.opaque = NULL; + deflateInit(&zouts, Z_DEFAULT_COMPRESSION); + zouts.next_in = reinterpret_cast(const_cast("testtest")); + zouts.avail_in = 8; + zouts.next_out = reinterpret_cast(buf.data()); + zouts.avail_out = buf.size(); + deflate(&zouts, Z_FINISH); + deflateEnd(&zouts); + QBuffer testBuffer(&buf); + testBuffer.open(QIODevice::ReadOnly); + QuaZIODevice testDevice(&testBuffer); + QVERIFY(testDevice.open(QIODevice::ReadOnly)); + char outBuf[4]; + QCOMPARE(testDevice.read(outBuf, 4), static_cast(4)); + QVERIFY(!testDevice.atEnd()); + QVERIFY(testDevice.bytesAvailable() > 0); + testDevice.close(); + QVERIFY(!testDevice.isOpen()); +} + void TestQuaZIODevice::write() { QByteArray buf(256, 0); diff --git a/quazip/qztest/testquaziodevice.h b/quazip/qztest/testquaziodevice.h index ff0c547..e602b8c 100644 --- a/quazip/qztest/testquaziodevice.h +++ b/quazip/qztest/testquaziodevice.h @@ -31,6 +31,7 @@ class TestQuaZIODevice: public QObject { Q_OBJECT private slots: void read(); + void readMany(); void write(); };