]> git.lizzy.rs Git - minetest.git/blobdiff - src/settings.h
Disable word wrap in vertical texts in main menu
[minetest.git] / src / settings.h
index e8f376938676ecee172f247e189325098ac47c06..b95fbd1845ef4f5eae88ba24f4160b587882134f 100644 (file)
@@ -31,6 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <sstream>
 #include "debug.h"
 #include "utility.h"
+#include "log.h"
 
 enum ValueType
 {
@@ -77,11 +78,11 @@ class Settings
                
                std::string trimmedline = trim(line);
                
-               // Ignore comments
-               if(trimmedline[0] == '#')
+               // Ignore empty lines and comments
+               if(trimmedline.size() == 0 || trimmedline[0] == '#')
                        return true;
 
-               //dstream<<"trimmedline=\""<<trimmedline<<"\""<<std::endl;
+               //infostream<<"trimmedline=\""<<trimmedline<<"\""<<std::endl;
 
                Strfnd sf(trim(line));
 
@@ -94,7 +95,7 @@ class Settings
                std::string value = sf.next("\n");
                value = trim(value);
 
-               /*dstream<<"Config name=\""<<name<<"\" value=\""
+               /*infostream<<"Config name=\""<<name<<"\" value=\""
                                <<value<<"\""<<std::endl;*/
                
                m_settings[name] = value;
@@ -130,7 +131,7 @@ class Settings
                */
                std::string line;
                std::getline(is, line);
-               //dstream<<"got line: \""<<line<<"\""<<std::endl;
+               //infostream<<"got line: \""<<line<<"\""<<std::endl;
 
                return parseConfigLine(line);
        }
@@ -144,13 +145,9 @@ class Settings
        {
                std::ifstream is(filename);
                if(is.good() == false)
-               {
-                       dstream<<"Error opening configuration file \""
-                                       <<filename<<"\""<<std::endl;
                        return false;
-               }
 
-               dstream<<"Parsing configuration file: \""
+               infostream<<"Parsing configuration file: \""
                                <<filename<<"\""<<std::endl;
                                
                while(parseConfigObject(is));
@@ -171,7 +168,8 @@ class Settings
        */
        bool getUpdatedConfigObject(std::istream &is,
                        core::list<std::string> &dst,
-                       core::map<std::string, bool> &updated)
+                       core::map<std::string, bool> &updated,
+                       bool &value_changed)
        {
                JMutexAutoLock lock(m_mutex);
                
@@ -188,8 +186,8 @@ class Settings
                if(is.eof() == false)
                        line_end = "\n";
                
-               // Ignore comments
-               if(trimmedline[0] == '#')
+               // Ignore empty lines and comments
+               if(trimmedline.size() == 0 || trimmedline[0] == '#')
                {
                        dst.push_back(line+line_end);
                        return true;
@@ -215,9 +213,10 @@ class Settings
                        
                        if(newvalue != value)
                        {
-                               dstream<<"Changing value of \""<<name<<"\" = \""
+                               infostream<<"Changing value of \""<<name<<"\" = \""
                                                <<value<<"\" -> \""<<newvalue<<"\""
                                                <<std::endl;
+                               value_changed = true;
                        }
 
                        dst.push_back(name + " = " + newvalue + line_end);
@@ -235,36 +234,59 @@ class Settings
        */
        bool updateConfigFile(const char *filename)
        {
-               dstream<<"Updating configuration file: \""
+               infostream<<"Updating configuration file: \""
                                <<filename<<"\""<<std::endl;
                
                core::list<std::string> objects;
                core::map<std::string, bool> updated;
+               bool something_actually_changed = false;
                
                // Read and modify stuff
                {
                        std::ifstream is(filename);
                        if(is.good() == false)
                        {
-                               dstream<<"INFO: updateConfigFile():"
+                               infostream<<"updateConfigFile():"
                                                " Error opening configuration file"
                                                " for reading: \""
                                                <<filename<<"\""<<std::endl;
                        }
                        else
                        {
-                               while(getUpdatedConfigObject(is, objects, updated));
+                               while(getUpdatedConfigObject(is, objects, updated,
+                                               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<std::string, std::string>::Iterator
+                                       i = m_settings.getIterator();
+                                       i.atEnd() == false; i++)
+                       {
+                               if(updated.find(i.getNode()->getKey()))
+                                       continue;
+                               something_actually_changed = true;
+                               break;
+                       }
+               }
+               
+               // If nothing was actually changed, skip writing the file
+               if(!something_actually_changed){
+                       infostream<<"Skipping writing of "<<filename
+                                       <<" because content wouldn't be modified"<<std::endl;
+                       return true;
+               }
+               
                // Write stuff back
                {
                        std::ofstream os(filename);
                        if(os.good() == false)
                        {
-                               dstream<<"Error opening configuration file"
+                               errorstream<<"Error opening configuration file"
                                                " for writing: \""
                                                <<filename<<"\""<<std::endl;
                                return false;
@@ -291,7 +313,7 @@ class Settings
                                        continue;
                                std::string name = i.getNode()->getKey();
                                std::string value = i.getNode()->getValue();
-                               dstream<<"Adding \""<<name<<"\" = \""<<value<<"\""
+                               infostream<<"Adding \""<<name<<"\" = \""<<value<<"\""
                                                <<std::endl;
                                os<<name<<" = "<<value<<"\n";
                        }
@@ -308,6 +330,7 @@ class Settings
        bool parseCommandLine(int argc, char *argv[],
                        core::map<std::string, ValueSpec> &allowed_options)
        {
+               int nonopt_index = 0;
                int i=1;
                for(;;)
                {
@@ -316,7 +339,16 @@ class Settings
                        std::string argname = argv[i];
                        if(argname.substr(0, 2) != "--")
                        {
-                               dstream<<"Invalid command-line parameter \""
+                               // 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 \""
                                                <<argname<<"\": --<option> expected."<<std::endl;
                                return false;
                        }
@@ -328,7 +360,7 @@ class Settings
                        n = allowed_options.find(name);
                        if(n == NULL)
                        {
-                               dstream<<"Unknown command-line parameter \""
+                               errorstream<<"Unknown command-line parameter \""
                                                <<argname<<"\""<<std::endl;
                                return false;
                        }
@@ -345,7 +377,7 @@ class Settings
                        {
                                if(i >= argc)
                                {
-                                       dstream<<"Invalid command-line parameter \""
+                                       errorstream<<"Invalid command-line parameter \""
                                                        <<name<<"\": missing value"<<std::endl;
                                        return false;
                                }
@@ -354,7 +386,7 @@ class Settings
                        }
                        
 
-                       dstream<<"Valid command-line parameter: \""
+                       infostream<<"Valid command-line parameter: \""
                                        <<name<<"\" = \""<<value<<"\""
                                        <<std::endl;
                        set(name, value);
@@ -403,8 +435,6 @@ class Settings
                        n = m_defaults.find(name);
                        if(n == NULL)
                        {
-                               dstream<<"INFO: Settings: Setting not found: \""
-                                               <<name<<"\""<<std::endl;
                                throw SettingNotFoundException("Setting not found");
                        }
                }