#include "settings.h"
#include "irrlichttypes_bloated.h"
#include "exceptions.h"
-#include "jthread/jmutexautolock.h"
+#include "threading/mutex_auto_lock.h"
#include "strfnd.h"
#include <iostream>
#include <fstream>
#include <cctype>
#include <algorithm>
+static Settings main_settings;
+Settings *g_settings = &main_settings;
+std::string g_settings_path;
Settings::~Settings()
{
if (&other == this)
return *this;
- JMutexAutoLock lock(m_mutex);
- JMutexAutoLock lock2(other.m_mutex);
+ MutexAutoLock lock(m_mutex);
+ MutexAutoLock lock2(other.m_mutex);
clearNoLock();
updateNoLock(other);
bool Settings::checkNameValid(const std::string &name)
{
- size_t pos = name.find_first_of("\t\n\v\f\r\b =\"{}#");
- if (pos != std::string::npos) {
- errorstream << "Invalid character '" << name[pos]
- << "' found in setting name" << std::endl;
+ bool valid = name.find_first_of("=\"{}#") == std::string::npos;
+ if (valid) valid = trim(name) == name;
+ if (!valid) {
+ errorstream << "Invalid setting name \"" << name << "\""
+ << std::endl;
return false;
}
return true;
if (value.substr(0, 3) == "\"\"\"" ||
value.find("\n\"\"\"") != std::string::npos) {
errorstream << "Invalid character sequence '\"\"\"' found in"
- " setting value" << std::endl;
+ " setting value!" << std::endl;
return false;
}
return true;
std::string Settings::sanitizeName(const std::string &name)
{
- std::string n(name);
+ std::string n = trim(name);
- for (const char *s = "\t\n\v\f\r\b =\"{}#"; *s; s++)
+ for (const char *s = "=\"{}#"; *s; s++)
n.erase(std::remove(n.begin(), n.end(), *s), n.end());
return n;
bool Settings::parseConfigLines(std::istream &is, const std::string &end)
{
- JMutexAutoLock lock(m_mutex);
+ MutexAutoLock lock(m_mutex);
std::string line, name, value;
void Settings::writeLines(std::ostream &os, u32 tab_depth) const
{
- JMutexAutoLock lock(m_mutex);
+ MutexAutoLock lock(m_mutex);
for (std::map<std::string, SettingsEntry>::const_iterator
it = m_settings.begin();
bool Settings::updateConfigFile(const char *filename)
{
- JMutexAutoLock lock(m_mutex);
+ MutexAutoLock lock(m_mutex);
std::ifstream is(filename);
std::ostringstream os(std::ios_base::binary);
const SettingsEntry &Settings::getEntry(const std::string &name) const
{
- JMutexAutoLock lock(m_mutex);
+ MutexAutoLock lock(m_mutex);
std::map<std::string, SettingsEntry>::const_iterator n;
if ((n = m_settings.find(name)) == m_settings.end()) {
bool Settings::exists(const std::string &name) const
{
- JMutexAutoLock lock(m_mutex);
+ MutexAutoLock lock(m_mutex);
return (m_settings.find(name) != m_settings.end() ||
m_defaults.find(name) != m_defaults.end());
return false;
{
- JMutexAutoLock lock(m_mutex);
+ MutexAutoLock lock(m_mutex);
SettingsEntry &entry = set_default ? m_defaults[name] : m_settings[name];
old_group = entry.group;
bool Settings::remove(const std::string &name)
{
- JMutexAutoLock lock(m_mutex);
+ MutexAutoLock lock(m_mutex);
- delete m_settings[name].group;
- return m_settings.erase(name);
+ std::map<std::string, SettingsEntry>::iterator it = m_settings.find(name);
+ if (it != m_settings.end()) {
+ delete it->second.group;
+ m_settings.erase(it);
+ return true;
+ } else {
+ return false;
+ }
}
void Settings::clear()
{
- JMutexAutoLock lock(m_mutex);
+ MutexAutoLock lock(m_mutex);
clearNoLock();
}
void Settings::clearDefaults()
{
- JMutexAutoLock lock(m_mutex);
+ MutexAutoLock lock(m_mutex);
clearDefaultsNoLock();
}
if (&other == this)
return;
- JMutexAutoLock lock(m_mutex);
+ MutexAutoLock lock(m_mutex);
try {
std::string val = other.get(name);
if (&other == this)
return;
- JMutexAutoLock lock(m_mutex);
- JMutexAutoLock lock2(other.m_mutex);
+ MutexAutoLock lock(m_mutex);
+ MutexAutoLock lock2(other.m_mutex);
updateNoLock(other);
}
void Settings::registerChangedCallback(std::string name,
setting_changed_callback cbf, void *userdata)
{
- JMutexAutoLock lock(m_callbackMutex);
+ MutexAutoLock lock(m_callbackMutex);
m_callbacks[name].push_back(std::make_pair(cbf, userdata));
}
void Settings::deregisterChangedCallback(std::string name, setting_changed_callback cbf, void *userdata)
{
- JMutexAutoLock lock(m_callbackMutex);
+ MutexAutoLock lock(m_callbackMutex);
std::map<std::string, std::vector<std::pair<setting_changed_callback, void*> > >::iterator iterToVector = m_callbacks.find(name);
if (iterToVector != m_callbacks.end())
{
void Settings::doCallbacks(const std::string name)
{
- JMutexAutoLock lock(m_callbackMutex);
+ MutexAutoLock lock(m_callbackMutex);
std::map<std::string, std::vector<std::pair<setting_changed_callback, void*> > >::iterator iterToVector = m_callbacks.find(name);
if (iterToVector != m_callbacks.end())
{
std::vector<std::pair<setting_changed_callback, void*> >::iterator iter;
- for (iter = iterToVector->second.begin(); iter != iterToVector->second.end(); iter++)
+ for (iter = iterToVector->second.begin(); iter != iterToVector->second.end(); ++iter)
{
(iter->first)(name, iter->second);
}