2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
2002-08-31 07:43:34 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2014-02-18 01:34:18 +00:00
|
|
|
*
|
2002-08-31 07:43:34 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2014-02-18 01:34:18 +00:00
|
|
|
*
|
2002-08-31 07:43:34 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-08-31 07:43:34 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-09-08 11:11:32 +00:00
|
|
|
#include "common/archive.h"
|
2009-01-30 05:25:17 +00:00
|
|
|
#include "common/debug.h"
|
2003-08-01 12:21:04 +00:00
|
|
|
#include "common/file.h"
|
2006-06-24 08:07:48 +00:00
|
|
|
#include "common/fs.h"
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "common/textconsole.h"
|
2016-05-25 10:32:22 +00:00
|
|
|
#include "common/system.h"
|
|
|
|
#include "backends/fs/fs-factory.h"
|
2006-03-15 07:43:44 +00:00
|
|
|
|
2005-05-10 22:56:25 +00:00
|
|
|
namespace Common {
|
2003-09-17 21:06:16 +00:00
|
|
|
|
2004-06-28 22:34:22 +00:00
|
|
|
File::File()
|
2018-04-05 18:25:28 +00:00
|
|
|
: _handle(nullptr) {
|
2002-08-31 07:43:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
File::~File() {
|
|
|
|
close();
|
|
|
|
}
|
2004-06-28 22:34:22 +00:00
|
|
|
|
2021-08-02 02:53:53 +00:00
|
|
|
bool File::open(const Path &filename) {
|
2008-09-30 09:12:02 +00:00
|
|
|
return open(filename, SearchMan);
|
|
|
|
}
|
|
|
|
|
2021-08-02 02:53:53 +00:00
|
|
|
bool File::open(const Path &filename, Archive &archive) {
|
2008-07-29 16:09:10 +00:00
|
|
|
assert(!filename.empty());
|
|
|
|
assert(!_handle);
|
2002-11-02 10:51:32 +00:00
|
|
|
|
2018-04-05 18:25:28 +00:00
|
|
|
SeekableReadStream *stream = nullptr;
|
2009-05-19 11:42:14 +00:00
|
|
|
|
2009-02-22 04:40:10 +00:00
|
|
|
if ((stream = archive.createReadStreamForMember(filename))) {
|
2021-08-02 02:53:53 +00:00
|
|
|
debug(8, "Opening hashed: %s", filename.toString().c_str());
|
2021-08-06 23:25:14 +00:00
|
|
|
} else if ((stream = archive.createReadStreamForMember(filename.append(".")))) {
|
2021-02-28 09:28:23 +00:00
|
|
|
// WORKAROUND: Bug #2548: "SIMON1: Game Detection fails"
|
2006-03-25 21:17:38 +00:00
|
|
|
// sometimes instead of "GAMEPC" we get "GAMEPC." (note trailing dot)
|
2021-08-02 02:53:53 +00:00
|
|
|
debug(8, "Opening hashed: %s.", filename.toString().c_str());
|
2002-09-02 22:06:26 +00:00
|
|
|
}
|
2008-12-22 11:22:15 +00:00
|
|
|
|
2021-08-02 02:53:53 +00:00
|
|
|
return open(stream, filename.toString());
|
2002-08-31 07:43:34 +00:00
|
|
|
}
|
|
|
|
|
2008-10-02 16:58:59 +00:00
|
|
|
bool File::open(const FSNode &node) {
|
2008-09-30 09:12:02 +00:00
|
|
|
assert(!_handle);
|
2006-07-22 17:01:50 +00:00
|
|
|
|
2007-06-16 17:31:36 +00:00
|
|
|
if (!node.exists()) {
|
2008-09-30 09:12:02 +00:00
|
|
|
warning("File::open: '%s' does not exist", node.getPath().c_str());
|
2006-07-22 17:01:50 +00:00
|
|
|
return false;
|
|
|
|
} else if (node.isDirectory()) {
|
2008-09-30 09:12:02 +00:00
|
|
|
warning("File::open: '%s' is a directory", node.getPath().c_str());
|
2006-07-22 17:01:50 +00:00
|
|
|
return false;
|
2008-09-13 16:51:46 +00:00
|
|
|
}
|
2006-07-22 17:01:50 +00:00
|
|
|
|
2009-01-23 03:41:36 +00:00
|
|
|
SeekableReadStream *stream = node.createReadStream();
|
2008-09-30 09:12:02 +00:00
|
|
|
return open(stream, node.getPath());
|
|
|
|
}
|
2006-04-30 23:08:37 +00:00
|
|
|
|
2011-08-06 07:47:19 +00:00
|
|
|
bool File::open(SeekableReadStream *stream, const String &name) {
|
2008-09-30 09:12:02 +00:00
|
|
|
assert(!_handle);
|
2006-04-30 23:08:37 +00:00
|
|
|
|
2008-09-30 09:12:02 +00:00
|
|
|
if (stream) {
|
|
|
|
_handle = stream;
|
|
|
|
_name = name;
|
|
|
|
} else {
|
|
|
|
debug(2, "File::open: opening '%s' failed", name.c_str());
|
|
|
|
}
|
2018-04-05 18:25:28 +00:00
|
|
|
return _handle != nullptr;
|
2006-04-30 23:08:37 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 09:12:02 +00:00
|
|
|
|
2021-08-02 02:53:53 +00:00
|
|
|
bool File::exists(const Path &filename) {
|
2008-09-27 23:00:46 +00:00
|
|
|
if (SearchMan.hasFile(filename)) {
|
2008-09-08 11:11:32 +00:00
|
|
|
return true;
|
2021-08-06 23:25:14 +00:00
|
|
|
} else if (SearchMan.hasFile(filename.append("."))) {
|
2021-02-28 09:28:23 +00:00
|
|
|
// WORKAROUND: Bug #2548: "SIMON1: Game Detection fails"
|
2008-09-08 11:11:32 +00:00
|
|
|
// sometimes instead of "GAMEPC" we get "GAMEPC." (note trailing dot)
|
|
|
|
return true;
|
2007-10-16 20:03:23 +00:00
|
|
|
}
|
2008-12-22 11:22:15 +00:00
|
|
|
|
2008-09-08 11:11:32 +00:00
|
|
|
return false;
|
2004-11-27 15:09:53 +00:00
|
|
|
}
|
|
|
|
|
2002-08-31 07:43:34 +00:00
|
|
|
void File::close() {
|
2008-09-06 20:49:48 +00:00
|
|
|
delete _handle;
|
2018-04-05 18:25:28 +00:00
|
|
|
_handle = nullptr;
|
2002-08-31 09:55:58 +00:00
|
|
|
}
|
|
|
|
|
2003-11-30 00:06:27 +00:00
|
|
|
bool File::isOpen() const {
|
2018-04-05 18:25:28 +00:00
|
|
|
return _handle != nullptr;
|
2002-08-31 07:43:34 +00:00
|
|
|
}
|
|
|
|
|
2008-09-14 22:28:53 +00:00
|
|
|
bool File::err() const {
|
|
|
|
assert(_handle);
|
|
|
|
return _handle->err();
|
|
|
|
}
|
|
|
|
|
|
|
|
void File::clearErr() {
|
|
|
|
assert(_handle);
|
|
|
|
_handle->clearErr();
|
|
|
|
}
|
|
|
|
|
2008-09-06 20:49:48 +00:00
|
|
|
bool File::eos() const {
|
2008-07-29 16:09:10 +00:00
|
|
|
assert(_handle);
|
2008-09-06 20:49:48 +00:00
|
|
|
return _handle->eos();
|
2002-08-31 07:43:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 03:30:41 +00:00
|
|
|
int64 File::pos() const {
|
2008-07-29 16:09:10 +00:00
|
|
|
assert(_handle);
|
2008-09-06 20:49:48 +00:00
|
|
|
return _handle->pos();
|
2002-08-31 07:43:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 03:30:41 +00:00
|
|
|
int64 File::size() const {
|
2008-07-29 16:09:10 +00:00
|
|
|
assert(_handle);
|
2008-09-06 20:49:48 +00:00
|
|
|
return _handle->size();
|
2002-09-13 18:02:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 03:30:41 +00:00
|
|
|
bool File::seek(int64 offs, int whence) {
|
2008-07-29 16:09:10 +00:00
|
|
|
assert(_handle);
|
2008-09-13 16:51:46 +00:00
|
|
|
return _handle->seek(offs, whence);
|
2002-08-31 07:43:34 +00:00
|
|
|
}
|
|
|
|
|
2002-10-22 11:34:21 +00:00
|
|
|
uint32 File::read(void *ptr, uint32 len) {
|
2008-07-29 16:09:10 +00:00
|
|
|
assert(_handle);
|
2008-09-06 20:49:48 +00:00
|
|
|
return _handle->read(ptr, len);
|
2002-08-31 07:43:34 +00:00
|
|
|
}
|
|
|
|
|
2008-07-29 16:09:10 +00:00
|
|
|
|
2018-04-05 18:25:28 +00:00
|
|
|
DumpFile::DumpFile() : _handle(nullptr) {
|
2008-07-29 16:09:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DumpFile::~DumpFile() {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2016-05-25 10:32:22 +00:00
|
|
|
bool DumpFile::open(const String &filename, bool createPath) {
|
2008-07-29 16:09:10 +00:00
|
|
|
assert(!filename.empty());
|
|
|
|
assert(!_handle);
|
|
|
|
|
2019-09-14 21:50:34 +00:00
|
|
|
if (createPath) {
|
|
|
|
for (uint32 i = 0; i < filename.size(); ++i) {
|
|
|
|
if (filename[i] == '/' || filename[i] == '\\') {
|
|
|
|
Common::String subpath = filename;
|
|
|
|
subpath.erase(i);
|
2021-08-08 15:43:49 +00:00
|
|
|
if (subpath.empty() || subpath == ".") continue;
|
2019-09-14 21:50:34 +00:00
|
|
|
AbstractFSNode *node = g_system->getFilesystemFactory()->makeFileNodePath(subpath);
|
|
|
|
if (node->exists()) {
|
|
|
|
delete node;
|
|
|
|
continue;
|
|
|
|
}
|
2021-08-08 15:43:49 +00:00
|
|
|
if (!node->createDirectory()) warning("DumpFile: unable to create directories from path prefix (%s)", subpath.c_str());
|
2019-09-14 21:50:34 +00:00
|
|
|
delete node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-02 17:08:33 +00:00
|
|
|
|
2019-09-14 21:50:34 +00:00
|
|
|
FSNode node(filename);
|
2008-09-08 11:11:32 +00:00
|
|
|
return open(node);
|
2008-07-29 16:09:10 +00:00
|
|
|
}
|
|
|
|
|
2008-10-02 16:58:59 +00:00
|
|
|
bool DumpFile::open(const FSNode &node) {
|
2008-08-03 18:11:27 +00:00
|
|
|
assert(!_handle);
|
|
|
|
|
2008-08-04 13:25:30 +00:00
|
|
|
if (node.isDirectory()) {
|
2008-10-02 16:58:59 +00:00
|
|
|
warning("DumpFile::open: FSNode is a directory");
|
2008-08-03 18:11:27 +00:00
|
|
|
return false;
|
2008-09-13 16:51:46 +00:00
|
|
|
}
|
2008-08-03 18:11:27 +00:00
|
|
|
|
2009-01-23 03:41:36 +00:00
|
|
|
_handle = node.createWriteStream();
|
2008-08-03 18:11:27 +00:00
|
|
|
|
2018-04-05 18:25:28 +00:00
|
|
|
if (_handle == nullptr)
|
2008-08-03 18:11:27 +00:00
|
|
|
debug(2, "File %s not found", node.getName().c_str());
|
|
|
|
|
2018-04-05 18:25:28 +00:00
|
|
|
return _handle != nullptr;
|
2008-08-03 18:11:27 +00:00
|
|
|
}
|
|
|
|
|
2008-07-29 16:09:10 +00:00
|
|
|
void DumpFile::close() {
|
2008-09-06 20:49:48 +00:00
|
|
|
delete _handle;
|
2018-04-05 18:25:28 +00:00
|
|
|
_handle = nullptr;
|
2008-07-29 16:09:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DumpFile::isOpen() const {
|
2018-04-05 18:25:28 +00:00
|
|
|
return _handle != nullptr;
|
2008-07-29 16:09:10 +00:00
|
|
|
}
|
|
|
|
|
2008-09-14 22:28:53 +00:00
|
|
|
bool DumpFile::err() const {
|
2008-07-29 16:09:10 +00:00
|
|
|
assert(_handle);
|
2009-05-19 11:42:14 +00:00
|
|
|
return _handle->err();
|
2008-07-29 16:09:10 +00:00
|
|
|
}
|
|
|
|
|
2008-09-14 22:28:53 +00:00
|
|
|
void DumpFile::clearErr() {
|
2008-07-29 16:09:10 +00:00
|
|
|
assert(_handle);
|
2009-05-19 11:42:14 +00:00
|
|
|
_handle->clearErr();
|
2008-07-29 16:09:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 DumpFile::write(const void *ptr, uint32 len) {
|
|
|
|
assert(_handle);
|
2008-09-06 20:49:48 +00:00
|
|
|
return _handle->write(ptr, len);
|
2002-09-02 22:06:26 +00:00
|
|
|
}
|
2005-05-10 22:56:25 +00:00
|
|
|
|
2008-09-13 16:51:46 +00:00
|
|
|
bool DumpFile::flush() {
|
2008-08-04 11:30:47 +00:00
|
|
|
assert(_handle);
|
2008-09-13 16:51:46 +00:00
|
|
|
return _handle->flush();
|
2008-08-04 11:30:47 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 03:30:41 +00:00
|
|
|
int64 DumpFile::pos() const { return _handle->pos(); }
|
2016-08-04 08:36:21 +00:00
|
|
|
|
2021-07-04 03:30:41 +00:00
|
|
|
bool DumpFile::seek(int64 offset, int whence) {
|
2019-05-04 06:20:22 +00:00
|
|
|
SeekableWriteStream *ws = dynamic_cast<SeekableWriteStream *>(_handle);
|
2019-05-04 23:58:14 +00:00
|
|
|
return ws ? ws->seek(offset, whence) : false;
|
2019-05-04 06:20:22 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 03:30:41 +00:00
|
|
|
int64 DumpFile::size() const {
|
2019-05-04 06:20:22 +00:00
|
|
|
SeekableWriteStream *ws = dynamic_cast<SeekableWriteStream *>(_handle);
|
|
|
|
return ws ? ws->size() : -1;
|
|
|
|
}
|
|
|
|
|
2013-01-26 18:33:27 +00:00
|
|
|
} // End of namespace Common
|