- Settings()
- {
- }
-
- void writeLines(std::ostream &os) const
- {
- JMutexAutoLock lock(m_mutex);
-
- for(std::map<std::string, std::string>::const_iterator
- i = m_settings.begin();
- i != m_settings.end(); ++i)
- {
- std::string name = i->first;
- std::string value = i->second;
- os<<name<<" = "<<value<<"\n";
- }
- }
-
- // return all keys used
- std::vector<std::string> getNames() const
- {
- std::vector<std::string> names;
- for(std::map<std::string, std::string>::const_iterator
- i = m_settings.begin();
- i != m_settings.end(); ++i)
- {
- names.push_back(i->first);
- }
- return names;
- }
-
- // remove a setting
- bool remove(const std::string &name)
- {
- return m_settings.erase(name);
- }
-
-
- bool parseConfigLine(const std::string &line)
- {
- JMutexAutoLock lock(m_mutex);
-
- std::string trimmedline = trim(line);
-
- // Ignore empty lines and comments
- if(trimmedline.size() == 0 || trimmedline[0] == '#')
- return true;
-
- //infostream<<"trimmedline=\""<<trimmedline<<"\""<<std::endl;
-
- Strfnd sf(trim(line));
-
- std::string name = sf.next("=");
- name = trim(name);
-
- if(name == "")
- return true;
-
- std::string value = sf.next("\n");
- value = trim(value);
-
- /*infostream<<"Config name=\""<<name<<"\" value=\""
- <<value<<"\""<<std::endl;*/
-
- m_settings[name] = value;
-
- return true;
- }
-
- void parseConfigLines(std::istream &is, const std::string &endstring)
- {
- for(;;){
- if(is.eof())
- break;
- std::string line;
- std::getline(is, line);
- std::string trimmedline = trim(line);
- if(endstring != ""){
- if(trimmedline == endstring)
- break;
- }
- parseConfigLine(line);
- }
- }
-
- // Returns false on EOF
- bool parseConfigObject(std::istream &is)
- {
- if(is.eof())
- return false;
-
- /*
- NOTE: This function might be expanded to allow multi-line
- settings.
- */
- std::string line;
- std::getline(is, line);
- //infostream<<"got line: \""<<line<<"\""<<std::endl;
-
- return parseConfigLine(line);
- }
-
- /*
- Read configuration file
-
- Returns true on success
- */
- bool readConfigFile(const char *filename)
- {
- std::ifstream is(filename);
- if(is.good() == false)
- return false;
-
- /*infostream<<"Parsing configuration file: \""
- <<filename<<"\""<<std::endl;*/
-
- while(parseConfigObject(is));
-
- return true;
- }
-
- /*
- Reads a configuration object from stream (usually a single line)
- and adds it to dst.
-
- Preserves comments and empty lines.
-
- Settings that were added to dst are also added to updated.
- key of updated is setting name, value of updated is dummy.
-
- Returns false on EOF
- */
- bool getUpdatedConfigObject(std::istream &is,
- std::list<std::string> &dst,
- std::set<std::string> &updated,
- bool &value_changed)
- {
- JMutexAutoLock lock(m_mutex);
-
- if(is.eof())
- return false;
-
- // NOTE: This function will be expanded to allow multi-line settings
- std::string line;
- std::getline(is, line);
-
- std::string trimmedline = trim(line);
-
- std::string line_end = "";
- if(is.eof() == false)
- line_end = "\n";
-
- // Ignore empty lines and comments
- if(trimmedline.size() == 0 || trimmedline[0] == '#')
- {
- dst.push_back(line+line_end);
- return true;
- }
-
- Strfnd sf(trim(line));
-
- std::string name = sf.next("=");
- name = trim(name);
-
- if(name == "")
- {
- dst.push_back(line+line_end);
- return true;
- }
-
- std::string value = sf.next("\n");
- value = trim(value);
-
- if(m_settings.find(name) != m_settings.end())
- {
- std::string newvalue = m_settings[name];
-
- if(newvalue != value)
- {
- infostream<<"Changing value of \""<<name<<"\" = \""
- <<value<<"\" -> \""<<newvalue<<"\""
- <<std::endl;
- value_changed = true;
- }
-
- dst.push_back(name + " = " + newvalue + line_end);
-
- updated.insert(name);
- }
- else //file contains a setting which is not in m_settings
- value_changed=true;
-
- return true;
- }