2020-11-25 00:20:41 +01:00
|
|
|
#include "settings.h"
|
|
|
|
|
2020-12-08 16:22:18 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2021-11-19 20:02:59 +01:00
|
|
|
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) {
|
2020-12-08 16:22:18 +01:00
|
|
|
FILE *f = fopen(path.c_str(), "r");
|
|
|
|
|
|
|
|
if (!f) {
|
2020-12-26 17:48:00 +01:00
|
|
|
printf("Settings::parse_file: Error opening file!\n");
|
2020-12-08 16:22:18 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
long fsize = ftell(f);
|
|
|
|
fseek(f, 0, SEEK_SET); /* same as rewind(f); */
|
|
|
|
|
2021-11-01 19:53:35 +01:00
|
|
|
String config_str;
|
2020-12-08 16:22:18 +01:00
|
|
|
config_str.resize(fsize);
|
|
|
|
|
2021-11-01 19:53:35 +01:00
|
|
|
fread(config_str.dataw(), 1, fsize, f);
|
2020-12-08 16:22:18 +01:00
|
|
|
fclose(f);
|
|
|
|
|
2021-11-19 20:02:59 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
String k = s.substr_index(0, eindex - 1);
|
|
|
|
String v = s.substr_index(eindex + 1, s.size() - 1);
|
|
|
|
|
|
|
|
_data[k] = v;
|
|
|
|
}
|
2020-12-08 16:22:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Settings *Settings::get_singleton() {
|
|
|
|
return _singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
Settings::Settings(const bool singleton) {
|
|
|
|
if (singleton) {
|
|
|
|
if (_singleton) {
|
|
|
|
printf("Settings singleton overridden!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
_singleton = this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Settings::~Settings() {
|
|
|
|
if (_singleton == this) {
|
|
|
|
_singleton = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Settings *Settings::_singleton = nullptr;
|