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