Back to index

scribus-ng  1.3.4.dfsg+svn20071115
scgzfile.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 
00008 #include <zlib.h>
00009 #include <qfileinfo.h>
00010 
00011 #include "scconfig.h"
00012 #include "scgzfile.h"
00013 
00014 const int ScGzFile::gzipExpansionFactor=8;
00015 
00016 ScGzFile::ScGzFile(const QString& filename)
00017 {
00018        fname = filename;
00019 }
00020 
00021 ScGzFile::ScGzFile(const QString& filename, const QByteArray& array)
00022 {
00023        fname = filename;
00024        barray = array;
00025 }
00026 
00027 bool ScGzFile::read(uint maxBytes)
00028 {
00029        int i;
00030        uint maxB = 0xFFFFFFFF;
00031        if (maxBytes != 0)
00032               maxB = maxBytes;
00033        barray.resize(0);
00034        gzFile gzDoc = NULL;
00035        QCString fn(fname.local8Bit());
00036        gzDoc = gzopen(fn.data(),"rb");
00037        if(gzDoc == NULL)
00038        { 
00039               return false; // FIXME: Needs better error return
00040        }
00041        // Allocate a buffer of a multiple of the compressed size of the file
00042        // as a starting point for loading. We'll expand this buffer by powers
00043        // of two if we run out of space.
00044        const QFileInfo fi(fname);
00045        uint bufSize = QMIN(4096, fi.size()*gzipExpansionFactor);
00046        barray.resize(bufSize);
00047        char* buf = barray.data();
00048        uint bytesRead = 0;
00049        // While there's free space, read into the buffer....
00050        while ((i = gzread(gzDoc,buf,bufSize-bytesRead-1)) > 0)
00051        {
00052               // Ensure the string is null-terminated and move the
00053               // write pointer to the current position.
00054               buf[i]=0;
00055               buf+=i;
00056               bytesRead += i;
00057               // And check that there's free space to work with, expanding the
00058               // buffer if there's not.
00059               if (bufSize - bytesRead < 4096)
00060               {
00061                      bufSize *= 2;
00062                      barray.resize(bufSize);
00063                      buf = barray.data() + bytesRead;
00064               }
00065               if (maxB >= 0 && bytesRead >= maxB)
00066                      break;
00067        }
00068        gzclose(gzDoc);
00069        return (barray.size() > 0);
00070 }
00071 
00072 bool ScGzFile::write(void)
00073 {
00074        gzFile gzDoc = NULL;
00075        QCString fn(fname.local8Bit());
00076        gzDoc = gzopen(fn.data(),"wb");
00077        if(gzDoc == NULL)
00078               return false;
00079        gzputs(gzDoc, barray.data());
00080        gzclose(gzDoc);
00081        return true;
00082 }
00083 
00084 bool ScGzFile::write(const char* header)
00085 {
00086        gzFile gzDoc = NULL;
00087        QCString fn(fname.local8Bit());
00088        gzDoc = gzopen(fn.data(),"wb");
00089        if(gzDoc == NULL)
00090               return false;
00091        gzputs(gzDoc, header);
00092        gzputs(gzDoc, barray.data());
00093        gzclose(gzDoc);
00094        return true;
00095 }