2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
#include <fstream>
|
2021-05-11 22:57:08 +02:00
|
|
|
#include <sstream>
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
#include "config.hpp"
|
2012-12-08 17:17:35 +01:00
|
|
|
#include "gecko/gecko.hpp"
|
2012-08-05 15:48:15 +02:00
|
|
|
#include "gui/text.hpp"
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
static const char *g_whitespaces = " \f\n\r\t\v";
|
|
|
|
static const int g_floatPrecision = 10;
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
const std::string Config::emptyString;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
Config::Config(void) :
|
|
|
|
m_loaded(false), m_changed(false), m_domains(), m_filename(), m_iter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
static std::string trimEnd(std::string line)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string::size_type i = line.find_last_not_of(g_whitespaces);
|
|
|
|
if (i == std::string::npos) line.clear();
|
2012-01-21 21:57:41 +01:00
|
|
|
else line.resize(i + 1);
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
static std::string trim(std::string line)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string::size_type i = line.find_last_not_of(g_whitespaces);
|
|
|
|
if (i == std::string::npos)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
line.clear();
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
line.resize(i + 1);
|
|
|
|
i = line.find_first_not_of(g_whitespaces);
|
|
|
|
if (i > 0)
|
|
|
|
line.erase(0, i);
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
static std::string unescNewlines(const std::string &text)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string s;
|
2012-01-21 21:57:41 +01:00
|
|
|
bool escaping = false;
|
|
|
|
|
|
|
|
s.reserve(text.size());
|
2021-09-06 21:16:45 +02:00
|
|
|
for (std::string::size_type i = 0; i < text.size(); ++i)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
if (escaping)
|
|
|
|
{
|
|
|
|
switch (text[i])
|
|
|
|
{
|
|
|
|
case 'n':
|
|
|
|
s.push_back('\n');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
s.push_back(text[i]);
|
|
|
|
}
|
|
|
|
escaping = false;
|
|
|
|
}
|
|
|
|
else if (text[i] == '\\')
|
|
|
|
escaping = true;
|
|
|
|
else
|
|
|
|
s.push_back(text[i]);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
static std::string escNewlines(const std::string &text)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string s;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
s.reserve(text.size());
|
2021-09-06 21:16:45 +02:00
|
|
|
for (std::string::size_type i = 0; i < text.size(); ++i)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
switch (text[i])
|
|
|
|
{
|
|
|
|
case '\n':
|
|
|
|
s.push_back('\\');
|
|
|
|
s.push_back('n');
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
s.push_back('\\');
|
|
|
|
s.push_back('\\');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
s.push_back(text[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
bool Config::hasDomain(const std::string &domain) const
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
return m_domains.find(domain) != m_domains.end();
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
void Config::copyDomain(const std::string &dst, const std::string &src)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2019-03-18 19:39:28 +01:00
|
|
|
m_domains[upperCase(dst)] = m_domains[upperCase(src)];
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
const std::string &Config::firstDomain(void)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
m_iter = m_domains.begin();
|
|
|
|
if (m_iter == m_domains.end())
|
|
|
|
return Config::emptyString;
|
|
|
|
return m_iter->first;
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
const std::string &Config::nextDomain(void)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
++m_iter;
|
|
|
|
if (m_iter == m_domains.end())
|
|
|
|
return Config::emptyString;
|
|
|
|
return m_iter->first;
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
const std::string &Config::nextDomain(const std::string &start) const
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
Config::DomainMap::const_iterator i;
|
|
|
|
Config::DomainMap::const_iterator j;
|
|
|
|
if (m_domains.empty())
|
|
|
|
return Config::emptyString;
|
|
|
|
i = m_domains.find(start);
|
|
|
|
if (i == m_domains.end())
|
|
|
|
return m_domains.begin()->first;
|
|
|
|
j = i;
|
|
|
|
++j;
|
|
|
|
return j != m_domains.end() ? j->first : i->first;
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
const std::string &Config::prevDomain(const std::string &start) const
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
Config::DomainMap::const_iterator i;
|
|
|
|
if (m_domains.empty())
|
|
|
|
return Config::emptyString;
|
|
|
|
i = m_domains.find(start);
|
|
|
|
if (i == m_domains.end() || i == m_domains.begin())
|
|
|
|
return m_domains.begin()->first;
|
|
|
|
--i;
|
|
|
|
return i->first;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Config::load(const char *filename)
|
|
|
|
{
|
2021-12-01 17:33:01 +01:00
|
|
|
if(m_loaded)
|
|
|
|
return true;
|
|
|
|
//if (m_loaded && m_changed) save();
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
std::ifstream file(filename, std::ios::in | std::ios::binary);
|
|
|
|
std::string line;
|
|
|
|
std::string domain("");
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
m_changed = false;
|
|
|
|
m_loaded = false;
|
|
|
|
m_filename = filename;
|
|
|
|
u32 n = 0;
|
|
|
|
if (!file.is_open()) return m_loaded;
|
|
|
|
m_domains.clear();
|
|
|
|
while (file.good())
|
|
|
|
{
|
|
|
|
line.clear();
|
2021-09-06 21:16:45 +02:00
|
|
|
std::getline(file, line, '\n');
|
2012-01-21 21:57:41 +01:00
|
|
|
++n;
|
|
|
|
if (!file.bad() && !file.fail())
|
|
|
|
{
|
|
|
|
line = trimEnd(line);
|
2018-05-17 01:33:56 +02:00
|
|
|
if (line.empty() || line[0] == '#' || line[0] == '\0') continue;
|
2012-01-21 21:57:41 +01:00
|
|
|
if (line[0] == '[')
|
|
|
|
{
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string::size_type i = line.find_first_of(']');
|
|
|
|
if (i != std::string::npos && i > 1)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
domain = upperCase(line.substr(1, i - 1));
|
|
|
|
if (m_domains.find(domain) != m_domains.end())
|
|
|
|
domain.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (!domain.empty())
|
|
|
|
{
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string::size_type i = line.find_first_of('=');
|
|
|
|
if (i != std::string::npos && i > 0)
|
2012-01-21 21:57:41 +01:00
|
|
|
m_domains[domain][lowerCase(trim(line.substr(0, i)))] = unescNewlines(trim(line.substr(i + 1)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-09 19:14:31 +02:00
|
|
|
file.close(); /* not sure if needed */
|
2012-01-21 21:57:41 +01:00
|
|
|
m_loaded = true;
|
|
|
|
return m_loaded;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Config::unload(void)
|
|
|
|
{
|
|
|
|
m_loaded = false;
|
|
|
|
m_changed = false;
|
|
|
|
m_filename = emptyString;
|
|
|
|
m_domains.clear();
|
2021-05-11 22:57:08 +02:00
|
|
|
m_groupCustomTitles.clear();
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Config::save(bool unload)
|
|
|
|
{
|
|
|
|
if (m_changed)
|
|
|
|
{
|
2014-03-17 18:38:32 +01:00
|
|
|
//gprintf("changed:%d\n",m_changed);
|
2021-09-06 21:16:45 +02:00
|
|
|
std::ofstream file(m_filename.c_str(), std::ios::out | std::ios::binary);
|
2012-01-21 21:57:41 +01:00
|
|
|
for (Config::DomainMap::iterator k = m_domains.begin(); k != m_domains.end(); ++k)
|
|
|
|
{
|
|
|
|
Config::KeyMap *m = &k->second;
|
|
|
|
file << '\n' << '[' << k->first << ']' << '\n';
|
|
|
|
for (Config::KeyMap::iterator l = m->begin(); l != m->end(); ++l)
|
|
|
|
file << l->first << '=' << escNewlines(l->second) << '\n';
|
|
|
|
}
|
2013-08-09 19:14:31 +02:00
|
|
|
file.close(); /* not sure if needed */
|
2012-01-21 21:57:41 +01:00
|
|
|
m_changed = false;
|
|
|
|
}
|
|
|
|
if(unload) this->unload();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Config::has(const std::string &domain, const std::string &key) const
|
|
|
|
{
|
|
|
|
if (domain.empty() || key.empty()) return false;
|
|
|
|
DomainMap::const_iterator i = m_domains.find(upperCase(domain));
|
|
|
|
if (i == m_domains.end()) return false;
|
|
|
|
return i->second.find(lowerCase(key)) != i->second.end();
|
|
|
|
}
|
|
|
|
|
2021-05-11 22:57:08 +02:00
|
|
|
void Config::groupCustomTitles(void)
|
|
|
|
{
|
|
|
|
for (Config::DomainMap::iterator k = m_domains.begin(); k != m_domains.end(); ++k)
|
|
|
|
{
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string uc_domain(upperCase(k->first));
|
|
|
|
std::istringstream f(uc_domain);
|
|
|
|
std::string s;
|
2021-05-11 22:57:08 +02:00
|
|
|
while (getline(f, s, ','))
|
|
|
|
m_groupCustomTitles[s] = uc_domain;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
void Config::setWString(const std::string &domain, const std::string &key, const wstringEx &val)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
if (domain.empty() || key.empty()) return;
|
2014-03-17 18:38:32 +01:00
|
|
|
//gprintf("setWString %s\n", val.toUTF8().c_str());
|
2012-01-21 21:57:41 +01:00
|
|
|
m_changed = true;
|
|
|
|
m_domains[upperCase(domain)][lowerCase(key)] = val.toUTF8();
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
void Config::setString(const std::string &domain, const std::string &key, const std::string &val)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
if (domain.empty() || key.empty()) return;
|
2014-03-17 18:38:32 +01:00
|
|
|
//gprintf("setString %s\n", val.c_str());
|
2012-01-21 21:57:41 +01:00
|
|
|
m_changed = true;
|
|
|
|
m_domains[upperCase(domain)][lowerCase(key)] = val;
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
void Config::setBool(const std::string &domain, const std::string &key, bool val)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
if (domain.empty() || key.empty()) return;
|
2014-03-17 18:38:32 +01:00
|
|
|
//gprintf("setBool %d\n", val);
|
2012-01-21 21:57:41 +01:00
|
|
|
m_changed = true;
|
|
|
|
m_domains[upperCase(domain)][lowerCase(key)] = val ? "yes" : "no";
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
void Config::remove(const std::string &domain, const std::string &key)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
if (domain.empty() || key.empty()) return;
|
2014-03-17 18:38:32 +01:00
|
|
|
//gprintf("remove %s\n", key.c_str());
|
2012-01-21 21:57:41 +01:00
|
|
|
m_changed = true;
|
|
|
|
m_domains[upperCase(domain)].erase(lowerCase(key));
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
void Config::setOptBool(const std::string &domain, const std::string &key, int val)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
if (domain.empty() || key.empty()) return;
|
2014-03-17 18:38:32 +01:00
|
|
|
//gprintf("setOptBool %d\n", val);
|
2012-01-21 21:57:41 +01:00
|
|
|
m_changed = true;
|
|
|
|
switch (val)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
m_domains[upperCase(domain)][lowerCase(key)] = "no";
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
m_domains[upperCase(domain)][lowerCase(key)] = "yes";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
m_domains[upperCase(domain)][lowerCase(key)] = "default";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
void Config::setInt(const std::string &domain, const std::string &key, int val)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
if (domain.empty() || key.empty()) return;
|
2014-03-17 18:38:32 +01:00
|
|
|
//gprintf("setInt %i\n", val);
|
2012-01-21 21:57:41 +01:00
|
|
|
m_changed = true;
|
|
|
|
m_domains[upperCase(domain)][lowerCase(key)] = sfmt("%i", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Config::setUInt(const std::string &domain, const std::string &key, unsigned int val)
|
|
|
|
{
|
|
|
|
if (domain.empty() || key.empty()) return;
|
2014-03-17 18:38:32 +01:00
|
|
|
//gprintf("setUInt %u\n", val);
|
2012-01-21 21:57:41 +01:00
|
|
|
m_changed = true;
|
|
|
|
m_domains[upperCase(domain)][lowerCase(key)] = sfmt("%u", val);
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
void Config::setFloat(const std::string &domain, const std::string &key, float val)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
if (domain.empty() || key.empty()) return;
|
2014-03-17 18:38:32 +01:00
|
|
|
//gprintf("setFloat %f\n", val);
|
2012-01-21 21:57:41 +01:00
|
|
|
m_changed = true;
|
|
|
|
m_domains[upperCase(domain)][lowerCase(key)] = sfmt("%.*g", g_floatPrecision, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Config::setVector3D(const std::string &domain, const std::string &key, const Vector3D &val)
|
|
|
|
{
|
|
|
|
if (domain.empty() || key.empty()) return;
|
2014-03-17 18:38:32 +01:00
|
|
|
//gprintf("setVector3D\n");
|
2012-01-21 21:57:41 +01:00
|
|
|
m_changed = true;
|
|
|
|
m_domains[upperCase(domain)][lowerCase(key)] = sfmt("%.*g, %.*g, %.*g", g_floatPrecision, val.x, g_floatPrecision, val.y, g_floatPrecision, val.z);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Config::setColor(const std::string &domain, const std::string &key, const CColor &val)
|
|
|
|
{
|
|
|
|
if (domain.empty() || key.empty()) return;
|
2014-03-17 18:38:32 +01:00
|
|
|
//gprintf("setColor\n");
|
2012-01-21 21:57:41 +01:00
|
|
|
m_changed = true;
|
|
|
|
m_domains[upperCase(domain)][lowerCase(key)] = sfmt("#%.2X%.2X%.2X%.2X", val.r, val.g, val.b, val.a);
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
wstringEx Config::getWString(const std::string &domain, const std::string &key, const wstringEx &defVal)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
if (domain.empty() || key.empty()) return defVal;
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string &data = m_domains[upperCase(domain)][lowerCase(key)];
|
2012-01-21 21:57:41 +01:00
|
|
|
if (data.empty())
|
|
|
|
{
|
|
|
|
data = defVal.toUTF8();
|
2014-03-17 18:38:32 +01:00
|
|
|
//gprintf("getWString %s\n", defVal.toUTF8().c_str());
|
2012-01-21 21:57:41 +01:00
|
|
|
m_changed = true;
|
|
|
|
return defVal;
|
|
|
|
}
|
|
|
|
wstringEx ws;
|
|
|
|
ws.fromUTF8(data.c_str());
|
|
|
|
return ws;
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string Config::getString(const std::string &domain, const std::string &key, const std::string &defVal)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-10-04 16:35:08 +02:00
|
|
|
if(domain.empty() || key.empty())
|
|
|
|
return defVal;
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string &data = m_domains[upperCase(domain)][lowerCase(key)];
|
2012-10-04 16:35:08 +02:00
|
|
|
if(data.empty())
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
data = defVal;
|
2014-03-17 18:38:32 +01:00
|
|
|
//gprintf("setString %s\n", defVal.c_str());
|
2021-05-11 22:57:08 +02:00
|
|
|
m_changed = true;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string Config::getStringCustomTitles(const std::string &domain, const std::string &key, const std::string &defVal)
|
2021-05-11 22:57:08 +02:00
|
|
|
{
|
|
|
|
if(domain.empty() || key.empty())
|
|
|
|
return defVal;
|
|
|
|
KeyMap::iterator i = m_groupCustomTitles.find(upperCase(domain));
|
|
|
|
if (i == m_groupCustomTitles.end()) return defVal;
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string &data = m_domains[i->second][lowerCase(key)];
|
2021-05-11 22:57:08 +02:00
|
|
|
if(data.empty())
|
|
|
|
{
|
|
|
|
data = defVal;
|
|
|
|
//gprintf("setString %s\n", defVal.c_str());
|
2012-01-21 21:57:41 +01:00
|
|
|
m_changed = true;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
std::vector<std::string> Config::getStrings(const std::string &domain, const std::string &key, char seperator, const std::string &defVal)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2021-09-06 21:16:45 +02:00
|
|
|
std::vector<std::string> retval;
|
2012-06-15 12:45:57 +02:00
|
|
|
|
|
|
|
if(domain.empty() || key.empty())
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-12-27 21:22:40 +01:00
|
|
|
if(!defVal.empty())
|
2012-01-21 21:57:41 +01:00
|
|
|
retval.push_back(defVal);
|
|
|
|
return retval;
|
|
|
|
}
|
2012-06-15 12:45:57 +02:00
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string &data = m_domains[upperCase(domain)][lowerCase(key)];
|
2012-06-15 12:45:57 +02:00
|
|
|
if(data.empty())
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-12-27 21:22:40 +01:00
|
|
|
if(!defVal.empty())
|
2012-01-21 21:57:41 +01:00
|
|
|
retval.push_back(defVal);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
// Parse the string into different substrings
|
|
|
|
|
|
|
|
// skip delimiters at beginning.
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string::size_type lastPos = data.find_first_not_of(seperator, 0);
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
// find first "non-delimiter".
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string::size_type pos = data.find_first_of(seperator, lastPos);
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-06-15 12:45:57 +02:00
|
|
|
// no seperator found, return data
|
2021-09-06 21:16:45 +02:00
|
|
|
if(pos == std::string::npos)
|
2012-06-15 12:45:57 +02:00
|
|
|
{
|
|
|
|
retval.push_back(data);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
while(std::string::npos != pos || std::string::npos != lastPos)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
// found a token, add it to the vector.
|
|
|
|
retval.push_back(data.substr(lastPos, pos - lastPos));
|
|
|
|
|
|
|
|
// skip delimiters. Note the "not_of"
|
|
|
|
lastPos = data.find_first_not_of(seperator, pos);
|
|
|
|
|
|
|
|
// find next "non-delimiter"
|
|
|
|
pos = data.find_first_of(seperator, lastPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
bool Config::getBool(const std::string &domain, const std::string &key, bool defVal)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
if (domain.empty() || key.empty()) return defVal;
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string &data = m_domains[upperCase(domain)][lowerCase(key)];
|
2012-01-21 21:57:41 +01:00
|
|
|
if (data.empty())
|
|
|
|
{
|
|
|
|
data = defVal ? "yes" : "no";
|
2014-03-17 18:38:32 +01:00
|
|
|
//gprintf("getBool %d\n", defVal);
|
2012-01-21 21:57:41 +01:00
|
|
|
m_changed = true;
|
|
|
|
return defVal;
|
|
|
|
}
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string s(lowerCase(trim(data)));
|
2012-01-21 21:57:41 +01:00
|
|
|
if (s == "yes" || s == "true" || s == "y" || s == "1")
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
bool Config::testOptBool(const std::string &domain, const std::string &key, bool defVal)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
if (domain.empty() || key.empty()) return defVal;
|
|
|
|
KeyMap &km = m_domains[upperCase(domain)];
|
|
|
|
KeyMap::iterator i = km.find(lowerCase(key));
|
|
|
|
if (i == km.end()) return defVal;
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string s(lowerCase(trim(i->second)));
|
2012-01-21 21:57:41 +01:00
|
|
|
if (s == "yes" || s == "true" || s == "y" || s == "1")
|
|
|
|
return true;
|
|
|
|
if (s == "no" || s == "false" || s == "n" || s == "0")
|
|
|
|
return false;
|
|
|
|
return defVal;
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
int Config::getOptBool(const std::string &domain, const std::string &key, int defVal)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
if (domain.empty() || key.empty()) return defVal;
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string &data = m_domains[upperCase(domain)][lowerCase(key)];
|
2012-01-21 21:57:41 +01:00
|
|
|
if (data.empty())
|
|
|
|
{
|
|
|
|
switch (defVal)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
data = "no";
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
data = "yes";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
data = "default";
|
|
|
|
}
|
2014-03-17 18:38:32 +01:00
|
|
|
//gprintf("getOptBool %s\n", data.c_str());
|
2012-01-21 21:57:41 +01:00
|
|
|
m_changed = true;
|
|
|
|
return defVal;
|
|
|
|
}
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string s(lowerCase(trim(data)));
|
2012-01-21 21:57:41 +01:00
|
|
|
if (s == "yes" || s == "true" || s == "y" || s == "1")
|
|
|
|
return 1;
|
|
|
|
if (s == "no" || s == "false" || s == "n" || s == "0")
|
|
|
|
return 0;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
int Config::getInt(const std::string &domain, const std::string &key, int defVal)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
if (domain.empty() || key.empty()) return defVal;
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string &data = m_domains[upperCase(domain)][lowerCase(key)];
|
2012-01-21 21:57:41 +01:00
|
|
|
if (data.empty())
|
|
|
|
{
|
|
|
|
data = sfmt("%i", defVal);
|
2014-03-17 18:38:32 +01:00
|
|
|
//gprintf("getInt %i\n", defVal);
|
2012-01-21 21:57:41 +01:00
|
|
|
m_changed = true;
|
|
|
|
return defVal;
|
|
|
|
}
|
|
|
|
return strtol(data.c_str(), 0, 10);
|
|
|
|
}
|
|
|
|
|
2018-10-15 16:23:34 +02:00
|
|
|
/* this returns true only if there's something after the '=' and value is set to the integer value */
|
2012-01-21 21:57:41 +01:00
|
|
|
bool Config::getInt(const std::string &domain, const std::string &key, int *value)
|
|
|
|
{
|
|
|
|
if (domain.empty() || key.empty()) return false;
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string &data = m_domains[upperCase(domain)][lowerCase(key)];
|
2012-01-21 21:57:41 +01:00
|
|
|
if (data.empty()) return false;
|
|
|
|
*value = strtol(data.c_str(), 0, 10);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
unsigned int Config::getUInt(const std::string &domain, const std::string &key, unsigned int defVal)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
if (domain.empty() || key.empty()) return defVal;
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string &data = m_domains[upperCase(domain)][lowerCase(key)];
|
2012-01-21 21:57:41 +01:00
|
|
|
if (data.empty())
|
|
|
|
{
|
|
|
|
data = sfmt("%u", defVal);
|
2014-03-17 18:38:32 +01:00
|
|
|
//gprintf("getUInt %u\n", defVal);
|
2012-01-21 21:57:41 +01:00
|
|
|
m_changed = true;
|
|
|
|
return defVal;
|
|
|
|
}
|
|
|
|
return strtoul(data.c_str(), 0, 10);
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:16:45 +02:00
|
|
|
float Config::getFloat(const std::string &domain, const std::string &key, float defVal)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
if (domain.empty() || key.empty()) return defVal;
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string &data = m_domains[upperCase(domain)][lowerCase(key)];
|
2012-01-21 21:57:41 +01:00
|
|
|
if (data.empty())
|
|
|
|
{
|
|
|
|
data = sfmt("%.*g", g_floatPrecision, defVal);
|
2014-03-17 18:38:32 +01:00
|
|
|
//gprintf("getFloat %s\n", data.c_str());
|
2012-01-21 21:57:41 +01:00
|
|
|
m_changed = true;
|
|
|
|
return defVal;
|
|
|
|
}
|
|
|
|
return strtod(data.c_str(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3D Config::getVector3D(const std::string &domain, const std::string &key, const Vector3D &defVal)
|
|
|
|
{
|
|
|
|
if (domain.empty() || key.empty()) return defVal;
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string &data = m_domains[upperCase(domain)][lowerCase(key)];
|
|
|
|
std::string::size_type i;
|
|
|
|
std::string::size_type j = std::string::npos;
|
2012-01-21 21:57:41 +01:00
|
|
|
i = data.find_first_of(',');
|
2021-09-06 21:16:45 +02:00
|
|
|
if (i != std::string::npos) j = data.find_first_of(',', i + 1);
|
|
|
|
if (j == std::string::npos)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
data = sfmt("%.*g, %.*g, %.*g", g_floatPrecision, defVal.x, g_floatPrecision, defVal.y, g_floatPrecision, defVal.z);
|
2014-03-17 18:38:32 +01:00
|
|
|
//gprintf("getVector3D\n");
|
2012-01-21 21:57:41 +01:00
|
|
|
m_changed = true;
|
|
|
|
return defVal;
|
|
|
|
}
|
|
|
|
return Vector3D(strtod(data.substr(0, i).c_str(), 0), strtod(data.substr(i + 1, j - i - 1).c_str(), 0), strtod(data.substr(j + 1).c_str(), 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
CColor Config::getColor(const std::string &domain, const std::string &key, const CColor &defVal)
|
|
|
|
{
|
|
|
|
if (domain.empty() || key.empty()) return defVal;
|
2021-09-06 21:16:45 +02:00
|
|
|
std::string &data = m_domains[upperCase(domain)][lowerCase(key)];
|
|
|
|
std::string text(upperCase(trim(data)));
|
2012-01-21 21:57:41 +01:00
|
|
|
u32 i = (u32)text.find_first_of('#');
|
2021-09-06 21:16:45 +02:00
|
|
|
if (i != std::string::npos)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
text.erase(0, i + 1);
|
|
|
|
i = (u32)text.find_first_not_of("0123456789ABCDEF");
|
2021-09-06 21:16:45 +02:00
|
|
|
if ((i != std::string::npos && i >= 6) || (i == std::string::npos && text.size() >= 6))
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2021-09-06 21:16:45 +02:00
|
|
|
u32 n = ((i != std::string::npos && i >= 8) || (i == std::string::npos && text.size() >= 8)) ? 8 : 6;
|
2012-01-21 21:57:41 +01:00
|
|
|
for (i = 0; i < n; ++i)
|
|
|
|
if (text[i] <= '9')
|
|
|
|
text[i] -= '0';
|
|
|
|
else
|
|
|
|
text[i] -= 'A' - 10;
|
|
|
|
CColor c(text[0] * 0x10 + text[1], text[2] * 0x10 + text[3], text[4] * 0x10 + text[5], 1.f);
|
|
|
|
if (n == 8)
|
|
|
|
c.a = text[6] * 0x10 + text[7];
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data = sfmt("#%.2X%.2X%.2X%.2X", defVal.r, defVal.g, defVal.b, defVal.a);
|
2014-03-17 18:38:32 +01:00
|
|
|
//gprintf("getColor\n");
|
2012-01-21 21:57:41 +01:00
|
|
|
m_changed = true;
|
|
|
|
return defVal;
|
|
|
|
}
|