-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiscutils.cpp
executable file
·224 lines (194 loc) · 5.18 KB
/
miscutils.cpp
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
#include "miscutils.h"
#include <QDir>
#include <QFile>
#include <QDebug>
void deltree(const QString &name)
{
QFileInfo info(name);
if (info.isDir()) {
QDir dir(name);
QFileInfoList list = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files);
foreach (QFileInfo file, list) {
deltree(file.absoluteFilePath());
}
QString n = dir.dirName();
dir.cdUp();
dir.rmdir(n);
} else {
QFile::remove(name);
}
}
/* FileTypes */
FileTypes::FileType FileTypes::getFileType(const QString &fileName)
{
QByteArray header;
FileType result = Unknown;
/* Check if it is a folder */
if (QFileInfo(fileName).isDir()) {
return Dir;
}
/* Read the file header */
{
QFile file(fileName);
if (file.open(QFile::ReadOnly)) {
header = file.read(4);
}
while (header.count() < 4) {
header.append('\x0');
}
}
/* Check if the file is gzipped */
bool gz = false;
if ((quint8)header.at(0) == 0x1f && (quint8)header.at(1) == 0x8b ) {
/* The file is gzipped, read the real header */
gz = true;
GzFile file(fileName);
if (file.open(QFile::ReadOnly)) {
header = file.read(4);
} else {
header = QByteArray(4, 0);
}
while (header.count() < 4) {
header.append('\x0');
}
}
quint8 b0 = header.at(0);
quint8 b1 = header.at(1);
quint8 b2 = header.at(2);
quint8 b3 = header.at(3);
/* Determine the file type */
if (b0 == 'A' && b1 == 'T' && b2 == '8' && b3 == 'X') {
result = Atx;
} else if (b0 == 'F' && b1 == 'U' && b2 == 'J' && b3 == 'I') {
result = Cas;
} else if (b0 == 0x96 && b1 == 0x02) {
result = Atr;
} else if (b0 == 0xF9 || b0 == 0xFA) {
result = Dcm;
} else if (b0 == 0xFF && b1 == 0xFF) {
result = Xex;
} else if (b0 == 0xFD && b1 == 0xFD) {
result = Scp;
} else if (b0 == 'D' && b1 == 'I') {
result = Di;
} else if (b2 == 'P' && (b3 == '2' || b3 == '3')) {
result = Pro;
} else if (fileName.endsWith(".XFD", Qt::CaseInsensitive) || fileName.endsWith(".XFZ", Qt::CaseInsensitive) || fileName.endsWith(".XFD.GZ", Qt::CaseInsensitive)) {
result = Xfd;
}
if (result != Unknown && gz) {
result = (FileType) (result + 1);
}
return result;
}
QString FileTypes::getFileTypeName(FileType type)
{
switch (type) {
case Atr:
return tr("ATR disk image");
case AtrGz:
return tr("gzipped ATR disk image");
case Xfd:
return tr("XFD disk image");
case XfdGz:
return tr("gziped XFD disk image");
case Dcm:
return tr("DCM disk image");
case DcmGz:
return tr("gzipped DCM disk image");
case Scp:
return tr("SCP disk image");
case ScpGz:
return tr("gzipped SCP disk image");
case Di:
return tr("DI disk image");
case DiGz:
return tr("gzipped DI disk image");
case Pro:
return tr("PRO disk image");
case ProGz:
return tr("gzipped PRO disk image");
case Atx:
return tr("VAPI (ATX) disk image");
case AtxGz:
return tr("gzipped VAPI (ATX) disk image");
case Cas:
return tr("CAS cassette image");
case CasGz:
return tr("gzipped CAS cassette image");
case Xex:
return tr("Atari executable");
case XexGz:
return tr("gzipped Atari executable");
default:
return tr("unknown file type");
}
}
/* GzFile */
GzFile::GzFile(const QString &path)
:QFile(path)
{
mPath = path;
mHandle = 0;
}
GzFile::~GzFile()
{
close();
}
bool GzFile::open(OpenMode mode)
{
if (QFile::open(mode)) {
if((mode & ReadOnly) == ReadOnly) {
mHandle = gzdopen(handle(), "rb");
} else if((mode & ReadWrite) == ReadWrite) {
mHandle = gzdopen(handle(), "wb+");
} else if((mode & WriteOnly) == WriteOnly) {
mHandle = gzdopen(handle(), "wb");
}
if(mHandle == NULL) {
setErrorString(tr("gzdopen() failed."));
return false;
}
} else {
return false;
}
return true;
}
void GzFile::close()
{
if (mHandle) {
gzclose(mHandle);
}
QFile::close();
mHandle = NULL;
}
bool GzFile::isSequential() const
{
return true;
}
bool GzFile::seek(qint64 pos)
{
bool result = gzseek(mHandle, pos, SEEK_SET) != -1;
if (!result) {
setErrorString(tr("gzseek() failed."));
}
return result;
}
qint64 GzFile::readData(char *data, qint64 maxSize)
{
if (mHandle == NULL) {
return 0;
}
return gzread(mHandle, data, maxSize);
}
qint64 GzFile::writeData(const char *data, qint64 maxSize)
{
if (mHandle == NULL) {
return 0;
}
return gzwrite(mHandle, data, maxSize);
}
bool GzFile::atEnd() const
{
return gzeof(mHandle);
}