author | unc0rr |
Tue, 04 Dec 2012 09:15:55 +0400 | |
branch | flibqtfrontend |
changeset 8208 | 171da01bd04d |
parent 8206 | 1633a6510834 |
child 8330 | aaefa587e277 |
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) |
8115 | 12 |
: m_handle(NULL) |
8206 | 13 |
, m_size(0) |
8115 | 14 |
, m_flags(0) |
8112
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
15 |
, m_bufferSet(false) |
8178 | 16 |
, m_readWrite(false) |
7768 | 17 |
{ |
18 |
setFileName(filename); |
|
19 |
} |
|
20 |
||
21 |
FileEngine::~FileEngine() |
|
22 |
{ |
|
23 |
close(); |
|
24 |
} |
|
25 |
||
26 |
bool FileEngine::open(QIODevice::OpenMode openMode) |
|
27 |
{ |
|
28 |
close(); |
|
29 |
||
8178 | 30 |
if ((openMode & QIODevice::ReadWrite) == QIODevice::ReadWrite) { |
31 |
m_handle = PHYSFS_openAppend(m_fileName.toUtf8().constData()); |
|
8206 | 32 |
if(m_handle) |
33 |
{ |
|
34 |
m_readWrite = true; |
|
35 |
seek(0); |
|
36 |
} |
|
8178 | 37 |
} |
38 |
||
39 |
else if (openMode & QIODevice::WriteOnly) { |
|
8115 | 40 |
m_handle = PHYSFS_openWrite(m_fileName.toUtf8().constData()); |
41 |
m_flags = QAbstractFileEngine::WriteOwnerPerm | QAbstractFileEngine::WriteUserPerm | QAbstractFileEngine::FileType; |
|
7768 | 42 |
} |
43 |
||
44 |
else if (openMode & QIODevice::ReadOnly) { |
|
8115 | 45 |
m_handle = PHYSFS_openRead(m_fileName.toUtf8().constData()); |
7768 | 46 |
} |
47 |
||
48 |
else if (openMode & QIODevice::Append) { |
|
8115 | 49 |
m_handle = PHYSFS_openAppend(m_fileName.toUtf8().constData()); |
7768 | 50 |
} |
51 |
||
52 |
else { |
|
8098 | 53 |
qWarning("[PHYSFS] Bad file open mode: %d", (int)openMode); |
7768 | 54 |
} |
55 |
||
8115 | 56 |
if (!m_handle) { |
57 |
qWarning("[PHYSFS] Failed to open %s, reason: %s", m_fileName.toUtf8().constData(), PHYSFS_getLastError()); |
|
7768 | 58 |
return false; |
59 |
} |
|
60 |
||
61 |
return true; |
|
62 |
} |
|
63 |
||
64 |
bool FileEngine::close() |
|
65 |
{ |
|
66 |
if (isOpened()) { |
|
8115 | 67 |
int result = PHYSFS_close(m_handle); |
68 |
m_handle = NULL; |
|
7768 | 69 |
return result != 0; |
70 |
} |
|
71 |
||
72 |
return true; |
|
73 |
} |
|
74 |
||
75 |
bool FileEngine::flush() |
|
76 |
{ |
|
8115 | 77 |
return PHYSFS_flush(m_handle) != 0; |
7768 | 78 |
} |
79 |
||
80 |
qint64 FileEngine::size() const |
|
81 |
{ |
|
8115 | 82 |
return m_size; |
7768 | 83 |
} |
84 |
||
85 |
qint64 FileEngine::pos() const |
|
86 |
{ |
|
8115 | 87 |
return PHYSFS_tell(m_handle); |
7768 | 88 |
} |
89 |
||
8178 | 90 |
bool FileEngine::setSize(qint64 size) |
91 |
{ |
|
92 |
if(size == 0) |
|
93 |
{ |
|
94 |
m_size = 0; |
|
95 |
return open(QIODevice::WriteOnly); |
|
96 |
} |
|
97 |
else |
|
98 |
return false; |
|
99 |
} |
|
100 |
||
7768 | 101 |
bool FileEngine::seek(qint64 pos) |
102 |
{ |
|
8178 | 103 |
bool ok = PHYSFS_seek(m_handle, pos) != 0; |
104 |
||
105 |
return ok; |
|
7768 | 106 |
} |
107 |
||
108 |
bool FileEngine::isSequential() const |
|
109 |
{ |
|
7770 | 110 |
return false; |
7768 | 111 |
} |
112 |
||
113 |
bool FileEngine::remove() |
|
114 |
{ |
|
8115 | 115 |
return PHYSFS_delete(m_fileName.toUtf8().constData()) != 0; |
7768 | 116 |
} |
117 |
||
118 |
bool FileEngine::mkdir(const QString &dirName, bool createParentDirectories) const |
|
119 |
{ |
|
120 |
Q_UNUSED(createParentDirectories); |
|
8206 | 121 |
|
7768 | 122 |
return PHYSFS_mkdir(dirName.toUtf8().constData()) != 0; |
123 |
} |
|
124 |
||
125 |
bool FileEngine::rmdir(const QString &dirName, bool recurseParentDirectories) const |
|
126 |
{ |
|
127 |
Q_UNUSED(recurseParentDirectories); |
|
8206 | 128 |
|
7768 | 129 |
return PHYSFS_delete(dirName.toUtf8().constData()) != 0; |
130 |
} |
|
131 |
||
132 |
bool FileEngine::caseSensitive() const |
|
133 |
{ |
|
134 |
return true; |
|
135 |
} |
|
136 |
||
137 |
bool FileEngine::isRelativePath() const |
|
138 |
{ |
|
8178 | 139 |
return false; |
7768 | 140 |
} |
141 |
||
7770 | 142 |
QAbstractFileEngineIterator * FileEngine::beginEntryList(QDir::Filters filters, const QStringList &filterNames) |
143 |
{ |
|
144 |
return new FileEngineIterator(filters, filterNames, entryList(filters, filterNames)); |
|
145 |
} |
|
146 |
||
7768 | 147 |
QStringList FileEngine::entryList(QDir::Filters filters, const QStringList &filterNames) const |
148 |
{ |
|
149 |
Q_UNUSED(filters); |
|
150 |
||
151 |
QString file; |
|
152 |
QStringList result; |
|
8115 | 153 |
char **files = PHYSFS_enumerateFiles(m_fileName.toUtf8().constData()); |
7768 | 154 |
|
155 |
for (char **i = files; *i != NULL; i++) { |
|
7770 | 156 |
file = QString::fromUtf8(*i); |
157 |
||
158 |
if (filterNames.isEmpty() || QDir::match(filterNames, file)) { |
|
7768 | 159 |
result << file; |
160 |
} |
|
161 |
} |
|
162 |
||
163 |
PHYSFS_freeList(files); |
|
7770 | 164 |
|
7768 | 165 |
return result; |
166 |
} |
|
167 |
||
168 |
QAbstractFileEngine::FileFlags FileEngine::fileFlags(FileFlags type) const |
|
169 |
{ |
|
8115 | 170 |
return type & m_flags; |
7768 | 171 |
} |
172 |
||
173 |
QString FileEngine::fileName(FileName file) const |
|
174 |
{ |
|
8085
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
175 |
switch(file) |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
176 |
{ |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
177 |
case QAbstractFileEngine::AbsolutePathName: |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
178 |
{ |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
179 |
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
|
180 |
return s; |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
181 |
} |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
182 |
case QAbstractFileEngine::BaseName: |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
183 |
{ |
8115 | 184 |
int l = m_fileName.lastIndexOf('/'); |
185 |
QString s = m_fileName.mid(l + 1); |
|
8085
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
186 |
return s; |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
187 |
} |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
188 |
case QAbstractFileEngine::DefaultName: |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
189 |
case QAbstractFileEngine::AbsoluteName: |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
190 |
default: |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
191 |
{ |
8115 | 192 |
QString s = "physfs:/" + m_fileName; |
8085
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
193 |
return s; |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
194 |
} |
6c059add1560
Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents:
8052
diff
changeset
|
195 |
} |
7768 | 196 |
} |
197 |
||
198 |
QDateTime FileEngine::fileTime(FileTime time) const |
|
199 |
{ |
|
200 |
switch (time) |
|
201 |
{ |
|
202 |
case QAbstractFileEngine::ModificationTime: |
|
203 |
default: |
|
8115 | 204 |
return m_date; |
7768 | 205 |
break; |
206 |
}; |
|
207 |
} |
|
208 |
||
209 |
void FileEngine::setFileName(const QString &file) |
|
210 |
{ |
|
7770 | 211 |
if(file.startsWith(FileEngineHandler::scheme)) |
8115 | 212 |
m_fileName = file.mid(FileEngineHandler::scheme.size()); |
7770 | 213 |
else |
8115 | 214 |
m_fileName = file; |
7768 | 215 |
PHYSFS_Stat stat; |
8178 | 216 |
if (PHYSFS_stat(m_fileName.toUtf8().constData(), &stat) != 0) { |
8115 | 217 |
m_size = stat.filesize; |
218 |
m_date = QDateTime::fromTime_t(stat.modtime); |
|
8206 | 219 |
// m_flags |= QAbstractFileEngine::WriteOwnerPerm; |
220 |
m_flags |= QAbstractFileEngine::ReadOwnerPerm; |
|
8115 | 221 |
m_flags |= QAbstractFileEngine::ReadUserPerm; |
222 |
m_flags |= QAbstractFileEngine::ExistsFlag; |
|
8206 | 223 |
m_flags |= QAbstractFileEngine::LocalDiskFlag; |
7768 | 224 |
|
225 |
switch (stat.filetype) |
|
226 |
{ |
|
227 |
case PHYSFS_FILETYPE_REGULAR: |
|
8115 | 228 |
m_flags |= QAbstractFileEngine::FileType; |
7768 | 229 |
break; |
230 |
case PHYSFS_FILETYPE_DIRECTORY: |
|
8115 | 231 |
m_flags |= QAbstractFileEngine::DirectoryType; |
7768 | 232 |
break; |
233 |
case PHYSFS_FILETYPE_SYMLINK: |
|
8115 | 234 |
m_flags |= QAbstractFileEngine::LinkType; |
7768 | 235 |
break; |
236 |
default: ; |
|
7955 | 237 |
} |
7768 | 238 |
} |
239 |
} |
|
240 |
||
241 |
bool FileEngine::atEnd() const |
|
242 |
{ |
|
8115 | 243 |
return PHYSFS_eof(m_handle) != 0; |
7768 | 244 |
} |
245 |
||
246 |
qint64 FileEngine::read(char *data, qint64 maxlen) |
|
247 |
{ |
|
8178 | 248 |
if(m_readWrite) |
249 |
{ |
|
250 |
if(pos() == 0) |
|
251 |
open(QIODevice::ReadOnly); |
|
252 |
else |
|
253 |
return -1; |
|
254 |
} |
|
255 |
||
256 |
qint64 len = PHYSFS_readBytes(m_handle, data, maxlen); |
|
257 |
return len; |
|
7768 | 258 |
} |
259 |
||
8112
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
260 |
qint64 FileEngine::readLine(char *data, qint64 maxlen) |
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
261 |
{ |
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
262 |
if(!m_bufferSet) |
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
263 |
{ |
8115 | 264 |
PHYSFS_setBuffer(m_handle, 4096); |
8112
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
265 |
m_bufferSet = true; |
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
266 |
} |
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
267 |
|
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
268 |
qint64 bytesRead = 0; |
8115 | 269 |
while(PHYSFS_readBytes(m_handle, data, 1) |
8112
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
270 |
&& maxlen |
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
271 |
&& (*data == '\n')) |
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
272 |
{ |
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
273 |
++data; |
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
274 |
--maxlen; |
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
275 |
++bytesRead; |
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
276 |
} |
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
277 |
|
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
278 |
return bytesRead; |
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
279 |
} |
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
280 |
|
7768 | 281 |
qint64 FileEngine::write(const char *data, qint64 len) |
282 |
{ |
|
8115 | 283 |
return PHYSFS_writeBytes(m_handle, data, len); |
7768 | 284 |
} |
285 |
||
286 |
bool FileEngine::isOpened() const |
|
287 |
{ |
|
8115 | 288 |
return m_handle != NULL; |
7768 | 289 |
} |
290 |
||
291 |
QFile::FileError FileEngine::error() const |
|
292 |
{ |
|
293 |
return QFile::UnspecifiedError; |
|
294 |
} |
|
295 |
||
296 |
QString FileEngine::errorString() const |
|
297 |
{ |
|
298 |
return PHYSFS_getLastError(); |
|
299 |
} |
|
300 |
||
301 |
bool FileEngine::supportsExtension(Extension extension) const |
|
302 |
{ |
|
8112
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
303 |
return |
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
304 |
(extension == QAbstractFileEngine::AtEndExtension) |
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
305 |
|| (extension == QAbstractFileEngine::FastReadLineExtension) |
d85dc8a8f41c
Implement QAbstractFileEngine::FastReadLineExtension
unc0rr
parents:
8098
diff
changeset
|
306 |
; |
7768 | 307 |
} |
308 |
||
7770 | 309 |
|
310 |
FileEngineHandler::FileEngineHandler(char *argv0) |
|
311 |
{ |
|
312 |
PHYSFS_init(argv0); |
|
313 |
} |
|
314 |
||
315 |
FileEngineHandler::~FileEngineHandler() |
|
316 |
{ |
|
317 |
PHYSFS_deinit(); |
|
318 |
} |
|
319 |
||
7768 | 320 |
QAbstractFileEngine* FileEngineHandler::create(const QString &filename) const |
321 |
{ |
|
7770 | 322 |
if (filename.startsWith(scheme)) |
8178 | 323 |
return new FileEngine(filename); |
7770 | 324 |
else |
325 |
return NULL; |
|
326 |
} |
|
327 |
||
328 |
void FileEngineHandler::mount(const QString &path) |
|
329 |
{ |
|
330 |
PHYSFS_mount(path.toUtf8().constData(), NULL, 1); |
|
331 |
} |
|
332 |
||
7955 | 333 |
void FileEngineHandler::mount(const QString & path, const QString & mountPoint) |
334 |
{ |
|
335 |
PHYSFS_mount(path.toUtf8().constData(), mountPoint.toUtf8().constData(), 1); |
|
336 |
} |
|
337 |
||
7770 | 338 |
void FileEngineHandler::setWriteDir(const QString &path) |
339 |
{ |
|
340 |
PHYSFS_setWriteDir(path.toUtf8().constData()); |
|
7768 | 341 |
} |
7770 | 342 |
|
8052 | 343 |
void FileEngineHandler::mountPacks() |
344 |
{ |
|
345 |
hedgewarsMountPackages(); |
|
346 |
} |
|
7770 | 347 |
|
348 |
||
349 |
FileEngineIterator::FileEngineIterator(QDir::Filters filters, const QStringList &nameFilters, const QStringList &entries) |
|
350 |
: QAbstractFileEngineIterator(filters, nameFilters) |
|
351 |
{ |
|
352 |
m_entries = entries; |
|
353 |
||
354 |
/* heck.. docs are unclear on this |
|
355 |
* QDirIterator puts iterator before first entry |
|
356 |
* but QAbstractFileEngineIterator example puts iterator on first entry |
|
357 |
* though QDirIterator approach seems to be the right one |
|
358 |
*/ |
|
359 |
||
360 |
m_index = -1; |
|
361 |
} |
|
362 |
||
363 |
bool FileEngineIterator::hasNext() const |
|
364 |
{ |
|
365 |
return m_index < m_entries.size() - 1; |
|
366 |
} |
|
367 |
||
368 |
QString FileEngineIterator::next() |
|
369 |
{ |
|
370 |
if (!hasNext()) |
|
371 |
return QString(); |
|
372 |
||
373 |
++m_index; |
|
374 |
return currentFilePath(); |
|
375 |
} |
|
376 |
||
377 |
QString FileEngineIterator::currentFileName() const |
|
378 |
{ |
|
379 |
return m_entries.at(m_index); |
|
380 |
} |