7768
|
1 |
/* borrowed from https://github.com/skhaz/qt-physfs-wrapper
|
|
2 |
* TODO: add copyright header, determine license
|
|
3 |
*/
|
|
4 |
|
7955
|
5 |
|
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 |
{
|
|
148 |
if (file == QAbstractFileEngine::AbsolutePathName)
|
|
149 |
return PHYSFS_getWriteDir();
|
|
150 |
|
7955
|
151 |
return QString("physfs://%1").arg(_filename);
|
7768
|
152 |
}
|
|
153 |
|
|
154 |
QDateTime FileEngine::fileTime(FileTime time) const
|
|
155 |
{
|
7955
|
156 |
|
7768
|
157 |
switch (time)
|
|
158 |
{
|
|
159 |
case QAbstractFileEngine::ModificationTime:
|
|
160 |
default:
|
|
161 |
return _datetime;
|
|
162 |
break;
|
|
163 |
};
|
|
164 |
}
|
|
165 |
|
|
166 |
void FileEngine::setFileName(const QString &file)
|
|
167 |
{
|
7770
|
168 |
if(file.startsWith(FileEngineHandler::scheme))
|
|
169 |
_filename = file.mid(FileEngineHandler::scheme.size());
|
|
170 |
else
|
|
171 |
_filename = file;
|
7931
|
172 |
|
7768
|
173 |
PHYSFS_Stat stat;
|
|
174 |
if (PHYSFS_stat(_filename.toUtf8().constData(), &stat) != 0) {
|
|
175 |
_size = stat.filesize;
|
|
176 |
_datetime = QDateTime::fromTime_t(stat.modtime);
|
7955
|
177 |
// _flags |= QAbstractFileEngine::WriteUserPerm;
|
7931
|
178 |
_flags |= QAbstractFileEngine::ReadUserPerm;
|
7768
|
179 |
_flags |= QAbstractFileEngine::ExistsFlag;
|
|
180 |
|
|
181 |
switch (stat.filetype)
|
|
182 |
{
|
|
183 |
case PHYSFS_FILETYPE_REGULAR:
|
|
184 |
_flags |= QAbstractFileEngine::FileType;
|
|
185 |
break;
|
|
186 |
|
|
187 |
case PHYSFS_FILETYPE_DIRECTORY:
|
|
188 |
_flags |= QAbstractFileEngine::DirectoryType;
|
|
189 |
break;
|
|
190 |
case PHYSFS_FILETYPE_SYMLINK:
|
|
191 |
_flags |= QAbstractFileEngine::LinkType;
|
|
192 |
break;
|
|
193 |
default: ;
|
7955
|
194 |
}
|
7768
|
195 |
}
|
|
196 |
}
|
|
197 |
|
|
198 |
bool FileEngine::atEnd() const
|
|
199 |
{
|
|
200 |
return PHYSFS_eof(_handler) != 0;
|
|
201 |
}
|
|
202 |
|
|
203 |
qint64 FileEngine::read(char *data, qint64 maxlen)
|
|
204 |
{
|
|
205 |
return PHYSFS_readBytes(_handler, data, maxlen);
|
|
206 |
}
|
|
207 |
|
|
208 |
qint64 FileEngine::write(const char *data, qint64 len)
|
|
209 |
{
|
|
210 |
return PHYSFS_writeBytes(_handler, data, len);
|
|
211 |
}
|
|
212 |
|
|
213 |
bool FileEngine::isOpened() const
|
|
214 |
{
|
|
215 |
return _handler != NULL;
|
|
216 |
}
|
|
217 |
|
|
218 |
QFile::FileError FileEngine::error() const
|
|
219 |
{
|
|
220 |
return QFile::UnspecifiedError;
|
|
221 |
}
|
|
222 |
|
|
223 |
QString FileEngine::errorString() const
|
|
224 |
{
|
|
225 |
return PHYSFS_getLastError();
|
|
226 |
}
|
|
227 |
|
|
228 |
bool FileEngine::supportsExtension(Extension extension) const
|
|
229 |
{
|
|
230 |
return extension == QAbstractFileEngine::AtEndExtension;
|
|
231 |
}
|
|
232 |
|
7770
|
233 |
|
|
234 |
|
|
235 |
FileEngineHandler::FileEngineHandler(char *argv0)
|
|
236 |
{
|
|
237 |
PHYSFS_init(argv0);
|
|
238 |
}
|
|
239 |
|
|
240 |
FileEngineHandler::~FileEngineHandler()
|
|
241 |
{
|
|
242 |
PHYSFS_deinit();
|
|
243 |
}
|
|
244 |
|
7768
|
245 |
QAbstractFileEngine* FileEngineHandler::create(const QString &filename) const
|
|
246 |
{
|
7770
|
247 |
if (filename.startsWith(scheme))
|
|
248 |
return new FileEngine(filename.mid(scheme.size()));
|
|
249 |
else
|
|
250 |
return NULL;
|
|
251 |
}
|
|
252 |
|
|
253 |
void FileEngineHandler::mount(const QString &path)
|
|
254 |
{
|
|
255 |
PHYSFS_mount(path.toUtf8().constData(), NULL, 1);
|
|
256 |
}
|
|
257 |
|
7955
|
258 |
void FileEngineHandler::mount(const QString & path, const QString & mountPoint)
|
|
259 |
{
|
|
260 |
PHYSFS_mount(path.toUtf8().constData(), mountPoint.toUtf8().constData(), 1);
|
|
261 |
}
|
|
262 |
|
7770
|
263 |
void FileEngineHandler::setWriteDir(const QString &path)
|
|
264 |
{
|
|
265 |
PHYSFS_setWriteDir(path.toUtf8().constData());
|
7768
|
266 |
}
|
7770
|
267 |
|
|
268 |
|
|
269 |
|
|
270 |
FileEngineIterator::FileEngineIterator(QDir::Filters filters, const QStringList &nameFilters, const QStringList &entries)
|
|
271 |
: QAbstractFileEngineIterator(filters, nameFilters)
|
|
272 |
{
|
|
273 |
m_entries = entries;
|
|
274 |
|
|
275 |
/* heck.. docs are unclear on this
|
|
276 |
* QDirIterator puts iterator before first entry
|
|
277 |
* but QAbstractFileEngineIterator example puts iterator on first entry
|
|
278 |
* though QDirIterator approach seems to be the right one
|
|
279 |
*/
|
|
280 |
|
|
281 |
m_index = -1;
|
|
282 |
}
|
|
283 |
|
|
284 |
bool FileEngineIterator::hasNext() const
|
|
285 |
{
|
|
286 |
return m_index < m_entries.size() - 1;
|
|
287 |
}
|
|
288 |
|
|
289 |
QString FileEngineIterator::next()
|
|
290 |
{
|
|
291 |
if (!hasNext())
|
|
292 |
return QString();
|
|
293 |
|
|
294 |
++m_index;
|
|
295 |
return currentFilePath();
|
|
296 |
}
|
|
297 |
|
|
298 |
QString FileEngineIterator::currentFileName() const
|
|
299 |
{
|
|
300 |
return m_entries.at(m_index);
|
|
301 |
}
|