X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Fsettings.h;h=13c8e1e65e6a7d42b61933629d624560bca05e97;hb=8852333eb3042580592ade478e5def20f8ed5d4c;hp=eec546b1a912c89aa3f2faaec3eca9c5e353eba3;hpb=11afcbff69c95915e5142bc4b55636ff6358ece9;p=oweals%2Fminetest.git diff --git a/src/settings.h b/src/settings.h index eec546b1a..13c8e1e65 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 @@ -21,10 +21,10 @@ with this program; if not, write to the Free Software Foundation, Inc., #define SETTINGS_HEADER #include "irrlichttypes_bloated.h" +#include "exceptions.h" #include -#include -#include -#include +#include "jthread/jmutex.h" +#include "jthread/jmutexautolock.h" #include "strfnd.h" #include #include @@ -32,6 +32,12 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "debug.h" #include "log.h" #include "util/string.h" +#include "util/serialize.h" +#include +#include +#include +#include "filesys.h" +#include enum ValueType { @@ -55,49 +61,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(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(); - names.push_back(name); + names.push_back(i->first); } - return names; + return names; } // remove a setting bool remove(const std::string& name) { - return m_settings.remove(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; @@ -111,15 +115,15 @@ public: 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); @@ -205,7 +209,7 @@ public: std::string line_end = ""; if(is.eof() == false) line_end = "\n"; - + // Ignore empty lines and comments if(trimmedline.size() == 0 || trimmedline[0] == '#') { @@ -223,14 +227,14 @@ public: 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); @@ -277,68 +283,68 @@ public: 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; @@ -376,19 +382,19 @@ public: 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"; @@ -404,7 +410,7 @@ public: 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 @@ -485,7 +492,7 @@ public: // 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; } @@ -665,25 +824,11 @@ public: { 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; @@ -693,19 +838,19 @@ public: { 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; };