|
|
@ -1,95 +1,144 @@
|
|
|
|
#include "settings.hpp"
|
|
|
|
#include "settings.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <boost/filesystem/path.hpp>
|
|
|
|
|
|
|
|
#include <boost/filesystem/fstream.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <OgreResourceGroupManager.h>
|
|
|
|
|
|
|
|
#include <OgreStringConverter.h>
|
|
|
|
#include <OgreStringConverter.h>
|
|
|
|
#include <OgreDataStream.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <components/files/constrainedfiledatastream.hpp>
|
|
|
|
#include <boost/filesystem/fstream.hpp>
|
|
|
|
|
|
|
|
#include <boost/filesystem/path.hpp>
|
|
|
|
using namespace Settings;
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
namespace bfs = boost::filesystem;
|
|
|
|
namespace Settings
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
Ogre::ConfigFile Manager::mFile = Ogre::ConfigFile();
|
|
|
|
CategorySettingValueMap Manager::mDefaultSettings = CategorySettingValueMap();
|
|
|
|
Ogre::ConfigFile Manager::mDefaultFile = Ogre::ConfigFile();
|
|
|
|
CategorySettingValueMap Manager::mUserSettings = CategorySettingValueMap();
|
|
|
|
CategorySettingVector Manager::mChangedSettings = CategorySettingVector();
|
|
|
|
CategorySettingVector Manager::mChangedSettings = CategorySettingVector();
|
|
|
|
CategorySettingValueMap Manager::mNewSettings = CategorySettingValueMap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Manager::loadUser (const std::string& file)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Ogre::DataStreamPtr stream = openConstrainedFileDataStream(file.c_str());
|
|
|
|
|
|
|
|
mFile.load(stream);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Manager::loadDefault (const std::string& file)
|
|
|
|
class SettingsFileParser
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Ogre::DataStreamPtr stream = openConstrainedFileDataStream(file.c_str());
|
|
|
|
public:
|
|
|
|
mDefaultFile.load(stream);
|
|
|
|
SettingsFileParser() : mLine(0) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Manager::saveUser(const std::string& file)
|
|
|
|
void loadSettingsFile (const std::string& file, CategorySettingValueMap& settings)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
mFile = file;
|
|
|
|
|
|
|
|
boost::filesystem::ifstream stream;
|
|
|
|
|
|
|
|
stream.open(boost::filesystem::path(file));
|
|
|
|
|
|
|
|
std::string currentCategory;
|
|
|
|
|
|
|
|
mLine = 0;
|
|
|
|
|
|
|
|
while (!stream.eof() && !stream.fail())
|
|
|
|
{
|
|
|
|
{
|
|
|
|
bfs::ofstream fout((bfs::path(file)));
|
|
|
|
++mLine;
|
|
|
|
|
|
|
|
std::string line;
|
|
|
|
|
|
|
|
std::getline( stream, line );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
if (!skipWhiteSpace(i, line))
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
Ogre::ConfigFile::SectionIterator seci = mFile.getSectionIterator();
|
|
|
|
if (line[i] == '#') // skip comment
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
while (seci.hasMoreElements())
|
|
|
|
if (line[i] == '[')
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Ogre::String sectionName = seci.peekNextKey();
|
|
|
|
size_t end = line.find(']', i);
|
|
|
|
|
|
|
|
if (end == std::string::npos)
|
|
|
|
|
|
|
|
fail("unterminated category");
|
|
|
|
|
|
|
|
|
|
|
|
if (sectionName.length() > 0)
|
|
|
|
currentCategory = line.substr(i+1, end - (i+1));
|
|
|
|
fout << '\n' << '[' << seci.peekNextKey() << ']' << '\n';
|
|
|
|
boost::algorithm::trim(currentCategory);
|
|
|
|
|
|
|
|
i = end+1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
|
|
|
|
if (!skipWhiteSpace(i, line))
|
|
|
|
Ogre::ConfigFile::SettingsMultiMap::iterator i;
|
|
|
|
continue;
|
|
|
|
for (i = settings->begin(); i != settings->end(); ++i)
|
|
|
|
|
|
|
|
{
|
|
|
|
if (currentCategory.empty())
|
|
|
|
fout << i->first.c_str() << " = " << i->second.c_str() << '\n';
|
|
|
|
fail("empty category name");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
size_t settingEnd = line.find('=', i);
|
|
|
|
|
|
|
|
if (settingEnd == std::string::npos)
|
|
|
|
|
|
|
|
fail("unterminated setting name");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::string setting = line.substr(i, (settingEnd-i));
|
|
|
|
|
|
|
|
boost::algorithm::trim(setting);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
size_t valueBegin = settingEnd+1;
|
|
|
|
|
|
|
|
std::string value = line.substr(valueBegin);
|
|
|
|
|
|
|
|
boost::algorithm::trim(value);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (settings.insert(std::make_pair(std::make_pair(currentCategory, setting), value)).second == false)
|
|
|
|
|
|
|
|
fail(std::string("duplicate setting: [" + currentCategory + "] " + setting));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CategorySettingValueMap::iterator it = mNewSettings.begin();
|
|
|
|
private:
|
|
|
|
while (it != mNewSettings.end())
|
|
|
|
/// Increment i until it longer points to a whitespace character
|
|
|
|
|
|
|
|
/// in the string or has reached the end of the string.
|
|
|
|
|
|
|
|
/// @return false if we have reached the end of the string
|
|
|
|
|
|
|
|
bool skipWhiteSpace(size_t& i, std::string& str)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (it->first.first == sectionName)
|
|
|
|
while (i < str.size() && std::isspace(str[i], std::locale::classic()))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
fout << it->first.second << " = " << it->second << '\n';
|
|
|
|
++i;
|
|
|
|
mNewSettings.erase(it++);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
++it;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return i < str.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string category = "";
|
|
|
|
void fail(const std::string& message)
|
|
|
|
for (CategorySettingValueMap::iterator it = mNewSettings.begin();
|
|
|
|
|
|
|
|
it != mNewSettings.end(); ++it)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (category != it->first.first)
|
|
|
|
std::stringstream error;
|
|
|
|
|
|
|
|
error << "Error on line " << mLine << " in " << mFile << ":\n" << message;
|
|
|
|
|
|
|
|
throw std::runtime_error(error.str());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::string mFile;
|
|
|
|
|
|
|
|
int mLine;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Manager::loadDefault(const std::string &file)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
category = it->first.first;
|
|
|
|
SettingsFileParser parser;
|
|
|
|
fout << '\n' << '[' << category << ']' << '\n';
|
|
|
|
parser.loadSettingsFile(file, mDefaultSettings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fout << it->first.second << " = " << it->second << '\n';
|
|
|
|
|
|
|
|
|
|
|
|
void Manager::loadUser(const std::string &file)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
SettingsFileParser parser;
|
|
|
|
|
|
|
|
parser.loadSettingsFile(file, mUserSettings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fout.close();
|
|
|
|
void Manager::saveUser(const std::string &file)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
boost::filesystem::ofstream stream;
|
|
|
|
|
|
|
|
stream.open(boost::filesystem::path(file));
|
|
|
|
|
|
|
|
std::string currentCategory;
|
|
|
|
|
|
|
|
for (CategorySettingValueMap::iterator it = mUserSettings.begin(); it != mUserSettings.end(); ++it)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (it->first.first != currentCategory)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
currentCategory = it->first.first;
|
|
|
|
|
|
|
|
stream << "\n[" << currentCategory << "]\n";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
stream << it->first.second << " = " << it->second << "\n";
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string Manager::getString(const std::string &setting, const std::string &category)
|
|
|
|
std::string Manager::getString(const std::string &setting, const std::string &category)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (mNewSettings.find(std::make_pair(category, setting)) != mNewSettings.end())
|
|
|
|
CategorySettingValueMap::key_type key = std::make_pair(category, setting);
|
|
|
|
return mNewSettings[std::make_pair(category, setting)];
|
|
|
|
CategorySettingValueMap::iterator it = mUserSettings.find(key);
|
|
|
|
|
|
|
|
if (it != mUserSettings.end())
|
|
|
|
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
|
|
|
|
std::string defaultval = mDefaultFile.getSetting(setting, category, "NOTFOUND");
|
|
|
|
it = mDefaultSettings.find(key);
|
|
|
|
std::string val = mFile.getSetting(setting, category, defaultval);
|
|
|
|
if (it != mDefaultSettings.end())
|
|
|
|
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
|
|
|
|
if (val == "NOTFOUND")
|
|
|
|
throw std::runtime_error(std::string("Trying to retrieve a non-existing setting: ") + setting
|
|
|
|
throw std::runtime_error("Trying to retrieve a non-existing setting: " + setting + " Make sure the settings-default.cfg file was properly installed.");
|
|
|
|
+ ".\nMake sure the settings-default.cfg file file was properly installed.");
|
|
|
|
return val;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float Manager::getFloat (const std::string& setting, const std::string& category)
|
|
|
|
float Manager::getFloat (const std::string& setting, const std::string& category)
|
|
|
@ -109,49 +158,18 @@ bool Manager::getBool (const std::string& setting, const std::string& category)
|
|
|
|
|
|
|
|
|
|
|
|
void Manager::setString(const std::string &setting, const std::string &category, const std::string &value)
|
|
|
|
void Manager::setString(const std::string &setting, const std::string &category, const std::string &value)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
CategorySetting s = std::make_pair(category, setting);
|
|
|
|
CategorySettingValueMap::key_type key = std::make_pair(category, setting);
|
|
|
|
|
|
|
|
|
|
|
|
bool found=false;
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Ogre::ConfigFile::SettingsIterator it = mFile.getSettingsIterator(category);
|
|
|
|
|
|
|
|
while (it.hasMoreElements())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Ogre::ConfigFile::SettingsMultiMap::iterator i = it.current();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((*i).first == setting)
|
|
|
|
CategorySettingValueMap::iterator found = mUserSettings.find(key);
|
|
|
|
{
|
|
|
|
if (found != mUserSettings.end())
|
|
|
|
if ((*i).second != value)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
mChangedSettings.push_back(std::make_pair(category, setting));
|
|
|
|
if (found->second == value)
|
|
|
|
(*i).second = value;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
found = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
it.getNext();
|
|
|
|
mUserSettings[key] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Ogre::Exception&)
|
|
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
mChangedSettings.insert(key);
|
|
|
|
{
|
|
|
|
|
|
|
|
if (mNewSettings.find(s) != mNewSettings.end())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (mNewSettings[s] != value)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
mChangedSettings.push_back(std::make_pair(category, setting));
|
|
|
|
|
|
|
|
mNewSettings[s] = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (mDefaultFile.getSetting(setting, category) != value)
|
|
|
|
|
|
|
|
mChangedSettings.push_back(std::make_pair(category, setting));
|
|
|
|
|
|
|
|
mNewSettings[s] = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Manager::setInt (const std::string& setting, const std::string& category, const int value)
|
|
|
|
void Manager::setInt (const std::string& setting, const std::string& category, const int value)
|
|
|
@ -175,3 +193,5 @@ const CategorySettingVector Manager::apply()
|
|
|
|
mChangedSettings.clear();
|
|
|
|
mChangedSettings.clear();
|
|
|
|
return vec;
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|