The settings class now uses a simple ini format.

This commit is contained in:
Relintai 2021-11-19 20:02:59 +01:00
parent 47545dc181
commit eaa7232559
2 changed files with 96 additions and 12 deletions

View File

@ -3,7 +3,53 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
void Settings::parse_file(const String &path) { String Settings::get_value(const String &key, const String &def) {
std::map<String, String>::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<String, String>::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<String, String>::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<String, String>::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<String, String>::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"); FILE *f = fopen(path.c_str(), "r");
if (!f) { if (!f) {
@ -21,7 +67,40 @@ void Settings::parse_file(const String &path) {
fread(config_str.dataw(), 1, fsize, f); fread(config_str.dataw(), 1, fsize, f);
fclose(f); fclose(f);
settings.Parse(config_str.c_str()); config_str.replace('\r', ' ');
Vector<String> 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() { Settings *Settings::get_singleton() {

View File

@ -1,15 +1,19 @@
#ifndef SETTINGS_H #ifndef SETTINGS_H
#define SETTINGS_H #define SETTINGS_H
#include <map>
#include "core/string.h" #include "core/string.h"
#include "rapidjson/document.h"
class Settings { class Settings {
public: public:
rapidjson::Document settings; 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();
@ -18,7 +22,8 @@ class Settings {
protected: protected:
static Settings *_singleton; static Settings *_singleton;
std::map<String, String> _data;
}; };
#endif #endif