]> git.lizzy.rs Git - minetest.git/blobdiff - src/settings.h
Make flag strings clear specified flag with 'no' prefix
[minetest.git] / src / settings.h
index 18d6ade7245c185fad1c1cc457e15c6ffee81f9b..2aa03074b7d6bebec053b5927ad4663dc715442a 100644 (file)
@@ -1,6 +1,6 @@
 /*
-Minetest-c55
-Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+Minetest
+Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
 
 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 <string>
-#include <jthread.h>
-#include <jmutex.h>
-#include <jmutexautolock.h>
+#include "jthread/jmutex.h"
+#include "jthread/jmutexautolock.h"
 #include "strfnd.h"
 #include <iostream>
 #include <fstream>
@@ -32,6 +32,11 @@ 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 <list>
+#include <map>
+#include <set>
+#include "filesys.h"
 
 enum ValueType
 {
@@ -55,40 +60,38 @@ class Settings
 public:
        Settings()
        {
-               m_mutex.Init();
        }
 
        void writeLines(std::ostream &os)
        {
                JMutexAutoLock lock(m_mutex);
 
-               for(core::map<std::string, std::string>::Iterator
-                               i = m_settings.getIterator();
-                               i.atEnd() == false; i++)
+               for(std::map<std::string, std::string>::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<<name<<" = "<<value<<"\n";
                }
        }
   
-       // return all keys used 
+       // return all keys used
        std::vector<std::string> getNames(){
                std::vector<std::string> names;
-               for(core::map<std::string, std::string>::Iterator
-                               i = m_settings.getIterator();
-                               i.atEnd() == false; i++)
+               for(std::map<std::string, std::string>::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);
        }
 
 
@@ -187,8 +190,8 @@ class Settings
                Returns false on EOF
        */
        bool getUpdatedConfigObject(std::istream &is,
-                       core::list<std::string> &dst,
-                       core::map<std::string, bool> &updated,
+                       std::list<std::string> &dst,
+                       std::set<std::string> &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: \""
                                <<filename<<"\""<<std::endl;
 
-               core::list<std::string> objects;
-               core::map<std::string, bool> updated;
+               std::list<std::string> objects;
+               std::set<std::string> 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<std::string, std::string>::Iterator
-                                       i = m_settings.getIterator();
-                                       i.atEnd() == false; i++)
+                       for(std::map<std::string, std::string>::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;
@@ -303,39 +308,39 @@ class Settings
 
                // Write stuff back
                {
-                       std::ofstream os(filename);
-                       if(os.good() == false)
-                       {
-                               errorstream<<"Error opening configuration file"
-                                               " for writing: \""
-                                               <<filename<<"\""<<std::endl;
-                               return false;
-                       }
+                       std::ostringstream ss(std::ios_base::binary);
 
                        /*
                                Write updated stuff
                        */
-                       for(core::list<std::string>::Iterator
+                       for(std::list<std::string>::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<std::string, std::string>::Iterator
-                                       i = m_settings.getIterator();
-                                       i.atEnd() == false; i++)
+                       for(std::map<std::string, std::string>::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 \""<<name<<"\" = \""<<value<<"\""
                                                <<std::endl;
-                               os<<name<<" = "<<value<<"\n";
+                               ss<<name<<" = "<<value<<"\n";
+                       }
+
+                       if(!fs::safeWriteToFile(filename, ss.str()))
+                       {
+                               errorstream<<"Error writing configuration file: \""
+                                               <<filename<<"\""<<std::endl;
+                               return false;
                        }
                }
 
@@ -348,7 +353,7 @@ class Settings
                returns true on success
        */
        bool parseCommandLine(int argc, char *argv[],
-                       core::map<std::string, ValueSpec> &allowed_options)
+                       std::map<std::string, ValueSpec> &allowed_options)
        {
                int nonopt_index = 0;
                int i=1;
@@ -376,16 +381,16 @@ class Settings
 
                        std::string name = argname.substr(2);
 
-                       core::map<std::string, ValueSpec>::Node *n;
+                       std::map<std::string, ValueSpec>::iterator n;
                        n = allowed_options.find(name);
-                       if(n == NULL)
+                       if(n == allowed_options.end())
                        {
                                errorstream<<"Unknown command-line parameter \""
                                                <<argname<<"\""<<std::endl;
                                return false;
                        }
 
-                       ValueType type = n->getValue().type;
+                       ValueType type = n->second.type;
 
                        std::string value = "";
 
@@ -441,27 +446,28 @@ 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<std::string, std::string>::Node *n;
+               std::map<std::string, std::string>::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));
@@ -566,35 +572,33 @@ class Settings
                return value;
        }
 
-//template<typename T> struct alignment_trick { char c; T member; };
-//#define ALIGNOF(type) offsetof (alignment_trick<type>, 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, u32 *flagmask)
+       {
+               std::string val = get(name);
+               return (isdigit(val[0])) ? stoi(val) :
+                       readFlagString(val, flagdesc, flagmask);
+       }
 
-       template <class T> T *getStruct(std::string name, std::string format)
+       // N.B. if getStruct() is used to read a non-POD aggregate type,
+       // the behavior is undefined.
+       bool getStruct(std::string name, std::string format, void *out, size_t olen)
        {
-               size_t len = sizeof(T);
+               size_t len = olen;
                std::vector<std::string *> strs_alloced;
-               std::string *str;
-               std::string valstr = get(name);
-               char *s = &valstr[0];
-               T *buf = new T;
-               char *bufpos = (char *)buf;
+               std::string *str, valstr;
                char *f, *snext;
                size_t pos;
 
+               try {
+                       valstr = get(name);
+               } catch (SettingNotFoundException &e) {
+                       return false;
+               }
+
+               char *s = &valstr[0];
+               char *buf = new char[len];
+               char *bufpos = buf;
+
                char *fmtpos, *fmt = &format[0];
                while ((f = strtok_r(fmt, ",", &fmtpos)) && s) {
                        fmt = NULL;
@@ -614,7 +618,7 @@ typedef long long int s64; //to be added to src/irrlichttypes.h later
                                case 'i':
                                        if (width == 16) {
                                                bufpos += PADDING(bufpos, u16);
-                                               if ((bufpos - (char *)buf) + sizeof(u16) <= len) {
+                                               if ((bufpos - buf) + sizeof(u16) <= len) {
                                                        if (is_unsigned)
                                                                *(u16 *)bufpos = (u16)strtoul(s, &s, 10);
                                                        else
@@ -623,7 +627,7 @@ typedef long long int s64; //to be added to src/irrlichttypes.h later
                                                bufpos += sizeof(u16);
                                        } else if (width == 32) {
                                                bufpos += PADDING(bufpos, u32);
-                                               if ((bufpos - (char *)buf) + sizeof(u32) <= len) {
+                                               if ((bufpos - buf) + sizeof(u32) <= len) {
                                                        if (is_unsigned)
                                                                *(u32 *)bufpos = (u32)strtoul(s, &s, 10);
                                                        else
@@ -632,7 +636,7 @@ typedef long long int s64; //to be added to src/irrlichttypes.h later
                                                bufpos += sizeof(u32);
                                        } else if (width == 64) {
                                                bufpos += PADDING(bufpos, u64);
-                                               if ((bufpos - (char *)buf) + sizeof(u64) <= len) {
+                                               if ((bufpos - buf) + sizeof(u64) <= len) {
                                                        if (is_unsigned)
                                                                *(u64 *)bufpos = (u64)strtoull(s, &s, 10);
                                                        else
@@ -648,7 +652,7 @@ typedef long long int s64; //to be added to src/irrlichttypes.h later
                                                *snext++ = 0;
 
                                        bufpos += PADDING(bufpos, bool);
-                                       if ((bufpos - (char *)buf) + sizeof(bool) <= len)
+                                       if ((bufpos - buf) + sizeof(bool) <= len)
                                                *(bool *)bufpos = is_yes(std::string(s));
                                        bufpos += sizeof(bool);
 
@@ -656,7 +660,7 @@ typedef long long int s64; //to be added to src/irrlichttypes.h later
                                        break;
                                case 'f':
                                        bufpos += PADDING(bufpos, float);
-                                       if ((bufpos - (char *)buf) + sizeof(float) <= len)
+                                       if ((bufpos - buf) + sizeof(float) <= len)
                                                *(float *)bufpos = strtof(s, &s);
                                        bufpos += sizeof(float);
 
@@ -680,7 +684,7 @@ typedef long long int s64; //to be added to src/irrlichttypes.h later
                                        while ((pos = str->find("\\\"", pos)) != std::string::npos)
                                                str->erase(pos, 1);
 
-                                       if ((bufpos - (char *)buf) + sizeof(std::string *) <= len)
+                                       if ((bufpos - buf) + sizeof(std::string *) <= len)
                                                *(std::string **)bufpos = str;
                                        bufpos += sizeof(std::string *);
                                        strs_alloced.push_back(str);
@@ -696,7 +700,7 @@ typedef long long int s64; //to be added to src/irrlichttypes.h later
                                        if (width == 2) {
                                                bufpos += PADDING(bufpos, v2f);
 
-                                               if ((bufpos - (char *)buf) + sizeof(v2f) <= len) {
+                                               if ((bufpos - buf) + sizeof(v2f) <= len) {
                                                v2f *v = (v2f *)bufpos;
                                                        v->X = strtof(s, &s);
                                                        s++;
@@ -706,7 +710,7 @@ typedef long long int s64; //to be added to src/irrlichttypes.h later
                                                bufpos += sizeof(v2f);
                                        } else if (width == 3) {
                                                bufpos += PADDING(bufpos, v3f);
-                                               if ((bufpos - (char *)buf) + sizeof(v3f) <= len) {
+                                               if ((bufpos - buf) + sizeof(v3f) <= len) {
                                                        v3f *v = (v3f *)bufpos;
                                                        v->X = strtof(s, &s);
                                                        s++;
@@ -726,22 +730,147 @@ typedef long long int s64; //to be added to src/irrlichttypes.h later
                        if (s && *s == ',')
                                s++;
 
-                       if ((size_t)(bufpos - (char *)buf) > len) //error, buffer too small
+                       if ((size_t)(bufpos - buf) > len) //error, buffer too small
                                goto fail;
                }
 
                if (f && *f) { //error, mismatched number of fields and values
 fail:
-                       for (unsigned int i = 0; i != strs_alloced.size(); i++)
+                       for (size_t i = 0; i != strs_alloced.size(); i++)
                                delete strs_alloced[i];
-                       delete buf;
-                       //delete[] buf;
-                       buf = NULL;
+                       delete[] buf;
+                       return false;
+               }
+
+               memcpy(out, buf, olen);
+               delete[] buf;
+               return true;
+       }
+
+       //////////// Try to get value, no exception thrown
+       bool getNoEx(std::string name, std::string &val)
+       {
+               try {
+                       val = get(name);
+                       return true;
+               } catch (SettingNotFoundException &e) {
+                       return false;
+               }
+       }
+
+       // N.B. getFlagStrNoEx() does not set val, but merely modifies it.  Thus,
+       // val must be initialized before using getFlagStrNoEx().  The intention of
+       // this is to simplify modifying a flags field from a default value.
+       bool getFlagStrNoEx(std::string name, u32 &val, FlagDesc *flagdesc)
+       {
+               try {
+                       u32 flags, flagmask;
+
+                       flags = getFlagStr(name, flagdesc, &flagmask);
+
+                       val &= ~flagmask;
+                       val |=  flags;
+
+                       return true;
+               } catch (SettingNotFoundException &e) {
+                       return false;
+               }
+       }
+
+       bool getFloatNoEx(std::string name, float &val)
+       {
+               try {
+                       val = getFloat(name);
+                       return true;
+               } catch (SettingNotFoundException &e) {
+                       return false;
+               }
+       }
+
+       bool getU16NoEx(std::string name, int &val)
+       {
+               try {
+                       val = getU16(name);
+                       return true;
+               } catch (SettingNotFoundException &e) {
+                       return false;
+               }
+       }
+
+       bool getU16NoEx(std::string name, u16 &val)
+       {
+               try {
+                       val = getU16(name);
+                       return true;
+               } catch (SettingNotFoundException &e) {
+                       return false;
+               }
+       }
+
+       bool getS16NoEx(std::string name, int &val)
+       {
+               try {
+                       val = getU16(name);
+                       return true;
+               } catch (SettingNotFoundException &e) {
+                       return false;
                }
+       }
 
-               return buf;
+       bool getS16NoEx(std::string name, s16 &val)
+       {
+               try {
+                       val = getS16(name);
+                       return true;
+               } catch (SettingNotFoundException &e) {
+                       return false;
+               }
+       }
+
+       bool getS32NoEx(std::string name, s32 &val)
+       {
+               try {
+                       val = getS32(name);
+                       return true;
+               } catch (SettingNotFoundException &e) {
+                       return false;
+               }
+       }
+
+       bool getV3FNoEx(std::string name, v3f &val)
+       {
+               try {
+                       val = getV3F(name);
+                       return true;
+               } catch (SettingNotFoundException &e) {
+                       return false;
+               }
        }
 
+       bool getV2FNoEx(std::string name, v2f &val)
+       {
+               try {
+                       val = getV2F(name);
+                       return true;
+               } catch (SettingNotFoundException &e) {
+                       return false;
+               }
+       }
+
+       bool getU64NoEx(std::string name, u64 &val)
+       {
+               try {
+                       val = getU64(name);
+                       return true;
+               } catch (SettingNotFoundException &e) {
+                       return false;
+               }
+       }
+
+       //////////// Set setting
+
+       // N.B. if setStruct() is used to write a non-POD aggregate type,
+       // the behavior is undefined.
        bool setStruct(std::string name, std::string format, void *value)
        {
                char sbuf[2048];
@@ -846,6 +975,12 @@ typedef long long int s64; //to be added to src/irrlichttypes.h later
                return true;
        }
 
+       void setFlagStr(std::string name, u32 flags,
+               FlagDesc *flagdesc, u32 flagmask)
+       {
+               set(name, writeFlagString(flags, flagdesc, flagmask));
+       }
+
        void setBool(std::string name, bool value)
        {
                if(value)
@@ -922,19 +1057,8 @@ typedef long long int s64; //to be added to src/irrlichttypes.h later
                if(&other == this)
                        return;
 
-               for(core::map<std::string, std::string>::Iterator
-                               i = other.m_settings.getIterator();
-                               i.atEnd() == false; i++)
-               {
-                       m_settings[i.getNode()->getKey()] = i.getNode()->getValue();
-               }
-
-               for(core::map<std::string, std::string>::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 +1071,7 @@ typedef long long int s64; //to be added to src/irrlichttypes.h later
                if(&other == this)
                        return *this;
 
-               for(core::map<std::string, std::string>::Iterator
-                               i = other.m_settings.getIterator();
-                               i.atEnd() == false; i++)
-               {
-                       m_settings.insert(i.getNode()->getKey(),
-                                       i.getNode()->getValue());
-               }
-
-               for(core::map<std::string, std::string>::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 +1092,8 @@ typedef long long int s64; //to be added to src/irrlichttypes.h later
        }
 
 private:
-       core::map<std::string, std::string> m_settings;
-       core::map<std::string, std::string> m_defaults;
+       std::map<std::string, std::string> m_settings;
+       std::map<std::string, std::string> m_defaults;
        // All methods that access m_settings/m_defaults directly should lock this.
        JMutex m_mutex;
 };