ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

file.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "common/archive.h"
00024 #include "common/debug.h"
00025 #include "common/file.h"
00026 #include "common/fs.h"
00027 #include "common/textconsole.h"
00028 #include "common/system.h"
00029 #include "backends/fs/fs-factory.h"
00030 
00031 namespace Common {
00032 
00033 File::File()
00034     : _handle(nullptr) {
00035 }
00036 
00037 File::~File() {
00038     close();
00039 }
00040 
00041 bool File::open(const String &filename) {
00042     return open(filename, SearchMan);
00043 }
00044 
00045 bool File::open(const String &filename, Archive &archive) {
00046     assert(!filename.empty());
00047     assert(!_handle);
00048 
00049     SeekableReadStream *stream = nullptr;
00050 
00051     if ((stream = archive.createReadStreamForMember(filename))) {
00052         debug(8, "Opening hashed: %s", filename.c_str());
00053     } else if ((stream = archive.createReadStreamForMember(filename + "."))) {
00054         // WORKAROUND: Bug #1458388: "SIMON1: Game Detection fails"
00055         // sometimes instead of "GAMEPC" we get "GAMEPC." (note trailing dot)
00056         debug(8, "Opening hashed: %s.", filename.c_str());
00057     }
00058 
00059     return open(stream, filename);
00060 }
00061 
00062 bool File::open(const FSNode &node) {
00063     assert(!_handle);
00064 
00065     if (!node.exists()) {
00066         warning("File::open: '%s' does not exist", node.getPath().c_str());
00067         return false;
00068     } else if (node.isDirectory()) {
00069         warning("File::open: '%s' is a directory", node.getPath().c_str());
00070         return false;
00071     }
00072 
00073     SeekableReadStream *stream = node.createReadStream();
00074     return open(stream, node.getPath());
00075 }
00076 
00077 bool File::open(SeekableReadStream *stream, const String &name) {
00078     assert(!_handle);
00079 
00080     if (stream) {
00081         _handle = stream;
00082         _name = name;
00083     } else {
00084         debug(2, "File::open: opening '%s' failed", name.c_str());
00085     }
00086     return _handle != nullptr;
00087 }
00088 
00089 
00090 bool File::exists(const String &filename) {
00091     if (SearchMan.hasFile(filename)) {
00092         return true;
00093     } else if (SearchMan.hasFile(filename + ".")) {
00094         // WORKAROUND: Bug #1458388: "SIMON1: Game Detection fails"
00095         // sometimes instead of "GAMEPC" we get "GAMEPC." (note trailing dot)
00096         return true;
00097     }
00098 
00099     return false;
00100 }
00101 
00102 void File::close() {
00103     delete _handle;
00104     _handle = nullptr;
00105 }
00106 
00107 bool File::isOpen() const {
00108     return _handle != nullptr;
00109 }
00110 
00111 bool File::err() const {
00112     assert(_handle);
00113     return _handle->err();
00114 }
00115 
00116 void File::clearErr() {
00117     assert(_handle);
00118     _handle->clearErr();
00119 }
00120 
00121 bool File::eos() const {
00122     assert(_handle);
00123     return _handle->eos();
00124 }
00125 
00126 int32 File::pos() const {
00127     assert(_handle);
00128     return _handle->pos();
00129 }
00130 
00131 int32 File::size() const {
00132     assert(_handle);
00133     return _handle->size();
00134 }
00135 
00136 bool File::seek(int32 offs, int whence) {
00137     assert(_handle);
00138     return _handle->seek(offs, whence);
00139 }
00140 
00141 uint32 File::read(void *ptr, uint32 len) {
00142     assert(_handle);
00143     return _handle->read(ptr, len);
00144 }
00145 
00146 
00147 DumpFile::DumpFile() : _handle(nullptr) {
00148 }
00149 
00150 DumpFile::~DumpFile() {
00151     close();
00152 }
00153 
00154 bool DumpFile::open(const String &filename, bool createPath) {
00155     assert(!filename.empty());
00156     assert(!_handle);
00157 
00158     if (createPath) {
00159         for (uint32 i = 0; i < filename.size(); ++i) {
00160             if (filename[i] == '/' || filename[i] == '\\') {
00161                 Common::String subpath = filename;
00162                 subpath.erase(i);
00163                 if (subpath.empty()) continue;
00164                 AbstractFSNode *node = g_system->getFilesystemFactory()->makeFileNodePath(subpath);
00165                 if (node->exists()) continue;
00166                 if (!node->create(true)) warning("DumpFile: unable to create directories from path prefix");
00167             }
00168         }
00169     }
00170 
00171     FSNode node(filename);
00172     return open(node);
00173 }
00174 
00175 bool DumpFile::open(const FSNode &node) {
00176     assert(!_handle);
00177 
00178     if (node.isDirectory()) {
00179         warning("DumpFile::open: FSNode is a directory");
00180         return false;
00181     }
00182 
00183     _handle = node.createWriteStream();
00184 
00185     if (_handle == nullptr)
00186         debug(2, "File %s not found", node.getName().c_str());
00187 
00188     return _handle != nullptr;
00189 }
00190 
00191 void DumpFile::close() {
00192     delete _handle;
00193     _handle = nullptr;
00194 }
00195 
00196 bool DumpFile::isOpen() const {
00197     return _handle != nullptr;
00198 }
00199 
00200 bool DumpFile::err() const {
00201     assert(_handle);
00202     return _handle->err();
00203 }
00204 
00205 void DumpFile::clearErr() {
00206     assert(_handle);
00207     _handle->clearErr();
00208 }
00209 
00210 uint32 DumpFile::write(const void *ptr, uint32 len) {
00211     assert(_handle);
00212     return _handle->write(ptr, len);
00213 }
00214 
00215 bool DumpFile::flush() {
00216     assert(_handle);
00217     return _handle->flush();
00218 }
00219 
00220 int32 DumpFile::pos() const { return _handle->pos(); }
00221 
00222 } // End of namespace Common


Generated on Sat Mar 23 2019 05:01:36 for ResidualVM by doxygen 1.7.1
curved edge   curved edge