author | unc0rr |
Thu, 22 Nov 2012 23:19:01 +0400 | |
branch | flibqtfrontend |
changeset 8094 | 6c5b4e69f03d |
parent 8085 | 6c059add1560 |
child 8098 | 4efee370e2de |
permissions | -rw-r--r-- |
7768 | 1 |
/* borrowed from https://github.com/skhaz/qt-physfs-wrapper |
2 |
* TODO: add copyright header, determine license |
|
3 |
*/ |
|
4 |
||
8052 | 5 |
#include "hwpacksmounter.h" |
7768 | 6 |
#include "FileEngine.h" |
7 |
||
7770 | 8 |
|
9 |
const QString FileEngineHandler::scheme = "physfs:/"; |
|
10 |
||
7768 | 11 |
FileEngine::FileEngine(const QString& filename) |
12 |
: _handler(NULL) |
|
13 |
, _flags(0) |
|
14 |
{ |
|
15 |
setFileName(filename); |
|
16 |
} |
|
17 |
||
18 |
FileEngine::~FileEngine() |
|
19 |
{ |
|
20 |
close(); |
|
21 |
} |
|
22 |
||
23 |
bool FileEngine::open(QIODevice::OpenMode openMode) |
|
24 |
{ |
|
25 |
close(); |
|
26 |
||
27 |
if (openMode & QIODevice::WriteOnly) { |
|
28 |
_handler = PHYSFS_openWrite(_filename.toUtf8().constData()); |
|
29 |
_flags = QAbstractFileEngine::WriteOwnerPerm | QAbstractFileEngine::WriteUserPerm | QAbstractFileEngine::FileType; |
|
30 |
} |
|
31 |
||
32 |
else if (openMode & QIODevice::ReadOnly) { |
|
33 |
_handler = PHYSFS_openRead(_filename.toUtf8().constData()); |
|
34 |
} |
|
35 |
||
36 |
else if (openMode & QIODevice::Append) { |
|
37 |
_handler = PHYSFS_openAppend(_filename.toUtf8().constData()); |
|
38 |
} |
|
39 |
||
40 |
else { |
|
41 |
qWarning("Bad file open mode: %d", (int)openMode); |
|
42 |
} |
|
43 |
||
44 |
if (!_handler) { |
|
45 |
qWarning("Failed to open %s, reason: %s", _filename.toUtf8().constData(), PHYSFS_getLastError()); |
|
46 |
return false; |
|
47 |
} |
|
48 |
||
49 |
return true; |
|
50 |
} |
|
51 |
||
52 |
bool FileEngine::close() |
|
53 |
{ |
|
54 |
if (isOpened()) { |
|
55 |
int result = PHYSFS_close(_handler); |
|
56 |
_handler = NULL; |
|
57 |
return result != 0; |
|
58 |
} |
|
59 |
||
60 |
return true; |
|
61 |
} |
|
62 |
||
63 |
bool FileEngine::flush() |
|
64 |
{ |
|
65 |
return PHYSFS_flush(_handler) != 0; |
|
66 |
} |
|
67 |
||
68 |
qint64 FileEngine::size() const |
|
69 |
{ |
|
70 |
return _size; |
|
71 |
} |
|
72 |
||
73 |
qint64 FileEngine::pos() const |
|
74 |
{ |
|
75 |
return PHYSFS_tell(_handler); |
|
76 |
} |
|
77 |
||
78 |
bool FileEngine::seek(qint64 pos) |
|
79 |
{ |
|
80 |
return PHYSFS_seek(_handler, pos) != 0; |
|
81 |
} |
|
82 |
||
83 |
bool FileEngine::isSequential() const |
|
84 |
{ |
|
7770 | 85 |
return false; |
7768 | 86 |
} |
87 |
||
88 |
bool FileEngine::remove() |
|
89 |
{ |
|
90 |
return PHYSFS_delete(_filename.toUtf8().constData()) != 0; |
|
91 |
} |
|
92 |
||
93 |
bool FileEngine::mkdir(const QString &dirName, bool createParentDirectories) const |
|
94 |
{ |
|
95 |
Q_UNUSED(createParentDirectories); |
|
96 |
return PHYSFS_mkdir(dirName.toUtf8().constData()) != 0; |
|
97 |
} |
|
98 |
||
99 |
bool FileEngine::rmdir(const QString &dirName, bool recurseParentDirectories) const |
|
100 |
{ |
|
101 |
Q_UNUSED(recurseParentDirectories); |
|
102 |
return PHYSFS_delete(dirName.toUtf8().constData()) != 0; |
|
103 |
} |
|
104 |
||
105 |
bool FileEngine::caseSensitive() const |
|
106 |
{ |
|
107 |
return true; |
|
108 |
} |
|
109 |
||
110 |
bool FileEngine::isRelativePath() const |
|
111 |
{ |
|
112 |
return true; |
|
113 |
} |
|
114 |
||
7770 | 115 |
QAbstractFileEngineIterator * FileEngine::beginEntryList(QDir::Filters filters, const QStringList &filterNames) |
116 |
{ |
|
117 |
return new FileEngineIterator(filters, filterNames, entryList(filters, filterNames)); |
|
118 |
} |
|
119 |
||
7768 | 120 |
QStringList FileEngine::entryList(QDir::Filters filters, const QStringList &filterNames) const |
121 |
{ |
|
122 |
Q_UNUSED(filters); |
|
123 |
||
124 |
QString file; |
|
125 |
QStringList result; |
|
7770 | 126 |
char **files = PHYSFS_enumerateFiles(_filename.toUtf8().constData()); |
7768 | 127 |
|
128 |
for (char **i = files; *i != NULL; i++) { |
|
7770 | 129 |
file = QString::fromUtf8(*i); |
130 |
||
131 |
if (filterNames.isEmpty() || QDir::match(filterNames, file)) { |
|
7768 | 132 |
result << file; |
133 |
} |
|
134 |
} |
|
135 |
||
136 |
PHYSFS_freeList(files); |
|
7770 | 137 |
|
7768 | 138 |
return result; |
139 |
} |
|
140 |
||
141 |
QAbstractFileEngine::FileFlags FileEngine::fileFlags(FileFlags type) const |
|
142 |
{ |
|
143 |
return type & _flags; |
|
144 |
} |
|
145 |
||
146 |
QString FileEngine::fileName(FileName file) const |
|
147 |
{ |
|
8085
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
148 |
switch(file) |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
149 |
{ |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
150 |
case QAbstractFileEngine::AbsolutePathName: |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
151 |
{ |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
152 |
QString s(PHYSFS_getWriteDir()); |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
153 |
return s; |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
154 |
} |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
155 |
case QAbstractFileEngine::BaseName: |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
156 |
{ |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
157 |
int l = _filename.lastIndexOf('/'); |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
158 |
QString s = _filename.mid(l + 1); |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
159 |
return s; |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
160 |
} |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
161 |
case QAbstractFileEngine::DefaultName: |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
162 |
case QAbstractFileEngine::AbsoluteName: |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
163 |
default: |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
164 |
{ |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
165 |
QString s = "physfs:/" + _filename; |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
166 |
return s; |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
167 |
} |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
168 |
} |
7768 | 169 |
} |
170 |
||
171 |
QDateTime FileEngine::fileTime(FileTime time) const |
|
172 |
{ |
|
7955 | 173 |
|
7768 | 174 |
switch (time) |
175 |
{ |
|
176 |
case QAbstractFileEngine::ModificationTime: |
|
177 |
default: |
|
178 |
return _datetime; |
|
179 |
break; |
|
180 |
}; |
|
181 |
} |
|
182 |
||
183 |
void FileEngine::setFileName(const QString &file) |
|
184 |
{ |
|
7770 | 185 |
if(file.startsWith(FileEngineHandler::scheme)) |
186 |
_filename = file.mid(FileEngineHandler::scheme.size()); |
|
187 |
else |
|
188 |
_filename = file; |
|
7931 | 189 |
|
7768 | 190 |
PHYSFS_Stat stat; |
191 |
if (PHYSFS_stat(_filename.toUtf8().constData(), &stat) != 0) { |
|
192 |
_size = stat.filesize; |
|
193 |
_datetime = QDateTime::fromTime_t(stat.modtime); |
|
7955 | 194 |
// _flags |= QAbstractFileEngine::WriteUserPerm; |
7931 | 195 |
_flags |= QAbstractFileEngine::ReadUserPerm; |
7768 | 196 |
_flags |= QAbstractFileEngine::ExistsFlag; |
197 |
||
198 |
switch (stat.filetype) |
|
199 |
{ |
|
200 |
case PHYSFS_FILETYPE_REGULAR: |
|
201 |
_flags |= QAbstractFileEngine::FileType; |
|
202 |
break; |
|
203 |
||
204 |
case PHYSFS_FILETYPE_DIRECTORY: |
|
205 |
_flags |= QAbstractFileEngine::DirectoryType; |
|
206 |
break; |
|
207 |
case PHYSFS_FILETYPE_SYMLINK: |
|
208 |
_flags |= QAbstractFileEngine::LinkType; |
|
209 |
break; |
|
210 |
default: ; |
|
7955 | 211 |
} |
7768 | 212 |
} |
213 |
} |
|
214 |
||
215 |
bool FileEngine::atEnd() const |
|
216 |
{ |
|
217 |
return PHYSFS_eof(_handler) != 0; |
|
218 |
} |
|
219 |
||
220 |
qint64 FileEngine::read(char *data, qint64 maxlen) |
|
221 |
{ |
|
222 |
return PHYSFS_readBytes(_handler, data, maxlen); |
|
223 |
} |
|
224 |
||
225 |
qint64 FileEngine::write(const char *data, qint64 len) |
|
226 |
{ |
|
227 |
return PHYSFS_writeBytes(_handler, data, len); |
|
228 |
} |
|
229 |
||
230 |
bool FileEngine::isOpened() const |
|
231 |
{ |
|
232 |
return _handler != NULL; |
|
233 |
} |
|
234 |
||
235 |
QFile::FileError FileEngine::error() const |
|
236 |
{ |
|
237 |
return QFile::UnspecifiedError; |
|
238 |
} |
|
239 |
||
240 |
QString FileEngine::errorString() const |
|
241 |
{ |
|
242 |
return PHYSFS_getLastError(); |
|
243 |
} |
|
244 |
||
245 |
bool FileEngine::supportsExtension(Extension extension) const |
|
246 |
{ |
|
247 |
return extension == QAbstractFileEngine::AtEndExtension; |
|
248 |
} |
|
249 |
||
7770 | 250 |
|
251 |
||
252 |
FileEngineHandler::FileEngineHandler(char *argv0) |
|
253 |
{ |
|
254 |
PHYSFS_init(argv0); |
|
255 |
} |
|
256 |
||
257 |
FileEngineHandler::~FileEngineHandler() |
|
258 |
{ |
|
259 |
PHYSFS_deinit(); |
|
260 |
} |
|
261 |
||
7768 | 262 |
QAbstractFileEngine* FileEngineHandler::create(const QString &filename) const |
263 |
{ |
|
7770 | 264 |
if (filename.startsWith(scheme)) |
265 |
return new FileEngine(filename.mid(scheme.size())); |
|
266 |
else |
|
267 |
return NULL; |
|
268 |
} |
|
269 |
||
270 |
void FileEngineHandler::mount(const QString &path) |
|
271 |
{ |
|
272 |
PHYSFS_mount(path.toUtf8().constData(), NULL, 1); |
|
273 |
} |
|
274 |
||
7955 | 275 |
void FileEngineHandler::mount(const QString & path, const QString & mountPoint) |
276 |
{ |
|
277 |
PHYSFS_mount(path.toUtf8().constData(), mountPoint.toUtf8().constData(), 1); |
|
278 |
} |
|
279 |
||
7770 | 280 |
void FileEngineHandler::setWriteDir(const QString &path) |
281 |
{ |
|
282 |
PHYSFS_setWriteDir(path.toUtf8().constData()); |
|
7768 | 283 |
} |
7770 | 284 |
|
8052 | 285 |
void FileEngineHandler::mountPacks() |
286 |
{ |
|
287 |
hedgewarsMountPackages(); |
|
288 |
} |
|
7770 | 289 |
|
290 |
||
291 |
FileEngineIterator::FileEngineIterator(QDir::Filters filters, const QStringList &nameFilters, const QStringList &entries) |
|
292 |
: QAbstractFileEngineIterator(filters, nameFilters) |
|
293 |
{ |
|
294 |
m_entries = entries; |
|
295 |
||
296 |
/* heck.. docs are unclear on this |
|
297 |
* QDirIterator puts iterator before first entry |
|
298 |
* but QAbstractFileEngineIterator example puts iterator on first entry |
|
299 |
* though QDirIterator approach seems to be the right one |
|
300 |
*/ |
|
301 |
||
302 |
m_index = -1; |
|
303 |
} |
|
304 |
||
305 |
bool FileEngineIterator::hasNext() const |
|
306 |
{ |
|
307 |
return m_index < m_entries.size() - 1; |
|
308 |
} |
|
309 |
||
310 |
QString FileEngineIterator::next() |
|
311 |
{ |
|
312 |
if (!hasNext()) |
|
313 |
return QString(); |
|
314 |
||
315 |
++m_index; |
|
316 |
return currentFilePath(); |
|
317 |
} |
|
318 |
||
319 |
QString FileEngineIterator::currentFileName() const |
|
320 |
{ |
|
321 |
return m_entries.at(m_index); |
|
322 |
} |