lsnes rr2-β24
[lsnes.git] / src / interface / setting.cpp
blobf7bfcf46743d1b069f57b7fab5cb80837119ff3b
1 #include "interface/setting.hpp"
2 #include "library/string.hpp"
4 core_setting_value::core_setting_value(const core_setting_value_param& p) throw(std::bad_alloc)
5 : iname(p.iname), hname(p.hname), index(p.index)
9 core_setting::core_setting(const core_setting_param& p)
10 : iname(p.iname), hname(p.hname), regex(p.regex ? p.regex : ""), dflt(p.dflt)
12 for(auto i : p.values)
13 values.push_back(core_setting_value(i));
16 bool core_setting::is_boolean() const throw()
18 if(values.size() != 2)
19 return false;
20 std::string a = values[0].iname;
21 std::string b = values[1].iname;
22 if(a > b)
23 std::swap(a, b);
24 return (a == "0" && b == "1");
27 bool core_setting::is_freetext() const throw()
29 return (values.size() == 0);
32 bool core_setting::validate(const std::string& value) const
34 if(values.size() != 0) {
35 for(auto i : values)
36 if(i.iname == value)
37 return true;
38 return false;
39 } else
40 return regex_match(regex, value);
43 core_setting_group::core_setting_group()
47 core_setting_group::core_setting_group(std::initializer_list<core_setting_param> _settings)
49 for(auto i : _settings)
50 settings.insert(std::make_pair(i.iname, core_setting(i)));
53 core_setting_group::core_setting_group(std::vector<core_setting_param> _settings)
55 for(auto i : _settings)
56 settings.insert(std::make_pair(i.iname, core_setting(i)));
59 void core_setting_group::fill_defaults(std::map<std::string, std::string>& values) throw(std::bad_alloc)
61 for(auto i : settings)
62 if(!values.count(i.first))
63 values[i.first] = i.second.dflt;
66 std::set<std::string> core_setting_group::get_setting_set()
68 std::set<std::string> r;
69 for(auto i : settings)
70 r.insert(i.first);
71 return r;
74 std::vector<std::string> core_setting::hvalues() const throw(std::runtime_error)
76 std::vector<std::string> x;
77 if(values.size() == 0)
78 throw std::runtime_error("hvalues() not valid for freetext settings");
79 for(auto i : values)
80 x.push_back(i.hname);
81 return x;
84 std::string core_setting::hvalue_to_ivalue(const std::string& hvalue) const throw(std::runtime_error)
86 for(auto i : values)
87 if(i.hname == hvalue)
88 return i.iname;
89 throw std::runtime_error("Invalid hvalue for setting");
92 signed core_setting::ivalue_to_index(const std::string& ivalue) const throw(std::runtime_error)
94 for(auto i : values)
95 if(i.iname == ivalue)
96 return i.index;
97 throw std::runtime_error("Invalid ivalue for setting");