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