QTfrontend/util/FileEngine.cpp
changeset 8115 ac1007c6dea8
parent 8112 d85dc8a8f41c
child 8178 8bd087478b48
equal deleted inserted replaced
8112:d85dc8a8f41c 8115:ac1007c6dea8
     7 
     7 
     8 
     8 
     9 const QString FileEngineHandler::scheme = "physfs:/";
     9 const QString FileEngineHandler::scheme = "physfs:/";
    10 
    10 
    11 FileEngine::FileEngine(const QString& filename)
    11 FileEngine::FileEngine(const QString& filename)
    12     : _handler(NULL)
    12     : m_handle(NULL)
    13     , _flags(0)
    13     , m_flags(0)
    14     , m_bufferSet(false)
    14     , m_bufferSet(false)
    15 {
    15 {
    16     setFileName(filename);
    16     setFileName(filename);
    17 }
    17 }
    18 
    18 
    24 bool FileEngine::open(QIODevice::OpenMode openMode)
    24 bool FileEngine::open(QIODevice::OpenMode openMode)
    25 {
    25 {
    26     close();
    26     close();
    27 
    27 
    28     if (openMode & QIODevice::WriteOnly) {
    28     if (openMode & QIODevice::WriteOnly) {
    29         _handler = PHYSFS_openWrite(_filename.toUtf8().constData());
    29         m_handle = PHYSFS_openWrite(m_fileName.toUtf8().constData());
    30         _flags = QAbstractFileEngine::WriteOwnerPerm | QAbstractFileEngine::WriteUserPerm | QAbstractFileEngine::FileType;
    30         m_flags = QAbstractFileEngine::WriteOwnerPerm | QAbstractFileEngine::WriteUserPerm | QAbstractFileEngine::FileType;
    31     }
    31     }
    32 
    32 
    33     else if (openMode & QIODevice::ReadOnly) {
    33     else if (openMode & QIODevice::ReadOnly) {
    34         _handler = PHYSFS_openRead(_filename.toUtf8().constData());
    34         m_handle = PHYSFS_openRead(m_fileName.toUtf8().constData());
    35     }
    35     }
    36 
    36 
    37     else if (openMode & QIODevice::Append) {
    37     else if (openMode & QIODevice::Append) {
    38         _handler = PHYSFS_openAppend(_filename.toUtf8().constData());
    38         m_handle = PHYSFS_openAppend(m_fileName.toUtf8().constData());
    39     }
    39     }
    40 
    40 
    41     else {
    41     else {
    42         qWarning("[PHYSFS] Bad file open mode: %d", (int)openMode);
    42         qWarning("[PHYSFS] Bad file open mode: %d", (int)openMode);
    43     }
    43     }
    44 
    44 
    45     if (!_handler) {
    45     if (!m_handle) {
    46         qWarning("[PHYSFS] Failed to open %s, reason: %s", _filename.toUtf8().constData(), PHYSFS_getLastError());
    46         qWarning("[PHYSFS] Failed to open %s, reason: %s", m_fileName.toUtf8().constData(), PHYSFS_getLastError());
    47         return false;
    47         return false;
    48     }
    48     }
    49 
    49 
    50     return true;
    50     return true;
    51 }
    51 }
    52 
    52 
    53 bool FileEngine::close()
    53 bool FileEngine::close()
    54 {
    54 {
    55     if (isOpened()) {
    55     if (isOpened()) {
    56         int result = PHYSFS_close(_handler);
    56         int result = PHYSFS_close(m_handle);
    57         _handler = NULL;
    57         m_handle = NULL;
    58         return result != 0;
    58         return result != 0;
    59     }
    59     }
    60 
    60 
    61     return true;
    61     return true;
    62 }
    62 }
    63 
    63 
    64 bool FileEngine::flush()
    64 bool FileEngine::flush()
    65 {
    65 {
    66     return PHYSFS_flush(_handler) != 0;
    66     return PHYSFS_flush(m_handle) != 0;
    67 }
    67 }
    68 
    68 
    69 qint64 FileEngine::size() const
    69 qint64 FileEngine::size() const
    70 {
    70 {
    71     return _size;
    71     return m_size;
    72 }
    72 }
    73 
    73 
    74 qint64 FileEngine::pos() const
    74 qint64 FileEngine::pos() const
    75 {
    75 {
    76     return PHYSFS_tell(_handler);
    76     return PHYSFS_tell(m_handle);
    77 }
    77 }
    78 
    78 
    79 bool FileEngine::seek(qint64 pos)
    79 bool FileEngine::seek(qint64 pos)
    80 {
    80 {
    81     return PHYSFS_seek(_handler, pos) != 0;
    81     return PHYSFS_seek(m_handle, pos) != 0;
    82 }
    82 }
    83 
    83 
    84 bool FileEngine::isSequential() const
    84 bool FileEngine::isSequential() const
    85 {
    85 {
    86     return false;
    86     return false;
    87 }
    87 }
    88 
    88 
    89 bool FileEngine::remove()
    89 bool FileEngine::remove()
    90 {
    90 {
    91     return PHYSFS_delete(_filename.toUtf8().constData()) != 0;
    91     return PHYSFS_delete(m_fileName.toUtf8().constData()) != 0;
    92 }
    92 }
    93 
    93 
    94 bool FileEngine::mkdir(const QString &dirName, bool createParentDirectories) const
    94 bool FileEngine::mkdir(const QString &dirName, bool createParentDirectories) const
    95 {
    95 {
    96     Q_UNUSED(createParentDirectories);
    96     Q_UNUSED(createParentDirectories);
   122 {
   122 {
   123     Q_UNUSED(filters);
   123     Q_UNUSED(filters);
   124 
   124 
   125     QString file;
   125     QString file;
   126     QStringList result;
   126     QStringList result;
   127     char **files = PHYSFS_enumerateFiles(_filename.toUtf8().constData());
   127     char **files = PHYSFS_enumerateFiles(m_fileName.toUtf8().constData());
   128 
   128 
   129     for (char **i = files; *i != NULL; i++) {
   129     for (char **i = files; *i != NULL; i++) {
   130         file = QString::fromUtf8(*i);
   130         file = QString::fromUtf8(*i);
   131 
   131 
   132         if (filterNames.isEmpty() || QDir::match(filterNames, file)) {
   132         if (filterNames.isEmpty() || QDir::match(filterNames, file)) {
   139     return result;
   139     return result;
   140 }
   140 }
   141 
   141 
   142 QAbstractFileEngine::FileFlags FileEngine::fileFlags(FileFlags type) const
   142 QAbstractFileEngine::FileFlags FileEngine::fileFlags(FileFlags type) const
   143 {
   143 {
   144     return type & _flags;
   144     return type & m_flags;
   145 }
   145 }
   146 
   146 
   147 QString FileEngine::fileName(FileName file) const
   147 QString FileEngine::fileName(FileName file) const
   148 {
   148 {
   149     switch(file)
   149     switch(file)
   153             QString s(PHYSFS_getWriteDir());
   153             QString s(PHYSFS_getWriteDir());
   154             return s;
   154             return s;
   155         }
   155         }
   156         case QAbstractFileEngine::BaseName:
   156         case QAbstractFileEngine::BaseName:
   157         {
   157         {
   158             int l = _filename.lastIndexOf('/');
   158             int l = m_fileName.lastIndexOf('/');
   159             QString s = _filename.mid(l + 1);
   159             QString s = m_fileName.mid(l + 1);
   160             return s;
   160             return s;
   161         }
   161         }
   162         case QAbstractFileEngine::DefaultName:
   162         case QAbstractFileEngine::DefaultName:
   163         case QAbstractFileEngine::AbsoluteName:
   163         case QAbstractFileEngine::AbsoluteName:
   164         default:
   164         default:
   165         {
   165         {
   166             QString s = "physfs:/" + _filename;
   166             QString s = "physfs:/" + m_fileName;
   167             return s;
   167             return s;
   168         }
   168         }
   169     }
   169     }
   170 }
   170 }
   171 
   171 
   174 
   174 
   175     switch (time)
   175     switch (time)
   176     {
   176     {
   177         case QAbstractFileEngine::ModificationTime:
   177         case QAbstractFileEngine::ModificationTime:
   178         default:
   178         default:
   179             return _datetime;
   179             return m_date;
   180             break;
   180             break;
   181     };
   181     };
   182 }
   182 }
   183 
   183 
   184 void FileEngine::setFileName(const QString &file)
   184 void FileEngine::setFileName(const QString &file)
   185 {
   185 {
   186     if(file.startsWith(FileEngineHandler::scheme))
   186     if(file.startsWith(FileEngineHandler::scheme))
   187         _filename = file.mid(FileEngineHandler::scheme.size());
   187         m_fileName = file.mid(FileEngineHandler::scheme.size());
   188     else
   188     else
   189         _filename = file;
   189         m_fileName = file;
   190 
   190 
   191     PHYSFS_Stat stat;
   191     PHYSFS_Stat stat;
   192     if (PHYSFS_stat(_filename.toUtf8().constData(), &stat) != 0) {
   192     if (PHYSFS_stat(m_fileName.toUtf8().constData(), &stat) != 0) {
   193         _size = stat.filesize;
   193         m_size = stat.filesize;
   194         _datetime = QDateTime::fromTime_t(stat.modtime);
   194         m_date = QDateTime::fromTime_t(stat.modtime);
   195 //        _flags |= QAbstractFileEngine::WriteUserPerm;
   195 //        _flags |= QAbstractFileEngine::WriteUserPerm;
   196         _flags |= QAbstractFileEngine::ReadUserPerm;
   196         m_flags |= QAbstractFileEngine::ReadUserPerm;
   197         _flags |= QAbstractFileEngine::ExistsFlag;
   197         m_flags |= QAbstractFileEngine::ExistsFlag;
   198 
   198 
   199         switch (stat.filetype)
   199         switch (stat.filetype)
   200         {
   200         {
   201             case PHYSFS_FILETYPE_REGULAR:
   201             case PHYSFS_FILETYPE_REGULAR:
   202                 _flags |= QAbstractFileEngine::FileType;
   202                 m_flags |= QAbstractFileEngine::FileType;
   203                 break;
   203                 break;
   204 
   204 
   205             case PHYSFS_FILETYPE_DIRECTORY:
   205             case PHYSFS_FILETYPE_DIRECTORY:
   206                 _flags |= QAbstractFileEngine::DirectoryType;
   206                 m_flags |= QAbstractFileEngine::DirectoryType;
   207                 break;
   207                 break;
   208             case PHYSFS_FILETYPE_SYMLINK:
   208             case PHYSFS_FILETYPE_SYMLINK:
   209                 _flags |= QAbstractFileEngine::LinkType;
   209                 m_flags |= QAbstractFileEngine::LinkType;
   210                 break;
   210                 break;
   211             default: ;
   211             default: ;
   212         }
   212         }
   213     }
   213     }
   214 }
   214 }
   215 
   215 
   216 bool FileEngine::atEnd() const
   216 bool FileEngine::atEnd() const
   217 {
   217 {
   218     return PHYSFS_eof(_handler) != 0;
   218     return PHYSFS_eof(m_handle) != 0;
   219 }
   219 }
   220 
   220 
   221 qint64 FileEngine::read(char *data, qint64 maxlen)
   221 qint64 FileEngine::read(char *data, qint64 maxlen)
   222 {
   222 {
   223     return PHYSFS_readBytes(_handler, data, maxlen);
   223     return PHYSFS_readBytes(m_handle, data, maxlen);
   224 }
   224 }
   225 
   225 
   226 qint64 FileEngine::readLine(char *data, qint64 maxlen)
   226 qint64 FileEngine::readLine(char *data, qint64 maxlen)
   227 {
   227 {
   228     if(!m_bufferSet)
   228     if(!m_bufferSet)
   229     {
   229     {
   230         PHYSFS_setBuffer(_handler, 4096);
   230         PHYSFS_setBuffer(m_handle, 4096);
   231         m_bufferSet = true;
   231         m_bufferSet = true;
   232     }
   232     }
   233 
   233 
   234     qint64 bytesRead = 0;
   234     qint64 bytesRead = 0;
   235     while(PHYSFS_readBytes(_handler, data, 1)
   235     while(PHYSFS_readBytes(m_handle, data, 1)
   236           && maxlen
   236           && maxlen
   237           && (*data == '\n'))
   237           && (*data == '\n'))
   238     {
   238     {
   239         ++data;
   239         ++data;
   240         --maxlen;
   240         --maxlen;
   244     return bytesRead;
   244     return bytesRead;
   245 }
   245 }
   246 
   246 
   247 qint64 FileEngine::write(const char *data, qint64 len)
   247 qint64 FileEngine::write(const char *data, qint64 len)
   248 {
   248 {
   249     return PHYSFS_writeBytes(_handler, data, len);
   249     return PHYSFS_writeBytes(m_handle, data, len);
   250 }
   250 }
   251 
   251 
   252 bool FileEngine::isOpened() const
   252 bool FileEngine::isOpened() const
   253 {
   253 {
   254     return _handler != NULL;
   254     return m_handle != NULL;
   255 }
   255 }
   256 
   256 
   257 QFile::FileError FileEngine::error() const
   257 QFile::FileError FileEngine::error() const
   258 {
   258 {
   259     return QFile::UnspecifiedError;
   259     return QFile::UnspecifiedError;