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
|
|
|
|
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
|
|
|
Common::ArchivePtr dataArchive(new Common::FSDirectory(dir, level));
|
2008-09-27 23:00:46 +00:00
|
|
|
SearchMan.add(dir.getPath(), dataArchive);
|
2004-06-28 00:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void File::resetDefaultDirectories() {
|
2008-09-27 23:00:46 +00:00
|
|
|
SearchMan.clear();
|
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-27 23:00:46 +00:00
|
|
|
if (SearchMan.hasFile(filename)) {
|
2008-09-08 11:11:32 +00:00
|
|
|
debug(3, "Opening hashed: %s", filename.c_str());
|
2008-09-27 23:00:46 +00:00
|
|
|
_handle = SearchMan.openFile(filename);
|
|
|
|
} else if (SearchMan.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());
|
2008-09-29 10:32:06 +00:00
|
|
|
_handle = SearchMan.openFile(filename + ".");
|
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)
|
2008-09-29 10:32:06 +00:00
|
|
|
debug(2, "File::open: '%s' not found", filename.c_str());
|
2008-07-29 16:09:10 +00:00
|
|
|
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()) {
|
2008-09-15 12:37:18 +00:00
|
|
|
warning("File::open: FilesystemNode does not exist");
|
2006-07-22 17:01:50 +00:00
|
|
|
return false;
|
|
|
|
} else if (node.isDirectory()) {
|
2008-09-15 12:37:18 +00:00
|
|
|
warning("File::open: FilesystemNode is a directory");
|
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
|
|
|
|
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)
|
2008-09-29 10:32:06 +00:00
|
|
|
debug(2, "File::open: '%s' not found", node.getPath().c_str());
|
2008-07-29 16:09:10 +00:00
|
|
|
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-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-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-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-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-09-15 12:37:18 +00:00
|
|
|
warning("DumpFile::open: FilesystemNode 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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-09-14 22:28:53 +00:00
|
|
|
bool DumpFile::err() const {
|
2008-07-29 16:09:10 +00:00
|
|
|
assert(_handle);
|
2008-09-06 20:49:48 +00:00
|
|
|
return _handle->ioFailed();
|
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);
|
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
|