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;
|
7768
|
170 |
PHYSFS_Stat stat;
|
|
171 |
if (PHYSFS_stat(_filename.toUtf8().constData(), &stat) != 0) {
|
|
172 |
_size = stat.filesize;
|
|
173 |
_datetime = QDateTime::fromTime_t(stat.modtime);
|
|
174 |
_flags |= QAbstractFileEngine::ExistsFlag;
|
|
175 |
|
|
176 |
switch (stat.filetype)
|
|
177 |
{
|
|
178 |
case PHYSFS_FILETYPE_REGULAR:
|
|
179 |
_flags |= QAbstractFileEngine::FileType;
|
|
180 |
break;
|
|
181 |
|
|
182 |
case PHYSFS_FILETYPE_DIRECTORY:
|
|
183 |
_flags |= QAbstractFileEngine::DirectoryType;
|
|
184 |
break;
|
|
185 |
case PHYSFS_FILETYPE_SYMLINK:
|
|
186 |
_flags |= QAbstractFileEngine::LinkType;
|
|
187 |
break;
|
|
188 |
default: ;
|
|
189 |
};
|
|
190 |
}
|
|
191 |
}
|
|
192 |
|
|
193 |
bool FileEngine::atEnd() const
|
|
194 |
{
|
|
195 |
return PHYSFS_eof(_handler) != 0;
|
|
196 |
}
|
|
197 |
|
|
198 |
qint64 FileEngine::read(char *data, qint64 maxlen)
|
|
199 |
{
|
|
200 |
return PHYSFS_readBytes(_handler, data, maxlen);
|
|
201 |
}
|
|
202 |
|
|
203 |
qint64 FileEngine::write(const char *data, qint64 len)
|
|
204 |
{
|
|
205 |
return PHYSFS_writeBytes(_handler, data, len);
|
|
206 |
}
|
|
207 |
|
|
208 |
bool FileEngine::isOpened() const
|
|
209 |
{
|
|
210 |
return _handler != NULL;
|
|
211 |
}
|
|
212 |
|
|
213 |
QFile::FileError FileEngine::error() const
|
|
214 |
{
|
|
215 |
return QFile::UnspecifiedError;
|
|
216 |
}
|
|
217 |
|
|
218 |
QString FileEngine::errorString() const
|
|
219 |
{
|
|
220 |
return PHYSFS_getLastError();
|
|
221 |
}
|
|
222 |
|
|
223 |
bool FileEngine::supportsExtension(Extension extension) const
|
|
224 |
{
|
|
225 |
return extension == QAbstractFileEngine::AtEndExtension;
|
|
226 |
}
|
|
227 |
|
7770
|
228 |
|
|
229 |
|
|
230 |
FileEngineHandler::FileEngineHandler(char *argv0)
|
|
231 |
{
|
|
232 |
PHYSFS_init(argv0);
|
|
233 |
}
|
|
234 |
|
|
235 |
FileEngineHandler::~FileEngineHandler()
|
|
236 |
{
|
|
237 |
PHYSFS_deinit();
|
|
238 |
}
|
|
239 |
|
7768
|
240 |
QAbstractFileEngine* FileEngineHandler::create(const QString &filename) const
|
|
241 |
{
|
7770
|
242 |
if (filename.startsWith(scheme))
|
|
243 |
return new FileEngine(filename.mid(scheme.size()));
|
|
244 |
else
|
|
245 |
return NULL;
|
|
246 |
}
|
|
247 |
|
|
248 |
void FileEngineHandler::mount(const QString &path)
|
|
249 |
{
|
|
250 |
PHYSFS_mount(path.toUtf8().constData(), NULL, 1);
|
|
251 |
}
|
|
252 |
|
|
253 |
void FileEngineHandler::setWriteDir(const QString &path)
|
|
254 |
{
|
|
255 |
PHYSFS_setWriteDir(path.toUtf8().constData());
|
7768
|
256 |
}
|
7770
|
257 |
|
|
258 |
|
|
259 |
|
|
260 |
FileEngineIterator::FileEngineIterator(QDir::Filters filters, const QStringList &nameFilters, const QStringList &entries)
|
|
261 |
: QAbstractFileEngineIterator(filters, nameFilters)
|
|
262 |
{
|
|
263 |
m_entries = entries;
|
|
264 |
|
|
265 |
/* heck.. docs are unclear on this
|
|
266 |
* QDirIterator puts iterator before first entry
|
|
267 |
* but QAbstractFileEngineIterator example puts iterator on first entry
|
|
268 |
* though QDirIterator approach seems to be the right one
|
|
269 |
*/
|
|
270 |
|
|
271 |
m_index = -1;
|
|
272 |
}
|
|
273 |
|
|
274 |
bool FileEngineIterator::hasNext() const
|
|
275 |
{
|
|
276 |
return m_index < m_entries.size() - 1;
|
|
277 |
}
|
|
278 |
|
|
279 |
QString FileEngineIterator::next()
|
|
280 |
{
|
|
281 |
if (!hasNext())
|
|
282 |
return QString();
|
|
283 |
|
|
284 |
++m_index;
|
|
285 |
return currentFilePath();
|
|
286 |
}
|
|
287 |
|
|
288 |
QString FileEngineIterator::currentFileName() const
|
|
289 |
{
|
|
290 |
return m_entries.at(m_index);
|
|
291 |
}
|