]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/settings.h
Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenu
[dragonfireclient.git] / src / settings.h
index 7ac308cc0ee468f2027afd2c56afac205d74e641..62596f869c9010b9bf47d1ce2a9eeb31aa18743c 100644 (file)
@@ -21,8 +21,8 @@ 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 "strfnd.h"
@@ -33,6 +33,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "log.h"
 #include "util/string.h"
 #include "porting.h"
+#include <list>
+#include <map>
+#include <set>
+#include "filesys.h"
 
 enum ValueType
 {
@@ -63,12 +67,12 @@ class Settings
        {
                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";
                }
        }
@@ -76,12 +80,11 @@ class Settings
        // 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;  
        }
@@ -89,7 +92,7 @@ class Settings
        // remove a setting
        bool remove(const std::string& name)
        {
-               return m_settings.remove(name);
+               return m_settings.erase(name);
        }
 
 
@@ -188,8 +191,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);
@@ -228,7 +231,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];
 
@@ -242,7 +245,7 @@ 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;
@@ -260,8 +263,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
@@ -286,11 +289,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;
@@ -306,39 +309,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;
                        }
                }
 
@@ -351,7 +354,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;
@@ -379,16 +382,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 = "";
 
@@ -444,25 +447,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<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;
        }
 
        bool getBool(std::string name)
@@ -575,15 +578,15 @@ class Settings
                return (isdigit(val[0])) ? stoi(val) : readFlagString(val, flagdesc);
        }
 
-       template <class T> T *getStruct(std::string name, std::string format)
+       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;
+               char *buf = new char[len];
+               char *bufpos = buf;
                char *f, *snext;
                size_t pos;
 
@@ -606,7 +609,7 @@ class Settings
                                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
@@ -615,7 +618,7 @@ class Settings
                                                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
@@ -624,7 +627,7 @@ class Settings
                                                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
@@ -640,7 +643,7 @@ class Settings
                                                *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);
 
@@ -648,7 +651,7 @@ class Settings
                                        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);
 
@@ -672,7 +675,7 @@ class Settings
                                        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);
@@ -688,7 +691,7 @@ class Settings
                                        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++;
@@ -698,7 +701,7 @@ class Settings
                                                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++;
@@ -718,20 +721,21 @@ class Settings
                        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;
                }
 
-               return buf;
+               memcpy(out, buf, olen);
+               delete[] buf;
+               return true;
        }
 
        bool setStruct(std::string name, std::string format, void *value)
@@ -919,19 +923,8 @@ class Settings
                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;
        }
@@ -944,21 +937,7 @@ class Settings
                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;
 
@@ -979,8 +958,8 @@ class Settings
        }
 
 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;
 };