From 6bc4cad0eddd7a7cf593ca1471599e2d75727379 Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Thu, 11 Sep 2014 18:22:05 -0400 Subject: Split settings into seperate source and header files This also cleans up settings a bit --- src/settings.cpp | 702 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 702 insertions(+) create mode 100644 src/settings.cpp (limited to 'src/settings.cpp') diff --git a/src/settings.cpp b/src/settings.cpp new file mode 100644 index 000000000..760f07e21 --- /dev/null +++ b/src/settings.cpp @@ -0,0 +1,702 @@ +/* +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 + + +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; +} + + +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::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; +} + + +void Settings::writeLines(std::ostream &os) const +{ + JMutexAutoLock lock(m_mutex); + + for (std::map::const_iterator + i = m_settings.begin(); + i != m_settings.end(); ++i) { + os << i->first << " = " << i->second << '\n'; + } +} + + +bool Settings::updateConfigFile(const char *filename) +{ + std::list objects; + std::set updated; + bool changed = false; + + JMutexAutoLock lock(m_mutex); + + // Read the file and check for differences + { + std::ifstream is(filename); + while (is.good()) { + getUpdatedConfigObject(is, objects, + updated, changed); + } + } + + // If something not yet determined to have been changed, check if + // any new stuff was added + if (!changed) { + for (std::map::const_iterator + i = m_settings.begin(); + i != m_settings.end(); ++i) { + if (updated.find(i->first) == updated.end()) { + changed = true; + break; + } + } + } + + // If nothing was actually changed, skip writing the file + if (!changed) { + return true; + } + + // Write stuff back + { + std::ostringstream ss(std::ios_base::binary); + + // Write changes settings + for (std::list::const_iterator + i = objects.begin(); + i != objects.end(); ++i) { + ss << (*i); + } + + // Write new settings + for (std::map::const_iterator + i = m_settings.begin(); + i != m_settings.end(); ++i) { + if (updated.find(i->first) != updated.end()) + continue; + ss << i->first << " = " << i->second << '\n'; + } + + if (!fs::safeWriteToFile(filename, ss.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 << "\": --