ff7tk  0.02
Toolkit for making FF7 Tools
Archive.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  ** Makou Reactor Final Fantasy VII Field Script Editor
3  ** Copyright (C) 2009-2013 Arzel Jérôme <myst6re@gmail.com>
4  **
5  ** This program is free software: you can redistribute it and/or modify
6  ** it under the terms of the GNU General Public License as published by
7  ** the Free Software Foundation, either version 3 of the License, or
8  ** (at your option) any later version.
9  **
10  ** This program is distributed in the hope that it will be useful,
11  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  ** GNU General Public License for more details.
14  **
15  ** You should have received a copy of the GNU General Public License
16  ** along with this program. If not, see <http://www.gnu.org/licenses/>.
17  ****************************************************************************/
18 #include "Archive.h"
19 
33  _error(NoError), _archiveIO(new QFile())
34 {
35 }
36 
40 Archive::Archive(const QString &filename) :
41  _error(NoError), _archiveIO(new QFile(filename))
42 {
43 }
44 
48 Archive::Archive(QFile *device) :
49  _error(NoError), _archiveIO(device)
50 {
51 }
52 
57 {
58  _archiveIO->deleteLater();
59 }
60 
65 QByteArray Archive::fileData(const QString &filePath)
66 {
67  QIODevice *io = file(filePath);
68  if(io == NULL || !io->open(QIODevice::ReadOnly)) {
69  qWarning() << "Archive::fileData error";
70  return QByteArray();
71  }
72  QByteArray data = io->readAll();
73  io->close();
74  return data;
75 }
76 
81 QByteArray Archive::modifiedFileData(const QString &filePath)
82 {
83  QIODevice *io = modifiedFile(filePath);
84  if(io == NULL || !io->open(QIODevice::ReadOnly)) {
85  qWarning() << "Archive::modifiedFileData error";
86  return QByteArray();
87  }
88  QByteArray data = io->readAll();
89  io->close();
90  return data;
91 }
92 
101 bool Archive::setFileData(const QString &filePath, const QByteArray &data)
102 {
103  QBuffer *buf = new QBuffer();
104  buf->setData(data);
105  return setFile(filePath, buf);
106 }
107 
116 bool Archive::addFileData(const QString &filePath, const QByteArray &data)
117 {
118  QBuffer *buf = new QBuffer();
119  buf->setData(data);
120  return addFile(filePath, buf);
121 }
122 
129 {
130  return !_archiveIO->exists() // Create the file
131  || (_archiveIO->open(QIODevice::ReadOnly)
132  && openHeader());
133 }
134 
140 bool Archive::isOpen() const
141 {
142  return _archiveIO->isOpen();
143 }
144 
150 {
151  _archiveIO->close();
152 }
153 
159 QString Archive::fileName() const
160 {
161  return _archiveIO->fileName();
162 }
163 
169 void Archive::setFileName(const QString &fileName)
170 {
171  _archiveIO->setFileName(fileName);
172 }
173 
178 QString Archive::errorString() const
179 {
180  return _errorString.isEmpty()
181  ? QLatin1String(QT_TRANSLATE_NOOP(Archive, ("Unknown error")))
182  : _errorString;
183 }
184 
190 {
191  return _error;
192 }
193 
199 {
200  _error = error;
201  setErrorString(errorString);
202 }
virtual bool addFile(const QString &filePath, QIODevice *data)=0
bool setFileData(const QString &filePath, const QByteArray &data)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: Archive.cpp:101
virtual QIODevice * modifiedFile(const QString &filePath)=0
virtual QIODevice * file(const QString &filePath)=0
void setFileName(const QString &fileName)
Sets the name of the file.
Definition: Archive.cpp:169
virtual ~Archive()
Destroys the archive object, closing it if necessary.
Definition: Archive.cpp:56
QString errorString() const
Returns the last error message.
Definition: Archive.cpp:178
QByteArray modifiedFileData(const QString &filePath)
Returns the data, modified by setData if modified, for the file named filePath.
Definition: Archive.cpp:81
virtual bool openHeader()=0
ArchiveError error() const
Returns the last error status.
Definition: Archive.cpp:189
virtual bool setFile(const QString &filePath, QIODevice *data)=0
void setError(ArchiveError error, const QString &errorString=QString())
Sets the file&#39;s error type and text.
Definition: Archive.cpp:198
bool addFileData(const QString &filePath, const QByteArray &data)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: Archive.cpp:116
The Archive class is a device list in a file system or an archive file.
Definition: Archive.h:31
ArchiveError
Definition: Archive.h:34
QString _errorString
Definition: Archive.h:89
QByteArray fileData(const QString &filePath)
Returns the data for the file named filePath.
Definition: Archive.cpp:65
virtual void close()
Closes the file.
Definition: Archive.cpp:149
QString fileName() const
Returns the name set by setFileName() or to the constructors.
Definition: Archive.cpp:159
Archive()
Constructs a new empty archive.
Definition: Archive.cpp:32
ArchiveError _error
Definition: Archive.h:90
QFile * _archiveIO
Definition: Archive.h:91
void setErrorString(const QString &errorString)
Definition: Archive.h:79
virtual bool open()
Opens the archive, returning true if successful; otherwise false.
Definition: Archive.cpp:128
virtual bool isOpen() const
Returns true if the archive is open; returns false otherwise.
Definition: Archive.cpp:140