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
|
|
|
*
|
2006-02-11 09:53:53 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
2002-08-31 07:43:34 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-09-08 11:11:32 +00:00
|
|
|
#include "common/archive.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"
|
2003-08-01 12:21:04 +00:00
|
|
|
#include "common/util.h"
|
2008-09-08 11:11:32 +00:00
|
|
|
#include "common/system.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
|
|
|
|
2008-09-08 11:11:32 +00:00
|
|
|
static Common::SearchSet *s_searchSet = 0;
|
2005-05-09 21:21:21 +00:00
|
|
|
|
2002-09-13 18:02:34 +00:00
|
|
|
|
2006-03-25 19:30:10 +00:00
|
|
|
void File::addDefaultDirectory(const String &directory) {
|
2006-05-01 21:06:34 +00:00
|
|
|
FilesystemNode dir(directory);
|
|
|
|
addDefaultDirectoryRecursive(dir, 1);
|
2006-03-25 04:17:17 +00:00
|
|
|
}
|
|
|
|
|
2008-09-08 11:11:32 +00:00
|
|
|
void File::addDefaultDirectoryRecursive(const String &directory, int level) {
|
2006-05-01 21:06:34 +00:00
|
|
|
FilesystemNode dir(directory);
|
2008-09-08 11:11:32 +00:00
|
|
|
addDefaultDirectoryRecursive(dir, level);
|
2006-05-01 21:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void File::addDefaultDirectory(const FilesystemNode &directory) {
|
|
|
|
addDefaultDirectoryRecursive(directory, 1);
|
|
|
|
}
|
|
|
|
|
2008-09-08 11:11:32 +00:00
|
|
|
void File::addDefaultDirectoryRecursive(const FilesystemNode &dir, int level) {
|
2008-09-07 21:47:46 +00:00
|
|
|
if (level <= 0 || !dir.exists() || !dir.isDirectory())
|
2006-03-25 04:17:17 +00:00
|
|
|
return;
|
|
|
|
|
2008-09-08 11:11:32 +00:00
|
|
|
if (!s_searchSet) {
|
|
|
|
s_searchSet = new Common::SearchSet();
|
|
|
|
g_system->addSysArchivesToSearchSet(*s_searchSet);
|
2006-05-03 20:43:26 +00:00
|
|
|
}
|
2006-05-01 21:06:34 +00:00
|
|
|
|
2008-09-08 11:11:32 +00:00
|
|
|
Common::ArchivePtr dataArchive(new Common::FSDirectory(dir, level));
|
|
|
|
s_searchSet->add(dir.getPath(), dataArchive, 1);
|
2004-06-28 00:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void File::resetDefaultDirectories() {
|
2008-09-08 11:11:32 +00:00
|
|
|
delete s_searchSet;
|
|
|
|
s_searchSet = 0;
|
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
|
|
|
|
2002-08-31 07:43:34 +00:00
|
|
|
|
2008-07-29 16:09:10 +00:00
|
|
|
bool File::open(const String &filename) {
|
|
|
|
assert(!filename.empty());
|
|
|
|
assert(!_handle);
|
2002-11-02 10:51:32 +00:00
|
|
|
|
2006-04-30 23:08:37 +00:00
|
|
|
_name.clear();
|
2002-09-02 22:06:26 +00:00
|
|
|
clearIOFailed();
|
2002-09-02 20:53:12 +00:00
|
|
|
|
2008-09-08 11:11:32 +00:00
|
|
|
if (s_searchSet && s_searchSet->hasFile(filename)) {
|
|
|
|
debug(3, "Opening hashed: %s", filename.c_str());
|
|
|
|
_handle = s_searchSet->openFile(filename);
|
|
|
|
} else if (s_searchSet && s_searchSet->hasFile(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)
|
2008-09-08 11:11:32 +00:00
|
|
|
debug(3, "Opening hashed: %s.", filename.c_str());
|
|
|
|
_handle = s_searchSet->openFile(filename);
|
2004-06-28 00:06:31 +00:00
|
|
|
} else {
|
2004-08-05 11:10:27 +00:00
|
|
|
// Last resort: try the current directory
|
2008-09-08 11:11:32 +00:00
|
|
|
FilesystemNode file(filename);
|
|
|
|
if (file.exists() && !file.isDirectory())
|
|
|
|
_handle = file.openForReading();
|
2002-09-02 22:06:26 +00:00
|
|
|
}
|
2008-09-06 20:49:48 +00:00
|
|
|
|
2008-07-29 16:09:10 +00:00
|
|
|
if (_handle == NULL)
|
|
|
|
debug(2, "File %s not opened", filename.c_str());
|
|
|
|
else
|
|
|
|
_name = filename;
|
2004-06-28 22:34:22 +00:00
|
|
|
|
2008-07-29 16:09:10 +00:00
|
|
|
return _handle != NULL;
|
2002-08-31 07:43:34 +00:00
|
|
|
}
|
|
|
|
|
2008-07-29 16:09:10 +00:00
|
|
|
bool File::open(const FilesystemNode &node) {
|
2006-07-22 17:01:50 +00:00
|
|
|
|
2007-06-16 17:31:36 +00:00
|
|
|
if (!node.exists()) {
|
|
|
|
warning("File::open: Trying to open a FilesystemNode which does not exist");
|
2006-07-22 17:01:50 +00:00
|
|
|
return false;
|
|
|
|
} else if (node.isDirectory()) {
|
|
|
|
warning("File::open: Trying to open a FilesystemNode which is a directory");
|
|
|
|
return false;
|
2008-09-13 16:51:46 +00:00
|
|
|
}
|
2006-07-22 17:01:50 +00:00
|
|
|
|
2007-06-05 21:02:35 +00:00
|
|
|
String filename(node.getName());
|
2006-04-30 23:08:37 +00:00
|
|
|
|
|
|
|
if (_handle) {
|
|
|
|
error("File::open: This file object already is opened (%s), won't open '%s'", _name.c_str(), filename.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
clearIOFailed();
|
|
|
|
_name.clear();
|
|
|
|
|
2008-09-06 20:49:48 +00:00
|
|
|
_handle = node.openForReading();
|
2006-04-30 23:08:37 +00:00
|
|
|
|
2008-07-29 16:09:10 +00:00
|
|
|
if (_handle == NULL)
|
|
|
|
debug(2, "File %s not found", filename.c_str());
|
|
|
|
else
|
|
|
|
_name = filename;
|
2006-04-30 23:08:37 +00:00
|
|
|
|
2008-07-29 16:09:10 +00:00
|
|
|
return _handle != NULL;
|
2006-04-30 23:08:37 +00:00
|
|
|
}
|
|
|
|
|
2006-04-26 08:05:40 +00:00
|
|
|
bool File::exists(const String &filename) {
|
2008-09-08 11:11:32 +00:00
|
|
|
if (s_searchSet && s_searchSet->hasFile(filename)) {
|
|
|
|
return true;
|
|
|
|
} else if (s_searchSet && s_searchSet->hasFile(filename + ".")) {
|
|
|
|
// WORKAROUND: Bug #1458388: "SIMON1: Game Detection fails"
|
|
|
|
// sometimes instead of "GAMEPC" we get "GAMEPC." (note trailing dot)
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// Last resort: try the current directory
|
|
|
|
FilesystemNode file(filename);
|
|
|
|
if (file.exists() && !file.isDirectory())
|
|
|
|
return true;
|
2007-10-16 20:03:23 +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
|
|
|
}
|
|
|
|
|
2003-11-30 00:06:27 +00:00
|
|
|
bool File::ioFailed() const {
|
2008-07-20 16:47:34 +00:00
|
|
|
// TODO/FIXME: Just use ferror() here?
|
2008-09-06 20:49:48 +00:00
|
|
|
return !_handle || _handle->ioFailed();
|
2002-08-31 07:43:34 +00:00
|
|
|
}
|
|
|
|
|
2002-09-02 22:06:26 +00:00
|
|
|
void File::clearIOFailed() {
|
2008-09-06 20:49:48 +00:00
|
|
|
if (_handle)
|
|
|
|
_handle->clearIOFailed();
|
2002-08-31 07:43:34 +00:00
|
|
|
}
|
|
|
|
|
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-09-08 11:11:32 +00:00
|
|
|
FilesystemNode node(filename);
|
|
|
|
return open(node);
|
2008-07-29 16:09:10 +00:00
|
|
|
}
|
|
|
|
|
2008-08-03 18:11:27 +00:00
|
|
|
bool DumpFile::open(const FilesystemNode &node) {
|
|
|
|
assert(!_handle);
|
|
|
|
|
2008-08-04 13:25:30 +00:00
|
|
|
if (node.isDirectory()) {
|
2008-08-03 18:11:27 +00:00
|
|
|
warning("File::open: Trying to open a FilesystemNode which is a directory");
|
|
|
|
return false;
|
2008-09-13 16:51:46 +00:00
|
|
|
}
|
2008-08-03 18:11:27 +00:00
|
|
|
|
2008-09-06 20:49:48 +00:00
|
|
|
_handle = node.openForWriting();
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DumpFile::ioFailed() const {
|
|
|
|
assert(_handle);
|
2008-09-06 20:49:48 +00:00
|
|
|
return _handle->ioFailed();
|
2008-07-29 16:09:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DumpFile::clearIOFailed() {
|
|
|
|
assert(_handle);
|
2008-09-06 20:49:48 +00:00
|
|
|
_handle->clearIOFailed();
|
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
|
|
|
}
|
|
|
|
|
2005-05-10 22:56:25 +00:00
|
|
|
} // End of namespace Common
|