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/fs.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/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 dir;
/*
// Try to use game specific savepath from config
dir = ConfMan.get("savepath");
@ -288,17 +280,6 @@ Common::String DefaultSaveFileManager::getSavePath() const {
#ifdef _WIN32_WCE
if (dir.empty())
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
return dir;

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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