1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
/*
For general Scribus (>=1.3.2) copyright and licensing information please refer
to the COPYING file provided with the program. Following this notice may exist
a copyright and/or license notice that predates the release of Scribus 1.3.2
for which a new license (GPL+exception) is in place.
*/
#include <cstdio>
#include <cstdlib>
#include <zlib.h>
#include <QDir>
#include <QFileInfo>
#include "scconfig.h"
#include "scgzfile.h"
const int ScGzFile::gzipExpansionFactor = 8;
struct ScGzFileDataPrivate
{
FILE* file;
gzFile gzfile;
};
ScGzFile::ScGzFile(const QString& fileName) : QIODevice(), m_fileName(fileName)
{
m_fileName = fileName;
m_data = NULL;
}
ScGzFile::~ScGzFile(void)
{
freeData();
}
int ScGzFile::error(void) const
{
int errCode = 0;
if (m_data)
gzerror(m_data->gzfile, &errCode);
return errCode;
}
bool ScGzFile::errorOccurred(void) const
{
bool errorOccurred = false;
if (m_data)
{
int errCode = error();
errorOccurred = (errCode != Z_OK && errCode != Z_STREAM_END);
}
return errorOccurred;
}
ScGzFileDataPrivate* ScGzFile::newPrivateData(void)
{
freeData();
m_data = (ScGzFileDataPrivate*) malloc(sizeof(ScGzFileDataPrivate));
if (m_data)
{
m_data->file = NULL;
m_data->gzfile = NULL;
}
return m_data;
}
void ScGzFile::freeData(void)
{
if (m_data)
{
free(m_data);
m_data = NULL;
}
}
bool ScGzFile::gzFileOpen(QString fileName, ScGzFileDataPrivate* data, QIODevice::OpenMode mode)
{
bool success = false;
FILE* file = NULL;
gzFile gzf = NULL;
QString localPath = QDir::toNativeSeparators(fileName);
#if defined(_WIN32)
if (mode == QIODevice::ReadOnly)
file = _wfopen((const wchar_t*) localPath.utf16(), L"rb");
else if (mode == QIODevice::WriteOnly)
file = _wfopen((const wchar_t*) localPath.utf16(), L"wb");
if (file)
{
int fno = _fileno(file);
gzf = gzdopen(fno,(mode == QIODevice::ReadOnly) ? "rb" : "wb");
if (!gzf) fclose(file);
}
#else
if (mode == QIODevice::ReadOnly)
file = fopen(localPath.toLocal8Bit().data(), "rb");
else if (mode == QIODevice::WriteOnly)
file = fopen(localPath.toLocal8Bit().data(), "wb");
if (file)
{
int fno = fileno(file);
gzf = gzdopen(fno,(mode == QIODevice::ReadOnly) ? "rb" : "wb");
if (!gzf) fclose(file);
}
#endif
if (gzf)
{
data->file = file;
data->gzfile = gzf;
success = true;
}
return success;
}
bool ScGzFile::open(QIODevice::OpenMode mode)
{
bool success = true;
if (isOpen()) return false;
freeData();
if (mode == QIODevice::ReadOnly || mode == QIODevice::WriteOnly)
m_data = newPrivateData();
if (!m_data) return false;
bool opened = gzFileOpen(m_fileName, m_data, mode);
if (opened)
{
opened &= QIODevice::open(mode);
if (!opened)
this->close();
success = opened;
}
return success;
}
bool ScGzFile::reset(void)
{
if (!m_data || (openMode() != QIODevice::ReadOnly))
return false;
int res = gzrewind(m_data->gzfile);
QIODevice::reset();
return (res == 0);
}
bool ScGzFile::atEnd() const
{
if (m_data)
{
if (openMode() == QIODevice::ReadOnly)
{
int result = gzeof(m_data->gzfile);
return result;
}
return QIODevice::atEnd();
}
return QIODevice::atEnd();
}
qint64 ScGzFile::readData (char * data, qint64 maxSize)
{
if (!m_data || (openMode() != QIODevice::ReadOnly))
return 0;
int result = gzread(m_data->gzfile, data, maxSize);
return result;
}
qint64 ScGzFile::writeData (const char * data, qint64 dataLen)
{
if (!m_data || (openMode() != QIODevice::WriteOnly))
return false;
int result = gzwrite(m_data->gzfile, data, dataLen);
return result;
}
void ScGzFile::close()
{
if (!isOpen() || !m_data)
return;
gzclose(m_data->gzfile);
QIODevice::close();
freeData();
}
bool ScGzFile::readFromFile(const QString& filename, QByteArray& bArray, uint maxBytes)
{
int i;
uint maxB = 0xFFFFFFFF;
if (maxBytes != 0)
maxB = maxBytes;
bArray.resize(0);
ScGzFile gzFile(filename);
if(!gzFile.open(QIODevice::ReadOnly))
{
return false; // FIXME: Needs better error return
}
// Allocate a buffer of a multiple of the compressed size of the file
// as a starting point for loading. We'll expand this buffer by powers
// of two if we run out of space.
const QFileInfo fi(filename);
uint bufSize = qMin<uint>(4096, fi.size()*gzipExpansionFactor);
bArray.resize(bufSize);
char* buf = bArray.data();
uint bytesRead = 0;
// While there's free space, read into the buffer....
while ((i = gzFile.read(buf,bufSize-bytesRead-1)) > 0)
{
// Ensure the string is null-terminated and move the
// write pointer to the current position.
buf[i]=0;
buf+=i;
bytesRead += i;
// And check that there's free space to work with, expanding the
// buffer if there's not.
if (bufSize - bytesRead < 4096)
{
bufSize *= 2;
bArray.resize(bufSize);
buf = bArray.data() + bytesRead;
}
if (maxB >= 0 && bytesRead >= maxB)
break;
}
gzFile.close();
return (bArray.size() > 0);
}
bool ScGzFile::writeToFile(const QString& filename, const QByteArray& bArray)
{
ScGzFile gzFile(filename);
if (gzFile.open(QIODevice::WriteOnly))
{
int res = gzFile.write(bArray.data(), bArray.size());
gzFile.close();
return (res > 0 && (res == bArray.size()));
}
return false;
}
bool ScGzFile::writeToFile(const QString& filename, const QByteArray& bArray, const char* header)
{
ScGzFile gzFile(filename);
if(gzFile.open(QIODevice::WriteOnly))
{
int res1 = gzFile.write(header, strlen(header));
int res2 = gzFile.write(bArray.data(), bArray.size());
gzFile.close();
bool done1 = (res1 > 0 && (res1 == static_cast<int>(strlen(header))));
bool done2 = (res2 > 0 && (res2 == bArray.size()));
return (done1 && done2);
}
return false;
}
|