2002-08-31 07:43:34 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2006-01-18 17:39:49 +00:00
|
|
|
* Copyright (C) 2002-2006 The ScummVM project
|
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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2003-08-01 12:21:04 +00:00
|
|
|
#include "common/file.h"
|
2006-03-28 10:05:25 +00:00
|
|
|
#include "common/hashmap.h"
|
2003-08-01 12:21:04 +00:00
|
|
|
#include "common/util.h"
|
2006-03-25 04:17:17 +00:00
|
|
|
#include "backends/fs/fs.h"
|
2002-08-31 07:43:34 +00:00
|
|
|
|
2006-03-15 07:43:44 +00:00
|
|
|
#ifdef MACOSX
|
|
|
|
#include "CoreFoundation/CoreFoundation.h"
|
|
|
|
#endif
|
|
|
|
|
2005-05-10 22:56:25 +00:00
|
|
|
namespace Common {
|
2003-09-17 21:06:16 +00:00
|
|
|
|
2006-03-28 10:05:25 +00:00
|
|
|
typedef HashMap<String, String> FilesMap;
|
2006-04-01 17:36:43 +00:00
|
|
|
typedef HashMap<String, int> StringIntMap;
|
2006-03-28 10:05:25 +00:00
|
|
|
|
|
|
|
// The following two objects could be turned into static members of class
|
|
|
|
// File. However, then we would be forced to #include hashmap in file.h
|
|
|
|
// which seems to be a high price just for a simple beautification...
|
2006-04-11 22:29:51 +00:00
|
|
|
static StringIntMap *_defaultDirectories;
|
|
|
|
static FilesMap *_filesMap;
|
2003-09-17 21:06:16 +00:00
|
|
|
|
2006-04-14 00:36:54 +00:00
|
|
|
static FILE *fopenNoCase(const String &filename, const String &directory, const char *mode) {
|
2002-09-13 18:02:34 +00:00
|
|
|
FILE *file;
|
2006-04-14 00:36:54 +00:00
|
|
|
String buf(directory);
|
|
|
|
uint i;
|
2003-06-14 18:20:56 +00:00
|
|
|
|
2005-09-03 16:12:52 +00:00
|
|
|
#if !defined(__GP32__) && !defined(PALMOS_MODE)
|
2004-07-18 17:08:10 +00:00
|
|
|
// Add a trailing slash, if necessary.
|
2006-04-14 00:36:54 +00:00
|
|
|
if (!buf.empty()) {
|
|
|
|
const char c = buf.lastChar();
|
|
|
|
if (c != ':' && c != '/' && c != '\\')
|
|
|
|
buf += '/';
|
2002-09-15 19:28:34 +00:00
|
|
|
}
|
2004-07-18 17:08:10 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Append the filename to the path string
|
2006-04-14 00:36:54 +00:00
|
|
|
const int offsetToFileName = buf.size();
|
|
|
|
buf += filename;
|
2002-09-15 19:28:34 +00:00
|
|
|
|
2004-07-18 17:08:10 +00:00
|
|
|
//
|
|
|
|
// Try to open the file normally
|
|
|
|
//
|
2006-04-14 00:36:54 +00:00
|
|
|
file = fopen(buf.c_str(), mode);
|
2004-06-28 00:06:31 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Try again, with file name converted to upper case
|
|
|
|
//
|
2004-07-18 17:08:10 +00:00
|
|
|
if (!file) {
|
2006-04-14 00:36:54 +00:00
|
|
|
for (i = offsetToFileName; i < buf.size(); ++i) {
|
|
|
|
buf[i] = toupper(buf[i]);
|
2004-07-18 17:08:10 +00:00
|
|
|
}
|
2006-04-14 00:36:54 +00:00
|
|
|
file = fopen(buf.c_str(), mode);
|
2004-06-28 00:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Try again, with file name converted to lower case
|
|
|
|
//
|
2004-07-18 17:08:10 +00:00
|
|
|
if (!file) {
|
2006-04-14 00:36:54 +00:00
|
|
|
for (i = offsetToFileName; i < buf.size(); ++i) {
|
|
|
|
buf[i] = tolower(buf[i]);
|
2004-07-18 17:08:10 +00:00
|
|
|
}
|
2006-04-14 00:36:54 +00:00
|
|
|
file = fopen(buf.c_str(), mode);
|
2004-06-28 00:06:31 +00:00
|
|
|
}
|
2002-09-13 18:02:34 +00:00
|
|
|
|
2004-11-14 21:10:50 +00:00
|
|
|
//
|
|
|
|
// Try again, with file name capitalized
|
|
|
|
//
|
|
|
|
if (!file) {
|
2006-04-14 00:36:54 +00:00
|
|
|
i = offsetToFileName;
|
|
|
|
buf[i] = toupper(buf[i]);
|
|
|
|
file = fopen(buf.c_str(), mode);
|
2004-11-14 21:10:50 +00:00
|
|
|
}
|
|
|
|
|
2005-05-09 21:21:21 +00:00
|
|
|
#ifdef __amigaos4__
|
|
|
|
//
|
|
|
|
// Work around for possibility that someone uses AmigaOS "newlib" build with SmartFileSystem (blocksize 512 bytes), leading
|
|
|
|
// to buffer size being only 512 bytes. "Clib2" sets the buffer size to 8KB, resulting smooth movie playback. This forces the buffer
|
|
|
|
// to be enough also when using "newlib" compile on SFS.
|
|
|
|
//
|
|
|
|
if (file) {
|
|
|
|
setvbuf(file, NULL, _IOFBF, 8192);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-07-01 12:18:28 +00:00
|
|
|
return file;
|
2002-09-13 18:02:34 +00:00
|
|
|
}
|
|
|
|
|
2006-03-25 19:30:10 +00:00
|
|
|
void File::addDefaultDirectory(const String &directory) {
|
2006-04-01 17:36:43 +00:00
|
|
|
addDefaultDirectoryRecursive(directory, 1);
|
2006-03-25 04:17:17 +00:00
|
|
|
}
|
|
|
|
|
2006-04-01 17:36:43 +00:00
|
|
|
void File::addDefaultDirectoryRecursive(const String &directory, int level) {
|
|
|
|
if (level <= 0)
|
2006-03-25 04:17:17 +00:00
|
|
|
return;
|
|
|
|
|
2006-04-11 22:29:51 +00:00
|
|
|
if (!_defaultDirectories)
|
|
|
|
_defaultDirectories = new StringIntMap;
|
|
|
|
|
2006-04-01 17:36:43 +00:00
|
|
|
// Do not add directories multiple times, unless this time they are added
|
|
|
|
// with a bigger depth.
|
2006-04-11 22:29:51 +00:00
|
|
|
if (_defaultDirectories->contains(directory) && (*_defaultDirectories)[directory] >= level)
|
2006-04-01 17:36:43 +00:00
|
|
|
return;
|
2006-03-25 04:17:17 +00:00
|
|
|
|
|
|
|
FilesystemNode dir(directory.c_str());
|
|
|
|
|
2006-04-01 17:36:43 +00:00
|
|
|
// ... and abort if this isn't a directory!
|
2006-03-25 04:17:17 +00:00
|
|
|
if (!dir.isDirectory())
|
|
|
|
return;
|
|
|
|
|
2006-04-11 22:29:51 +00:00
|
|
|
(*_defaultDirectories)[directory] = level;
|
|
|
|
|
|
|
|
if (!_filesMap)
|
|
|
|
_filesMap = new FilesMap;
|
2006-03-25 04:17:17 +00:00
|
|
|
|
2006-04-04 21:18:58 +00:00
|
|
|
const FSList fslist(dir.listDir(FilesystemNode::kListAllNoRoot));
|
2006-03-25 04:17:17 +00:00
|
|
|
|
|
|
|
for (FSList::const_iterator file = fslist.begin(); file != fslist.end(); ++file) {
|
|
|
|
if (file->isDirectory()) {
|
2006-04-01 17:36:43 +00:00
|
|
|
addDefaultDirectoryRecursive(file->path(), level - 1);
|
2006-03-25 04:17:17 +00:00
|
|
|
} else {
|
2006-04-01 17:36:43 +00:00
|
|
|
String lfn = file->displayName();
|
2006-03-25 04:17:17 +00:00
|
|
|
lfn.toLowercase();
|
2006-04-11 22:29:51 +00:00
|
|
|
if (!_filesMap->contains(lfn))
|
|
|
|
(*_filesMap)[lfn] = file->path();
|
2006-03-25 04:17:17 +00:00
|
|
|
}
|
|
|
|
}
|
2004-06-28 00:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void File::resetDefaultDirectories() {
|
2006-04-11 22:29:51 +00:00
|
|
|
delete _defaultDirectories;
|
|
|
|
delete _filesMap;
|
|
|
|
|
|
|
|
_defaultDirectories = 0;
|
|
|
|
_filesMap = 0;
|
2003-09-17 21:06:16 +00:00
|
|
|
}
|
|
|
|
|
2004-06-28 22:34:22 +00:00
|
|
|
File::File()
|
2006-03-25 04:17:17 +00:00
|
|
|
: _handle(0), _ioFailed(false), _refcount(1) {
|
2002-08-31 07:43:34 +00:00
|
|
|
}
|
|
|
|
|
2004-06-28 22:34:22 +00:00
|
|
|
//#define DEBUG_FILE_REFCOUNT
|
|
|
|
|
2002-08-31 07:43:34 +00:00
|
|
|
File::~File() {
|
2004-06-28 22:34:22 +00:00
|
|
|
#ifdef DEBUG_FILE_REFCOUNT
|
2004-11-27 15:09:53 +00:00
|
|
|
warning("File::~File on file '%s'", _name.c_str());
|
2004-06-28 22:34:22 +00:00
|
|
|
#endif
|
2002-08-31 07:43:34 +00:00
|
|
|
close();
|
|
|
|
}
|
2004-06-28 22:34:22 +00:00
|
|
|
void File::incRef() {
|
|
|
|
#ifdef DEBUG_FILE_REFCOUNT
|
2004-11-27 15:09:53 +00:00
|
|
|
warning("File::incRef on file '%s'", _name.c_str());
|
2004-06-28 22:34:22 +00:00
|
|
|
#endif
|
|
|
|
_refcount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void File::decRef() {
|
|
|
|
#ifdef DEBUG_FILE_REFCOUNT
|
2004-11-27 15:09:53 +00:00
|
|
|
warning("File::decRef on file '%s'", _name.c_str());
|
2004-06-28 22:34:22 +00:00
|
|
|
#endif
|
|
|
|
if (--_refcount == 0) {
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-31 07:43:34 +00:00
|
|
|
|
2006-04-14 01:48:51 +00:00
|
|
|
bool File::open(const String &filename, AccessMode mode, const char *directory) {
|
2004-07-31 09:34:10 +00:00
|
|
|
assert(mode == kFileReadMode || mode == kFileWriteMode);
|
|
|
|
|
2006-04-14 01:48:51 +00:00
|
|
|
if (filename.empty()) {
|
2006-04-23 17:02:39 +00:00
|
|
|
error("File::open: No filename was specified");
|
2002-08-31 07:43:34 +00:00
|
|
|
}
|
|
|
|
|
2006-04-14 00:36:54 +00:00
|
|
|
if (_handle) {
|
2006-04-14 01:48:51 +00:00
|
|
|
error("File::open: This file object already is opened (%s), won't open '%s'", _name.c_str(), filename.c_str());
|
2004-06-28 00:06:31 +00:00
|
|
|
}
|
2002-11-02 10:51:32 +00:00
|
|
|
|
2002-09-02 22:06:26 +00:00
|
|
|
clearIOFailed();
|
2002-09-02 20:53:12 +00:00
|
|
|
|
2006-03-25 04:17:17 +00:00
|
|
|
String fname(filename);
|
|
|
|
fname.toLowercase();
|
|
|
|
|
2004-06-28 00:06:31 +00:00
|
|
|
const char *modeStr = (mode == kFileReadMode) ? "rb" : "wb";
|
2004-11-07 17:20:32 +00:00
|
|
|
if (mode == kFileWriteMode || directory) {
|
2006-04-14 00:36:54 +00:00
|
|
|
String dir(directory ? directory : "");
|
|
|
|
_handle = fopenNoCase(filename, dir, modeStr);
|
2006-04-11 22:29:51 +00:00
|
|
|
} else if (_filesMap && _filesMap->contains(fname)) {
|
|
|
|
fname = (*_filesMap)[fname];
|
|
|
|
debug(3, "Opening hashed: %s", fname.c_str());
|
|
|
|
_handle = fopen(fname.c_str(), modeStr);
|
|
|
|
} else if (_filesMap && _filesMap->contains(fname + ".")) {
|
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)
|
2006-04-12 08:10:49 +00:00
|
|
|
fname = (*_filesMap)[fname + "."];
|
2006-04-11 22:29:51 +00:00
|
|
|
debug(3, "Opening hashed: %s", fname.c_str());
|
|
|
|
_handle = fopen(fname.c_str(), modeStr);
|
2004-06-28 00:06:31 +00:00
|
|
|
} else {
|
2006-03-25 04:17:17 +00:00
|
|
|
|
2006-04-11 22:29:51 +00:00
|
|
|
if (_defaultDirectories) {
|
|
|
|
// Try all default directories
|
|
|
|
StringIntMap::const_iterator x(_defaultDirectories->begin());
|
|
|
|
for (; _handle == NULL && x != _defaultDirectories->end(); ++x) {
|
2006-04-14 00:36:54 +00:00
|
|
|
_handle = fopenNoCase(filename, x->_key, modeStr);
|
2006-04-11 22:29:51 +00:00
|
|
|
}
|
2002-08-31 13:29:10 +00:00
|
|
|
}
|
2006-04-11 22:29:51 +00:00
|
|
|
|
2004-08-05 11:10:27 +00:00
|
|
|
// Last resort: try the current directory
|
|
|
|
if (_handle == NULL)
|
|
|
|
_handle = fopenNoCase(filename, "", modeStr);
|
2006-03-15 09:41:22 +00:00
|
|
|
|
2006-04-14 00:36:54 +00:00
|
|
|
// Last last (really) resort: try looking inside the application bundle on Mac OS X for the lowercase file.
|
2006-03-15 09:41:22 +00:00
|
|
|
#ifdef MACOSX
|
|
|
|
if (!_handle) {
|
2006-04-14 00:36:54 +00:00
|
|
|
CFStringRef cfFileName = CFStringCreateWithBytes(NULL, (const UInt8 *)filename.c_str(), filename.size(), kCFStringEncodingASCII, false);
|
2006-03-15 09:41:22 +00:00
|
|
|
CFURLRef fileUrl = CFBundleCopyResourceURL(CFBundleGetMainBundle(), cfFileName, NULL, NULL);
|
|
|
|
if (fileUrl) {
|
|
|
|
UInt8 buf[256];
|
|
|
|
if (CFURLGetFileSystemRepresentation(fileUrl, false, (UInt8 *)buf, 256)) {
|
|
|
|
_handle = fopen((char *)buf, modeStr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-09-02 22:06:26 +00:00
|
|
|
}
|
2004-06-28 00:06:31 +00:00
|
|
|
|
|
|
|
if (_handle == NULL) {
|
|
|
|
if (mode == kFileReadMode)
|
2006-04-14 00:36:54 +00:00
|
|
|
debug(2, "File %s not found", filename.c_str());
|
2004-06-28 00:06:31 +00:00
|
|
|
else
|
2006-04-14 00:36:54 +00:00
|
|
|
debug(2, "File %s not opened", filename.c_str());
|
2002-08-31 07:43:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-11-27 15:09:53 +00:00
|
|
|
|
|
|
|
_name = filename;
|
2002-09-10 07:34:27 +00:00
|
|
|
|
2004-06-28 22:34:22 +00:00
|
|
|
#ifdef DEBUG_FILE_REFCOUNT
|
2004-11-27 15:09:53 +00:00
|
|
|
warning("File::open on file '%s'", _name.c_str());
|
2004-06-28 22:34:22 +00:00
|
|
|
#endif
|
|
|
|
|
2002-08-31 07:43:34 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-04-14 01:48:51 +00:00
|
|
|
bool File::exists(const String &filename, const char *directory) {
|
2004-11-27 15:09:53 +00:00
|
|
|
// FIXME: Ugly ugly hack!
|
|
|
|
File tmp;
|
|
|
|
return tmp.open(filename, kFileReadMode, directory);
|
|
|
|
}
|
|
|
|
|
2002-08-31 07:43:34 +00:00
|
|
|
void File::close() {
|
|
|
|
if (_handle)
|
|
|
|
fclose(_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 {
|
2002-09-02 22:06:26 +00:00
|
|
|
return _ioFailed != 0;
|
2002-08-31 07:43:34 +00:00
|
|
|
}
|
|
|
|
|
2002-09-02 22:06:26 +00:00
|
|
|
void File::clearIOFailed() {
|
|
|
|
_ioFailed = false;
|
2002-08-31 07:43:34 +00:00
|
|
|
}
|
|
|
|
|
2005-01-09 01:41:43 +00:00
|
|
|
bool File::eof() const {
|
2002-08-31 07:43:34 +00:00
|
|
|
if (_handle == NULL) {
|
2004-07-26 22:53:29 +00:00
|
|
|
error("File::eof: File is not open!");
|
2002-08-31 07:43:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return feof(_handle) != 0;
|
|
|
|
}
|
|
|
|
|
2005-01-09 01:41:43 +00:00
|
|
|
uint32 File::pos() const {
|
2002-08-31 07:43:34 +00:00
|
|
|
if (_handle == NULL) {
|
2004-07-26 22:53:29 +00:00
|
|
|
error("File::pos: File is not open!");
|
2002-08-31 07:43:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ftell(_handle);
|
|
|
|
}
|
|
|
|
|
2005-01-09 01:41:43 +00:00
|
|
|
uint32 File::size() const {
|
2002-09-13 18:02:34 +00:00
|
|
|
if (_handle == NULL) {
|
2004-07-26 22:53:29 +00:00
|
|
|
error("File::size: File is not open!");
|
2002-09-13 18:02:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 oldPos = ftell(_handle);
|
|
|
|
fseek(_handle, 0, SEEK_END);
|
|
|
|
uint32 length = ftell(_handle);
|
|
|
|
fseek(_handle, oldPos, SEEK_SET);
|
2003-03-06 16:27:06 +00:00
|
|
|
|
2002-09-13 18:02:34 +00:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2002-09-13 12:16:03 +00:00
|
|
|
void File::seek(int32 offs, int whence) {
|
2002-08-31 07:43:34 +00:00
|
|
|
if (_handle == NULL) {
|
2004-07-26 22:53:29 +00:00
|
|
|
error("File::seek: File is not open!");
|
2002-08-31 07:43:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fseek(_handle, offs, whence) != 0)
|
|
|
|
clearerr(_handle);
|
|
|
|
}
|
|
|
|
|
2002-10-22 11:34:21 +00:00
|
|
|
uint32 File::read(void *ptr, uint32 len) {
|
2002-08-31 07:43:34 +00:00
|
|
|
byte *ptr2 = (byte *)ptr;
|
2003-07-03 07:03:18 +00:00
|
|
|
uint32 real_len;
|
2002-08-31 07:43:34 +00:00
|
|
|
|
|
|
|
if (_handle == NULL) {
|
2004-07-26 22:53:29 +00:00
|
|
|
error("File::read: File is not open!");
|
2002-09-02 20:15:14 +00:00
|
|
|
return 0;
|
2002-08-31 07:43:34 +00:00
|
|
|
}
|
|
|
|
|
2002-10-22 11:34:21 +00:00
|
|
|
if (len == 0)
|
2002-09-02 20:15:14 +00:00
|
|
|
return 0;
|
2002-08-31 07:43:34 +00:00
|
|
|
|
2003-07-03 07:03:18 +00:00
|
|
|
real_len = fread(ptr2, 1, len, _handle);
|
|
|
|
if (real_len < len) {
|
2002-09-02 22:06:26 +00:00
|
|
|
_ioFailed = true;
|
2002-08-31 07:43:34 +00:00
|
|
|
}
|
|
|
|
|
2003-07-03 07:03:18 +00:00
|
|
|
return real_len;
|
2002-08-31 07:43:34 +00:00
|
|
|
}
|
|
|
|
|
2003-05-28 19:03:12 +00:00
|
|
|
uint32 File::write(const void *ptr, uint32 len) {
|
2002-09-02 22:06:26 +00:00
|
|
|
if (_handle == NULL) {
|
2004-07-26 22:53:29 +00:00
|
|
|
error("File::write: File is not open!");
|
2002-09-02 22:06:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-10-22 11:35:45 +00:00
|
|
|
if (len == 0)
|
2002-09-02 22:06:26 +00:00
|
|
|
return 0;
|
|
|
|
|
2003-05-28 19:03:12 +00:00
|
|
|
if ((uint32)fwrite(ptr, 1, len, _handle) != len) {
|
2002-09-02 22:06:26 +00:00
|
|
|
_ioFailed = true;
|
|
|
|
}
|
|
|
|
|
2002-10-22 11:35:45 +00:00
|
|
|
return len;
|
2002-09-02 22:06:26 +00:00
|
|
|
}
|
2005-05-10 22:56:25 +00:00
|
|
|
|
|
|
|
} // End of namespace Common
|