Skip to content

Commit

Permalink
Make # only start a comment in openmw.cfg if it starts a line and int…
Browse files Browse the repository at this point in the history
…roduct compilation errors
  • Loading branch information
AnyOldName3 committed Jul 11, 2016
1 parent 432723b commit e17e354
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 9 deletions.
13 changes: 7 additions & 6 deletions apps/openmw/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> StringsVector;
typedef Files::EscapeHashString string;
typedef std::vector<string> StringsVector;

bpo::options_description desc("Syntax: openmw <options>\nAllowed options");

Expand All @@ -79,13 +80,13 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat
("data", bpo::value<Files::PathContainer>()->default_value(Files::PathContainer(), "data")
->multitoken()->composing(), "set data directories (later directories have higher priority)")

("data-local", bpo::value<std::string>()->default_value(""),
("data-local", bpo::value<string>()->default_value(""),
"set local data directory (highest priority)")

("fallback-archive", bpo::value<StringsVector>()->default_value(StringsVector(), "fallback-archive")
->multitoken(), "set fallback BSA archives (later archives have higher priority)")

("resources", bpo::value<std::string>()->default_value("resources"),
("resources", bpo::value<string>()->default_value("resources"),
"set resources directory")

("start", bpo::value<std::string>()->default_value(""),
Expand All @@ -109,7 +110,7 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat
("script-console", bpo::value<bool>()->implicit_value(true)
->default_value(false), "enable console-only script functionality")

("script-run", bpo::value<std::string>()->default_value(""),
("script-run", bpo::value<string>()->default_value(""),
"select a file containing a list of console commands that is executed on startup")

("script-warn", bpo::value<int>()->implicit_value (1)
Expand All @@ -125,7 +126,7 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat
("script-blacklist-use", bpo::value<bool>()->implicit_value(true)
->default_value(true), "enable script blacklisting")

("load-savegame", bpo::value<std::string>()->default_value(""),
("load-savegame", bpo::value<string>()->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<bool>()->implicit_value(true)
Expand All @@ -137,7 +138,7 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat
("fs-strict", bpo::value<bool>()->implicit_value(true)
->default_value(false), "strict file system handling (no case folding)")

( "encoding", bpo::value<std::string>()->
( "encoding", bpo::value<string>()->
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"
Expand Down
4 changes: 3 additions & 1 deletion components/fallback/validate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <boost/program_options.hpp>

#include <components\files\configurationmanager.hpp>

// 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;
Expand All @@ -11,7 +13,7 @@ namespace Fallback
{

struct FallbackMap {
std::map<std::string,std::string> mMap;
std::map<Files::EscapeHashString, Files::EscapeHashString> mMap;
};

void validate(boost::any &v, std::vector<std::string> const &tokens, FallbackMap*, int)
Expand Down
114 changes: 112 additions & 2 deletions components/files/configurationmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
#include <string>
#include <iostream>
#include <algorithm>
#include <ctype.h>

#include <boost/bind.hpp>
#include <boost/algorithm/string/erase.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/filesystem/fstream.hpp>

/**
Expand Down Expand Up @@ -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);
Expand All @@ -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 <typename Source>
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<std::string>(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 <class InputIterator>
EscapeHashString::EscapeHashString(InputIterator first, InputIterator last) : std::string(EscapeHashString::processString(std::string(first, last)))
{
}

const boost::filesystem::path& ConfigurationManager::getGlobalPath() const
{
return mFixedPath.getGlobalConfigPath();
Expand Down
43 changes: 43 additions & 0 deletions components/files/configurationmanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
#define COMPONENTS_FILES_CONFIGURATIONMANAGER_HPP

#include <map>
#include <queue>

#include <boost/program_options.hpp>
#include <boost/iostreams/filtering_stream.hpp>

#include <components/files/fixedpath.hpp>
#include <components/files/collections.hpp>
Expand Down Expand Up @@ -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 <typename Source> int get(Source & src);

private:
std::queue<int> 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 <class InputIterator>
EscapeHashString(InputIterator first, InputIterator last);
};

} /* namespace Cfg */

#endif /* COMPONENTS_FILES_CONFIGURATIONMANAGER_HPP */

0 comments on commit e17e354

Please sign in to comment.