/
gzipreader.h
177 lines (149 loc) · 3.83 KB
/
gzipreader.h
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
#ifndef GZIPREADER_H
#define GZIPREADER_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists for the convenience
// of the QLibrary class. This header file may change from
// version to version without notice, or even be removed.
//
// We mean it.
//
#include <qdebug.h>
#include <QtCore/qfile.h>
#include <QtCore/qstring.h>
#include <QBuffer>
#include <QDomDocument>
#include <QIODevice>
#include <QByteArray>
#include <QFile>
#include <QProcess>
extern inline QString run_which( QString apps = QString("gs") )
{
QStringList potential_paths;
potential_paths.append("/usr/bin");
potential_paths.append("/usr/local/bin");
potential_paths.append("/sw/bin"); /* to use on mac as same */
potential_paths.append("/opt/bin");
QProcess *process = new QProcess(NULL);
process->setReadChannelMode(QProcess::MergedChannels);
QStringList env = process->systemEnvironment();
env.replaceInStrings(QRegExp("^PATH=(.*)", Qt::CaseInsensitive), "PATH=\\1;"+potential_paths.join(";"));
process->setEnvironment(env);
process->start( QString("which") , QStringList() << apps , QIODevice::ReadOnly );
if (!process->waitForFinished()) {
return QString();
} else {
QString finder = process->readAll().trimmed();
if (finder.endsWith(apps,Qt::CaseInsensitive)) {
qDebug() << "### finder " << finder;
return finder;
} else {
return QString();
}
}
}
class RamStream
{
public:
RamStream()
:d(new QBuffer())
{
d->open(QIODevice::ReadWrite);
start();
}
~RamStream()
{
d->close();
}
bool clear()
{
d->write(QByteArray());
return d->bytesAvailable() == 0 ? true : false;
}
qint64 ramwrite( QByteArray x ) {
d->write(x);
start();
return d->bytesAvailable();
}
void start() {
d->seek(0);
}
bool LoadFile( const QString file ) {
if (clear()) {
QFile f(file);
if (f.exists()) {
if (f.open(QFile::ReadOnly)) {
d->write(f.readAll());
f.close();
start();
return true;
}
}
}
return false;
}
bool PutOnFile( const QString file ) {
QFile f(file);
start();
if (f.open(QFile::WriteOnly)) {
uint bi = f.write(d->readAll());
f.close();
start();
return bi > 0 ? true : false;
}
return false;
}
QDomDocument Dom() { return doc; }
QBuffer *device() { return d; }
bool isValid() { return doc.setContent(stream(),false,0,0,0); }
QByteArray stream() { return d->data(); }
QDomDocument doc;
QBuffer *d;
};
class GZipReaderPrivate;
namespace CZIP {
class QZipReader
{
public:
QZipReader(const QString &fileName, QIODevice::OpenMode mode = QIODevice::ReadOnly );
explicit QZipReader(QIODevice *device);
~QZipReader();
bool isReadable() const;
bool exists() const;
struct Q_AUTOTEST_EXPORT FileInfo
{
FileInfo();
FileInfo(const FileInfo &other);
~FileInfo();
FileInfo &operator=(const FileInfo &other);
QString filePath;
uint isDir : 1;
uint isFile : 1;
uint isSymLink : 1;
QFile::Permissions permissions;
uint crc32;
qint64 size;
void *d;
};
QList<FileInfo> fileInfoList() const;
int count() const;
FileInfo entryInfoAt(int index) const;
QByteArray fileData(const QString &fileName) const;
bool extractAll(const QString &destinationDir) const;
enum Status {
NoError,
FileReadError,
FileOpenError,
FilePermissionsError,
FileError
};
Status status() const;
void close();
private:
GZipReaderPrivate *d;
Q_DISABLE_COPY(QZipReader)
};
}
#endif // QZIPREADER_H