- std::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);
-
- std::map<std::string, ValueSpec>::iterator n;
- n = allowed_options.find(name);
- if(n == allowed_options.end())
- {
- errorstream<<"Unknown command-line parameter \""
- <<argname<<"\""<<std::endl;
- return false;
- }
-
- ValueType type = n->second.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(const std::string &name, std::string value)
- {
- JMutexAutoLock lock(m_mutex);
-
- m_settings[name] = value;
- }
-
- void set(const std::string &name, const char *value)
- {
- JMutexAutoLock lock(m_mutex);
-
- m_settings[name] = value;
- }
-
-
- void setDefault(const std::string &name, std::string value)
- {
- JMutexAutoLock lock(m_mutex);
-
- m_defaults[name] = value;
- }
-
- bool exists(const std::string &name) const
- {
- JMutexAutoLock lock(m_mutex);
-
- return (m_settings.find(name) != m_settings.end() ||
- m_defaults.find(name) != m_defaults.end());
- }
-
- std::string get(const std::string &name) const
- {
- JMutexAutoLock lock(m_mutex);
-
- std::map<std::string, std::string>::const_iterator n;
- if ((n = m_settings.find(name)) == m_settings.end())
- if ((n = m_defaults.find(name)) == m_defaults.end())
- throw SettingNotFoundException(("Setting [" + name + "] not found ").c_str());
-
- return n->second;
- }
-
- //////////// Get setting
- bool getBool(const std::string &name) const
- {
- return is_yes(get(name));
- }
-
- bool getFlag(const std::string &name) const
- {
- try {
- return getBool(name);
- } catch(SettingNotFoundException &e) {
- return false;
- }
- }
-
- float getFloat(const std::string &name) const
- {
- return stof(get(name));
- }
-
- u16 getU16(const std::string &name) const
- {
- return stoi(get(name), 0, 65535);
- }
-
- s16 getS16(const std::string &name) const
- {
- return stoi(get(name), -32768, 32767);
- }
-
- s32 getS32(const std::string &name) const
- {
- return stoi(get(name));
- }
-
- v3f getV3F(const std::string &name) const
- {
- v3f value;
- Strfnd f(get(name));
- f.next("(");
- value.X = stof(f.next(","));
- value.Y = stof(f.next(","));
- value.Z = stof(f.next(")"));
- return value;
- }
-
- v2f getV2F(const std::string &name) const
- {
- v2f value;
- Strfnd f(get(name));
- f.next("(");
- value.X = stof(f.next(","));
- value.Y = stof(f.next(")"));
- return value;
- }
-
- u64 getU64(const std::string &name) const
- {
- u64 value = 0;
- std::string s = get(name);
- std::istringstream ss(s);
- ss >> value;
- return value;
- }
-
+ 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;