- core::map<std::string, ValueSpec> &allowed_options)
- {
- int nonopt_index = 0;
- int i=1;
- for(;;)
- {
- if(i >= argc)
- break;
- std::string argname = argv[i];
- if(argname.substr(0, 2) != "--")
- {
- // 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;
- }
- i++;
-
- std::string name = argname.substr(2);
-
- core::map<std::string, ValueSpec>::Node *n;
- n = allowed_options.find(name);
- if(n == NULL)
- {
- errorstream<<"Unknown command-line parameter \""
- <<argname<<"\""<<std::endl;
- return false;
- }
-
- ValueType type = n->getValue().type;
-
- std::string value = "";
-
- if(type == VALUETYPE_FLAG)
- {
- value = "true";
- }
- else
- {
- if(i >= argc)
- {
- errorstream<<"Invalid command-line parameter \""
- <<name<<"\": missing value"<<std::endl;
- return false;
- }
- value = argv[i];
- i++;
- }
-
-
- infostream<<"Valid command-line parameter: \""
- <<name<<"\" = \""<<value<<"\""
- <<std::endl;
- set(name, value);
- }
-
- return true;
- }
-
- void set(std::string name, std::string value)
- {
- JMutexAutoLock lock(m_mutex);
-
- m_settings[name] = value;
- }
-
- void set(std::string name, const char *value)
- {
- JMutexAutoLock lock(m_mutex);
-
- m_settings[name] = value;
- }
-
-
- void setDefault(std::string name, std::string value)
- {
- JMutexAutoLock lock(m_mutex);
-
- m_defaults[name] = value;
- }
-
- bool exists(std::string name)
- {
- JMutexAutoLock lock(m_mutex);
-
- return (m_settings.find(name) || m_defaults.find(name));
- }
-
- std::string get(std::string name)
- {
- JMutexAutoLock lock(m_mutex);
-
- core::map<std::string, std::string>::Node *n;
- n = m_settings.find(name);
- if(n == NULL)
- {
- n = m_defaults.find(name);
- if(n == NULL)
- {
- throw SettingNotFoundException("Setting not found");
- }
- }
-
- return n->getValue();
- }
-
- bool getBool(std::string name)
- {
- return is_yes(get(name));
- }
-
- bool getFlag(std::string name)
- {
- try
- {
- return getBool(name);
- }
- catch(SettingNotFoundException &e)
- {
- return false;
- }
- }
-
- // Asks if empty
- bool getBoolAsk(std::string name, std::string question, bool def)
- {
- // If it is in settings
- if(exists(name))
- return getBool(name);
-
- std::string s;
- char templine[10];
- std::cout<<question<<" [y/N]: ";
- std::cin.getline(templine, 10);
- s = templine;
-
- if(s == "")
- return def;
-
- return is_yes(s);
- }
-
- float getFloat(std::string name)
- {
- return stof(get(name));
- }
+ std::map<std::string, ValueSpec> &allowed_options);
+ bool parseConfigLines(std::istream &is, const std::string &end = "");
+ void writeLines(std::ostream &os, u32 tab_depth=0) const;
+
+ SettingsParseEvent parseConfigObject(const std::string &line,
+ const std::string &end, std::string &name, std::string &value);
+ bool updateConfigObject(std::istream &is, std::ostream &os,
+ const std::string &end, u32 tab_depth=0);
+
+ static bool checkNameValid(const std::string &name);
+ static bool checkValueValid(const std::string &value);
+ static std::string sanitizeName(const std::string &name);
+ static std::string sanitizeValue(const std::string &value);
+ static std::string getMultiline(std::istream &is, size_t *num_lines=NULL);
+ static void printEntry(std::ostream &os, const std::string &name,
+ const SettingsEntry &entry, u32 tab_depth=0);
+
+ /***********
+ * Getters *
+ ***********/
+
+ const SettingsEntry &getEntry(const std::string &name) const;
+ Settings *getGroup(const std::string &name) const;
+ std::string get(const std::string &name) const;
+ bool getBool(const std::string &name) const;
+ u16 getU16(const std::string &name) const;
+ s16 getS16(const std::string &name) const;
+ s32 getS32(const std::string &name) const;
+ u64 getU64(const std::string &name) const;
+ float getFloat(const std::string &name) const;
+ v2f getV2F(const std::string &name) const;
+ v3f getV3F(const std::string &name) const;
+ u32 getFlagStr(const std::string &name, const FlagDesc *flagdesc,
+ u32 *flagmask) const;
+ // N.B. if getStruct() is used to read a non-POD aggregate type,
+ // the behavior is undefined.
+ bool getStruct(const std::string &name, const std::string &format,
+ void *out, size_t olen) const;
+ bool getNoiseParams(const std::string &name, NoiseParams &np) const;
+ bool getNoiseParamsFromValue(const std::string &name, NoiseParams &np) const;
+ bool getNoiseParamsFromGroup(const std::string &name, NoiseParams &np) const;
+
+ // return all keys used
+ std::vector<std::string> getNames() const;
+ bool exists(const std::string &name) const;
+
+
+ /***************************************
+ * Getters that don't throw exceptions *
+ ***************************************/
+
+ bool getEntryNoEx(const std::string &name, SettingsEntry &val) const;
+ bool getGroupNoEx(const std::string &name, Settings *&val) const;
+ bool getNoEx(const std::string &name, std::string &val) const;
+ bool getFlag(const std::string &name) const;
+ bool getU16NoEx(const std::string &name, u16 &val) const;
+ bool getS16NoEx(const std::string &name, s16 &val) const;
+ bool getS32NoEx(const std::string &name, s32 &val) const;
+ bool getU64NoEx(const std::string &name, u64 &val) const;
+ bool getFloatNoEx(const std::string &name, float &val) const;
+ bool getV2FNoEx(const std::string &name, v2f &val) const;
+ bool getV3FNoEx(const std::string &name, v3f &val) const;
+ // 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(const std::string &name, u32 &val, FlagDesc *flagdesc) const;
+
+
+ /***********
+ * Setters *
+ ***********/
+
+ // N.B. Groups not allocated with new must be set to NULL in the settings
+ // tree before object destruction.
+ bool setEntry(const std::string &name, const void *entry,
+ bool set_group, bool set_default);
+ bool set(const std::string &name, const std::string &value);
+ bool setDefault(const std::string &name, const std::string &value);
+ bool setGroup(const std::string &name, Settings *group);
+ bool setGroupDefault(const std::string &name, Settings *group);
+ bool setBool(const std::string &name, bool value);
+ bool setS16(const std::string &name, s16 value);
+ bool setU16(const std::string &name, u16 value);
+ bool setS32(const std::string &name, s32 value);
+ bool setU64(const std::string &name, u64 value);
+ bool setFloat(const std::string &name, float value);
+ bool setV2F(const std::string &name, v2f value);
+ bool setV3F(const std::string &name, v3f value);
+ bool setFlagStr(const std::string &name, u32 flags,
+ const FlagDesc *flagdesc, u32 flagmask);
+ bool setNoiseParams(const std::string &name, const NoiseParams &np,
+ bool set_default=false);
+ // N.B. if setStruct() is used to write a non-POD aggregate type,
+ // the behavior is undefined.
+ bool setStruct(const std::string &name, const std::string &format, void *value);
+
+ // remove a setting
+ bool remove(const std::string &name);
+ void clear();
+ void updateValue(const Settings &other, const std::string &name);
+ void update(const Settings &other);
+ void registerChangedCallback(std::string name, setting_changed_callback cbf);