27 #pragma warning(disable : 4996)
29 #define QUAZIO_INBUFSIZE 4096
30 #define QUAZIO_OUTBUFSIZE 4096
33 class QuaZIODevicePrivate {
35 QuaZIODevicePrivate(QIODevice *io);
36 ~QuaZIODevicePrivate();
48 int doFlush(QString &
error);
51 QuaZIODevicePrivate::QuaZIODevicePrivate(QIODevice *io)
61 zins.zalloc = (alloc_func)
NULL;
62 zins.zfree = (free_func)
NULL;
64 zouts.zalloc = (alloc_func)
NULL;
65 zouts.zfree = (free_func)
NULL;
69 #ifdef QUAZIP_ZIODEVICE_DEBUG_OUTPUT
70 debug.setFileName(
"debug.out");
71 debug.open(QIODevice::WriteOnly);
73 #ifdef QUAZIP_ZIODEVICE_DEBUG_INPUT
74 indebug.setFileName(
"debug.in");
75 indebug.open(QIODevice::WriteOnly);
79 QuaZIODevicePrivate::~QuaZIODevicePrivate() {
80 #ifdef QUAZIP_ZIODEVICE_DEBUG_OUTPUT
83 #ifdef QUAZIP_ZIODEVICE_DEBUG_INPUT
86 if (inBuf !=
NULL)
delete[] inBuf;
87 if (outBuf !=
NULL)
delete[] outBuf;
90 int QuaZIODevicePrivate::doFlush(QString &
error) {
92 while (outBufPos < outBufSize) {
93 int more = io->write(outBuf + outBufPos, outBufSize - outBufPos);
95 error = io->errorString();
102 if (outBufPos == outBufSize) {
103 outBufPos = outBufSize = 0;
112 #ifdef QUAZIP_ZIODEVICE_DEBUG_OUTPUT
116 #ifdef QUAZIP_ZIODEVICE_DEBUG_INPUT
118 static QFile indebug;
122 : QIODevice(parent), d(new QuaZIODevicePrivate(io)) {
123 connect(io, SIGNAL(readyRead()), SIGNAL(readyRead()));
136 #
if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
137 tr(
"QIODevice::Append is not supported for"
140 trUtf8(
"QIODevice::Append is not supported for"
145 if ((mode & QIODevice::ReadWrite) == QIODevice::ReadWrite) {
147 #
if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
148 tr(
"QIODevice::ReadWrite is not supported for"
151 trUtf8(
"QIODevice::ReadWrite is not supported for"
156 if ((mode & QIODevice::ReadOnly) != 0) {
157 if (inflateInit(&d->zins) != Z_OK) {
158 setErrorString(d->zins.msg);
162 if ((mode & QIODevice::WriteOnly) != 0) {
163 if (deflateInit(&d->zouts, Z_DEFAULT_COMPRESSION) != Z_OK) {
164 setErrorString(d->zouts.msg);
168 return QIODevice::open(mode);
172 if ((openMode() & QIODevice::ReadOnly) != 0) {
173 if (inflateEnd(&d->zins) != Z_OK) {
174 setErrorString(d->zins.msg);
177 if ((openMode() & QIODevice::WriteOnly) != 0) {
179 if (deflateEnd(&d->zouts) != Z_OK) {
180 setErrorString(d->zouts.msg);
188 while (read < maxSize) {
189 if (d->inBufPos == d->inBufSize) {
192 if (d->inBufSize == -1) {
194 setErrorString(d->io->errorString());
197 if (d->inBufSize == 0)
break;
199 while (read < maxSize && d->inBufPos < d->inBufSize) {
200 d->zins.next_in = (Bytef *)(d->inBuf + d->inBufPos);
201 d->zins.avail_in = d->inBufSize - d->inBufPos;
202 d->zins.next_out = (Bytef *)(data + read);
204 (uInt)(maxSize - read);
206 switch (inflate(&d->zins, Z_SYNC_FLUSH)) {
208 read = (
char *)d->zins.next_out - data;
209 d->inBufPos = (
char *)d->zins.next_in - d->inBuf;
212 read = (
char *)d->zins.next_out - data;
213 d->inBufPos = (
char *)d->zins.next_in - d->inBuf;
219 "Z_BUF_ERROR detected with %d/%d in/out, weird",
220 d->zins.avail_in, d->zins.avail_out);
223 memmove(d->inBuf, d->inBuf + d->inBufPos,
224 d->inBufSize - d->inBufPos);
225 d->inBufSize -= d->inBufPos;
227 more = d->io->read(d->inBuf + d->inBufSize,
230 setErrorString(d->io->errorString());
233 if (more == 0)
return read;
234 d->inBufSize += more;
237 setErrorString(QString::fromLocal8Bit(d->zins.msg));
242 #ifdef QUAZIP_ZIODEVICE_DEBUG_INPUT
243 indebug.write(data, read);
251 if (d->doFlush(
error) == -1) {
252 setErrorString(
error);
255 while (written < maxSize) {
257 if (d->outBufPos < d->outBufSize)
return written;
258 d->zouts.next_in = (Bytef *)(data + written);
260 (uInt)(maxSize - written);
261 d->zouts.next_out = (Bytef *)d->outBuf;
263 switch (deflate(&d->zouts, Z_NO_FLUSH)) {
265 written = (
char *)d->zouts.next_in - data;
266 d->outBufSize = (
char *)d->zouts.next_out - d->outBuf;
269 setErrorString(QString::fromLocal8Bit(d->zouts.msg));
272 if (d->doFlush(
error) == -1) {
273 setErrorString(
error);
277 #ifdef QUAZIP_ZIODEVICE_DEBUG_OUTPUT
278 debug.write(data, written);
285 if (d->doFlush(
error) < 0) {
286 setErrorString(
error);
290 if (d->outBufPos < d->outBufSize)
return true;
292 d->zouts.next_in = &c;
293 d->zouts.avail_in = 0;
295 d->zouts.next_out = (Bytef *)d->outBuf;
297 switch (deflate(&d->zouts, Z_SYNC_FLUSH)) {
299 d->outBufSize = (
char *)d->zouts.next_out - d->outBuf;
300 if (d->doFlush(
error) < 0) {
301 setErrorString(
error);
304 if (d->outBufPos < d->outBufSize)
return true;
309 setErrorString(QString::fromLocal8Bit(d->zouts.msg));
312 }
while (d->zouts.avail_out == 0);
322 return (openMode() == NotOpen) ||
323 (QIODevice::bytesAvailable() == 0 && d->atEnd);
330 return (d->atEnd ? 0 : 1) + QIODevice::bytesAvailable();
A class to compress/decompress QIODevice.
virtual bool open(QIODevice::OpenMode mode)
Opens the device.
virtual bool flush()
Flushes data waiting to be written.
virtual bool atEnd() const
Returns true iff the end of the compressed stream is reached.
QuaZIODevice(QIODevice *io, QObject *parent=NULL)
Constructor.
virtual qint64 readData(char *data, qint64 maxSize)
Implementation of QIODevice::readData().
virtual qint64 writeData(const char *data, qint64 maxSize)
Implementation of QIODevice::writeData().
~QuaZIODevice()
Destructor.
virtual qint64 bytesAvailable() const
Returns the number of the bytes buffered.
virtual void close()
Closes this device, but not the underlying one.
QIODevice * getIoDevice() const
Returns the underlying device.
virtual bool isSequential() const
Returns true.
static void error(char *msg)
Tensor Append(const Tensor &self, const Tensor &other, const utility::optional< int64_t > &axis)
Appends the two tensors, along the given axis into a new tensor. Both the tensors must have same data...
#define QUAZIO_OUTBUFSIZE