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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2003-08-01 12:21:04 +00:00
|
|
|
#include "common/file.h"
|
2006-06-24 08:07:48 +00:00
|
|
|
#include "common/fs.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-07-30 12:21:54 +00:00
|
|
|
#include "common/hash-str.h"
|
2007-07-20 19:42:38 +00:00
|
|
|
#include <errno.h>
|
2002-08-31 07:43:34 +00:00
|
|
|
|
2007-11-13 08:06:15 +00:00
|
|
|
#if defined(MACOSX) || defined(IPHONE)
|
2006-03-15 07:43:44 +00:00
|
|
|
#include "CoreFoundation/CoreFoundation.h"
|
|
|
|
#endif
|
|
|
|
|
2007-03-08 16:46:02 +00:00
|
|
|
#ifdef __PLAYSTATION2__
|
|
|
|
// for those replaced fopen/fread/etc functions
|
|
|
|
typedef unsigned long uint64;
|
|
|
|
typedef signed long int64;
|
|
|
|
#include "backends/platform/ps2/fileio.h"
|
|
|
|
|
|
|
|
#define fopen(a, b) ps2_fopen(a, b)
|
|
|
|
#define fclose(a) ps2_fclose(a)
|
|
|
|
#define fseek(a, b, c) ps2_fseek(a, b, c)
|
|
|
|
#define ftell(a) ps2_ftell(a)
|
|
|
|
#define feof(a) ps2_feof(a)
|
|
|
|
#define fread(a, b, c, d) ps2_fread(a, b, c, d)
|
|
|
|
#define fwrite(a, b, c, d) ps2_fwrite(a, b, c, d)
|
2007-03-08 17:36:52 +00:00
|
|
|
|
|
|
|
//#define fprintf ps2_fprintf // used in common/util.cpp
|
|
|
|
//#define fflush(a) ps2_fflush(a) // used in common/util.cpp
|
|
|
|
|
|
|
|
//#define fgetc(a) ps2_fgetc(a) // not used
|
|
|
|
//#define fgets(a, b, c) ps2_fgets(a, b, c) // not used
|
|
|
|
//#define fputc(a, b) ps2_fputc(a, b) // not used
|
|
|
|
//#define fputs(a, b) ps2_fputs(a, b) // not used
|
|
|
|
|
|
|
|
//#define fsize(a) ps2_fsize(a) // not used -- and it is not a standard function either
|
2007-03-08 16:46:02 +00:00
|
|
|
#endif
|
|
|
|
|
2007-03-08 16:57:14 +00:00
|
|
|
#ifdef __DS__
|
|
|
|
|
|
|
|
// These functions replease the standard library functions of the same name.
|
|
|
|
// As this header is included after the standard one, I have the chance to #define
|
|
|
|
// all of these to my own code.
|
|
|
|
//
|
|
|
|
// A #define is the only way, as redefinig the functions would cause linker errors.
|
2008-01-27 19:47:41 +00:00
|
|
|
|
|
|
|
// These functions need to be #undef'ed, as their original definition
|
2007-03-08 16:57:14 +00:00
|
|
|
// in devkitarm is done with #includes (ugh!)
|
|
|
|
#undef feof
|
|
|
|
#undef clearerr
|
2007-03-08 17:36:52 +00:00
|
|
|
//#undef getc
|
|
|
|
//#undef ferror
|
2007-10-13 15:44:56 +00:00
|
|
|
|
2008-01-18 17:24:34 +00:00
|
|
|
#include "backends/fs/ds/ds-fs.h"
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-03-08 17:36:52 +00:00
|
|
|
|
2008-01-28 00:14:17 +00:00
|
|
|
//void std_fprintf(FILE* handle, const char* fmt, ...); // used in common/util.cpp
|
|
|
|
//void std_fflush(FILE* handle); // used in common/util.cpp
|
2007-03-08 17:36:52 +00:00
|
|
|
|
2008-01-28 00:14:17 +00:00
|
|
|
//char* std_fgets(char* str, int size, FILE* file); // not used
|
|
|
|
//int std_getc(FILE* handle); // not used
|
|
|
|
//char* std_getcwd(char* dir, int dunno); // not used
|
|
|
|
//void std_cwd(char* dir); // not used
|
|
|
|
//int std_ferror(FILE* handle); // not used
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-03-08 16:57:14 +00:00
|
|
|
// Only functions used in the ScummVM source have been defined here!
|
2008-01-28 00:14:17 +00:00
|
|
|
#define fopen(name, mode) DS::std_fopen(name, mode)
|
|
|
|
#define fclose(handle) DS::std_fclose(handle)
|
2007-03-08 16:57:14 +00:00
|
|
|
#define fread(ptr, size, items, file) DS::std_fread(ptr, size, items, file)
|
|
|
|
#define fwrite(ptr, size, items, file) DS::std_fwrite(ptr, size, items, file)
|
|
|
|
#define feof(handle) DS::std_feof(handle)
|
|
|
|
#define ftell(handle) DS::std_ftell(handle)
|
|
|
|
#define fseek(handle, offset, whence) DS::std_fseek(handle, offset, whence)
|
|
|
|
#define clearerr(handle) DS::std_clearerr(handle)
|
2007-03-08 17:36:52 +00:00
|
|
|
|
2007-03-08 17:38:11 +00:00
|
|
|
//#define printf(fmt, ...) consolePrintf(fmt, ##__VA_ARGS__)
|
2007-03-08 17:36:52 +00:00
|
|
|
|
|
|
|
//#define fprintf(file, fmt, ...) { char str[128]; sprintf(str, fmt, ##__VA_ARGS__); DS::std_fwrite(str, strlen(str), 1, file); }
|
|
|
|
//#define fflush(file) DS::std_fflush(file) // used in common/util.cpp
|
|
|
|
|
|
|
|
//#define fgets(str, size, file) DS::std_fgets(str, size, file) // not used
|
|
|
|
//#define getc(handle) DS::std_getc(handle) // not used
|
|
|
|
//#define getcwd(dir, dunno) DS::std_getcwd(dir, dunno) // not used
|
2008-07-29 16:09:10 +00:00
|
|
|
#define ferror(handle) DS::std_ferror(handle)
|
2007-03-08 16:57:14 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2007-03-11 14:28:03 +00:00
|
|
|
#ifdef __SYMBIAN32__
|
|
|
|
#undef feof
|
|
|
|
#undef clearerr
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-03-11 14:28:03 +00:00
|
|
|
#define FILE void
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2008-01-28 00:14:17 +00:00
|
|
|
FILE* symbian_fopen(const char* name, const char* mode);
|
|
|
|
void symbian_fclose(FILE* handle);
|
|
|
|
size_t symbian_fread(const void* ptr, size_t size, size_t numItems, FILE* handle);
|
|
|
|
size_t symbian_fwrite(const void* ptr, size_t size, size_t numItems, FILE* handle);
|
|
|
|
bool symbian_feof(FILE* handle);
|
2007-03-11 14:28:03 +00:00
|
|
|
long int symbian_ftell(FILE* handle);
|
2008-01-28 00:14:17 +00:00
|
|
|
int symbian_fseek(FILE* handle, long int offset, int whence);
|
|
|
|
void symbian_clearerr(FILE* handle);
|
2007-03-11 14:28:03 +00:00
|
|
|
|
|
|
|
// Only functions used in the ScummVM source have been defined here!
|
2008-01-28 00:14:17 +00:00
|
|
|
#define fopen(name, mode) symbian_fopen(name, mode)
|
|
|
|
#define fclose(handle) symbian_fclose(handle)
|
2007-03-11 14:28:03 +00:00
|
|
|
#define fread(ptr, size, items, file) symbian_fread(ptr, size, items, file)
|
|
|
|
#define fwrite(ptr, size, items, file) symbian_fwrite(ptr, size, items, file)
|
|
|
|
#define feof(handle) symbian_feof(handle)
|
|
|
|
#define ftell(handle) symbian_ftell(handle)
|
|
|
|
#define fseek(handle, offset, whence) symbian_fseek(handle, offset, whence)
|
|
|
|
#define clearerr(handle) symbian_clearerr(handle)
|
|
|
|
#endif
|
2007-03-08 16:46:02 +00:00
|
|
|
|
2005-05-10 22:56:25 +00:00
|
|
|
namespace Common {
|
2003-09-17 21:06:16 +00:00
|
|
|
|
2008-06-11 06:00:56 +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;
|
2006-07-30 12:21:54 +00:00
|
|
|
static StringMap *_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;
|
2008-03-30 18:37:09 +00:00
|
|
|
String dirBuf(directory);
|
|
|
|
String fileBuf(filename);
|
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.
|
2008-03-30 18:37:09 +00:00
|
|
|
if (!dirBuf.empty()) {
|
|
|
|
const char c = dirBuf.lastChar();
|
2006-04-14 00:36:54 +00:00
|
|
|
if (c != ':' && c != '/' && c != '\\')
|
2008-03-30 18:37:09 +00:00
|
|
|
dirBuf += '/';
|
2002-09-15 19:28:34 +00:00
|
|
|
}
|
2004-07-18 17:08:10 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Append the filename to the path string
|
2008-03-30 18:37:09 +00:00
|
|
|
String pathBuf(dirBuf);
|
|
|
|
pathBuf += fileBuf;
|
2002-09-15 19:28:34 +00:00
|
|
|
|
2004-07-18 17:08:10 +00:00
|
|
|
//
|
|
|
|
// Try to open the file normally
|
|
|
|
//
|
2008-03-30 18:37:09 +00:00
|
|
|
file = fopen(pathBuf.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) {
|
2008-03-30 18:37:09 +00:00
|
|
|
fileBuf.toUppercase();
|
2008-04-07 20:38:52 +00:00
|
|
|
pathBuf = dirBuf + fileBuf;
|
2008-03-30 18:37:09 +00:00
|
|
|
file = fopen(pathBuf.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) {
|
2008-03-30 18:37:09 +00:00
|
|
|
fileBuf.toLowercase();
|
2008-04-07 20:38:52 +00:00
|
|
|
pathBuf = dirBuf + fileBuf;
|
2008-03-30 18:37:09 +00:00
|
|
|
file = fopen(pathBuf.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) {
|
2008-03-30 18:37:09 +00:00
|
|
|
fileBuf.toLowercase();
|
|
|
|
fileBuf.setChar(toupper(fileBuf[0]),0);
|
2008-04-07 20:38:52 +00:00
|
|
|
pathBuf = dirBuf + fileBuf;
|
2008-03-30 18:37:09 +00:00
|
|
|
file = fopen(pathBuf.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-05-01 21:06:34 +00:00
|
|
|
FilesystemNode dir(directory);
|
|
|
|
addDefaultDirectoryRecursive(dir, 1);
|
2006-03-25 04:17:17 +00:00
|
|
|
}
|
|
|
|
|
2006-05-13 18:14:23 +00:00
|
|
|
void File::addDefaultDirectoryRecursive(const String &directory, int level, const String &prefix) {
|
2006-05-01 21:06:34 +00:00
|
|
|
FilesystemNode dir(directory);
|
2006-05-13 18:14:23 +00:00
|
|
|
addDefaultDirectoryRecursive(dir, level, prefix);
|
2006-05-01 21:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void File::addDefaultDirectory(const FilesystemNode &directory) {
|
|
|
|
addDefaultDirectoryRecursive(directory, 1);
|
|
|
|
}
|
|
|
|
|
2006-05-13 18:14:23 +00:00
|
|
|
void File::addDefaultDirectoryRecursive(const FilesystemNode &dir, int level, const String &prefix) {
|
2006-04-01 17:36:43 +00:00
|
|
|
if (level <= 0)
|
2006-03-25 04:17:17 +00:00
|
|
|
return;
|
|
|
|
|
2006-05-03 20:43:26 +00:00
|
|
|
FSList fslist;
|
2007-06-05 21:02:35 +00:00
|
|
|
if (!dir.getChildren(fslist, FilesystemNode::kListAll)) {
|
2008-01-27 19:47:41 +00:00
|
|
|
// Failed listing the contents of this node, so it is either not a
|
2006-05-03 20:43:26 +00:00
|
|
|
// directory, or just doesn't exist at all.
|
2006-05-01 21:06:34 +00:00
|
|
|
return;
|
2006-05-03 20:43:26 +00:00
|
|
|
}
|
2006-05-01 21:06:34 +00:00
|
|
|
|
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.
|
2007-06-05 21:02:35 +00:00
|
|
|
const String &directory(dir.getPath());
|
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-04-11 22:29:51 +00:00
|
|
|
(*_defaultDirectories)[directory] = level;
|
|
|
|
|
|
|
|
if (!_filesMap)
|
2006-07-30 12:21:54 +00:00
|
|
|
_filesMap = new StringMap;
|
2006-03-25 04:17:17 +00:00
|
|
|
|
|
|
|
for (FSList::const_iterator file = fslist.begin(); file != fslist.end(); ++file) {
|
|
|
|
if (file->isDirectory()) {
|
2007-06-05 21:02:35 +00:00
|
|
|
addDefaultDirectoryRecursive(file->getPath(), level - 1, prefix + file->getName() + "/");
|
2006-03-25 04:17:17 +00:00
|
|
|
} else {
|
2006-05-13 18:14:23 +00:00
|
|
|
String lfn(prefix);
|
2007-06-05 21:02:35 +00:00
|
|
|
lfn += file->getName();
|
2006-03-25 04:17:17 +00:00
|
|
|
lfn.toLowercase();
|
2006-05-13 18:14:23 +00:00
|
|
|
if (!_filesMap->contains(lfn)) {
|
2007-06-05 21:02:35 +00:00
|
|
|
(*_filesMap)[lfn] = file->getPath();
|
2006-05-13 18:14:23 +00:00
|
|
|
}
|
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;
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2006-04-11 22:29:51 +00:00
|
|
|
_defaultDirectories = 0;
|
|
|
|
_filesMap = 0;
|
2003-09-17 21:06:16 +00:00
|
|
|
}
|
|
|
|
|
2004-06-28 22:34:22 +00:00
|
|
|
File::File()
|
2007-03-01 17:25:49 +00:00
|
|
|
: _handle(0), _ioFailed(false) {
|
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
|
|
|
|
2006-03-25 04:17:17 +00:00
|
|
|
String fname(filename);
|
|
|
|
fname.toLowercase();
|
|
|
|
|
2008-07-29 16:09:10 +00:00
|
|
|
if (_filesMap && _filesMap->contains(fname)) {
|
2006-04-11 22:29:51 +00:00
|
|
|
fname = (*_filesMap)[fname];
|
2006-10-14 03:15:35 +00:00
|
|
|
debug(3, "Opening hashed: %s", fname.c_str());
|
2008-07-29 16:09:10 +00:00
|
|
|
_handle = fopen(fname.c_str(), "rb");
|
2006-04-11 22:29:51 +00:00
|
|
|
} 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());
|
2008-07-29 16:09:10 +00:00
|
|
|
_handle = fopen(fname.c_str(), "rb");
|
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) {
|
2008-07-29 16:09:10 +00:00
|
|
|
_handle = fopenNoCase(filename, x->_key, "rb");
|
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)
|
2008-07-29 16:09:10 +00:00
|
|
|
_handle = fopenNoCase(filename, "", "rb");
|
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.
|
2007-11-13 08:06:15 +00:00
|
|
|
#if defined(MACOSX) || defined(IPHONE)
|
2006-03-15 09:41:22 +00:00
|
|
|
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)) {
|
2008-07-29 16:09:10 +00:00
|
|
|
_handle = fopen((char *)buf, "rb");
|
2006-03-15 09:41:22 +00:00
|
|
|
}
|
2006-06-11 02:23:17 +00:00
|
|
|
CFRelease(fileUrl);
|
2006-03-15 09:41:22 +00:00
|
|
|
}
|
2006-06-11 02:23:17 +00:00
|
|
|
CFRelease(cfFileName);
|
2006-03-15 09:41:22 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-09-02 22:06:26 +00:00
|
|
|
}
|
2004-06-28 00:06:31 +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;
|
2007-06-16 17:31:36 +00:00
|
|
|
} /*else if (!node.isReadable() && mode == kFileReadMode) {
|
|
|
|
warning("File::open: Trying to open an unreadable FilesystemNode object for reading");
|
|
|
|
return false;
|
|
|
|
} else if (!node.isWritable() && mode == kFileWriteMode) {
|
|
|
|
warning("File::open: Trying to open an unwritable FilesystemNode object for writing");
|
|
|
|
return false;
|
|
|
|
}*/
|
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-07-29 16:09:10 +00:00
|
|
|
_handle = fopen(node.getPath().c_str(), "rb");
|
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) {
|
2007-09-18 23:37:31 +00:00
|
|
|
// First try to find the file via a FilesystemNode (in case an absolute
|
2007-10-16 20:03:23 +00:00
|
|
|
// path was passed). This is only used to filter out directories.
|
2007-11-11 08:08:40 +00:00
|
|
|
FilesystemNode file(filename);
|
|
|
|
if (file.exists())
|
|
|
|
return !file.isDirectory();
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-10-16 20:03:23 +00:00
|
|
|
// See if the file is already mapped
|
2007-11-11 08:08:40 +00:00
|
|
|
if (_filesMap && _filesMap->contains(filename)) {
|
|
|
|
FilesystemNode file2((*_filesMap)[filename]);
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-11-11 08:08:40 +00:00
|
|
|
if (file2.exists())
|
|
|
|
return !file2.isDirectory();
|
2007-10-16 20:03:23 +00:00
|
|
|
}
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-10-16 20:03:23 +00:00
|
|
|
// Try all default directories
|
|
|
|
if (_defaultDirectories) {
|
|
|
|
StringIntMap::const_iterator i(_defaultDirectories->begin());
|
|
|
|
for (; i != _defaultDirectories->end(); ++i) {
|
2007-11-11 08:08:40 +00:00
|
|
|
FilesystemNode file2(i->_key + filename);
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-11-11 08:08:40 +00:00
|
|
|
if(file2.exists())
|
|
|
|
return !file2.isDirectory();
|
2007-10-16 20:03:23 +00:00
|
|
|
}
|
|
|
|
}
|
2008-01-27 19:47:41 +00:00
|
|
|
|
|
|
|
//Try opening the file inside the local directory as a last resort
|
2006-04-26 08:29:32 +00:00
|
|
|
File tmp;
|
2008-07-29 16:09:10 +00:00
|
|
|
return tmp.open(filename);
|
2004-11-27 15:09:53 +00:00
|
|
|
}
|
|
|
|
|
2002-08-31 07:43:34 +00:00
|
|
|
void File::close() {
|
|
|
|
if (_handle)
|
2007-03-08 16:46:02 +00:00
|
|
|
fclose((FILE *)_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?
|
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() {
|
2008-07-20 16:47:34 +00:00
|
|
|
// TODO/FIXME: Just use clearerr() here?
|
2002-09-02 22:06:26 +00:00
|
|
|
_ioFailed = false;
|
2002-08-31 07:43:34 +00:00
|
|
|
}
|
|
|
|
|
2005-01-09 01:41:43 +00:00
|
|
|
bool File::eof() const {
|
2008-07-29 16:09:10 +00:00
|
|
|
assert(_handle);
|
2002-08-31 07:43:34 +00:00
|
|
|
|
2007-03-08 16:46:02 +00:00
|
|
|
return feof((FILE *)_handle) != 0;
|
2002-08-31 07:43:34 +00:00
|
|
|
}
|
|
|
|
|
2005-01-09 01:41:43 +00:00
|
|
|
uint32 File::pos() const {
|
2008-07-29 16:09:10 +00:00
|
|
|
assert(_handle);
|
2002-08-31 07:43:34 +00:00
|
|
|
|
2007-03-08 16:46:02 +00:00
|
|
|
return ftell((FILE *)_handle);
|
2002-08-31 07:43:34 +00:00
|
|
|
}
|
|
|
|
|
2005-01-09 01:41:43 +00:00
|
|
|
uint32 File::size() const {
|
2008-07-29 16:09:10 +00:00
|
|
|
assert(_handle);
|
2002-09-13 18:02:34 +00:00
|
|
|
|
2007-03-08 16:46:02 +00:00
|
|
|
uint32 oldPos = ftell((FILE *)_handle);
|
|
|
|
fseek((FILE *)_handle, 0, SEEK_END);
|
|
|
|
uint32 length = ftell((FILE *)_handle);
|
|
|
|
fseek((FILE *)_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) {
|
2008-07-29 16:09:10 +00:00
|
|
|
assert(_handle);
|
2002-08-31 07:43:34 +00:00
|
|
|
|
2007-03-08 16:46:02 +00:00
|
|
|
if (fseek((FILE *)_handle, offs, whence) != 0)
|
|
|
|
clearerr((FILE *)_handle);
|
2002-08-31 07:43:34 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2008-07-29 16:09:10 +00:00
|
|
|
assert(_handle);
|
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
|
|
|
|
2007-03-08 16:46:02 +00:00
|
|
|
real_len = fread(ptr2, 1, len, (FILE *)_handle);
|
2003-07-03 07:03:18 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
String fname(filename);
|
|
|
|
fname.toLowercase();
|
|
|
|
|
|
|
|
_handle = fopenNoCase(filename, "", "wb");
|
|
|
|
|
|
|
|
if (_handle == NULL)
|
|
|
|
debug(2, "Failed to open '%s' for writing", filename.c_str());
|
|
|
|
|
|
|
|
return _handle != NULL;
|
|
|
|
}
|
|
|
|
|
2008-08-03 18:11:27 +00:00
|
|
|
bool DumpFile::open(const FilesystemNode &node) {
|
|
|
|
assert(!_handle);
|
|
|
|
|
|
|
|
if (!node.exists()) {
|
|
|
|
warning("File::open: Trying to open a FilesystemNode which does not exist");
|
|
|
|
return false;
|
|
|
|
} else if (node.isDirectory()) {
|
|
|
|
warning("File::open: Trying to open a FilesystemNode which is a directory");
|
|
|
|
return false;
|
|
|
|
} /*else if (!node.isReadable() && mode == kFileReadMode) {
|
|
|
|
warning("File::open: Trying to open an unreadable FilesystemNode object for reading");
|
|
|
|
return false;
|
|
|
|
} else if (!node.isWritable() && mode == kFileWriteMode) {
|
|
|
|
warning("File::open: Trying to open an unwritable FilesystemNode object for writing");
|
|
|
|
return false;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
_handle = fopen(node.getPath().c_str(), "rb");
|
|
|
|
|
|
|
|
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() {
|
|
|
|
if (_handle)
|
|
|
|
fclose((FILE *)_handle);
|
|
|
|
_handle = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DumpFile::isOpen() const {
|
|
|
|
return _handle != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DumpFile::ioFailed() const {
|
|
|
|
assert(_handle);
|
|
|
|
return ferror((FILE *)_handle) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DumpFile::clearIOFailed() {
|
|
|
|
assert(_handle);
|
|
|
|
clearerr((FILE *)_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DumpFile::eof() const {
|
|
|
|
assert(_handle);
|
|
|
|
return feof((FILE *)_handle) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 DumpFile::write(const void *ptr, uint32 len) {
|
|
|
|
assert(_handle);
|
2002-09-02 22:06:26 +00:00
|
|
|
|
2002-10-22 11:35:45 +00:00
|
|
|
if (len == 0)
|
2002-09-02 22:06:26 +00:00
|
|
|
return 0;
|
|
|
|
|
2008-07-29 16:09:10 +00:00
|
|
|
return (uint32)fwrite(ptr, 1, len, (FILE *)_handle);
|
2002-09-02 22:06:26 +00:00
|
|
|
}
|
2005-05-10 22:56:25 +00:00
|
|
|
|
2008-08-04 11:30:47 +00:00
|
|
|
void DumpFile::flush() {
|
|
|
|
assert(_handle);
|
|
|
|
// TODO: Should check the return value of fflush, and if it is non-zero,
|
|
|
|
// check errno and set an error flag.
|
|
|
|
fflush((FILE *)_handle);
|
|
|
|
}
|
|
|
|
|
2008-07-29 16:09:10 +00:00
|
|
|
|
2005-05-10 22:56:25 +00:00
|
|
|
} // End of namespace Common
|