From d3477eedc113a2c126f36f41384b8921d610906a Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Fri, 8 Feb 2008 19:24:12 +0000 Subject: updated filesystem, removed inifile, updated game and tga loader minor cleanups --- src/filesystem/Makefile.am | 4 +- src/filesystem/diskfile.cc | 95 ++++++++++++++++++++++++++++++++++++++++++++ src/filesystem/diskfile.h | 43 ++++++++++++++++++++ src/filesystem/file.cc | 58 +-------------------------- src/filesystem/file.h | 43 +++++++++++--------- src/filesystem/filesystem.cc | 59 ++++++++++++++++++++++----- src/filesystem/filesystem.h | 26 ++++++++---- src/filesystem/path.cc | 33 --------------- src/filesystem/path.h | 26 ------------ src/filesystem/vfile.cc | 24 ----------- src/filesystem/vfile.h | 31 --------------- 11 files changed, 233 insertions(+), 209 deletions(-) create mode 100644 src/filesystem/diskfile.cc create mode 100644 src/filesystem/diskfile.h delete mode 100644 src/filesystem/path.cc delete mode 100644 src/filesystem/path.h delete mode 100644 src/filesystem/vfile.cc delete mode 100644 src/filesystem/vfile.h (limited to 'src/filesystem') diff --git a/src/filesystem/Makefile.am b/src/filesystem/Makefile.am index 65540f1..20cd25a 100644 --- a/src/filesystem/Makefile.am +++ b/src/filesystem/Makefile.am @@ -1,9 +1,9 @@ METASOURCES = AUTO -libfilesystem_la_SOURCES = file.cc filesystem.cc inifile.cc path.cc vfile.cc +libfilesystem_la_SOURCES = diskfile.cc file.cc filesystem.cc libfilesystem_la_LDFLAGS = -avoid-version -no-undefined libfilesystem_la_LIBADD = $(top_builddir)/src/sys/libsys.la noinst_LTLIBRARIES = libfilesystem.la -noinst_HEADERS = file.h filesystem.h inifile.h path.h vfile.h +noinst_HEADERS = diskfile.h file.h filesystem.h INCLUDES = -I$(top_srcdir)/src diff --git a/src/filesystem/diskfile.cc b/src/filesystem/diskfile.cc new file mode 100644 index 0000000..85f6ccc --- /dev/null +++ b/src/filesystem/diskfile.cc @@ -0,0 +1,95 @@ +/* + filesystem/file.c + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include "filesystem/diskfile.h" + +namespace filesystem { + +DiskFile::DiskFile() +{ + diskfile_handle = 0; +} + +DiskFile::~DiskFile() +{ + if (diskfile_handle) + fclose(diskfile_handle); +} + +bool DiskFile::open(const char *filename) +{ + if (diskfile_handle) { + con_warn << file_name << " already open!" << std::endl; + return false; + } + + file_name.assign(filename); + std::string fn; + + // if moddir is set, try the mods subdir first + if (moddir.size()) { + // try homedir + moddir + file_path = homedir; + file_path.append(moddir); + fn = file_path; + fn.append(filename); + diskfile_handle = fopen(fn.c_str(), "r"); + if (diskfile_handle) + return true; + + // try datadir + moddir + file_path = datadir; + file_path.append(moddir); + fn = file_path; + fn.append(filename); + diskfile_handle = fopen(fn.c_str(), "r"); + if (diskfile_handle) + return true; + } + // try homedir + basedir + file_path = homedir; + file_path.append(basedir); + fn = file_path; + fn.append(filename); + diskfile_handle = fopen(fn.c_str(), "r"); + if (diskfile_handle) + return true; + + // try datadir + basedir + file_path = datadir; + file_path.append(basedir); + fn = file_path; + fn.append(filename); + diskfile_handle = fopen(fn.c_str(), "r"); + if (diskfile_handle) + return true; + + con_warn << "Could not open " << filename << std::endl; + return false; +} + +void DiskFile::close() +{ + if (diskfile_handle) + fclose(diskfile_handle); + diskfile_handle = 0; +} + +size_t DiskFile::read(void *buffer, const size_t count) +{ + if (!diskfile_handle) + return 0; + + return (fread(buffer, count, 1, diskfile_handle)); +} + +void DiskFile::skip(size_t count) +{ + fseek(diskfile_handle, (long) count, SEEK_CUR); +} + +} // namespace filesystem + diff --git a/src/filesystem/diskfile.h b/src/filesystem/diskfile.h new file mode 100644 index 0000000..b71576d --- /dev/null +++ b/src/filesystem/diskfile.h @@ -0,0 +1,43 @@ +/* + filesystem/diskfile.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_FILESYSTEM_DISKFILE_H__ +#define __INCLUDED_FILESYSTEM_DISKFILE_H__ + +#include "filesystem/filesystem.h" +#include "sys/sys.h" + +#include + +#include + +namespace filesystem +{ + +/** + * @brief implementation of File for a file on disk + */ +class DiskFile : public File +{ +public: + DiskFile(); + virtual ~DiskFile(); + + virtual bool open(const char * filename); + virtual void close(); + + virtual size_t read(void *buffer, size_t count); + + void skip(size_t count); + +private: + FILE *diskfile_handle; +}; + +} // namespace filesystem + +#endif // __INCLUDED_FILESYSTEM_DISKFILE_H__ + diff --git a/src/filesystem/file.cc b/src/filesystem/file.cc index f05e60c..07f8870 100644 --- a/src/filesystem/file.cc +++ b/src/filesystem/file.cc @@ -5,8 +5,7 @@ */ // project headers -#include "sys/sys.h" -#include "filesystem/filesystem.h" +#include "filesystem/file.h" namespace filesystem { @@ -14,60 +13,5 @@ File::File() {} File::~File() {} -void File::open(const char * filename, ios_base::openmode mode) { - file_name.assign(filename); - std::string fn; - - real_name.clear(); - - // if moddir is set, try the mods subdir first - if (moddir.size()) { - // try homedir + moddir - fn = homedir; - fn.append(moddir); - fn.append(filename); - std::ifstream::open(fn.c_str(), mode); - if (this->is_open()) { - real_name = fn; - con_debug << "File opened " << fn << std::endl; - return; - } - - // try datadir + moddir - fn = datadir; - fn.append(moddir); - std::ifstream::open(fn.c_str(), mode); - if (this->is_open()) { - con_debug << "File opened " << fn << std::endl; - real_name = fn; - return; - } - } - - // try homedir + basedir - fn = homedir; - fn.append(basedir); - fn.append(filename); - std::ifstream::open(fn.c_str(), mode); - if (this->is_open()) { - con_debug << "File opened " << fn << std::endl; - real_name = fn; - return; - } - - // try datadir + basedir - fn = datadir; - fn.append(basedir); - fn.append(filename); - std::ifstream::open(fn.c_str(), mode); - - if (!this->is_open()) { - con_warn << "Could not open " << filename << std::endl; - } else { - con_debug << "File opened " << fn << std::endl; - real_name = fn; - } -} - } // namespace filesystem diff --git a/src/filesystem/file.h b/src/filesystem/file.h index ba392de..37ac8b4 100644 --- a/src/filesystem/file.h +++ b/src/filesystem/file.h @@ -9,34 +9,41 @@ // C++ headers #include -#include namespace filesystem { -/// a class to open data files -class File : public std::ifstream { +/** + * @brief an abstract interface to handle file access + */ +class File +{ public: File(); virtual ~File(); - /// open the file for reading - virtual void open(const char * filename, std::ios_base::openmode mode = std::ios_base::in); + /// open file for reading + virtual bool open(const char * filename) = 0; - /// current filename - inline std::string name() { - return file_name; - } - - /// current full path - inline std::string path() { - return real_name; - } + /// close file + virtual void close() = 0; -private: + /// read bytes + virtual size_t read(void *buffer, size_t count) = 0; + + /// skip bytes + virtual void skip(size_t count) = 0; + + /// name of the file in the virtual filesystem + inline const std::string name() const { return file_name; } + + /// the path holding the virtual filename + inline const std::string path() const { return file_path; } + +protected: std::string file_name; - std::string real_name; -} -; // class File + + std::string file_path; +}; } // namespace filesystem diff --git a/src/filesystem/filesystem.cc b/src/filesystem/filesystem.cc index da80ea5..f34ac42 100644 --- a/src/filesystem/filesystem.cc +++ b/src/filesystem/filesystem.cc @@ -5,15 +5,22 @@ */ // project headers + #include "filesystem/filesystem.h" +#include "filesystem/diskfile.h" +#include "filesystem/file.h" #include "sys/sys.h" -std::string filesystem::datadir = ""; -std::string filesystem::homedir = ""; -std::string filesystem::basedir = ""; -std::string filesystem::moddir = ""; +namespace filesystem +{ + +std::string datadir = ""; +std::string homedir = ""; +std::string basedir = ""; +std::string moddir = ""; +std::string writedir = ""; -void filesystem::init() +void init() { con_print << "Initializing filesystem..." << std::endl; @@ -27,14 +34,46 @@ void filesystem::init() // FIXME win32 homedir = getenv("HOME"); homedir = homedir + "/.osirion/"; - Path::create(homedir); - Path::create(homedir+basedir); - if (moddir.size() && !Path::exists(homedir+moddir)) - Path::create(homedir+moddir); + + sys::mkdir(homedir); + sys::mkdir(homedir+basedir); + + writedir = homedir; + if (moddir.size()) { + writedir.append(moddir); + } else + writedir.append(basedir); + + sys::mkdir(writedir); + + con_print << " files are created in " << writedir << std::endl; } -void filesystem::shutdown() +void shutdown() { con_print << "Shutting down filesystem..." << std::endl; } +File *open(const char *filename) +{ + // for now, File is always a DiskFile + DiskFile *f = new DiskFile(); + if (!f->open(filename)) { + delete f; + f = 0; + return 0; + } + return f; +} + +void close(File *file) +{ + if (!file) + return; + + file->close(); + delete file; +} + +} + diff --git a/src/filesystem/filesystem.h b/src/filesystem/filesystem.h index 1685fd2..83e93c0 100644 --- a/src/filesystem/filesystem.h +++ b/src/filesystem/filesystem.h @@ -7,7 +7,9 @@ #ifndef __INCLUDED_FILESYSTEM_H__ #define __INCLUDED_FILESYSTEM_H__ -// C++ headers +#include "filesystem/file.h" +#include "filesystem/diskfile.h" + #include /// The filesystem namespace contains classes and functions for common file operations. @@ -17,25 +19,33 @@ namespace filesystem { /// location of the main data files, includes trailing / extern std::string datadir; + /// location of the personal data files, includes trailing / extern std::string homedir; + +/// writeable location +extern std::string writedir; + /// subdirectory with the base data files, includes trailing / extern std::string basedir; + /// subdirectory for the current mod, includes trailing / extern std::string moddir; -/// Initialize the filesystem subsystem +/// initialize the filesystem subsystem void init(); -/// Shutdown the filesystem subsystem +/// shutdown the filesystem subsystem void shutdown(); -} // namespace filesystem +/// open a file and return a pointer to a File instance +File *open(const char *filename); +/// open a file and return a pointer to a File instance +File *open(const std::string &filename); +/// close and delete a file instance +void close(File *file); -// project headers -#include "filesystem/file.h" -#include "filesystem/path.h" -#include "filesystem/inifile.h" +} // namespace filesystem #endif // __INCLUDED_FILYSYSTEM_H__ diff --git a/src/filesystem/path.cc b/src/filesystem/path.cc deleted file mode 100644 index 096825c..0000000 --- a/src/filesystem/path.cc +++ /dev/null @@ -1,33 +0,0 @@ -/* - filesystem/path.cc - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2 -*/ - -// project headers -#include "filesystem/path.h" -#include "sys/sys.h" - -namespace filesystem -{ - -void Path::create(std::string path) -{ - std::string tmp(path); - if (tmp[tmp.size()-1] == '/') - tmp = tmp.substr(0, tmp.size() - 1); - - if (!sys::mkdir(tmp.c_str())) - // FIXME check error value - con_warn << "Could not create directory " << tmp << std::endl; - else - con_debug << "Directory created " << tmp << std::endl; -} - -bool Path::exists(std::string path) -{ - // FIXME make it work - return false; -} - -} diff --git a/src/filesystem/path.h b/src/filesystem/path.h deleted file mode 100644 index 9b443b0..0000000 --- a/src/filesystem/path.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - common/path.h - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2 -*/ - -#ifndef __INCLUDED_FILESYSTEM_PATH_H__ -#define __INCLUDED_FILESYSTEM_PATH_H__ - -// C++ headers -#include -#include - -namespace filesystem { - -/// a class to create directories -class Path { -public: - static bool exists(std::string path); - static void create(std::string path); -}; // class Path - -} // namespace filesystem - -#endif // __INCLUDED_FILESYSTEM_PATH_H__ - diff --git a/src/filesystem/vfile.cc b/src/filesystem/vfile.cc deleted file mode 100644 index 9fcb2b3..0000000 --- a/src/filesystem/vfile.cc +++ /dev/null @@ -1,24 +0,0 @@ -/* - filesystem/vfile.cc - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2 -*/ - -// project headers -#include "filesystem/vfile.h" - -namespace filesystem -{ - -std::string VFile::find(const char *filename) -{ - return std::string(); -} - - -bool VFile::exists(const char *filename) -{ - return (find(filename).size() == 0); -} - -} diff --git a/src/filesystem/vfile.h b/src/filesystem/vfile.h deleted file mode 100644 index a64dbc6..0000000 --- a/src/filesystem/vfile.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - filesystem/vfile.h - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2 -*/ - -#ifndef __INCLUDED_FILESYSTEM_VFILE_H__ -#define __INCLUDED_FILESYSTEM_VFILE_H__ - -// project headers -#include "filesystem/filesystem.h" - -namespace filesystem { - -/// a file in the virtual file system -class VFile { - -public: - /// search the path for a file in the virtual - /** If the file can not be found in any of the data directories, - * an empty string will be returned - */ - static std::string find(const char *filename); - - /// returns true if a file exists in the virtual filesystem - static bool exists(const char *filename); -}; - -} - -#endif //__INCLUDED_FILESYSTEM_VFILE_H__ -- cgit v1.2.3