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