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