2011-09-13 17:50:18 +03:00
|
|
|
#include "settings.hpp"
|
|
|
|
#include "misc.hpp"
|
|
|
|
#include <map>
|
|
|
|
#include <sstream>
|
|
|
|
#include "misc.hpp"
|
2011-09-14 20:06:36 +03:00
|
|
|
#include "command.hpp"
|
2011-09-24 13:40:13 +03:00
|
|
|
#include "globalwrap.hpp"
|
2011-09-13 17:50:18 +03:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2011-09-24 13:40:13 +03:00
|
|
|
globalwrap<std::map<std::string, setting*>> settings;
|
2011-09-14 20:06:36 +03:00
|
|
|
|
2011-09-17 13:15:20 +03:00
|
|
|
function_ptr_command<tokensplitter&> set_setting("set-setting", "set a setting",
|
2011-09-17 01:12:53 +03:00
|
|
|
"Syntax: set-setting <setting> [<value>]\nSet setting to a new value. Omit <value> to set to ''\n",
|
2011-09-17 13:15:20 +03:00
|
|
|
[](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) {
|
2011-09-14 20:06:36 +03:00
|
|
|
std::string syntax = "Syntax: set-setting <setting> [<value>]";
|
|
|
|
std::string settingname = t;
|
|
|
|
std::string settingvalue = t.tail();
|
|
|
|
if(settingname == "")
|
|
|
|
throw std::runtime_error("Setting name required.");
|
2011-09-15 15:12:26 +03:00
|
|
|
setting::set(settingname, settingvalue);
|
2011-09-17 09:55:35 +03:00
|
|
|
messages << "Setting '" << settingname << "' set to '" << settingvalue << "'"
|
2011-09-17 01:05:41 +03:00
|
|
|
<< std::endl;
|
2011-09-17 01:12:53 +03:00
|
|
|
});
|
2011-09-15 22:56:33 +03:00
|
|
|
|
2011-09-17 13:15:20 +03:00
|
|
|
function_ptr_command<tokensplitter&> unset_setting("unset-setting", "unset a setting",
|
2011-09-17 01:12:53 +03:00
|
|
|
"Syntax: unset-setting <setting>\nTry to unset a setting. Note that not all settings can be unset\n",
|
2011-09-17 13:15:20 +03:00
|
|
|
[](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) {
|
2011-09-14 20:06:36 +03:00
|
|
|
std::string syntax = "Syntax: unset-setting <setting>";
|
|
|
|
std::string settingname = t;
|
|
|
|
if(settingname == "" || t)
|
|
|
|
throw std::runtime_error("Expected setting name and nothing else");
|
2011-09-15 15:12:26 +03:00
|
|
|
setting::blank(settingname);
|
2011-09-17 09:55:35 +03:00
|
|
|
messages << "Setting '" << settingname << "' unset" << std::endl;
|
2011-09-17 01:12:53 +03:00
|
|
|
});
|
2011-09-14 20:06:36 +03:00
|
|
|
|
2011-09-17 13:15:20 +03:00
|
|
|
function_ptr_command<tokensplitter&> get_command("get-setting", "get value of a setting",
|
2011-09-17 01:12:53 +03:00
|
|
|
"Syntax: get-setting <setting>\nShow value of setting\n",
|
2011-09-17 13:15:20 +03:00
|
|
|
[](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) {
|
2011-09-14 20:06:36 +03:00
|
|
|
std::string settingname = t;
|
|
|
|
if(settingname == "" || t.tail() != "")
|
|
|
|
throw std::runtime_error("Expected setting name and nothing else");
|
2011-09-15 15:12:26 +03:00
|
|
|
if(setting::is_set(settingname))
|
2011-09-17 09:55:35 +03:00
|
|
|
messages << "Setting '" << settingname << "' has value '"
|
2011-09-17 01:05:41 +03:00
|
|
|
<< setting::get(settingname) << "'" << std::endl;
|
2011-09-14 20:06:36 +03:00
|
|
|
else
|
2011-09-17 09:55:35 +03:00
|
|
|
messages << "Setting '" << settingname << "' unset" << std::endl;
|
2011-09-17 01:12:53 +03:00
|
|
|
});
|
2011-09-15 22:56:33 +03:00
|
|
|
|
2011-09-17 12:00:49 +03:00
|
|
|
function_ptr_command<> show_settings("show-settings", "Show values of all settings",
|
2011-09-17 01:12:53 +03:00
|
|
|
"Syntax: show-settings\nShow value of all settings\n",
|
2011-09-17 12:00:49 +03:00
|
|
|
[]() throw(std::bad_alloc, std::runtime_error) {
|
2011-09-17 01:05:41 +03:00
|
|
|
setting::print_all();
|
2011-09-17 01:12:53 +03:00
|
|
|
});
|
2011-09-13 17:50:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
setting::setting(const std::string& name) throw(std::bad_alloc)
|
|
|
|
{
|
2011-09-24 13:40:13 +03:00
|
|
|
settings()[settingname = name] = this;
|
2011-09-13 17:50:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
setting::~setting() throw()
|
|
|
|
{
|
2011-09-24 13:40:13 +03:00
|
|
|
settings().erase(settingname);
|
2011-09-13 17:50:18 +03:00
|
|
|
}
|
|
|
|
|
2011-09-15 15:12:26 +03:00
|
|
|
void setting::set(const std::string& _setting, const std::string& value) throw(std::bad_alloc, std::runtime_error)
|
2011-09-13 17:50:18 +03:00
|
|
|
{
|
2011-09-24 13:40:13 +03:00
|
|
|
if(!settings().count(_setting))
|
2011-09-13 17:50:18 +03:00
|
|
|
throw std::runtime_error("No such setting '" + _setting + "'");
|
|
|
|
try {
|
2011-09-24 13:40:13 +03:00
|
|
|
settings()[_setting]->set(value);
|
2011-09-13 17:50:18 +03:00
|
|
|
} catch(std::bad_alloc& e) {
|
|
|
|
throw;
|
|
|
|
} catch(std::exception& e) {
|
|
|
|
throw std::runtime_error("Can't set setting '" + _setting + "': " + e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-15 15:12:26 +03:00
|
|
|
void setting::blank(const std::string& _setting) throw(std::bad_alloc, std::runtime_error)
|
2011-09-13 17:50:18 +03:00
|
|
|
{
|
2011-09-24 13:40:13 +03:00
|
|
|
if(!settings().count(_setting))
|
2011-09-13 17:50:18 +03:00
|
|
|
throw std::runtime_error("No such setting '" + _setting + "'");
|
|
|
|
try {
|
2011-09-24 13:40:13 +03:00
|
|
|
settings()[_setting]->blank();
|
2011-09-13 17:50:18 +03:00
|
|
|
} catch(std::bad_alloc& e) {
|
|
|
|
throw;
|
|
|
|
} catch(std::exception& e) {
|
|
|
|
throw std::runtime_error("Can't blank setting '" + _setting + "': " + e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-15 15:12:26 +03:00
|
|
|
std::string setting::get(const std::string& _setting) throw(std::bad_alloc, std::runtime_error)
|
2011-09-13 17:50:18 +03:00
|
|
|
{
|
2011-09-24 13:40:13 +03:00
|
|
|
if(!settings().count(_setting))
|
2011-09-13 17:50:18 +03:00
|
|
|
throw std::runtime_error("No such setting '" + _setting + "'");
|
2011-09-24 13:40:13 +03:00
|
|
|
return settings()[_setting]->get();
|
2011-09-13 17:50:18 +03:00
|
|
|
}
|
|
|
|
|
2011-09-15 15:12:26 +03:00
|
|
|
bool setting::is_set(const std::string& _setting) throw(std::bad_alloc, std::runtime_error)
|
2011-09-13 17:50:18 +03:00
|
|
|
{
|
2011-09-24 13:40:13 +03:00
|
|
|
if(!settings().count(_setting))
|
2011-09-13 17:50:18 +03:00
|
|
|
throw std::runtime_error("No such setting '" + _setting + "'");
|
2011-09-24 13:40:13 +03:00
|
|
|
return settings()[_setting]->is_set();
|
2011-09-13 17:50:18 +03:00
|
|
|
}
|
|
|
|
|
2011-09-17 01:05:41 +03:00
|
|
|
void setting::print_all() throw(std::bad_alloc)
|
2011-09-13 17:50:18 +03:00
|
|
|
{
|
2011-10-19 19:25:31 +03:00
|
|
|
for(auto i : settings()) {
|
|
|
|
if(!i.second->is_set())
|
|
|
|
messages << i.first << ": (unset)" << std::endl;
|
2011-09-13 17:50:18 +03:00
|
|
|
else
|
2011-10-19 19:25:31 +03:00
|
|
|
messages << i.first << ": " << i.second->get() << std::endl;
|
2011-09-13 17:50:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
numeric_setting::numeric_setting(const std::string& sname, int32_t minv, int32_t maxv, int32_t dflt)
|
|
|
|
throw(std::bad_alloc)
|
|
|
|
: setting(sname)
|
|
|
|
{
|
|
|
|
minimum = minv;
|
|
|
|
maximum = maxv;
|
|
|
|
value = dflt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void numeric_setting::blank() throw(std::bad_alloc, std::runtime_error)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("This setting can't be blanked");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool numeric_setting::is_set() throw()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void numeric_setting::set(const std::string& _value) throw(std::bad_alloc, std::runtime_error)
|
|
|
|
{
|
|
|
|
int32_t v = parse_value<int32_t>(_value);
|
|
|
|
if(v < minimum || v > maximum) {
|
|
|
|
std::ostringstream x;
|
|
|
|
x << "Value out of range (" << minimum << " - " << maximum << ")";
|
|
|
|
throw std::runtime_error(x.str());
|
|
|
|
}
|
|
|
|
value = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string numeric_setting::get() throw(std::bad_alloc)
|
|
|
|
{
|
|
|
|
std::ostringstream x;
|
|
|
|
x << value;
|
|
|
|
return x.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
numeric_setting::operator int32_t() throw()
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
2011-09-14 23:16:24 +03:00
|
|
|
|
|
|
|
boolean_setting::boolean_setting(const std::string& sname, bool dflt) throw(std::bad_alloc)
|
|
|
|
: setting(sname)
|
|
|
|
{
|
|
|
|
value = dflt;
|
|
|
|
}
|
|
|
|
void boolean_setting::blank() throw(std::bad_alloc, std::runtime_error)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("This setting can't be unset");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool boolean_setting::is_set() throw()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void boolean_setting::set(const std::string& v) throw(std::bad_alloc, std::runtime_error)
|
|
|
|
{
|
|
|
|
if(v == "true" || v == "yes" || v == "on" || v == "1" || v == "enable" || v == "enabled")
|
|
|
|
value = true;
|
|
|
|
else if(v == "false" || v == "no" || v == "off" || v == "0" || v == "disable" || v == "disabled")
|
|
|
|
value = false;
|
|
|
|
else
|
|
|
|
throw std::runtime_error("Invalid value for boolean setting");
|
|
|
|
}
|
|
|
|
std::string boolean_setting::get() throw(std::bad_alloc)
|
|
|
|
{
|
|
|
|
if(value)
|
|
|
|
return "true";
|
|
|
|
else
|
|
|
|
return "false";
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean_setting::operator bool() throw()
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|