X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fsettings.h;h=1b7e3cb095bbba716ba13988adea002103f7ceb9;hb=e1ff5b13619666e5b987ecf4faaf294400ffd979;hp=18d6ade7245c185fad1c1cc457e15c6ffee81f9b;hpb=631a835e0782a2696762e3d55f75616f5a063394;p=dragonfireclient.git diff --git a/src/settings.h b/src/settings.h index 18d6ade72..1b7e3cb09 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 @@ -32,6 +32,10 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "debug.h" #include "log.h" #include "util/string.h" +#include "porting.h" +#include +#include +#include enum ValueType { @@ -62,12 +66,12 @@ class Settings { 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; } @@ -88,7 +91,7 @@ class Settings // remove a setting bool remove(const std::string& name) { - return m_settings.remove(name); + return m_settings.erase(name); } @@ -187,8 +190,8 @@ class Settings Returns false on EOF */ bool getUpdatedConfigObject(std::istream &is, - core::list &dst, - core::map &updated, + std::list &dst, + std::set &updated, bool &value_changed) { JMutexAutoLock lock(m_mutex); @@ -227,7 +230,7 @@ class Settings 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]; @@ -241,9 +244,11 @@ class Settings dst.push_back(name + " = " + newvalue + line_end); - updated[name] = true; + updated.insert(name); } - + else //file contains a setting which is not in m_settings + value_changed=true; + return true; } @@ -257,8 +262,8 @@ class Settings infostream<<"Updating configuration file: \"" < objects; - core::map updated; + std::list objects; + std::set updated; bool something_actually_changed = false; // Read and modify stuff @@ -283,11 +288,11 @@ class Settings // 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; @@ -315,9 +320,9 @@ class Settings /* Write updated stuff */ - for(core::list::Iterator + for(std::list::iterator i = objects.begin(); - i != objects.end(); i++) + i != objects.end(); ++i) { os<<(*i); } @@ -325,14 +330,14 @@ class Settings /* 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,16 +381,16 @@ 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 = ""; @@ -441,25 +446,25 @@ class Settings { JMutexAutoLock lock(m_mutex); - return (m_settings.find(name) || m_defaults.find(name)); + return (m_settings.find(name) != m_settings.end() || m_defaults.find(name) != m_defaults.end()); } std::string get(std::string name) { JMutexAutoLock lock(m_mutex); - core::map::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; } bool getBool(std::string name) @@ -566,22 +571,11 @@ class Settings return value; } -//template struct alignment_trick { char c; T member; }; -//#define ALIGNOF(type) offsetof (alignment_trick, member) -#ifdef _WIN32 - #define ALIGNOF(x) __alignof(x) -#else - #define ALIGNOF(x) __alignof__(x) -#endif -#define PADDING(x, y) ((ALIGNOF(y) - ((uintptr_t)(x) & (ALIGNOF(y) - 1))) & (ALIGNOF(y) - 1)) -#ifdef _WIN32 - #define strtok_r(x, y, z) strtok_s(x, y, z) - #define strtof(x, y) (float)strtod(x, y) - #define strtoll(x, y, z) _strtoi64(x, y, z) - #define strtoull(x, y, z) _strtoui64(x, y, z) -#endif - -typedef long long int s64; //to be added to src/irrlichttypes.h later + u32 getFlagStr(std::string name, FlagDesc *flagdesc) + { + std::string val = get(name); + return (isdigit(val[0])) ? stoi(val) : readFlagString(val, flagdesc); + } template T *getStruct(std::string name, std::string format) { @@ -845,6 +839,11 @@ typedef long long int s64; //to be added to src/irrlichttypes.h later set(name, std::string(sbuf)); return true; } + + void setFlagStr(std::string name, u32 flags, FlagDesc *flagdesc) + { + set(name, writeFlagString(flags, flagdesc)); + } void setBool(std::string name, bool value) { @@ -922,19 +921,8 @@ typedef long long int s64; //to be added to src/irrlichttypes.h later if(&other == this) return; - for(core::map::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; } @@ -947,21 +935,7 @@ typedef long long int s64; //to be added to src/irrlichttypes.h later 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; @@ -982,8 +956,8 @@ typedef long long int s64; //to be added to src/irrlichttypes.h later } 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; };