Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
160 lines (131 loc) · 3.42 KB
/
Copy pathmain.cpp
File metadata and controls
160 lines (131 loc) · 3.42 KB
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
#include <QCoreApplication>
#include <QResource>
#include <QStringList>
#include <QTextStream>
#include <QTimer>
#include <QFile>
#include <QFileInfo>
#include <QRegExp>
#include <magic.h>
/// extend QResource class to give access
/// to protected functions
class ExtRes : public QResource
{
public:
ExtRes(const QString & file = QString(), const QLocale & locale = QLocale())
: QResource(file, locale)
{
}
virtual ~ExtRes()
{
}
virtual QStringList children() const
{
return QResource::children();
}
virtual bool isDir() const
{
return QResource::isDir();
}
virtual bool isFile() const
{
return QResource::isFile();
}
};
/// cout like stuff Qt taste
QTextStream& qStdOut()
{
static QTextStream ts( stdout );
return ts;
}
/// tak to run in our application
class Task : public QObject
{
Q_OBJECT
public:
Task(QObject *parent = 0) : QObject(parent)
{
mCookie = magic_open(MAGIC_MIME_TYPE);
magic_load(mCookie, NULL);
}
~Task()
{
magic_close(mCookie);
}
protected:
void parseResources(const ExtRes& res)
{
if (res.isValid())
{
if (res.isDir())
{
foreach (const QString& fName, res.children())
{
QString combName = res.fileName().isEmpty() ? QString(":/%1").arg(fName) : QString("%1/%2").arg(res.fileName()).arg(fName);
parseResources(ExtRes(combName));
}
}
else if (res.isFile())
{
qStdOut() << "Trying to extract " << res.absoluteFilePath() << " (" << res.size() << "B)\n";
QFileInfo fi(res.fileName());
QString outName = QString("%1.raw").arg(fi.baseName());
QFile::copy(res.fileName(), outName);
QString tmpName = outName;
if (!createFileName(tmpName))
{
QFile::rename(outName, tmpName);
}
}
}
else
{
qStdOut() << "No valid resource!\n";
}
}
int createFileName (QString& tmpName)
{
QRegExp rx("^(.*)/(.*)$");
QString n = magic_file(mCookie, tmpName.toUtf8().constData());
if (rx.indexIn(n) > -1)
{
QFileInfo fi(tmpName);
tmpName = QString("%1.%2").arg(fi.baseName()).arg(rx.cap((2)));
return 0;
}
return -1;
}
public slots:
void run()
{
QStringList args = QCoreApplication::arguments();
if (args.count() < 2)
{
qStdOut() << "Give the file you want to extract as parameter!\n";
}
else
{
QResource::registerResource(args.at(1));
parseResources(ExtRes());
}
emit finished();
}
signals:
void finished();
private:
magic_t mCookie;
};
#include "main.moc"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// Task parented to the application so that it
// will be deleted by the application.
Task *task = new Task(&a);
// This will cause the application to exit when
// the task signals finished.
QObject::connect(task, SIGNAL(finished()), &a, SLOT(quit()));
// This will run the task from the application event loop.
QTimer::singleShot(0, task, SLOT(run()));
return a.exec();
}