From 698f901a6d983f3f44b07f6560b1370850483fa0 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sat, 18 Feb 2012 22:13:02 +0000 Subject: Added map entities, added ini file reader. --- src/inistream.cc | 261 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 261 insertions(+) create mode 100644 src/inistream.cc (limited to 'src/inistream.cc') diff --git a/src/inistream.cc b/src/inistream.cc new file mode 100644 index 0000000..1e76ab8 --- /dev/null +++ b/src/inistream.cc @@ -0,0 +1,261 @@ +/* + inistream.cc + This file is part of the Project::OSiRiON world editor + and is distributed under the terms and conditions of + the GNU General Public License version 2 +*/ + +#include "inistream.h" + +namespace editor +{ + +IniStream::IniStream() +{ + clear(); +} + +IniStream::~IniStream() +{ + clear(); +} + +void IniStream::clear() { + last_read_was_section = false; + last_read_was_key = false; + + key_current.clear(); + value_current.clear(); + section_current.clear(); + + line_number = 0; +} + +bool IniStream::getline(QTextStream &textstream) +{ + if (textstream.atEnd()) + return false; + + QString line; + + last_read_was_section = false; + last_read_was_key = false; + key_current = ""; + value_current = ""; + + line = textstream.readLine(1023); + line = line.trimmed(); + + if (line.size()) { + line_number++; + + if (line[0] == '#' || line[0] == ';') { + // line with comment + } else { + // section header + if (line.size() > 2 && line.startsWith('[') && line.endsWith(']')) { + section_current = line; + section_current.remove(0, 1); + section_current.remove(line.size()-2, 1); + + section_current = section_current.trimmed(); + section_current.toLower(); + + last_read_was_section = true; + return true; + } else { + // key=value pair + int found = line.indexOf('='); + if (found > -1) { + // make lowercase and strip spaces + key_current = line; + key_current.remove(found, line.size() - found); + key_current = key_current.trimmed(); + key_current.toLower(); + + if (key_current.size()) { + value_current = line; + value_current.remove(0, key_current.size() + 1); + value_current = value_current.trimmed(); + + last_read_was_key = true; + return true; + } + + } + } + } + } + + return true; +} + +bool IniStream::got_section() const +{ + return last_read_was_section; +} + +bool IniStream::got_section(const char *sectionlabel) const +{ + return (last_read_was_section && (section_current.compare(sectionlabel) == 0)); +} + +bool IniStream::in_section(const char *sectionlabel) const +{ + return ((section_current.compare(sectionlabel) == 0)); +} + +bool IniStream::got_key(const char *keylabel) +{ + return (last_read_was_key && (key_current.compare(keylabel) == 0)); +} + +bool IniStream::got_key() const +{ + return last_read_was_key; +} + + +bool IniStream::got_key_string(const char *keylabel, QString &valuestring) +{ + if (last_read_was_key && (key_current.compare(keylabel) == 0)) { + valuestring = value_current; + return true; + } else { + return false; + } +} + +bool IniStream::got_key_vector3f(const char *keylabel, float &x, float &y, float &z) +{ + if (last_read_was_key && (key_current.compare(keylabel) == 0)) { + QTextStream ts(&value_current, QIODevice::ReadOnly); + x = y = z = 0; + ts >> x; + ts >> y; + ts >> z; + return true; + } else { + return false; + } +} + +/* +bool IniStream::got_key_label(const char * keylabel, QString & labelstring) +{ + if (last_read_was_key && (key_current.compare(keylabel) == 0)) { + labelstring = value_current.trimmed(); + return true; + } else { + return false; + } +} + +bool IniStream::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 IniStream::got_key_long(const char * keylabel, long & l) +{ + if (last_read_was_key && (key_current.compare(keylabel) == 0)) { + std::istringstream is(value_current); + if (!(is >> l)) { + l = 0; + } + return true; + } else { + return false; + } +} + +bool IniStream::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 IniStream::got_key_color(const char *keylabel, QColor & color) +{ + if (last_read_was_key && (key_current.compare(keylabel) == 0)) { + std::istringstream is(value_current); + float r, g, b, a; + if ((is >> r) && (is >> g) && (is >> b)) { + if ((r > 1.0f) || (g > 1.0f) || (b > 1.0f)) { + if (is >> a) { + a /= 255.0f; + } + r /= 255.0f; + g /= 255.0f; + b /= 255.0f; + } else { + if (!(is >> a)) { + a = 1.0f; + } + } + color = QColor(r, g, b, a); + } else { + color = QColor(); + } + return true; + } else { + return false; + } +} + +bool IniStream::got_key_bool(const char *keylabel, bool & b) +{ + if (last_read_was_key && (key_current.compare(keylabel) == 0)) { + std::istringstream is(value_current); + + unsigned int i; + if (is >> i) { + if (i > 0 ) { + b = true; + } else { + b = false; + } + return true; + } + + QString val(value_current); + aux::trim(val); + aux::lowercase(val); + + if (val.compare("yes") == 0) { + b = true; + return true; + } else if (val.compare("true") == 0) { + b = true; + return true; + } else if (val.compare("no") == 0) { + b = false; + return true; + } else if (val.compare("false") == 0) { + b = false; + return true; + } + } + + return false; +} +*/ + +} // namespace editor -- cgit v1.2.3