From 2386bda56b33de68370fa8acc76e39ddddd836c6 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 8 Feb 2009 15:27:59 +0000 Subject: changed inifile and map to use filesystem::IFileStream --- src/filesystem/filestream.cc | 9 ++++--- src/filesystem/filestream.h | 8 +++--- src/filesystem/inifile.cc | 61 +++++++++++++++++++++++++++----------------- src/filesystem/inifile.h | 32 +++++++++++++++-------- 4 files changed, 69 insertions(+), 41 deletions(-) (limited to 'src/filesystem') diff --git a/src/filesystem/filestream.cc b/src/filesystem/filestream.cc index 693d1c1..cfbaaec 100644 --- a/src/filesystem/filestream.cc +++ b/src/filesystem/filestream.cc @@ -44,9 +44,12 @@ void IFileStream::open(const char *name) if (fstream_filename[i] == '/') fstream_filename[i] = '\\'; #endif */ - std::ifstream::open(fstream_filename.c_str()); - if (good()) - return; + + if (sys::file_exists(fstream_filename)) { + std::ifstream::open(fstream_filename.c_str()); + if (good()) + return; + } } fstream_filename.clear(); diff --git a/src/filesystem/filestream.h b/src/filesystem/filestream.h index 7dddda9..9739f38 100644 --- a/src/filesystem/filestream.h +++ b/src/filesystem/filestream.h @@ -18,7 +18,7 @@ namespace filesystem class IFileStream : public std::ifstream { public: - IFileStream(const char *name); + IFileStream(const char *name = 0); IFileStream(const std::string &name); @@ -27,10 +27,10 @@ public: void open(const std::string &name); /// name of the file in the virtual filesystem - inline const std::string &name() { return fstream_name; } + inline const std::string &name() const { return fstream_name; } - /// system filename - inline const std::string &filename() { return fstream_filename; } + /// actual dilename + inline const std::string &filename() const { return fstream_filename; } private: std::string fstream_name; diff --git a/src/filesystem/inifile.cc b/src/filesystem/inifile.cc index e83c08f..c6cc625 100644 --- a/src/filesystem/inifile.cc +++ b/src/filesystem/inifile.cc @@ -13,11 +13,37 @@ namespace filesystem { -IniFile::IniFile() {} +IniFile::IniFile(const char *ininame) +{ + if (ininame) + open(ininame); +} + +IniFile::IniFile(const std::string & ininame) +{ + if (ininame.size()) + open(ininame.c_str()); +} + +IniFile::~IniFile() +{ + if (inifile_stream.is_open()) + inifile_stream.close(); +} + +bool IniFile::open(std::string const & ininame) +{ + return open(ininame.c_str()); +} -IniFile::~IniFile() {} +bool IniFile::open(const char *ininame) +{ -bool IniFile::open(std::string const & name) { + if (!ininame) + return false; + + if (inifile_stream.is_open()) + return false; last_read_was_section = false; last_read_was_key = false; @@ -26,26 +52,15 @@ bool IniFile::open(std::string const & name) { section_current = ""; line_number = 0; - inifile_name.assign("ini/"); - inifile_name.append(name); + std::string inifile_name("ini/"); + inifile_name.append(ininame); inifile_name.append(".ini"); - - filesystem::File *f = filesystem::open(inifile_name.c_str()); - if (!f) { - con_warn << "Could not open " << inifile_name << std::endl; - return false; - } - - std::string fn = f->path(); - fn.append(f->name()); - filesystem::close(f); - - inifile_ifs.open(fn.c_str()); - if (!inifile_ifs.is_open()) { - con_warn << "Could not stream " << fn << "!\n"; + + inifile_stream.open(inifile_name); + if (!inifile_stream.good()) { + con_warn << "Could not open " << inifile_stream.name() << "!\n"; return false; } - return true; } @@ -70,10 +85,10 @@ bool IniFile::getline() { key_current = ""; value_current = ""; - if (!inifile_ifs.is_open()) + if (!inifile_stream.is_open()) return false; - if (inifile_ifs.getline(line, 1023)) { + if (inifile_stream.getline(line, 1023)) { std::string s(line); aux::trim(s); line_number++; @@ -266,7 +281,7 @@ void IniFile::unknown_section() const void IniFile::close() { - inifile_ifs.close(); + inifile_stream.close(); } } // namespace filesystem diff --git a/src/filesystem/inifile.h b/src/filesystem/inifile.h index f064bc7..09f6ebb 100644 --- a/src/filesystem/inifile.h +++ b/src/filesystem/inifile.h @@ -12,7 +12,7 @@ #include "math/vector3f.h" #include "math/color.h" -#include "filesystem/file.h" +#include "filesystem/filestream.h" namespace filesystem { @@ -23,13 +23,17 @@ namespace filesystem { */ class IniFile { public: - IniFile(); + IniFile(const char *ininame = 0); + + IniFile(std::string const & ininame); + ~IniFile(); - /// open the file for reading - /** the filename will get the "ini/" prefix and ".ini" suffix - */ - bool open(std::string const & name); + /// open an ini file for reading + bool open(std::string const & ininame); + + /// open an ini file for reading + bool open(const char *ininame); /// parse one line, returns false on end-of-file bool getline(); @@ -93,10 +97,16 @@ public: void unknown_section() const; /// return true of the ini file is open for reading - inline bool is_open() { return inifile_ifs.is_open(); } + inline bool is_open() const { return inifile_stream.is_open(); } - /// current filename - inline std::string const & name() const {return inifile_name; } + /// return true of the ini file is open for reading + inline bool good() const { return inifile_stream.good(); } + + /// current name in the virtual filesystem + inline std::string const & name() const {return inifile_stream.name(); } + + /// current actual filename + inline std::string const & filename() const { return inifile_stream.filename(); } /// close the file void close(); @@ -110,8 +120,8 @@ private: bool last_read_was_section; unsigned int line_number; - std::ifstream inifile_ifs; - std::string inifile_name; + + IFileStream inifile_stream; }; } -- cgit v1.2.3