QTfrontend/util/FileEngine.cpp
author koda
Tue, 25 Dec 2012 04:45:22 +0100
branchwebgl
changeset 8330 aaefa587e277
parent 8206 1633a6510834
child 8714 ab201a62d115
permissions -rw-r--r--
update branch with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8330
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
     1
/* borrowed from https://github.com/skhaz/qt-physfs-wrapper
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
     2
 * TODO: add copyright header, determine license
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
     3
 */
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
     4
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
     5
#include "hwpacksmounter.h"
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
     6
#include "FileEngine.h"
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
     7
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
     8
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
     9
const QString FileEngineHandler::scheme = "physfs:/";
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    10
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    11
FileEngine::FileEngine(const QString& filename)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    12
    : m_handle(NULL)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    13
    , m_size(0)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    14
    , m_flags(0)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    15
    , m_bufferSet(false)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    16
    , m_readWrite(false)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    17
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    18
    setFileName(filename);
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    19
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    20
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    21
FileEngine::~FileEngine()
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    22
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    23
    close();
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    24
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    25
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    26
bool FileEngine::open(QIODevice::OpenMode openMode)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    27
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    28
    close();
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    29
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    30
    if ((openMode & QIODevice::ReadWrite) == QIODevice::ReadWrite) {
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    31
        m_handle = PHYSFS_openAppend(m_fileName.toUtf8().constData());
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    32
        if(m_handle)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    33
        {
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    34
            m_readWrite = true;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    35
            seek(0);
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    36
        }
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    37
    }
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    38
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    39
    else if (openMode & QIODevice::WriteOnly) {
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    40
        m_handle = PHYSFS_openWrite(m_fileName.toUtf8().constData());
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    41
        m_flags = QAbstractFileEngine::WriteOwnerPerm | QAbstractFileEngine::WriteUserPerm | QAbstractFileEngine::FileType;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    42
    }
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    43
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    44
    else if (openMode & QIODevice::ReadOnly) {
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    45
        m_handle = PHYSFS_openRead(m_fileName.toUtf8().constData());
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    46
    }
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    47
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    48
    else if (openMode & QIODevice::Append) {
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    49
        m_handle = PHYSFS_openAppend(m_fileName.toUtf8().constData());
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    50
    }
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    51
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    52
    else {
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    53
        qWarning("[PHYSFS] Bad file open mode: %d", (int)openMode);
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    54
    }
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    55
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    56
    if (!m_handle) {
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    57
        qWarning("[PHYSFS] Failed to open %s, reason: %s", m_fileName.toUtf8().constData(), PHYSFS_getLastError());
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    58
        return false;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    59
    }
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    60
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    61
    return true;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    62
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    63
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    64
bool FileEngine::close()
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    65
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    66
    if (isOpened()) {
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    67
        int result = PHYSFS_close(m_handle);
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    68
        m_handle = NULL;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    69
        return result != 0;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    70
    }
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    71
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    72
    return true;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    73
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    74
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    75
bool FileEngine::flush()
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    76
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    77
    return PHYSFS_flush(m_handle) != 0;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    78
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    79
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    80
qint64 FileEngine::size() const
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    81
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    82
    return m_size;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    83
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    84
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    85
qint64 FileEngine::pos() const
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    86
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    87
    return PHYSFS_tell(m_handle);
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    88
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    89
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    90
bool FileEngine::setSize(qint64 size)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    91
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    92
    if(size == 0)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    93
    {
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    94
        m_size = 0;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    95
        return open(QIODevice::WriteOnly);
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    96
    }
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    97
    else
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    98
        return false;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
    99
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   100
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   101
bool FileEngine::seek(qint64 pos)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   102
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   103
    bool ok = PHYSFS_seek(m_handle, pos) != 0;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   104
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   105
    return ok;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   106
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   107
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   108
bool FileEngine::isSequential() const
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   109
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   110
    return false;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   111
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   112
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   113
bool FileEngine::remove()
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   114
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   115
    return PHYSFS_delete(m_fileName.toUtf8().constData()) != 0;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   116
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   117
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   118
bool FileEngine::mkdir(const QString &dirName, bool createParentDirectories) const
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   119
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   120
    Q_UNUSED(createParentDirectories);
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   121
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   122
    return PHYSFS_mkdir(dirName.toUtf8().constData()) != 0;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   123
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   124
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   125
bool FileEngine::rmdir(const QString &dirName, bool recurseParentDirectories) const
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   126
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   127
    Q_UNUSED(recurseParentDirectories);
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   128
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   129
    return PHYSFS_delete(dirName.toUtf8().constData()) != 0;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   130
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   131
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   132
bool FileEngine::caseSensitive() const
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   133
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   134
    return true;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   135
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   136
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   137
bool FileEngine::isRelativePath() const
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   138
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   139
    return false;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   140
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   141
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   142
QAbstractFileEngineIterator * FileEngine::beginEntryList(QDir::Filters filters, const QStringList &filterNames)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   143
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   144
    return new FileEngineIterator(filters, filterNames, entryList(filters, filterNames));
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   145
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   146
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   147
QStringList FileEngine::entryList(QDir::Filters filters, const QStringList &filterNames) const
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   148
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   149
    Q_UNUSED(filters);
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   150
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   151
    QString file;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   152
    QStringList result;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   153
    char **files = PHYSFS_enumerateFiles(m_fileName.toUtf8().constData());
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   154
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   155
    for (char **i = files; *i != NULL; i++) {
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   156
        file = QString::fromUtf8(*i);
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   157
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   158
        if (filterNames.isEmpty() || QDir::match(filterNames, file)) {
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   159
            result << file;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   160
        }
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   161
    }
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   162
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   163
    PHYSFS_freeList(files);
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   164
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   165
    return result;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   166
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   167
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   168
QAbstractFileEngine::FileFlags FileEngine::fileFlags(FileFlags type) const
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   169
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   170
    return type & m_flags;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   171
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   172
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   173
QString FileEngine::fileName(FileName file) const
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   174
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   175
    switch(file)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   176
    {
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   177
        case QAbstractFileEngine::AbsolutePathName:
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   178
        {
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   179
            QString s(PHYSFS_getWriteDir());
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   180
            return s;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   181
        }
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   182
        case QAbstractFileEngine::BaseName:
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   183
        {
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   184
            int l = m_fileName.lastIndexOf('/');
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   185
            QString s = m_fileName.mid(l + 1);
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   186
            return s;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   187
        }
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   188
        case QAbstractFileEngine::DefaultName:
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   189
        case QAbstractFileEngine::AbsoluteName:
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   190
        default:
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   191
        {
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   192
            QString s = "physfs:/" + m_fileName;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   193
            return s;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   194
        }
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   195
    }
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   196
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   197
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   198
QDateTime FileEngine::fileTime(FileTime time) const
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   199
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   200
    switch (time)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   201
    {
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   202
        case QAbstractFileEngine::ModificationTime:
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   203
        default:
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   204
            return m_date;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   205
            break;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   206
    };
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   207
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   208
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   209
void FileEngine::setFileName(const QString &file)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   210
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   211
    if(file.startsWith(FileEngineHandler::scheme))
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   212
        m_fileName = file.mid(FileEngineHandler::scheme.size());
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   213
    else
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   214
        m_fileName = file;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   215
    PHYSFS_Stat stat;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   216
    if (PHYSFS_stat(m_fileName.toUtf8().constData(), &stat) != 0) {
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   217
        m_size = stat.filesize;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   218
        m_date = QDateTime::fromTime_t(stat.modtime);
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   219
//        m_flags |= QAbstractFileEngine::WriteOwnerPerm;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   220
        m_flags |= QAbstractFileEngine::ReadOwnerPerm;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   221
        m_flags |= QAbstractFileEngine::ReadUserPerm;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   222
        m_flags |= QAbstractFileEngine::ExistsFlag;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   223
        m_flags |= QAbstractFileEngine::LocalDiskFlag;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   224
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   225
        switch (stat.filetype)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   226
        {
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   227
            case PHYSFS_FILETYPE_REGULAR:
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   228
                m_flags |= QAbstractFileEngine::FileType;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   229
                break;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   230
            case PHYSFS_FILETYPE_DIRECTORY:
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   231
                m_flags |= QAbstractFileEngine::DirectoryType;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   232
                break;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   233
            case PHYSFS_FILETYPE_SYMLINK:
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   234
                m_flags |= QAbstractFileEngine::LinkType;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   235
                break;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   236
            default: ;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   237
        }
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   238
    }
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   239
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   240
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   241
bool FileEngine::atEnd() const
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   242
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   243
    return PHYSFS_eof(m_handle) != 0;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   244
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   245
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   246
qint64 FileEngine::read(char *data, qint64 maxlen)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   247
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   248
    if(m_readWrite)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   249
    {
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   250
        if(pos() == 0)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   251
            open(QIODevice::ReadOnly);
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   252
        else
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   253
            return -1;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   254
    }
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   255
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   256
    qint64 len = PHYSFS_readBytes(m_handle, data, maxlen);
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   257
    return len;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   258
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   259
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   260
qint64 FileEngine::readLine(char *data, qint64 maxlen)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   261
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   262
    if(!m_bufferSet)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   263
    {
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   264
        PHYSFS_setBuffer(m_handle, 4096);
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   265
        m_bufferSet = true;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   266
    }
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   267
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   268
    qint64 bytesRead = 0;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   269
    while(PHYSFS_readBytes(m_handle, data, 1)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   270
          && maxlen
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   271
          && (*data == '\n'))
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   272
    {
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   273
        ++data;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   274
        --maxlen;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   275
        ++bytesRead;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   276
    }
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   277
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   278
    return bytesRead;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   279
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   280
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   281
qint64 FileEngine::write(const char *data, qint64 len)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   282
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   283
    return PHYSFS_writeBytes(m_handle, data, len);
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   284
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   285
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   286
bool FileEngine::isOpened() const
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   287
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   288
    return m_handle != NULL;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   289
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   290
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   291
QFile::FileError FileEngine::error() const
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   292
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   293
    return QFile::UnspecifiedError;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   294
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   295
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   296
QString FileEngine::errorString() const
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   297
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   298
    return PHYSFS_getLastError();
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   299
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   300
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   301
bool FileEngine::supportsExtension(Extension extension) const
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   302
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   303
    return
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   304
            (extension == QAbstractFileEngine::AtEndExtension)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   305
            || (extension == QAbstractFileEngine::FastReadLineExtension)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   306
            ;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   307
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   308
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   309
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   310
FileEngineHandler::FileEngineHandler(char *argv0)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   311
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   312
    PHYSFS_init(argv0);
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   313
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   314
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   315
FileEngineHandler::~FileEngineHandler()
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   316
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   317
    PHYSFS_deinit();
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   318
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   319
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   320
QAbstractFileEngine* FileEngineHandler::create(const QString &filename) const
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   321
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   322
    if (filename.startsWith(scheme))
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   323
        return new FileEngine(filename);
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   324
    else
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   325
        return NULL;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   326
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   327
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   328
void FileEngineHandler::mount(const QString &path)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   329
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   330
    PHYSFS_mount(path.toUtf8().constData(), NULL, 1);
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   331
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   332
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   333
void FileEngineHandler::mount(const QString & path, const QString & mountPoint)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   334
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   335
    PHYSFS_mount(path.toUtf8().constData(), mountPoint.toUtf8().constData(), 1);
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   336
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   337
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   338
void FileEngineHandler::setWriteDir(const QString &path)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   339
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   340
    PHYSFS_setWriteDir(path.toUtf8().constData());
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   341
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   342
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   343
void FileEngineHandler::mountPacks()
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   344
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   345
    hedgewarsMountPackages();
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   346
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   347
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   348
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   349
FileEngineIterator::FileEngineIterator(QDir::Filters filters, const QStringList &nameFilters, const QStringList &entries)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   350
    : QAbstractFileEngineIterator(filters, nameFilters)
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   351
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   352
    m_entries = entries;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   353
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   354
    /* heck.. docs are unclear on this
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   355
     * QDirIterator puts iterator before first entry
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   356
     * but QAbstractFileEngineIterator example puts iterator on first entry
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   357
     * though QDirIterator approach seems to be the right one
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   358
     */
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   359
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   360
    m_index = -1;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   361
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   362
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   363
bool FileEngineIterator::hasNext() const
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   364
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   365
    return m_index < m_entries.size() - 1;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   366
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   367
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   368
QString FileEngineIterator::next()
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   369
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   370
   if (!hasNext())
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   371
       return QString();
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   372
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   373
   ++m_index;
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   374
   return currentFilePath();
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   375
}
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   376
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   377
QString FileEngineIterator::currentFileName() const
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   378
{
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   379
    return m_entries.at(m_index);
aaefa587e277 update branch with default
koda
parents: 8206
diff changeset
   380
}