diff --git a/apps/openmw/main.cpp b/apps/openmw/main.cpp index a4bf1fe5b..e4a918271 100644 --- a/apps/openmw/main.cpp +++ b/apps/openmw/main.cpp @@ -69,7 +69,8 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat { // Create a local alias for brevity namespace bpo = boost::program_options; - typedef std::vector StringsVector; + typedef Files::EscapeHashString string; + typedef std::vector StringsVector; bpo::options_description desc("Syntax: openmw \nAllowed options"); @@ -79,13 +80,13 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat ("data", bpo::value()->default_value(Files::PathContainer(), "data") ->multitoken()->composing(), "set data directories (later directories have higher priority)") - ("data-local", bpo::value()->default_value(""), + ("data-local", bpo::value()->default_value(""), "set local data directory (highest priority)") ("fallback-archive", bpo::value()->default_value(StringsVector(), "fallback-archive") ->multitoken(), "set fallback BSA archives (later archives have higher priority)") - ("resources", bpo::value()->default_value("resources"), + ("resources", bpo::value()->default_value("resources"), "set resources directory") ("start", bpo::value()->default_value(""), @@ -109,7 +110,7 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat ("script-console", bpo::value()->implicit_value(true) ->default_value(false), "enable console-only script functionality") - ("script-run", bpo::value()->default_value(""), + ("script-run", bpo::value()->default_value(""), "select a file containing a list of console commands that is executed on startup") ("script-warn", bpo::value()->implicit_value (1) @@ -125,7 +126,7 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat ("script-blacklist-use", bpo::value()->implicit_value(true) ->default_value(true), "enable script blacklisting") - ("load-savegame", bpo::value()->default_value(""), + ("load-savegame", bpo::value()->default_value(""), "load a save game file on game startup (specify an absolute filename or a filename relative to the current working directory)") ("skip-menu", bpo::value()->implicit_value(true) @@ -137,7 +138,7 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat ("fs-strict", bpo::value()->implicit_value(true) ->default_value(false), "strict file system handling (no case folding)") - ( "encoding", bpo::value()-> + ( "encoding", bpo::value()-> default_value("win1252"), "Character encoding used in OpenMW game messages:\n" "\n\twin1250 - Central and Eastern European such as Polish, Czech, Slovak, Hungarian, Slovene, Bosnian, Croatian, Serbian (Latin script), Romanian and Albanian languages\n" diff --git a/components/fallback/validate.hpp b/components/fallback/validate.hpp index 3b6398d6a..531711544 100644 --- a/components/fallback/validate.hpp +++ b/components/fallback/validate.hpp @@ -3,6 +3,8 @@ #include +#include + // Parses and validates a fallback map from boost program_options. // Note: for boost to pick up the validate function, you need to pull in the namespace e.g. // by using namespace Fallback; @@ -11,7 +13,7 @@ namespace Fallback { struct FallbackMap { - std::map mMap; + std::map mMap; }; void validate(boost::any &v, std::vector const &tokens, FallbackMap*, int) diff --git a/components/files/configurationmanager.cpp b/components/files/configurationmanager.cpp index 5ac3dd695..c50303d97 100644 --- a/components/files/configurationmanager.cpp +++ b/components/files/configurationmanager.cpp @@ -3,9 +3,11 @@ #include #include #include +#include #include #include +#include #include /** @@ -139,8 +141,11 @@ bool ConfigurationManager::loadConfig(const boost::filesystem::path& path, if (!mSilent) std::cout << "Loading config file: " << cfgFile.string() << "... "; - boost::filesystem::ifstream configFileStream(cfgFile); - if (configFileStream.is_open()) + boost::filesystem::ifstream configFileStreamUnfiltered(cfgFile); + boost::iostreams::filtering_istream configFileStream; + configFileStream.push(escape_hash_filter()); + configFileStream.push(configFileStreamUnfiltered); + if (configFileStreamUnfiltered.is_open()) { boost::program_options::store(boost::program_options::parse_config_file( configFileStream, description, true), variables); @@ -159,6 +164,111 @@ bool ConfigurationManager::loadConfig(const boost::filesystem::path& path, return false; } +escape_hash_filter::escape_hash_filter() : mNext() +{ +} + +escape_hash_filter::~escape_hash_filter() +{ +} + +template +int escape_hash_filter::get(Source & src) +{ + if (mNext.empty()) + { + int character = boost::iostreams::get(src); + bool record = true; + if (character == boost::iostreams::WOULD_BLOCK) + { + mNext.push(character); + record = false; + } + else if (character == EOF) + { + seenNonWhitespace = false; + finishLine = false; + mNext.push(character); + } + else if (character == '\n') + { + seenNonWhitespace = false; + finishLine = false; + mNext.push(character); + } + else if (finishLine) + { + mNext.push(character); + } + else if (character == '#') + { + if (seenNonWhitespace) + { + mNext.push(sEscape); + mNext.push(sHashIdentifier); + } + else + { + //it's fine being interpreted by Boost as a comment, and so is anything afterwards + mNext.push(character); + finishLine = true; + } + } + else if (mPrevious == sEscape) + { + mNext.push(sEscape); + mNext.push(sEscapeIdentifier); + } + else + { + mNext.push(character); + } + if (!seenNonWhitespace && !isspace(character)) + seenNonWhitespace = true; + if (record) + mPrevious = character; + } + int retval = mNext.front(); + mNext.pop(); + return retval; +} + +std::string EscapeHashString::processString(const std::string & str) +{ + std::string temp = boost::replace_all_copy(str, std::string() + (char)escape_hash_filter::sEscape + (char)escape_hash_filter::sHashIdentifier, "#"); + boost::replace_all(temp, std::string() + (char)escape_hash_filter::sEscape + (char)escape_hash_filter::sEscapeIdentifier, std::string((char) escape_hash_filter::sEscape, 1)); + return temp; +} + +EscapeHashString::EscapeHashString() +{ +} + +EscapeHashString::EscapeHashString(const std::string & str) : std::string(EscapeHashString::processString(str)) +{ +} + +EscapeHashString::EscapeHashString(const std::string & str, size_t pos, size_t len) : std::string(EscapeHashString::processString(str), pos, len) +{ +} + +EscapeHashString::EscapeHashString(const char * s) : std::string(EscapeHashString::processString(std::string(s))) +{ +} + +EscapeHashString::EscapeHashString(const char * s, size_t n) : std::string(EscapeHashString::processString(std::string(s)), 0, n) +{ +} + +EscapeHashString::EscapeHashString(size_t n, char c) : std::string(n, c) +{ +} + +template +EscapeHashString::EscapeHashString(InputIterator first, InputIterator last) : std::string(EscapeHashString::processString(std::string(first, last))) +{ +} + const boost::filesystem::path& ConfigurationManager::getGlobalPath() const { return mFixedPath.getGlobalConfigPath(); diff --git a/components/files/configurationmanager.hpp b/components/files/configurationmanager.hpp index c05dbbb45..7ef7d6fae 100644 --- a/components/files/configurationmanager.hpp +++ b/components/files/configurationmanager.hpp @@ -2,8 +2,10 @@ #define COMPONENTS_FILES_CONFIGURATIONMANAGER_HPP #include +#include #include +#include #include #include @@ -63,6 +65,47 @@ struct ConfigurationManager bool mSilent; }; + +/** + * \struct escape_hash_filter + */ +struct escape_hash_filter : public boost::iostreams::input_filter +{ + static const int sEscape = '@'; + static const int sHashIdentifier = 'h'; + static const int sEscapeIdentifier = 'a'; + + escape_hash_filter(); + virtual ~escape_hash_filter(); + + template int get(Source & src); + + private: + std::queue mNext; + int mPrevious; + + bool seenNonWhitespace = false; + bool finishLine = false; +}; + +/** + * \class EscapeHashString + */ +class EscapeHashString : public std::string +{ + public: + static std::string processString(const std::string & str); + + EscapeHashString(); + EscapeHashString(const std::string & str); + EscapeHashString(const std::string & str, size_t pos, size_t len = std::string::npos); + EscapeHashString(const char * s); + EscapeHashString(const char * s, size_t n); + EscapeHashString(size_t n, char c); + template + EscapeHashString(InputIterator first, InputIterator last); +}; + } /* namespace Cfg */ #endif /* COMPONENTS_FILES_CONFIGURATIONMANAGER_HPP */