X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fsettings.h;h=53e8d8ef5c85d43b30fa7c6889bb5b539824f5e2;hb=ea0df3e4cb75a7a104a81e050c019049219c4fee;hp=d75b0bec19616d7498ddeb4221b2bee4bbf5fac2;hpb=037b2591971d752e67fa7d47095b996b3f56da5a;p=minetest.git diff --git a/src/settings.h b/src/settings.h index d75b0bec1..53e8d8ef5 100644 --- a/src/settings.h +++ b/src/settings.h @@ -1,6 +1,6 @@ /* -Minetest-c55 -Copyright (C) 2010-2011 celeron55, Perttu Ahola +Minetest +Copyright (C) 2010-2013 celeron55, Perttu Ahola This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -20,18 +20,23 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifndef SETTINGS_HEADER #define SETTINGS_HEADER -#include "common_irrlicht.h" +#include "irrlichttypes_bloated.h" +#include "exceptions.h" #include -#include -#include -#include +#include "jthread/jmutex.h" +#include "jthread/jmutexautolock.h" #include "strfnd.h" #include #include #include #include "debug.h" -#include "utility.h" #include "log.h" +#include "util/string.h" +#include "util/serialize.h" +#include +#include +#include +#include "filesys.h" enum ValueType { @@ -55,29 +60,47 @@ class Settings public: Settings() { - m_mutex.Init(); } void writeLines(std::ostream &os) { JMutexAutoLock lock(m_mutex); - - for(core::map::Iterator - i = m_settings.getIterator(); - i.atEnd() == false; i++) + + for(std::map::iterator + i = m_settings.begin(); + i != m_settings.end(); ++i) { - std::string name = i.getNode()->getKey(); - std::string value = i.getNode()->getValue(); + std::string name = i->first; + std::string value = i->second; os< getNames(){ + std::vector names; + for(std::map::iterator + i = m_settings.begin(); + i != m_settings.end(); ++i) + { + names.push_back(i->first); + } + return names; + } + + // remove a setting + bool remove(const std::string& name) + { + return m_settings.erase(name); + } + bool parseConfigLine(const std::string &line) { JMutexAutoLock lock(m_mutex); - + std::string trimmedline = trim(line); - + // Ignore empty lines and comments if(trimmedline.size() == 0 || trimmedline[0] == '#') return true; @@ -91,15 +114,15 @@ class Settings if(name == "") return true; - + std::string value = sf.next("\n"); value = trim(value); /*infostream<<"Config name=\""< &dst, - core::map &updated, + std::list &dst, + std::set &updated, bool &value_changed) { JMutexAutoLock lock(m_mutex); - + if(is.eof()) return false; - + // NOTE: This function will be expanded to allow multi-line settings std::string line; std::getline(is, line); @@ -185,7 +208,7 @@ class Settings std::string line_end = ""; if(is.eof() == false) line_end = "\n"; - + // Ignore empty lines and comments if(trimmedline.size() == 0 || trimmedline[0] == '#') { @@ -203,14 +226,14 @@ class Settings dst.push_back(line+line_end); return true; } - + std::string value = sf.next("\n"); value = trim(value); - - if(m_settings.find(name)) + + if(m_settings.find(name) != m_settings.end()) { std::string newvalue = m_settings[name]; - + if(newvalue != value) { infostream<<"Changing value of \""< objects; - core::map updated; + + std::list objects; + std::set updated; bool something_actually_changed = false; - + // Read and modify stuff { std::ifstream is(filename); @@ -257,68 +282,68 @@ class Settings something_actually_changed)); } } - + JMutexAutoLock lock(m_mutex); - + // If something not yet determined to have been changed, check if // any new stuff was added if(!something_actually_changed){ - for(core::map::Iterator - i = m_settings.getIterator(); - i.atEnd() == false; i++) + for(std::map::iterator + i = m_settings.begin(); + i != m_settings.end(); ++i) { - if(updated.find(i.getNode()->getKey())) + if(updated.find(i->first) != updated.end()) continue; something_actually_changed = true; break; } } - + // If nothing was actually changed, skip writing the file if(!something_actually_changed){ infostream<<"Skipping writing of "<::Iterator + for(std::list::iterator i = objects.begin(); - i != objects.end(); i++) + i != objects.end(); ++i) { - os<<(*i); + ss<<(*i); } /* Write stuff that was not already in the file */ - for(core::map::Iterator - i = m_settings.getIterator(); - i.atEnd() == false; i++) + for(std::map::iterator + i = m_settings.begin(); + i != m_settings.end(); ++i) { - if(updated.find(i.getNode()->getKey())) + if(updated.find(i->first) != updated.end()) continue; - std::string name = i.getNode()->getKey(); - std::string value = i.getNode()->getValue(); + std::string name = i->first; + std::string value = i->second; infostream<<"Adding \""< &allowed_options) + std::map &allowed_options) { int nonopt_index = 0; int i=1; @@ -356,19 +381,19 @@ class Settings std::string name = argname.substr(2); - core::map::Node *n; + std::map::iterator n; n = allowed_options.find(name); - if(n == NULL) + if(n == allowed_options.end()) { errorstream<<"Unknown command-line parameter \"" <getValue().type; + ValueType type = n->second.type; std::string value = ""; - + if(type == VALUETYPE_FLAG) { value = "true"; @@ -384,7 +409,7 @@ class Settings value = argv[i]; i++; } - + infostream<<"Valid command-line parameter: \"" <::Node *n; + + std::map::iterator n; n = m_settings.find(name); - if(n == NULL) + if(n == m_settings.end()) { n = m_defaults.find(name); - if(n == NULL) + if(n == m_defaults.end()) { - throw SettingNotFoundException("Setting not found"); + throw SettingNotFoundException(("Setting [" + name + "] not found ").c_str()); } } - return n->getValue(); + return n->second; } + //////////// Get setting bool getBool(std::string name) { return is_yes(get(name)); } - + bool getFlag(std::string name) { try @@ -465,7 +491,7 @@ class Settings // If it is in settings if(exists(name)) return getBool(name); - + std::string s; char templine[10]; std::cout<::Iterator - i = other.m_settings.getIterator(); - i.atEnd() == false; i++) - { - m_settings[i.getNode()->getKey()] = i.getNode()->getValue(); - } - - for(core::map::Iterator - i = other.m_defaults.getIterator(); - i.atEnd() == false; i++) - { - m_defaults[i.getNode()->getKey()] = i.getNode()->getValue(); - } + m_settings.insert(other.m_settings.begin(), other.m_settings.end()); + m_defaults.insert(other.m_defaults.begin(), other.m_defaults.end()); return; } @@ -638,25 +823,11 @@ class Settings { JMutexAutoLock lock(m_mutex); JMutexAutoLock lock2(other.m_mutex); - + if(&other == this) return *this; - for(core::map::Iterator - i = other.m_settings.getIterator(); - i.atEnd() == false; i++) - { - m_settings.insert(i.getNode()->getKey(), - i.getNode()->getValue()); - } - - for(core::map::Iterator - i = other.m_defaults.getIterator(); - i.atEnd() == false; i++) - { - m_defaults.insert(i.getNode()->getKey(), - i.getNode()->getValue()); - } + update(other); return *this; @@ -666,19 +837,19 @@ class Settings { JMutexAutoLock lock(m_mutex); JMutexAutoLock lock2(other.m_mutex); - + if(&other == this) return *this; clear(); (*this) += other; - + return *this; } private: - core::map m_settings; - core::map m_defaults; + std::map m_settings; + std::map m_defaults; // All methods that access m_settings/m_defaults directly should lock this. JMutex m_mutex; };