/* Minetest Copyright (C) 2010-2013 celeron55, Perttu Ahola This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "settings.h" #include "irrlichttypes_bloated.h" #include "exceptions.h" #include "jthread/jmutexautolock.h" #include "strfnd.h" #include #include #include #include "debug.h" #include "log.h" #include "util/serialize.h" #include "filesys.h" #include "noise.h" #include #include Settings::~Settings() { std::map::const_iterator it; for (it = m_settings.begin(); it != m_settings.end(); ++it) delete it->second.group; } Settings & Settings::operator += (const Settings &other) { update(other); return *this; } Settings & Settings::operator = (const Settings &other) { if (&other == this) return *this; JMutexAutoLock lock(m_mutex); JMutexAutoLock lock2(other.m_mutex); clearNoLock(); updateNoLock(other); return *this; } std::string Settings::sanitizeString(const std::string &value) { std::string str = value; for (const char *s = "\t\n\v\f\r\b =\""; *s; s++) str.erase(std::remove(str.begin(), str.end(), *s), str.end()); return str; } std::string Settings::getMultiline(std::istream &is, size_t *num_lines) { size_t lines = 1; std::string value; std::string line; while (is.good()) { lines++; std::getline(is, line); if (line == "\"\"\"") break; value += line; value.push_back('\n'); } size_t len = value.size(); if (len) value.erase(len - 1); if (num_lines) *num_lines = lines; return value; } bool Settings::parseConfigLines(std::istream &is, const std::string &end) { JMutexAutoLock 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 *branch = new Settings; if (!branch->parseConfigLines(is, "}")) return false; m_settings[name] = SettingsEntry(branch); break; } case SPE_MULTILINE: m_settings[name] = SettingsEntry(getMultiline(is)); break; } } return end.empty(); } bool Settings::readConfigFile(const char *filename) { std::ifstream is(filename); if (!is.good()) return false; return parseConfigLines(is, ""); } void Settings::writeLines(std::ostream &os, u32 tab_depth) const { JMutexAutoLock lock(m_mutex); for (std::map::const_iterator it = m_settings.begin(); it != m_settings.end(); ++it) printEntry(os, it->first, it->second, tab_depth); } bool Settings::printEntry(std::ostream &os, const std::string &name, const SettingsEntry &entry, u32 tab_depth) { bool printed = false; if (!entry.group || entry.value != "") { printValue(os, name, entry.value, tab_depth); printed = true; } if (entry.group) { printGroup(os, name, entry.group, tab_depth); printed = true; } return printed; } void Settings::printValue(std::ostream &os, const std::string &name, const std::string &value, u32 tab_depth) { for (u32 i = 0; i != tab_depth; i++) os << "\t"; os << name << " = "; if (value.find('\n') != std::string::npos) os << "\"\"\"\n" << value << "\n\"\"\"\n"; else os << value << "\n"; } void Settings::printGroup(std::ostream &os, const std::string &name, const Settings *group, u32 tab_depth) { // Recursively write group contents for (u32 i = 0; i != tab_depth; i++) os << "\t"; os << name << " = {\n"; group->writeLines(os, tab_depth + 1); for (u32 i = 0; i != tab_depth; i++) os << "\t"; os << "}\n"; } void Settings::getNamesPresent(std::istream &is, const std::string &end, std::set &present_values, std::set &present_groups) { std::string name, value, line; bool end_found = false; int depth = 0; size_t old_pos = is.tellg(); while (is.good() && !end_found) { std::getline(is, line); SettingsParseEvent event = parseConfigObject(line, depth ? "}" : end, name, value); switch (event) { case SPE_END: if (depth == 0) end_found = true; else depth--; break; case SPE_MULTILINE: while (is.good() && line != "\"\"\"") std::getline(is, line); /* FALLTHROUGH */ case SPE_KVPAIR: if (depth == 0) present_values.insert(name); break; case SPE_GROUP: if (depth == 0) present_groups.insert(name); depth++; break; case SPE_NONE: case SPE_COMMENT: case SPE_INVALID: break; } } is.clear(); is.seekg(old_pos); } bool Settings::updateConfigObject(std::istream &is, std::ostream &os, const std::string &end, u32 tab_depth) { std::map::const_iterator it; std::set present_values, present_groups; std::string line, name, value; bool was_modified = false; bool end_found = false; getNamesPresent(is, end, present_values, present_groups); // 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() && value != it->second.value) { if (!it->second.group || it->second.value != "") printValue(os, name, it->second.value, tab_depth); was_modified = true; } else { os << line << "\n"; if (event == SPE_MULTILINE) os << value << "\n\"\"\"\n"; } // If this value name has a group not in the file, print it if (it != m_settings.end() && it->second.group && present_groups.find(name) == present_groups.end()) { printGroup(os, name, it->second.group, tab_depth); was_modified = true; } break; case SPE_GROUP: { Settings *group = NULL; it = m_settings.find(name); if (it != m_settings.end()) group = it->second.group; // If this group name has a non-blank value not in the file, print it if (it != m_settings.end() && it->second.value != "" && present_values.find(name) == present_values.end()) { printValue(os, name, it->second.value, tab_depth); was_modified = true; } os << line << "\n"; if (group) { was_modified |= group->updateConfigObject(is, os, "}", tab_depth + 1); } else { // If a group exists in the file but not memory, don't touch it Settings dummy_settings; dummy_settings.updateConfigObject(is, os, "}", tab_depth + 1); } 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_values.find(it->first) != present_values.end() || present_groups.find(it->first) != present_groups.end()) continue; was_modified |= printEntry(os, it->first, it->second, tab_depth); } return was_modified; } bool Settings::updateConfigFile(const char *filename) { JMutexAutoLock lock(m_mutex); std::ifstream is(filename); std::ostringstream os(std::ios_base::binary); bool was_modified = updateConfigObject(is, os, ""); is.close(); if (!was_modified) return true; if (!fs::safeWriteToFile(filename, os.str())) { errorstream << "Error writing configuration file: \"" << filename << "\"" << std::endl; return false; } return true; } bool Settings::parseCommandLine(int argc, char *argv[], std::map &allowed_options) { int nonopt_index = 0; for (int i = 1; i < argc; i++) { std::string arg_name = argv[i]; if (arg_name.substr(0, 2) != "--") { // If option doesn't start with -, read it in as nonoptX if (arg_name[0] != '-'){ std::string name = "nonopt"; name += itos(nonopt_index); set(name, arg_name); nonopt_index++; continue; } errorstream << "Invalid command-line parameter \"" << arg_name << "\": --