"Fossies" - the Fresh Open Source Software Archive

Member "tea-49.0.0/quagzipfile.cpp" (4 Nov 2019, 4175 Bytes) of package /linux/privat/tea-49.0.0.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "quagzipfile.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 47.1.0_vs_48.0.0.

    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 <QFile>
   26 
   27 
   28 #include "quagzipfile.h"
   29 
   30 /// \cond internal
   31 class QuaGzipFilePrivate {
   32     friend class QuaGzipFile;
   33     QString fileName;
   34     gzFile gzd;
   35     inline QuaGzipFilePrivate(): gzd(NULL) {}
   36     inline QuaGzipFilePrivate(const QString &fileName):
   37         fileName(fileName), gzd(NULL) {}
   38     template<typename FileId> bool open(FileId id,
   39         QIODevice::OpenMode mode, QString &error);
   40     gzFile open(int fd, const char *modeString);
   41     gzFile open(const QString &name, const char *modeString);
   42 };
   43 
   44 gzFile QuaGzipFilePrivate::open(const QString &name, const char *modeString)
   45 {
   46     return gzopen(QFile::encodeName(name).constData(), modeString);
   47 }
   48 
   49 gzFile QuaGzipFilePrivate::open(int fd, const char *modeString)
   50 {
   51     return gzdopen(fd, modeString);
   52 }
   53 
   54 template<typename FileId>
   55 bool QuaGzipFilePrivate::open(FileId id, QIODevice::OpenMode mode,
   56                               QString &error)
   57 {
   58     char modeString[2];
   59     modeString[0] = modeString[1] = '\0';
   60     if ((mode & QIODevice::Append) != 0) {
   61         error = "QIODevice::Append is not supported for GZIP";
   62         return false;
   63     }
   64     if ((mode & QIODevice::ReadOnly) != 0
   65             && (mode & QIODevice::WriteOnly) != 0) {
   66         error = "Opening gzip for both reading and writing is not supported";
   67         return false;
   68     } else if ((mode & QIODevice::ReadOnly) != 0) {
   69         modeString[0] = 'r';
   70     } else if ((mode & QIODevice::WriteOnly) != 0) {
   71         modeString[0] = 'w';
   72     } else {
   73         error = "You can open a gzip either for reading or for writing. Which is it?";
   74         return false;
   75     }
   76     gzd = open(id, modeString);
   77     if (gzd == NULL) {
   78         error = "Could not gzopen() file";
   79         return false;
   80     }
   81     return true;
   82 }
   83 /// \endcond
   84 
   85 QuaGzipFile::QuaGzipFile():
   86 d(new QuaGzipFilePrivate())
   87 {
   88 }
   89 
   90 QuaGzipFile::QuaGzipFile(QObject *parent):
   91 QIODevice(parent),
   92 d(new QuaGzipFilePrivate())
   93 {
   94 }
   95 
   96 QuaGzipFile::QuaGzipFile(const QString &fileName, QObject *parent):
   97   QIODevice(parent),
   98 d(new QuaGzipFilePrivate(fileName))
   99 {
  100 }
  101 
  102 QuaGzipFile::~QuaGzipFile()
  103 {
  104   if (isOpen()) {
  105     close();
  106   }
  107   delete d;
  108 }
  109 
  110 void QuaGzipFile::setFileName(const QString& fileName)
  111 {
  112     d->fileName = fileName;
  113 }
  114 
  115 QString QuaGzipFile::getFileName() const
  116 {
  117     return d->fileName;
  118 }
  119 
  120 bool QuaGzipFile::isSequential() const
  121 {
  122   return true;
  123 }
  124 
  125 bool QuaGzipFile::open(QIODevice::OpenMode mode)
  126 {
  127     QString error;
  128     if (!d->open(d->fileName, mode, error)) {
  129         setErrorString(error);
  130         return false;
  131     }
  132     return QIODevice::open(mode);
  133 }
  134 
  135 bool QuaGzipFile::open(int fd, QIODevice::OpenMode mode)
  136 {
  137     QString error;
  138     if (!d->open(fd, mode, error)) {
  139         setErrorString(error);
  140         return false;
  141     }
  142     return QIODevice::open(mode);
  143 }
  144 
  145 bool QuaGzipFile::flush()
  146 {
  147     return gzflush(d->gzd, Z_SYNC_FLUSH) == Z_OK;
  148 }
  149 
  150 void QuaGzipFile::close()
  151 {
  152   QIODevice::close();
  153   gzclose(d->gzd);
  154 }
  155 
  156 qint64 QuaGzipFile::readData(char *data, qint64 maxSize)
  157 {
  158     return gzread(d->gzd, (voidp)data, (unsigned)maxSize);
  159 }
  160 
  161 qint64 QuaGzipFile::writeData(const char *data, qint64 maxSize)
  162 {
  163     if (maxSize == 0)
  164         return 0;
  165     int written = gzwrite(d->gzd, (voidp)data, (unsigned)maxSize);
  166     if (written == 0)
  167         return -1;
  168     else
  169         return written;
  170 }