bool Settings::readConfigFile(const char *filename) { std::ifstream is(filename); if (!is.good()) return false; JMutexAutoLock lock(m_mutex); std::string name, value; while (is.good()) { if (parseConfigObject(is, name, value)) { m_settings[name] = value; } } return true; }
bool Settings::parseConfigLines(std::istream &is, const std::string &end) { JMutexAutoLock lock(m_mutex); std::string name, value; bool end_found = false; while (is.good() && !end_found) { if (parseConfigObject(is, name, value, end, end_found)) { m_settings[name] = value; } } if (!end.empty() && !end_found) { return false; } return true; }
bool Settings::parseConfigLines(std::istream &is, const std::string &end) { MutexAutoLock lock(m_mutex); std::string line, name, value; while (is.good()) { std::getline(is, line); SettingsParseEvent event = parseConfigObject(line, end, name, value); switch (event) { case SPE_NONE: case SPE_INVALID: case SPE_COMMENT: break; case SPE_KVPAIR: m_settings[name] = SettingsEntry(value); break; case SPE_END: return true; case SPE_GROUP: { Settings *group = new Settings; if (!group->parseConfigLines(is, "}")) { delete group; return false; } m_settings[name] = SettingsEntry(group); break; } case SPE_MULTILINE: m_settings[name] = SettingsEntry(getMultiline(is)); break; } } return end.empty(); }
void Settings::getUpdatedConfigObject(std::istream &is, std::list<std::string> &dst, std::set<std::string> &updated, bool &changed) { std::string name, value; if (!parseConfigObject(is, name, value)) { dst.push_back(value + '\n'); return; } if (m_settings.find(name) != m_settings.end()) { std::string new_value = m_settings[name]; if (new_value != value) { changed = true; } dst.push_back(name + " = " + new_value + '\n'); updated.insert(name); } else { // File contains a setting which is not in m_settings changed = true; } }
bool Settings::updateConfigObject(std::istream &is, std::ostream &os, const std::string &end, u32 tab_depth) { std::map<std::string, SettingsEntry>::const_iterator it; std::set<std::string> present_entries; std::string line, name, value; bool was_modified = false; bool end_found = false; // Add any settings that exist in the config file with the current value // in the object if existing while (is.good() && !end_found) { std::getline(is, line); SettingsParseEvent event = parseConfigObject(line, end, name, value); switch (event) { case SPE_END: os << line << (is.eof() ? "" : "\n"); end_found = true; break; case SPE_MULTILINE: value = getMultiline(is); /* FALLTHROUGH */ case SPE_KVPAIR: it = m_settings.find(name); if (it != m_settings.end() && (it->second.is_group || it->second.value != value)) { printEntry(os, name, it->second, tab_depth); was_modified = true; } else { os << line << "\n"; if (event == SPE_MULTILINE) os << value << "\n\"\"\"\n"; } present_entries.insert(name); break; case SPE_GROUP: it = m_settings.find(name); if (it != m_settings.end() && it->second.is_group) { os << line << "\n"; sanity_check(it->second.group != NULL); was_modified |= it->second.group->updateConfigObject(is, os, "}", tab_depth + 1); } else { printEntry(os, name, it->second, tab_depth); was_modified = true; } present_entries.insert(name); break; default: os << line << (is.eof() ? "" : "\n"); break; } } // Add any settings in the object that don't exist in the config file yet for (it = m_settings.begin(); it != m_settings.end(); ++it) { if (present_entries.find(it->first) != present_entries.end()) continue; printEntry(os, it->first, it->second, tab_depth); was_modified = true; } return was_modified; }
inline bool Settings::parseConfigObject(std::istream &is, std::string &name, std::string &value) { bool end_found = false; return parseConfigObject(is, name, value, "", end_found); }