From eaa7232559e60e285775c83179ec082c643da6ad Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 19 Nov 2021 20:02:59 +0100 Subject: [PATCH] The settings class now uses a simple ini format. --- core/settings.cpp | 83 +++++++++++++++++++++++++++++++++++++++++++++-- core/settings.h | 25 ++++++++------ 2 files changed, 96 insertions(+), 12 deletions(-) diff --git a/core/settings.cpp b/core/settings.cpp index 422ce0d..3ba860b 100644 --- a/core/settings.cpp +++ b/core/settings.cpp @@ -3,7 +3,53 @@ #include #include -void Settings::parse_file(const String &path) { +String Settings::get_value(const String &key, const String &def) { + std::map::iterator e = _data.find(key); + + if (e != _data.end()) { + return e->second; + } else { + return def; + } +} +int Settings::get_value_int(const String &key, const int def) { + std::map::iterator e = _data.find(key); + + if (e != _data.end()) { + return e->second.to_int(); + } else { + return def; + } +} +float Settings::get_value_float(const String &key, const float def) { + std::map::iterator e = _data.find(key); + + if (e != _data.end()) { + return e->second.to_float(); + } else { + return def; + } +} +double Settings::get_value_double(const String &key, const double def) { + std::map::iterator e = _data.find(key); + + if (e != _data.end()) { + return e->second.to_double(); + } else { + return def; + } +} +bool Settings::get_value_bool(const String &key, const bool def) { + std::map::iterator e = _data.find(key); + + if (e != _data.end()) { + return e->second.to_bool(); + } else { + return def; + } +} + +void Settings::parse_ini_file(const String &path) { FILE *f = fopen(path.c_str(), "r"); if (!f) { @@ -21,7 +67,40 @@ void Settings::parse_file(const String &path) { fread(config_str.dataw(), 1, fsize, f); fclose(f); - settings.Parse(config_str.c_str()); + config_str.replace('\r', ' '); + Vector ns = config_str.split('\n'); + + for (int i = 0; i < ns.size(); ++i) { + String s = ns[i]; + + s.trim(); + + if (s.size() == 0) { + continue; + } + + if (s[0] == '#') { + continue; + } + + int eindex = s.find('='); + + if (eindex == -1 || eindex == s.size() - 1) { + continue; + } + + s.print(); + + String k = s.substr_index(0, eindex - 1); + String v = s.substr_index(eindex + 1, s.size() - 1); + + k.print(); + v.print(); + printf("-------\n"); + + _data[k] = v; + } + } Settings *Settings::get_singleton() { diff --git a/core/settings.h b/core/settings.h index cd8da58..f7de0f4 100644 --- a/core/settings.h +++ b/core/settings.h @@ -1,24 +1,29 @@ #ifndef SETTINGS_H #define SETTINGS_H +#include + #include "core/string.h" -#include "rapidjson/document.h" - class Settings { - public: - rapidjson::Document settings; +public: + String get_value(const String &key, const String &def = ""); + int get_value_int(const String &key, const int def = 0); + float get_value_float(const String &key, const float def = 0); + double get_value_double(const String &key, const double def = 0); + bool get_value_bool(const String &key, const bool def = false); - void parse_file(const String &path); + void parse_ini_file(const String &path); - static Settings *get_singleton(); + static Settings *get_singleton(); - Settings(const bool singleton = false); - virtual ~Settings(); + Settings(const bool singleton = false); + virtual ~Settings(); protected: - static Settings *_singleton; + static Settings *_singleton; + + std::map _data; }; - #endif \ No newline at end of file