Many bugfixes and features to FileEngine physfslayer
authorunc0rr
Sun, 21 Oct 2012 00:31:48 +0400
branchphysfslayer
changeset 7770 ff3442338882
parent 7768 13e2037ebc79
child 7772 a33077239712
Many bugfixes and features to FileEngine
QTfrontend/util/FileEngine.cpp
QTfrontend/util/FileEngine.h
--- a/QTfrontend/util/FileEngine.cpp	Sat Oct 20 00:20:39 2012 +0400
+++ b/QTfrontend/util/FileEngine.cpp	Sun Oct 21 00:31:48 2012 +0400
@@ -4,6 +4,9 @@
 
 #include "FileEngine.h"
 
+
+const QString FileEngineHandler::scheme = "physfs:/";
+
 FileEngine::FileEngine(const QString& filename)
 : _handler(NULL)
 , _flags(0)
@@ -78,7 +81,7 @@
 
 bool FileEngine::isSequential() const
 {
-    return true;
+    return false;
 }
 
 bool FileEngine::remove()
@@ -108,22 +111,29 @@
     return true;
 }
 
+QAbstractFileEngineIterator * FileEngine::beginEntryList(QDir::Filters filters, const QStringList &filterNames)
+{
+    return new FileEngineIterator(filters, filterNames, entryList(filters, filterNames));
+}
+
 QStringList FileEngine::entryList(QDir::Filters filters, const QStringList &filterNames) const
 {
     Q_UNUSED(filters);
 
     QString file;
     QStringList result;
-    char **files = PHYSFS_enumerateFiles("");
+    char **files = PHYSFS_enumerateFiles(_filename.toUtf8().constData());
 
     for (char **i = files; *i != NULL; i++) {
-        file = QString::fromAscii(*i);
-        if (QDir::match(filterNames, file)) {
+        file = QString::fromUtf8(*i);
+
+        if (filterNames.isEmpty() || QDir::match(filterNames, file)) {
             result << file;
         }
     }
 
     PHYSFS_freeList(files);
+
     return result;
 }
 
@@ -153,7 +163,10 @@
 
 void FileEngine::setFileName(const QString &file)
 {
-    _filename = file;
+    if(file.startsWith(FileEngineHandler::scheme))
+        _filename = file.mid(FileEngineHandler::scheme.size());
+    else
+        _filename = file;
     PHYSFS_Stat stat;
     if (PHYSFS_stat(_filename.toUtf8().constData(), &stat) != 0) {
         _size = stat.filesize;
@@ -187,14 +200,6 @@
     return PHYSFS_readBytes(_handler, data, maxlen);
 }
 
-qint64 FileEngine::readLine(char *data, qint64 maxlen)
-{
-    Q_UNUSED(data);
-    Q_UNUSED(maxlen);
-    // TODO
-    return 0;
-}
-
 qint64 FileEngine::write(const char *data, qint64 len)
 {
     return PHYSFS_writeBytes(_handler, data, len);
@@ -220,7 +225,67 @@
     return extension == QAbstractFileEngine::AtEndExtension;
 }
 
+
+
+FileEngineHandler::FileEngineHandler(char *argv0)
+{
+    PHYSFS_init(argv0);
+}
+
+FileEngineHandler::~FileEngineHandler()
+{
+    PHYSFS_deinit();
+}
+
 QAbstractFileEngine* FileEngineHandler::create(const QString &filename) const
 {
-    return new FileEngine(filename);
+    if (filename.startsWith(scheme))
+        return new FileEngine(filename.mid(scheme.size()));
+    else
+        return NULL;
+}
+
+void FileEngineHandler::mount(const QString &path)
+{
+    PHYSFS_mount(path.toUtf8().constData(), NULL, 1);
+}
+
+void FileEngineHandler::setWriteDir(const QString &path)
+{
+    PHYSFS_setWriteDir(path.toUtf8().constData());
 }
+
+
+
+FileEngineIterator::FileEngineIterator(QDir::Filters filters, const QStringList &nameFilters, const QStringList &entries)
+    : QAbstractFileEngineIterator(filters, nameFilters)
+{
+    m_entries = entries;
+
+    /* heck.. docs are unclear on this
+     * QDirIterator puts iterator before first entry
+     * but QAbstractFileEngineIterator example puts iterator on first entry
+     * though QDirIterator approach seems to be the right one
+     */
+
+    m_index = -1;
+}
+
+bool FileEngineIterator::hasNext() const
+{
+    return m_index < m_entries.size() - 1;
+}
+
+QString FileEngineIterator::next()
+{
+   if (!hasNext())
+       return QString();
+
+   ++m_index;
+   return currentFilePath();
+}
+
+QString FileEngineIterator::currentFileName() const
+{
+    return m_entries.at(m_index);
+}
--- a/QTfrontend/util/FileEngine.h	Sat Oct 20 00:20:39 2012 +0400
+++ b/QTfrontend/util/FileEngine.h	Sun Oct 21 00:31:48 2012 +0400
@@ -3,6 +3,7 @@
 
 #include <QAbstractFileEngine>
 #include <QAbstractFileEngineHandler>
+#include <QAbstractFileEngineIterator>
 #include <QDateTime>
 
 #include "physfs.h"
@@ -28,6 +29,7 @@
         virtual bool rmdir(const QString &dirName, bool recurseParentDirectories) const;
         virtual bool caseSensitive() const;
         virtual bool isRelativePath() const;
+        QAbstractFileEngineIterator *beginEntryList(QDir::Filters filters, const QStringList & filterNames);
         virtual QStringList entryList(QDir::Filters filters, const QStringList &filterNames) const;
         virtual FileFlags fileFlags(FileFlags type=FileInfoAll) const;
         virtual QString fileName(FileName file=DefaultName) const;
@@ -36,7 +38,6 @@
         bool atEnd() const;
 
         virtual qint64 read(char *data, qint64 maxlen);
-        virtual qint64 readLine(char *data, qint64 maxlen);
         virtual qint64 write(const char *data, qint64 len);
 
         bool isOpened() const;
@@ -57,7 +58,29 @@
 class FileEngineHandler : public QAbstractFileEngineHandler
 {
     public:
+        FileEngineHandler(char * argv0);
+        ~FileEngineHandler();
+
         QAbstractFileEngine *create(const QString &filename) const;
+
+        void mount(const QString & path);
+        void setWriteDir(const QString & path);
+
+//    private:
+        static const QString scheme;
+};
+
+class FileEngineIterator : public QAbstractFileEngineIterator
+{
+public:
+        FileEngineIterator(QDir::Filters filters, const QStringList & nameFilters, const QStringList & entries);
+
+        bool hasNext() const;
+        QString next();
+        QString currentFileName() const;
+private:
+        QStringList m_entries;
+        int m_index;
 };
 
 #endif