move into File class

This commit is contained in:
Pawel Kolodziejski 2008-08-01 18:27:11 +00:00
parent 5a006d7ad9
commit 4af1c2d22b
7 changed files with 66 additions and 125 deletions

View file

@ -30,7 +30,7 @@
#include "common/debug.h" #include "common/debug.h"
#include "common/fs.h" #include "common/fs.h"
#include "common/file.h" #include "common/file.h"
//#include "common/config-manager.h" #include "common/config-manager.h"
#include "engine/backend/saves/default/default-saves.h" #include "engine/backend/saves/default/default-saves.h"
#include "engine/backend/saves/compressed/compressed-saves.h" #include "engine/backend/saves/compressed/compressed-saves.h"
@ -262,18 +262,10 @@ bool DefaultSaveFileManager::removeSavefile(const char *filename) {
} }
} }
#ifdef UNIX
#ifdef MACOSX
#define DEFAULT_SAVE_PATH "Documents/Residual Savegames"
#else
#define DEFAULT_SAVE_PATH ".residual"
#endif
#endif
Common::String DefaultSaveFileManager::getSavePath() const { Common::String DefaultSaveFileManager::getSavePath() const {
Common::String dir; Common::String dir;
/*
// Try to use game specific savepath from config // Try to use game specific savepath from config
dir = ConfMan.get("savepath"); dir = ConfMan.get("savepath");
@ -288,17 +280,6 @@ Common::String DefaultSaveFileManager::getSavePath() const {
#ifdef _WIN32_WCE #ifdef _WIN32_WCE
if (dir.empty()) if (dir.empty())
dir = ConfMan.get("path"); dir = ConfMan.get("path");
#endif
*/
#ifdef DEFAULT_SAVE_PATH
char savePath[MAXPATHLEN];
#if defined(UNIX)
const char *home = getenv("HOME");
if (home && *home && strlen(home) < MAXPATHLEN) {
snprintf(savePath, MAXPATHLEN, "%s/%s", home, DEFAULT_SAVE_PATH);
dir = savePath;
}
#endif
#endif #endif
return dir; return dir;

View file

@ -66,7 +66,7 @@ Engine::Engine() :
for (int i = 0; i < lastKey; i++) for (int i = 0; i < lastKey; i++)
_controlsEnabled[i] = false; _controlsEnabled[i] = false;
_speechMode = 3; // VOICE + TEXT _speechMode = 3; // VOICE + TEXT
_textSpeed = 6; _textSpeed = 7;
_mode = _previousMode = ENGINE_MODE_IDLE; _mode = _previousMode = ENGINE_MODE_IDLE;
_flipEnable = true; _flipEnable = true;
_lastUpdateTime = 0; _lastUpdateTime = 0;

View file

@ -37,12 +37,13 @@ bool Lab::open(const char *filename) {
_labFileName = filename; _labFileName = filename;
close(); close();
_f = std::fopen(filename, "rb"); _f = new Common::File();
if (!isOpen()) _f->open(filename);
if (!_f->isOpen())
return false; return false;
char header[16]; char header[16];
if (std::fread(header, 1, sizeof(header), _f) < sizeof(header)) { if (_f->read(header, sizeof(header)) < sizeof(header)) {
close(); close();
return false; return false;
} }
@ -55,13 +56,13 @@ bool Lab::open(const char *filename) {
int string_table_size = READ_LE_UINT32(header + 12); int string_table_size = READ_LE_UINT32(header + 12);
char *string_table = new char[string_table_size]; char *string_table = new char[string_table_size];
std::fseek(_f, 16 * (num_entries + 1), SEEK_SET); _f->seek(16 * (num_entries + 1), SEEK_SET);
std::fread(string_table, 1, string_table_size, _f); _f->read(string_table, string_table_size);
std::fseek(_f, 16, SEEK_SET); _f->seek(16, SEEK_SET);
char binary_entry[16]; char binary_entry[16];
for (int i = 0; i < num_entries; i++) { for (int i = 0; i < num_entries; i++) {
std::fread(binary_entry, 1, 16, _f); _f->read(binary_entry, 16);
int fname_offset = READ_LE_UINT32(binary_entry); int fname_offset = READ_LE_UINT32(binary_entry);
int start = READ_LE_UINT32(binary_entry + 4); int start = READ_LE_UINT32(binary_entry + 4);
int size = READ_LE_UINT32(binary_entry + 8); int size = READ_LE_UINT32(binary_entry + 8);
@ -86,13 +87,13 @@ Block *Lab::getFileBlock(const char *filename) const {
if (i == _fileMap.end()) if (i == _fileMap.end())
return NULL; return NULL;
std::fseek(_f, i->second.offset, SEEK_SET); _f->seek(i->second.offset, SEEK_SET);
// The sound decoder reads up to two bytes past the end of data // The sound decoder reads up to two bytes past the end of data
// (but shouldn't actually use those bytes). So allocate two extra bytes // (but shouldn't actually use those bytes). So allocate two extra bytes
// to be safe against crashes. // to be safe against crashes.
char *data = new char[i->second.len + 2]; char *data = new char[i->second.len + 2];
std::fread(data, 1, i->second.len, _f); _f->read(data, i->second.len);
data[i->second.len] = '\0'; // For valgrind cleanness data[i->second.len] = '\0'; // For valgrind cleanness
data[i->second.len + 1] = '\0'; data[i->second.len + 1] = '\0';
return new Block(data, i->second.len); return new Block(data, i->second.len);
@ -127,7 +128,7 @@ Lab::FileMapType::const_iterator Lab::findFilename(const char *filename) const {
void Lab::close() { void Lab::close() {
if (_f) if (_f)
std::fclose(_f); delete _f;
_f = NULL; _f = NULL;
_fileMap.clear(); _fileMap.clear();
} }

View file

@ -28,12 +28,9 @@
#include <string> #include <string>
#include <cstring> #include <cstring>
#include <cstdio>
#include <map> #include <map>
namespace Common { #include "common/file.h"
class File;
}
class Block { class Block {
public: public:
@ -55,7 +52,7 @@ public:
Lab() : _f(NULL) { } Lab() : _f(NULL) { }
explicit Lab(const char *filename) : _f(NULL) { open(filename); } explicit Lab(const char *filename) : _f(NULL) { open(filename); }
bool open(const char *filename); bool open(const char *filename);
bool isOpen() const { return _f != NULL; } bool isOpen() const { return _f->isOpen(); }
void close(); void close();
bool fileExists(const char *filename) const; bool fileExists(const char *filename) const;
Block *getFileBlock(const char *filename) const; Block *getFileBlock(const char *filename) const;
@ -71,7 +68,7 @@ private:
int offset, len; int offset, len;
}; };
std::FILE *_f; Common::File *_f;
typedef std::map<std::string, LabEntry> FileMapType; typedef std::map<std::string, LabEntry> FileMapType;
FileMapType _fileMap; FileMapType _fileMap;
std::string _labFileName; std::string _labFileName;

View file

@ -25,18 +25,18 @@
#include "common/sys.h" #include "common/sys.h"
#include "common/debug.h" #include "common/debug.h"
#include "common/file.h"
#include "engine/localize.h" #include "engine/localize.h"
#include "engine/registry.h" #include "engine/registry.h"
#include "engine/engine.h" #include "engine/engine.h"
#include <cstdio>
#include <cstring> #include <cstring>
Localizer *g_localizer = NULL; Localizer *g_localizer = NULL;
Localizer::Localizer() { Localizer::Localizer() {
std::FILE *f; Common::File f;
const char *namesToTry[] = { "/GRIM.TAB", "/Grim.tab", "/grim.tab" }; const char *namesToTry[] = { "/GRIM.TAB", "/Grim.tab", "/grim.tab" };
if (g_flags & GF_DEMO) if (g_flags & GF_DEMO)
@ -46,25 +46,22 @@ Localizer::Localizer() {
const char *datadir = g_registry->get("GrimDataDir", "."); const char *datadir = g_registry->get("GrimDataDir", ".");
std::string fname = (datadir != NULL ? datadir : "."); std::string fname = (datadir != NULL ? datadir : ".");
fname += namesToTry[i]; fname += namesToTry[i];
f = std::fopen(fname.c_str(), "rb"); f.open(fname.c_str());
if (f) if (f.isOpen())
break; break;
} }
if (!f) { if (!f.isOpen()) {
error("Localizer::Localizer: Unable to find localization information (grim.tab)!"); error("Localizer::Localizer: Unable to find localization information (grim.tab)!");
return; return;
} }
// Get the file size long filesize = f.size();
std::fseek(f, 0, SEEK_END);
long filesize = std::ftell(f);
std::fseek(f, 0, SEEK_SET);
// Read in the data // Read in the data
char *data = new char[filesize + 1]; char *data = new char[filesize + 1];
std::fread(data, 1, filesize, f); f.read(data, filesize);
data[filesize] = '\0'; data[filesize] = '\0';
std::fclose(f); f.close();
if (filesize < 4 || std::memcmp(data, "RCNE", 4) != 0) if (filesize < 4 || std::memcmp(data, "RCNE", 4) != 0)
error("Invalid magic reading grim.tab\n"); error("Invalid magic reading grim.tab\n");
@ -98,7 +95,7 @@ Localizer::Localizer() {
std::string Localizer::localize(const char *str) const { std::string Localizer::localize(const char *str) const {
assert(str); assert(str);
if ((str[0] != '/') || (str[0] == 0)) if (str[0] != '/' || str[0] == 0)
return str; return str;
const char *slash2 = std::strchr(str + 1, '/'); const char *slash2 = std::strchr(str + 1, '/');

View file

@ -34,6 +34,7 @@
#include "mixer/mixer.h" #include "mixer/mixer.h"
#include "engine/lua.h" #include "engine/lua.h"
#include "engine/cmd_line.h"
#include "engine/resource.h" #include "engine/resource.h"
#include "engine/actor.h" #include "engine/actor.h"
#include "engine/registry.h" #include "engine/registry.h"
@ -2137,14 +2138,6 @@ static void luaFileFindNext() {
} }
} }
#ifdef UNIX
#ifdef MACOSX
#define DEFAULT_SAVE_PATH "Documents/Residual Savegames"
#else
#define DEFAULT_SAVE_PATH ".residual"
#endif
#endif
static void luaFileFindFirst() { static void luaFileFindFirst() {
const char *path, *extension; const char *path, *extension;
lua_Object pathObj; lua_Object pathObj;
@ -2155,16 +2148,10 @@ static void luaFileFindFirst() {
FileFindDispose(); FileFindDispose();
if (lua_isnil(pathObj)) { if (lua_isnil(pathObj)) {
path = ""; path = ConfMan.get("savepath").c_str();
#ifdef DEFAULT_SAVE_PATH #ifdef _WIN32_WCE
#if defined(UNIX) if (path.empty())
char tmpPath[MAXPATHLEN]; path = ConfMan.get("path").c_str();
const char *home = getenv("HOME");
if (home && *home && strlen(home) < MAXPATHLEN) {
snprintf(tmpPath, MAXPATHLEN, "%s/%s", home, DEFAULT_SAVE_PATH);
path = tmpPath;
}
#endif
#endif #endif
} else } else
path = lua_getstring(pathObj); path = lua_getstring(pathObj);

View file

@ -25,6 +25,7 @@
#include "common/sys.h" #include "common/sys.h"
#include "common/debug.h" #include "common/debug.h"
#include "common/fs.h"
#include "engine/resource.h" #include "engine/resource.h"
#include "engine/registry.h" #include "engine/registry.h"
@ -55,71 +56,48 @@ ResourceLoader *g_resourceloader = NULL;
ResourceLoader::ResourceLoader() { ResourceLoader::ResourceLoader() {
const char *directory = g_registry->get("GrimDataDir", "."); const char *directory = g_registry->get("GrimDataDir", ".");
std::string dir_str = (directory != NULL ? directory : ".");
dir_str += '/';
int lab_counter = 0; int lab_counter = 0;
FSList *fslist;
FilesystemNode *fsdir;
#ifdef _WIN32 fslist = new FSList();
WIN32_FIND_DATAA find_file_data; fsdir = new FilesystemNode(directory);
std::string dir_strWin32 = dir_str + '*'; fsdir->lookupFile(*fslist, "*.lab", false, true, 0);
HANDLE d = FindFirstFile(dir_strWin32.c_str(), &find_file_data); if (fslist->empty())
#else error("Cannot find game data - check configuration file");
DIR *d = opendir(dir_str.c_str());
#endif
if (!directory) Lab *l;
error("Cannot find DataDir registry entry - check configuration file");
if (!d) for (FSList::const_iterator findfile = fslist->begin(); findfile != fslist->end(); ++findfile) {
error("Cannot open DataDir (%s)- check configuration file", dir_str.c_str()); Common::String filename(findfile->getName());
l = new Lab(findfile->getPath().c_str());
#ifdef _WIN32
do {
int namelen = strlen(find_file_data.cFileName);
if (namelen > 4 && ((stricmp(find_file_data.cFileName + namelen - 4, ".lab") == 0) || (stricmp(find_file_data.cFileName + namelen - 4, ".mus") == 0))) {
std::string fullname = dir_str + find_file_data.cFileName;
Lab *l = new Lab(fullname.c_str());
lab_counter++;
if (l->isOpen()) { if (l->isOpen()) {
if (strstr(find_file_data.cFileName, "005")) if (filename == "005.lab")
_labs.push_front(l); _labs.push_front(l);
else { else {
if (strstr(find_file_data.cFileName, "gfdemo01")) if (filename == "gfdemo01.lab")
g_flags |= GF_DEMO; g_flags |= GF_DEMO;
_labs.push_back(l); _labs.push_back(l);
} }
} else
delete l;
}
} while (FindNextFile(d, &find_file_data));
FindClose(d);
#else
dirent *de;
while ((de = readdir(d))) {
int namelen = strlen(de->d_name);
if (namelen > 4 && ((strcasecmp(de->d_name + namelen - 4, ".lab") == 0) || (strcasecmp(de->d_name + namelen - 4, ".mus") == 0))) {
std::string fullname = dir_str + de->d_name;
Lab *l = new Lab(fullname.c_str());
lab_counter++; lab_counter++;
if (l->isOpen()) } else {
// Handle the Grim 1.1 patch's datafile
if (strstr(de->d_name, "005"))
_labs.push_front(l);
else {
if (strstr(de->d_name, "gfdemo01"))
g_flags |= GF_DEMO;
_labs.push_back(l);
}
else
delete l; delete l;
} }
} }
closedir(d); fsdir->lookupFile(*fslist, "*.mus", false, true, 0);
#endif for (FSList::const_iterator findfile = fslist->begin(); findfile != fslist->end(); ++findfile) {
Common::String filename(findfile->getName());
if (lab_counter == 0) l = new Lab(filename.c_str());
error("Cannot find any resource files in %s - check configuration file", dir_str.c_str()); if (l->isOpen()) {
_labs.push_back(l);
lab_counter++;
} else {
delete l;
}
}
delete fsdir;
delete fslist;
} }
ResourceLoader::~ResourceLoader() { ResourceLoader::~ResourceLoader() {