]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/settings.h
Add a callback: minetest.register_on_craft(itemstack, player,
[dragonfireclient.git] / src / settings.h
index 0696b819015c6bd7c5af630973bf626d8191ec60..4ee5b591322a9070f6bf7b685f034cd823b1e54c 100644 (file)
@@ -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>
@@ -36,6 +36,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <list>
 #include <map>
 #include <set>
+#include "filesys.h"
 
 enum ValueType
 {
@@ -308,14 +309,7 @@ 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
@@ -324,7 +318,7 @@ class Settings
                                        i = objects.begin();
                                        i != objects.end(); ++i)
                        {
-                               os<<(*i);
+                               ss<<(*i);
                        }
 
                        /*
@@ -340,7 +334,14 @@ class Settings
                                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;
                        }
                }
 
@@ -460,7 +461,7 @@ class Settings
                        n = m_defaults.find(name);
                        if(n == m_defaults.end())
                        {
-                               throw SettingNotFoundException("Setting not found");
+                               throw SettingNotFoundException(("Setting [" + name + "] not found ").c_str());
                        }
                }
 
@@ -577,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;
 
@@ -608,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
@@ -617,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
@@ -626,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
@@ -642,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);
 
@@ -650,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);
 
@@ -674,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);
@@ -690,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++;
@@ -700,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++;
@@ -720,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)