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.
|
|
|
|
|
|
|
|
* 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.
|
|
|
|
|
|
|
|
* 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"
|
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()
|
2008-09-06 20:49:48 +00:00
|
|
|
: _handle(0) {
|
2002-08-31 07:43:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
File::~File() {
|
|
|
|
close();
|
|
|
|
}
|
2004-06-28 22:34:22 +00:00
|
|
|
|
2008-07-29 16:09:10 +00:00
|
|
|
bool File::open(const String &filename) {
|
2008-09-30 09:12:02 +00:00
|
|
|
return open(filename, SearchMan);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool File::open(const String &filename, Archive &archive) {
|
2008-07-29 16:09:10 +00:00
|
|
|
assert(!filename.empty());
|
|
|
|
assert(!_handle);
|
2002-11-02 10:51:32 +00:00
|
|
|
|
2008-09-30 09:12:02 +00:00
|
|
|
SeekableReadStream *stream = 0;
|
2009-05-19 11:42:14 +00:00
|
|
|
|
2009-02-22 04:40:10 +00:00
|
|
|
if ((stream = archive.createReadStreamForMember(filename))) {
|
2010-06-15 10:19:06 +00:00
|
|
|
debug(8, "Opening hashed: %s", filename.c_str());
|
2009-02-22 04:40:10 +00:00
|
|
|
} else if ((stream = archive.createReadStreamForMember(filename + "."))) {
|
2006-03-25 21:17:38 +00:00
|
|
|
// WORKAROUND: Bug #1458388: "SIMON1: Game Detection fails"
|
|
|
|
// sometimes instead of "GAMEPC" we get "GAMEPC." (note trailing dot)
|
2010-06-15 10:19:06 +00:00
|
|
|
debug(8, "Opening hashed: %s.", filename.c_str());
|
2002-09-02 22:06:26 +00:00
|
|
|
}
|
2008-12-22 11:22:15 +00:00
|
|
|
|
2008-09-30 09:12:02 +00:00
|
|
|
return open(stream, filename);
|
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());
|
|
|
|
}
|
2008-07-29 16:09:10 +00:00
|
|
|
return _handle != NULL;
|
2006-04-30 23:08:37 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 09:12:02 +00:00
|
|
|
|
2006-04-26 08:05:40 +00:00
|
|
|
bool File::exists(const String &filename) {
|
2008-09-27 23:00:46 +00:00
|
|
|
if (SearchMan.hasFile(filename)) {
|
2008-09-08 11:11:32 +00:00
|
|
|
return true;
|
2008-09-27 23:00:46 +00:00
|
|
|
} else if (SearchMan.hasFile(filename + ".")) {
|
2008-09-08 11:11:32 +00:00
|
|
|
// WORKAROUND: Bug #1458388: "SIMON1: Game Detection fails"
|
|
|
|
// 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;
|
2002-08-31 09:55:58 +00:00
|
|
|
_handle = NULL;
|
|
|
|
}
|
|
|
|
|
2003-11-30 00:06:27 +00:00
|
|
|
bool File::isOpen() const {
|
2002-08-31 09:55:58 +00:00
|
|
|
return _handle != NULL;
|
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
|
|
|
}
|
|
|
|
|
2008-09-13 16:51:46 +00:00
|
|
|
int32 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
|
|
|
}
|
|
|
|
|
2008-09-13 16:51:46 +00:00
|
|
|
int32 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
|
|
|
}
|
|
|
|
|
2008-09-13 16:51:46 +00:00
|
|
|
bool File::seek(int32 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
|
|
|
|
|
|
|
DumpFile::DumpFile() : _handle(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
DumpFile::~DumpFile() {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DumpFile::open(const String &filename) {
|
|
|
|
assert(!filename.empty());
|
|
|
|
assert(!_handle);
|
|
|
|
|
2008-10-02 16:58:59 +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
|
|
|
|
|
|
|
if (_handle == NULL)
|
|
|
|
debug(2, "File %s not found", node.getName().c_str());
|
|
|
|
|
|
|
|
return _handle != NULL;
|
|
|
|
}
|
|
|
|
|
2008-07-29 16:09:10 +00:00
|
|
|
void DumpFile::close() {
|
2008-09-06 20:49:48 +00:00
|
|
|
delete _handle;
|
2008-07-29 16:09:10 +00:00
|
|
|
_handle = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DumpFile::isOpen() const {
|
|
|
|
return _handle != NULL;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-01-26 18:33:27 +00:00
|
|
|
} // End of namespace Common
|