X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fsettings.h;h=13c8e1e65e6a7d42b61933629d624560bca05e97;hb=eda9214f81b32d9606f425e3777616e8cfc0a44f;hp=9eb2254f06423c742da95fd76a54028ac4736f84;hpb=6ec447a1aa3678371af31aad4e186885ca4b8f27;p=dragonfireclient.git diff --git a/src/settings.h b/src/settings.h index 9eb2254f0..13c8e1e65 100644 --- a/src/settings.h +++ b/src/settings.h @@ -1,18 +1,18 @@ /* -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 General Public License as published by -the Free Software Foundation; either version 2 of the License, or +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +GNU Lesser General Public License for more details. -You should have received a copy of the GNU General Public License along +You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ @@ -20,18 +20,24 @@ 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" +#include enum ValueType { @@ -55,29 +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(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 +115,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 +209,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 +227,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 +283,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; for(;;) { @@ -338,6 +365,15 @@ class Settings std::string argname = argv[i]; if(argname.substr(0, 2) != "--") { + // If option doesn't start with -, read it in as nonoptX + if(argname[0] != '-'){ + std::string name = "nonopt"; + name += itos(nonopt_index); + set(name, argname); + nonopt_index++; + i++; + continue; + } errorstream<<"Invalid command-line parameter \"" < expected."<::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"; @@ -374,7 +410,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()) { - infostream<<"Settings: Setting not found: \"" - <getValue(); + return n->second; } + //////////// Get setting bool getBool(std::string name) { return is_yes(get(name)); } - + bool getFlag(std::string name) { try @@ -457,7 +492,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; } @@ -630,25 +824,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; @@ -658,19 +838,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; };