]> git.lizzy.rs Git - minetest.git/blobdiff - src/settings.cpp
Small fixes of minetest.has_feature
[minetest.git] / src / settings.cpp
index 2f515caba9165ef83dad7e7116b7ca3b679fe628..e1e01e81a28518bfc61836273477138796a52a6c 100644 (file)
@@ -20,7 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #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>
@@ -33,6 +33,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <cctype>
 #include <algorithm>
 
+static Settings main_settings;
+Settings *g_settings = &main_settings;
+std::string g_settings_path;
 
 Settings::~Settings()
 {
@@ -53,8 +56,8 @@ Settings & Settings::operator = (const Settings &other)
        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);
@@ -65,10 +68,11 @@ Settings & Settings::operator = (const Settings &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;
@@ -80,7 +84,7 @@ bool Settings::checkValueValid(const std::string &value)
        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;
@@ -89,9 +93,9 @@ bool Settings::checkValueValid(const std::string &value)
 
 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;
@@ -151,7 +155,7 @@ bool Settings::readConfigFile(const char *filename)
 
 bool Settings::parseConfigLines(std::istream &is, const std::string &end)
 {
-       JMutexAutoLock lock(m_mutex);
+       MutexAutoLock lock(m_mutex);
 
        std::string line, name, value;
 
@@ -190,7 +194,7 @@ bool Settings::parseConfigLines(std::istream &is, const std::string &end)
 
 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();
@@ -264,7 +268,7 @@ bool Settings::updateConfigObject(std::istream &is, std::ostream &os,
                        it = m_settings.find(name);
                        if (it != m_settings.end() && it->second.is_group) {
                                os << line << "\n";
-                               assert(it->second.group != NULL);
+                               sanity_check(it->second.group != NULL);
                                was_modified |= it->second.group->updateConfigObject(is, os,
                                        "}", tab_depth + 1);
                        } else {
@@ -294,7 +298,7 @@ bool Settings::updateConfigObject(std::istream &is, std::ostream &os,
 
 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);
@@ -375,7 +379,7 @@ bool Settings::parseCommandLine(int argc, char *argv[],
 
 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()) {
@@ -558,7 +562,7 @@ bool Settings::getNoiseParamsFromGroup(const std::string &name,
 
 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());
@@ -738,7 +742,7 @@ bool Settings::setEntry(const std::string &name, const void *data,
                return false;
 
        {
-               JMutexAutoLock lock(m_mutex);
+               MutexAutoLock lock(m_mutex);
 
                SettingsEntry &entry = set_default ? m_defaults[name] : m_settings[name];
                old_group = entry.group;
@@ -874,7 +878,7 @@ bool Settings::setNoiseParams(const std::string &name,
 
 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);
@@ -883,17 +887,22 @@ bool Settings::remove(const std::string &name)
 
 void Settings::clear()
 {
-       JMutexAutoLock lock(m_mutex);
+       MutexAutoLock lock(m_mutex);
        clearNoLock();
 }
 
+void Settings::clearDefaults()
+{
+       MutexAutoLock lock(m_mutex);
+       clearDefaultsNoLock();
+}
 
 void Settings::updateValue(const Settings &other, const std::string &name)
 {
        if (&other == this)
                return;
 
-       JMutexAutoLock lock(m_mutex);
+       MutexAutoLock lock(m_mutex);
 
        try {
                std::string val = other.get(name);
@@ -909,8 +918,8 @@ void Settings::update(const Settings &other)
        if (&other == this)
                return;
 
-       JMutexAutoLock lock(m_mutex);
-       JMutexAutoLock lock2(other.m_mutex);
+       MutexAutoLock lock(m_mutex);
+       MutexAutoLock lock2(other.m_mutex);
 
        updateNoLock(other);
 }
@@ -958,6 +967,12 @@ void Settings::clearNoLock()
                delete it->second.group;
        m_settings.clear();
 
+       clearDefaultsNoLock();
+}
+
+void Settings::clearDefaultsNoLock()
+{
+       std::map<std::string, SettingsEntry>::const_iterator it;
        for (it = m_defaults.begin(); it != m_defaults.end(); ++it)
                delete it->second.group;
        m_defaults.clear();
@@ -965,26 +980,38 @@ void Settings::clearNoLock()
 
 
 void Settings::registerChangedCallback(std::string name,
-       setting_changed_callback cbf)
+       setting_changed_callback cbf, void *userdata)
 {
-       m_callbacks[name].push_back(cbf);
+       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)
+{
+       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*> > &vector = iterToVector->second;
+
+               std::vector<std::pair<setting_changed_callback, void*> >::iterator position =
+                       std::find(vector.begin(), vector.end(), std::make_pair(cbf, userdata));
+
+               if (position != vector.end())
+                       vector.erase(position);
+       }
+}
 
 void Settings::doCallbacks(const std::string name)
 {
-       std::vector<setting_changed_callback> tempvector;
+       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())
        {
-               JMutexAutoLock lock(m_mutex);
-               if (m_callbacks.find(name) != m_callbacks.end())
+               std::vector<std::pair<setting_changed_callback, void*> >::iterator iter;
+               for (iter = iterToVector->second.begin(); iter != iterToVector->second.end(); ++iter)
                {
-                       tempvector = m_callbacks[name];
+                       (iter->first)(name, iter->second);
                }
        }
-
-       std::vector<setting_changed_callback>::iterator iter;
-       for (iter = tempvector.begin(); iter != tempvector.end(); iter++)
-       {
-               (*iter)(name);
-       }
 }