Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/filesystem')
-rw-r--r--src/filesystem/Makefile.am2
-rw-r--r--src/filesystem/inifile.cc59
-rw-r--r--src/filesystem/inifile.h39
3 files changed, 66 insertions, 34 deletions
diff --git a/src/filesystem/Makefile.am b/src/filesystem/Makefile.am
index 20cd25a..0793f61 100644
--- a/src/filesystem/Makefile.am
+++ b/src/filesystem/Makefile.am
@@ -1,6 +1,6 @@
METASOURCES = AUTO
-libfilesystem_la_SOURCES = diskfile.cc file.cc filesystem.cc
+libfilesystem_la_SOURCES = diskfile.cc file.cc filesystem.cc inifile.cc
libfilesystem_la_LDFLAGS = -avoid-version -no-undefined
libfilesystem_la_LIBADD = $(top_builddir)/src/sys/libsys.la
noinst_LTLIBRARIES = libfilesystem.la
diff --git a/src/filesystem/inifile.cc b/src/filesystem/inifile.cc
index f04d116..c76e06a 100644
--- a/src/filesystem/inifile.cc
+++ b/src/filesystem/inifile.cc
@@ -5,17 +5,17 @@
*/
// project headers
+#include "filesystem/filesystem.h"
#include "filesystem/inifile.h"
-namespace filesystem
-{
+namespace filesystem {
IniFile::IniFile() {}
IniFile::~IniFile() {}
-void IniFile::open(const char * filename, std::ios_base::openmode mode)
-{
+void IniFile::open(std::string const & name) {
+
last_read_was_section = false;
last_read_was_key = false;
key_current = "";
@@ -23,22 +23,36 @@ void IniFile::open(const char * filename, std::ios_base::openmode mode)
section_current = "";
line_number = 0;
- File::open(filename, mode);
+ std::string inifile_name("ini/");
+ inifile_name.append(name);
+ inifile_name.append(".ini");
+
+ filesystem::File *f = filesystem::open(inifile_name.c_str());
+ if (!f) {
+ return;
+ }
+
+ 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";
+ return;
+ }
}
-bool IniFile::got_section() const
-{
+bool IniFile::got_section() const {
return last_read_was_section;
}
-bool IniFile::got_section(const char * sectionlabel) const
-{
+bool IniFile::got_section(const char * sectionlabel) const {
return (last_read_was_section && section_current == sectionlabel);
}
-IniFile & IniFile::getline()
-{
+bool IniFile::getline() {
char line[1024];
last_read_was_section = false;
@@ -46,8 +60,10 @@ IniFile & IniFile::getline()
key_current = "";
value_current = "";
- while ((*this)) {
- File::getline(line, 1024);
+ if (!inifile_ifs.is_open())
+ return false;
+
+ if (inifile_ifs.getline(line, 1024)) {
std::string s(line);
line_number++;
@@ -63,7 +79,7 @@ IniFile & IniFile::getline()
// condebug << "Inifile got section header " << s << std::endl;
section_current = s.substr(1, s.size()-2);
last_read_was_section = true;
- break;
+ return true;
} else {
// key=value pair
size_t found = s.find('=');
@@ -74,18 +90,17 @@ IniFile & IniFile::getline()
if (key_current.size() > 0) {
value_current = s.substr(found+1, s.size() - found - 1);
last_read_was_key = true;
- break;
+ return true;
}
}
}
- }
-
- return (*this);
+ return true;
+ } else
+ return false;
}
-bool IniFile::got_key_string(char * const keylabel, std::string & valuestring)
-{
+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);
@@ -95,6 +110,10 @@ bool IniFile::got_key_string(char * const keylabel, std::string & valuestring)
}
}
+void IniFile::close()
+{
+ inifile_ifs.close();
+}
} // namespace filesystem
diff --git a/src/filesystem/inifile.h b/src/filesystem/inifile.h
index d3984c5..ba7eb6a 100644
--- a/src/filesystem/inifile.h
+++ b/src/filesystem/inifile.h
@@ -7,13 +7,11 @@
#ifndef __INCLUDED_FILESYSTEM_INIFILE_H__
#define __INCLUDED_FILESYSTEM_INIFILE_H__
-// project headers
-#include "filesystem/file.h"
-
-// C++ headers
#include <string>
#include <fstream>
+#include "filesystem/file.h"
+
namespace filesystem {
/// a class to read .ini files
@@ -21,15 +19,17 @@ namespace filesystem {
* 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 {
+class IniFile {
public:
IniFile();
virtual ~IniFile();
/// open the file for reading
- virtual void open(const char * filename, std::ios_base::openmode mode = std::ios_base::in);
+ /** the filename will get the "ini/" prefix and ".ini" suffix
+ */
+ virtual void open(std::string const & name);
- IniFile & getline();
+ bool getline();
/// current section label
inline std::string section() const {
@@ -48,6 +48,7 @@ public:
/// 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;
@@ -55,6 +56,7 @@ public:
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);
@@ -62,15 +64,26 @@ public:
return line_number;
}
+ /// return true of the ini file is open for reading
+ inline bool is_open() const { return inifile_ifs.is_open(); }
+
+ /// current filename
+ inline std::string const & name() const {return inifile_name; }
+
+ /// close the file
+ void close();
+
private:
- std::string section_current;
- std::string key_current;
- std::string value_current;
+ std::string section_current;
+ std::string key_current;
+ std::string value_current;
- bool last_read_was_key;
- bool last_read_was_section;
+ bool last_read_was_key;
+ bool last_read_was_section;
- unsigned int line_number;
+ unsigned int line_number;
+ std::ifstream inifile_ifs;
+ std::string inifile_name;
};
}