1 #include "settings.hpp"
11 std::map
<std::string
, setting
*>* settings
;
13 function_ptr_command
<const std::string
&> set_setting("set-setting", "set a setting",
14 "Syntax: set-setting <setting> [<value>]\nSet setting to a new value. Omit <value> to set to ''\n",
15 [](const std::string
& args
) throw(std::bad_alloc
, std::runtime_error
) {
16 std::string syntax
= "Syntax: set-setting <setting> [<value>]";
17 tokensplitter
t(args
);
18 std::string settingname
= t
;
19 std::string settingvalue
= t
.tail();
21 throw std::runtime_error("Setting name required.");
22 setting::set(settingname
, settingvalue
);
23 messages
<< "Setting '" << settingname
<< "' set to '" << settingvalue
<< "'"
27 function_ptr_command
<const std::string
&> unset_setting("unset-setting", "unset a setting",
28 "Syntax: unset-setting <setting>\nTry to unset a setting. Note that not all settings can be unset\n",
29 [](const std::string
& args
) throw(std::bad_alloc
, std::runtime_error
) {
30 std::string syntax
= "Syntax: unset-setting <setting>";
31 tokensplitter
t(args
);
32 std::string settingname
= t
;
33 if(settingname
== "" || t
)
34 throw std::runtime_error("Expected setting name and nothing else");
35 setting::blank(settingname
);
36 messages
<< "Setting '" << settingname
<< "' unset" << std::endl
;
39 function_ptr_command
<const std::string
&> get_command("get-setting", "get value of a setting",
40 "Syntax: get-setting <setting>\nShow value of setting\n",
41 [](const std::string
& args
) throw(std::bad_alloc
, std::runtime_error
) {
42 tokensplitter
t(args
);
43 std::string settingname
= t
;
44 if(settingname
== "" || t
.tail() != "")
45 throw std::runtime_error("Expected setting name and nothing else");
46 if(setting::is_set(settingname
))
47 messages
<< "Setting '" << settingname
<< "' has value '"
48 << setting::get(settingname
) << "'" << std::endl
;
50 messages
<< "Setting '" << settingname
<< "' unset" << std::endl
;
53 function_ptr_command
<> show_settings("show-settings", "Show values of all settings",
54 "Syntax: show-settings\nShow value of all settings\n",
55 []() throw(std::bad_alloc
, std::runtime_error
) {
60 setting::setting(const std::string
& name
) throw(std::bad_alloc
)
63 settings
= new std::map
<std::string
, setting
*>();
64 (*settings
)[settingname
= name
] = this;
67 setting::~setting() throw()
71 settings
->erase(settingname
);
74 void setting::set(const std::string
& _setting
, const std::string
& value
) throw(std::bad_alloc
, std::runtime_error
)
76 if(!settings
|| !settings
->count(_setting
))
77 throw std::runtime_error("No such setting '" + _setting
+ "'");
79 (*settings
)[_setting
]->set(value
);
80 } catch(std::bad_alloc
& e
) {
82 } catch(std::exception
& e
) {
83 throw std::runtime_error("Can't set setting '" + _setting
+ "': " + e
.what());
87 void setting::blank(const std::string
& _setting
) throw(std::bad_alloc
, std::runtime_error
)
89 if(!settings
|| !settings
->count(_setting
))
90 throw std::runtime_error("No such setting '" + _setting
+ "'");
92 (*settings
)[_setting
]->blank();
93 } catch(std::bad_alloc
& e
) {
95 } catch(std::exception
& e
) {
96 throw std::runtime_error("Can't blank setting '" + _setting
+ "': " + e
.what());
100 std::string
setting::get(const std::string
& _setting
) throw(std::bad_alloc
, std::runtime_error
)
102 if(!settings
|| !settings
->count(_setting
))
103 throw std::runtime_error("No such setting '" + _setting
+ "'");
104 return (*settings
)[_setting
]->get();
107 bool setting::is_set(const std::string
& _setting
) throw(std::bad_alloc
, std::runtime_error
)
109 if(!settings
|| !settings
->count(_setting
))
110 throw std::runtime_error("No such setting '" + _setting
+ "'");
111 return (*settings
)[_setting
]->is_set();
114 void setting::print_all() throw(std::bad_alloc
)
118 for(auto i
= settings
->begin(); i
!= settings
->end(); i
++) {
119 if(!i
->second
->is_set())
120 messages
<< i
->first
<< ": (unset)" << std::endl
;
122 messages
<< i
->first
<< ": " << i
->second
->get() << std::endl
;
126 numeric_setting::numeric_setting(const std::string
& sname
, int32_t minv
, int32_t maxv
, int32_t dflt
)
127 throw(std::bad_alloc
)
135 void numeric_setting::blank() throw(std::bad_alloc
, std::runtime_error
)
137 throw std::runtime_error("This setting can't be blanked");
140 bool numeric_setting::is_set() throw()
145 void numeric_setting::set(const std::string
& _value
) throw(std::bad_alloc
, std::runtime_error
)
147 int32_t v
= parse_value
<int32_t>(_value
);
148 if(v
< minimum
|| v
> maximum
) {
149 std::ostringstream x
;
150 x
<< "Value out of range (" << minimum
<< " - " << maximum
<< ")";
151 throw std::runtime_error(x
.str());
156 std::string
numeric_setting::get() throw(std::bad_alloc
)
158 std::ostringstream x
;
163 numeric_setting::operator int32_t() throw()
168 boolean_setting::boolean_setting(const std::string
& sname
, bool dflt
) throw(std::bad_alloc
)
173 void boolean_setting::blank() throw(std::bad_alloc
, std::runtime_error
)
175 throw std::runtime_error("This setting can't be unset");
178 bool boolean_setting::is_set() throw()
183 void boolean_setting::set(const std::string
& v
) throw(std::bad_alloc
, std::runtime_error
)
185 if(v
== "true" || v
== "yes" || v
== "on" || v
== "1" || v
== "enable" || v
== "enabled")
187 else if(v
== "false" || v
== "no" || v
== "off" || v
== "0" || v
== "disable" || v
== "disabled")
190 throw std::runtime_error("Invalid value for boolean setting");
192 std::string
boolean_setting::get() throw(std::bad_alloc
)
200 boolean_setting::operator bool() throw()