diff options
Diffstat (limited to 'src/filesystem')
-rw-r--r-- | src/filesystem/inifile.cc | 79 | ||||
-rw-r--r-- | src/filesystem/inifile.h | 13 |
2 files changed, 86 insertions, 6 deletions
diff --git a/src/filesystem/inifile.cc b/src/filesystem/inifile.cc index 02dbcf8..dcc0876 100644 --- a/src/filesystem/inifile.cc +++ b/src/filesystem/inifile.cc @@ -4,10 +4,12 @@ the terms of the GNU General Public License version 2 */ -// project headers +#include "math/mathlib.h" #include "filesystem/filesystem.h" #include "filesystem/inifile.h" +#include <sstream> + namespace filesystem { IniFile::IniFile() {} @@ -49,7 +51,7 @@ bool IniFile::got_section() const { } bool IniFile::got_section(const char * sectionlabel) const { - return (last_read_was_section && section_current == sectionlabel); + return (last_read_was_section && (section_current.compare(sectionlabel) == 0)); } bool IniFile::getline() { @@ -83,11 +85,14 @@ bool IniFile::getline() { } else { // key=value pair size_t found = s.find('='); - if (found !=std::string::npos) { - // FIXME strip spaces, make lowercase + if (found != std::string::npos) { + // make lowercase key_current = s.substr(0, found); - if (key_current.size() > 0) { + while (key_current.size() && key_current[key_current.size()-1] == ' ') + key_current.erase(key_current.size()-1, 1); + + if (key_current.size()) { value_current = s.substr(found+1, s.size() - found - 1); last_read_was_key = true; return true; @@ -101,7 +106,6 @@ bool IniFile::getline() { } bool IniFile::got_key_string(const char * keylabel, std::string & valuestring) { - //condebug << "IniFile got_value_string " << keylabel << " " << last_read_was_key << std::endl; if (last_read_was_key && (key_current.compare(keylabel) == 0 )) { valuestring.assign(value_current); return true; @@ -110,6 +114,69 @@ bool IniFile::got_key_string(const char * keylabel, std::string & valuestring) { } } +bool IniFile::got_key_vector3f(const char * keylabel, math::Vector3f & v) { + if (last_read_was_key && (key_current.compare(keylabel) == 0 )) { + std::istringstream is(value_current); + float x, y, z; + if ((is >> x) && (is >> y) && (is >> z)) { + v = math::Vector3f(x,y,z); + } else { + v= math::Vector3f(); + } + return true; + } else { + return false; + } +} + +bool IniFile::got_key_float(const char * keylabel, float & f) { + if (last_read_was_key && (key_current.compare(keylabel) == 0 )) { + std::istringstream is(value_current); + if (!(is >> f)) { + f = 0; + } + return true; + } else { + return false; + } +} + +bool IniFile::got_key(const char * keylabel) { + return (last_read_was_key && (key_current.compare(keylabel) == 0 )); +} + +bool IniFile::got_key_angle(const char * keylabel, float & f) { + if (last_read_was_key && (key_current.compare(keylabel) == 0 )) { + std::istringstream is(value_current); + if ((is >> f)) { + f = math::degrees360f(f); + } else { + f = 0; + } + return true; + } else { + return false; + } +} + +bool IniFile::got_key_color(const char * keylabel, math::Color & color) { + if (last_read_was_key && (key_current.compare(keylabel) == 0 )) { + std::istringstream is(value_current); + float r, g, b; + if ((is >> r) && (is >> g) && (is >> b)) { + if ((r > 1) || (g > 1) || (b > 1)) { + r /= 255; g /= 255; b /= 255; + } + color = math::Color(r, g, b); + } else { + color = math::Color(); + } + return true; + } else { + return false; + } +} + void IniFile::close() { inifile_ifs.close(); diff --git a/src/filesystem/inifile.h b/src/filesystem/inifile.h index 632d432..568eade 100644 --- a/src/filesystem/inifile.h +++ b/src/filesystem/inifile.h @@ -10,6 +10,8 @@ #include <string> #include <fstream> +#include "math/vector3f.h" +#include "math/color.h" #include "filesystem/file.h" namespace filesystem { @@ -57,9 +59,20 @@ public: return last_read_was_key; } + bool got_key(const char * keylabel); + /// check if the last read key=value pair matches keylabel and store the value in valuestring bool got_key_string(const char * keylabel, std::string & valuestring); + bool got_key_color(const char * keylabel, math::Color & color); + + bool got_key_float(const char * keylabel, float & f); + + bool got_key_angle(const char * keylabel, float & f); + + bool got_key_vector3f(const char * keylabel, math::Vector3f & v); + + inline unsigned int line() const { return line_number; } |