ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
quagzipfile.cpp
Go to the documentation of this file.
1 /*
2 Copyright (C) 2005-2014 Sergey A. Tachenov
3 
4 This file is part of QuaZIP.
5 
6 QuaZIP is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation, either version 2.1 of the License, or
9 (at your option) any later version.
10 
11 QuaZIP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15 
16 You should have received a copy of the GNU Lesser General Public License
17 along with QuaZIP. If not, see <http://www.gnu.org/licenses/>.
18 
19 See COPYING file for the full LGPL text.
20 
21 Original ZIP package is copyrighted by Gilles Vollant and contributors,
22 see quazip/(un)zip.h files for details. Basically it's the zlib license.
23 */
24 
25 #include "quagzipfile.h"
26 
27 #include <QFile>
28 
30 class QuaGzipFilePrivate {
31  friend class QuaGzipFile;
32  QString fileName;
33  gzFile gzd;
34  inline QuaGzipFilePrivate() : gzd(NULL) {}
35  inline QuaGzipFilePrivate(const QString &fileName)
36  : fileName(fileName), gzd(NULL) {}
37  template <typename FileId>
38  bool open(FileId id, QIODevice::OpenMode mode, QString &error);
39  gzFile open(int fd, const char *modeString);
40  gzFile open(const QString &name, const char *modeString);
41 };
42 
43 gzFile QuaGzipFilePrivate::open(const QString &name, const char *modeString) {
44  return gzopen(QFile::encodeName(name).constData(), modeString);
45 }
46 
47 gzFile QuaGzipFilePrivate::open(int fd, const char *modeString) {
48  return gzdopen(fd, modeString);
49 }
50 
51 template <typename FileId>
52 bool QuaGzipFilePrivate::open(FileId id,
53  QIODevice::OpenMode mode,
54  QString &error) {
55  char modeString[2];
56  modeString[0] = modeString[1] = '\0';
57  if ((mode & QIODevice::Append) != 0) {
58  error = QString::fromUtf8(
59  "QIODevice::Append is not "
60  "supported for GZIP");
61  return false;
62  }
63  if ((mode & QIODevice::ReadOnly) != 0 &&
64  (mode & QIODevice::WriteOnly) != 0) {
65  error = QString::fromUtf8(
66  "Opening gzip for both reading"
67  " and writing is not supported");
68  return false;
69  } else if ((mode & QIODevice::ReadOnly) != 0) {
70  modeString[0] = 'r';
71  } else if ((mode & QIODevice::WriteOnly) != 0) {
72  modeString[0] = 'w';
73  } else {
74  error = QString::fromUtf8(
75  "You can open a gzip either for reading"
76  " or for writing. Which is it?");
77  return false;
78  }
79  gzd = open(id, modeString);
80  if (gzd == NULL) {
81  error = QString::fromUtf8("Could not gzopen() file");
82  return false;
83  }
84  return true;
85 }
87 
88 QuaGzipFile::QuaGzipFile() : d(new QuaGzipFilePrivate()) {}
89 
90 QuaGzipFile::QuaGzipFile(QObject *parent)
91  : QIODevice(parent), d(new QuaGzipFilePrivate()) {}
92 
93 QuaGzipFile::QuaGzipFile(const QString &fileName, QObject *parent)
94  : QIODevice(parent), d(new QuaGzipFilePrivate(fileName)) {}
95 
97  if (isOpen()) {
98  close();
99  }
100  delete d;
101 }
102 
103 void QuaGzipFile::setFileName(const QString &fileName) {
104  d->fileName = fileName;
105 }
106 
107 QString QuaGzipFile::getFileName() const { return d->fileName; }
108 
109 bool QuaGzipFile::isSequential() const { return true; }
110 
111 bool QuaGzipFile::open(QIODevice::OpenMode mode) {
112  QString error;
113  if (!d->open(d->fileName, mode, error)) {
114  setErrorString(error);
115  return false;
116  }
117  return QIODevice::open(mode);
118 }
119 
120 bool QuaGzipFile::open(int fd, QIODevice::OpenMode mode) {
121  QString error;
122  if (!d->open(fd, mode, error)) {
123  setErrorString(error);
124  return false;
125  }
126  return QIODevice::open(mode);
127 }
128 
129 bool QuaGzipFile::flush() { return gzflush(d->gzd, Z_SYNC_FLUSH) == Z_OK; }
130 
132  QIODevice::close();
133  gzclose(d->gzd);
134 }
135 
136 qint64 QuaGzipFile::readData(char *data, qint64 maxSize) {
137  return gzread(d->gzd, (voidp)data, (unsigned)maxSize);
138 }
139 
140 qint64 QuaGzipFile::writeData(const char *data, qint64 maxSize) {
141  if (maxSize == 0) return 0;
142  int written = gzwrite(d->gzd, (voidp)data, (unsigned)maxSize);
143  if (written == 0)
144  return -1;
145  else
146  return written;
147 }
std::string name
#define NULL
GZIP file.
Definition: quagzipfile.h:44
QString getFileName() const
Returns the name of the GZIP file.
virtual ~QuaGzipFile()
Destructor.
Definition: quagzipfile.cpp:96
virtual bool open(QIODevice::OpenMode mode)
Opens the file.
void setFileName(const QString &fileName)
Sets the name of the GZIP file to be opened.
virtual void close()
Closes the file.
virtual bool isSequential() const
Returns true.
virtual qint64 writeData(const char *data, qint64 maxSize)
Implementation of QIODevice::writeData().
QuaGzipFile()
Empty constructor.
Definition: quagzipfile.cpp:88
virtual qint64 readData(char *data, qint64 maxSize)
Implementation of QIODevice::readData().
virtual bool flush()
Flushes data to file.
static void error(char *msg)
Definition: lsd.c:159
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 isOpen(pFd)
Definition: sqlite3.c:52420