diff options
author | Stijn Buys <ingar@osirion.org> | 2008-01-30 17:29:25 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2008-01-30 17:29:25 +0000 |
commit | 28180e6b6763e4ce5d65c02e4df5380f11e6d10a (patch) | |
tree | 2f3c9953e501543052ea440476fb671fa845376b /src | |
parent | e3c27429831245a151706c395991b42f2876b2e1 (diff) |
filesystem subsystem
Diffstat (limited to 'src')
-rw-r--r-- | src/filesystem/Makefile.am | 10 | ||||
-rw-r--r-- | src/filesystem/file.cc | 63 | ||||
-rw-r--r-- | src/filesystem/file.h | 35 | ||||
-rw-r--r-- | src/filesystem/filesystem.cc | 23 | ||||
-rw-r--r-- | src/filesystem/filesystem.h | 38 | ||||
-rw-r--r-- | src/filesystem/inifile.cc | 90 | ||||
-rw-r--r-- | src/filesystem/inifile.h | 76 | ||||
-rw-r--r-- | src/filesystem/path.cc | 38 | ||||
-rw-r--r-- | src/filesystem/path.h | 26 |
9 files changed, 399 insertions, 0 deletions
diff --git a/src/filesystem/Makefile.am b/src/filesystem/Makefile.am new file mode 100644 index 0000000..9ef09bd --- /dev/null +++ b/src/filesystem/Makefile.am @@ -0,0 +1,10 @@ +METASOURCES = AUTO + +libfilesystem_la_SOURCES = file.cc filesystem.cc inifile.cc path.cc +libfilesystem_la_LDFLAGS = -avoid-version -no-undefined +libfilesystem_la_LIBADD = $(top_builddir)/src/common/libcommon.la + +noinst_LTLIBRARIES = libfilesystem.la +noinst_HEADERS = file.h filesystem.h path.h + +INCLUDES = -I$(top_srcdir)/src diff --git a/src/filesystem/file.cc b/src/filesystem/file.cc new file mode 100644 index 0000000..7f33337 --- /dev/null +++ b/src/filesystem/file.cc @@ -0,0 +1,63 @@ +/* + fs/file.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 "common/common.h" +#include "filesystem/filesystem.h" + +namespace filesystem { + +void File::open(const char * filename, ios_base::openmode mode) { + file_name.assign(filename); + std::string fn; + + // 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()) { + 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; + 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; + 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; + } +} + +} // namespace common + diff --git a/src/filesystem/file.h b/src/filesystem/file.h new file mode 100644 index 0000000..5e997a2 --- /dev/null +++ b/src/filesystem/file.h @@ -0,0 +1,35 @@ +/* + filesystem/file.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_FILE_H__ +#define __INCLUDED_FILESYSTEM_FILE_H__ + +// C++ headers +#include <string> +#include <fstream> + +namespace filesystem { + +/// a class to open data files +class File : public std::ifstream { +public: + /// open the file for reading + virtual void open(const char * filename, std::ios_base::openmode mode = std::ios_base::in); + + /// current filename + inline std::string name() { + return file_name; + } + +private: + std::string file_name; +} +; // class File + +} // namespace filesystem + +#endif // __INCLUDED_FILESYSTEM_FILE_H__ + diff --git a/src/filesystem/filesystem.cc b/src/filesystem/filesystem.cc new file mode 100644 index 0000000..9c00930 --- /dev/null +++ b/src/filesystem/filesystem.cc @@ -0,0 +1,23 @@ +/* + filesystem/filesystem.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/filesystem.h" +#include "common/common.h" + +std::string filesystem::datadir = ""; +std::string filesystem::homedir = ""; +std::string filesystem::basedir = ""; +std::string filesystem::moddir = ""; + +void filesystem::init() { + con_debug << "Initializing filesystem..." << std::endl; +} + +void filesystem::shutdown() { + con_debug << "Shutting down filesystem..." << std::endl; +} + diff --git a/src/filesystem/filesystem.h b/src/filesystem/filesystem.h new file mode 100644 index 0000000..4032575 --- /dev/null +++ b/src/filesystem/filesystem.h @@ -0,0 +1,38 @@ +/* + filesystem/filesystem.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_H__ +#define __INCLUDED_FILESYSTEM_H__ + +// C++ headers +#include <string> + +/// The filesystem namespace contains classes and functions for common file operations. +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; +/// 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 +void init(); + +/// Shutdown the filesystem subsystem +void shutdown(); + +} // namespace filesystem + +// project headers +#include "filesystem/file.h" +#include "filesystem/path.h" + +#endif // __INCLUDED_FILYSYSTEM_H__ + diff --git a/src/filesystem/inifile.cc b/src/filesystem/inifile.cc new file mode 100644 index 0000000..308a87d --- /dev/null +++ b/src/filesystem/inifile.cc @@ -0,0 +1,90 @@ +/* + filesystem/inifile.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/inifile.h" + +namespace filesystem { + +void IniFile::open(const char * filename, std::ios_base::openmode mode) { + last_read_was_section = false; + last_read_was_key = false; + key_current = ""; + value_current = ""; + section_current = ""; + line_number = 0; + + File::open(filename, mode); +} + + +bool IniFile::got_section() const { + return last_read_was_section; +} + +bool IniFile::got_section(const char * sectionlabel) const { + return (last_read_was_section && section_current == sectionlabel); +} + +IniFile & IniFile::getline() { + char line[1024]; + + last_read_was_section = false; + last_read_was_key = false; + key_current = ""; + value_current = ""; + + while ((*this)) { + File::getline(line, 1024); + std::string s(line); + line_number++; + + if (s.size() == 0) { + // empty line + } else + // comment + if (s[0] == '#' || s[0] == ';') { + // condebug << "IniFile got comment " << s << std::endl; + } else + // section header + if (s.size() > 2 && s[0] == '[' && s[s.size()-1] == ']') { + // condebug << "Inifile got section header " << s << std::endl; + section_current = s.substr(1, s.size()-2); + last_read_was_section = true; + break; + } else { + // key=value pair + size_t found = s.find('='); + if (found !=std::string::npos) { + // FIXME strip spaces, make lowercase + key_current = s.substr(0, found); + + if (key_current.size() > 0) { + value_current = s.substr(found+1, s.size() - found - 1); + last_read_was_key = true; + break; + } + + } + } + } + + return (*this); +} + +bool IniFile::got_key_string(char * const keylabel, std::string & valuestring) { + //condebug << "IniFile got_value_string " << keylabel << " " << last_read_was_key << std::endl; + if (last_read_was_key && key_current == keylabel) { + valuestring.assign(value_current); + return true; + } else { + return false; + } +} + + +} // namespace filesystem + diff --git a/src/filesystem/inifile.h b/src/filesystem/inifile.h new file mode 100644 index 0000000..f5b74a3 --- /dev/null +++ b/src/filesystem/inifile.h @@ -0,0 +1,76 @@ +/* + common/inifile.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_INIFILE_H__ +#define __INCLUDED_FILESYSTEM_INIFILE_H__ + +// project headers +#include "filesystem/file.h" + +// C++ headers +#include <string> +#include <fstream> + +namespace filesystem { + +/// a class to read .ini files +/*! The IniFile class provides functions to read .ini files. A .ini file + * consists of one or more [section] headers followed by one or more key=value + * pairs. Lines starting with # or ; are considered comments + */ +class IniFile : public File { +public: + /// open the file for reading + virtual void open(const char * filename, std::ios_base::openmode mode = std::ios_base::in); + + IniFile & getline(); + + /// current section label + inline std::string section() const { + return section_current; + } + + /// current key + inline std::string key() const { + return key_current; + } + + /// current value + inline std::string value() const { + return value_current; + } + + /// true if the last read statement was a section header + bool got_section() const; + /// true if the last read statement was a certain section header + bool got_section(const char * sectionlabel) const; + + /// true if the last read statement was a key=value pair + inline bool got_key() const { + return last_read_was_key; + } + /// check if the last read key=value pair matches keylabel and store the value in valuestring + bool got_key_string(char * const keylabel, std::string & valuestring); + + inline unsigned int line() const { + return line_number; + } + +private: + std::string section_current; + std::string key_current; + std::string value_current; + + bool last_read_was_key; + bool last_read_was_section; + + unsigned int line_number; +} +; // class IniFile + +} // namespace common + +#endif // __INCLUDED_FILESYSTEM_INIFILE_H__ diff --git a/src/filesystem/path.cc b/src/filesystem/path.cc new file mode 100644 index 0000000..ac780c4 --- /dev/null +++ b/src/filesystem/path.cc @@ -0,0 +1,38 @@ +/* + 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 "common/common.h" + +#ifdef _WIN32 +#include <dlfcn.h> +#else +#include <sys/stat.h> +#endif + +namespace filesystem { + +void Path::create(std::string path) { + std::string tmp(path); + if (tmp[tmp.size()-1] == '/') + tmp = tmp.substr(0, tmp.size() - 1); + +#ifdef _WIN32 + mkdir(tmp.c_str()); +#else + if (!mkdir(tmp.c_str(), 0777)) + con_warn << "Could not create directory " << tmp << std::endl; + else + con_debug << "Path created " << tmp << std::endl; +#endif +} + +bool Path::exists(std::string path) { + return false; +} + +} diff --git a/src/filesystem/path.h b/src/filesystem/path.h new file mode 100644 index 0000000..9b443b0 --- /dev/null +++ b/src/filesystem/path.h @@ -0,0 +1,26 @@ +/* + 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 <string> +#include <fstream> + +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__ + |