cprover
Toggle main menu visibility
Loading...
Searching...
No Matches
options.cpp
Go to the documentation of this file.
1
/*******************************************************************\
2
3
Module: Options
4
5
Author: Daniel Kroening, kroening@kroening.com
6
7
\*******************************************************************/
8
11
12
#include "
options.h
"
13
14
#include "
constructor_of.h
"
15
#include "
json.h
"
16
#include "
range.h
"
17
#include "
string2int.h
"
18
#include "
xml.h
"
19
20
void
optionst::set_option
(
const
std::string &option,
21
const
std::string &value)
22
{
23
value_listt
&value_list=
option_map
[option];
24
value_list.clear();
25
value_list.push_back(value);
26
}
27
28
void
optionst::set_option
(
const
std::string &option,
29
const
bool
value)
30
{
31
set_option
(option, std::string(value?
"1"
:
"0"
));
32
}
33
34
void
optionst::set_option
(
const
std::string &option,
const
int
value)
35
{
36
set_option
(option, std::to_string(value));
37
}
38
39
void
optionst::set_option
(
const
std::string &option,
const
unsigned
value)
40
{
41
set_option
(option, std::to_string(value));
42
}
43
44
bool
optionst::get_bool_option
(
const
std::string &option)
const
45
{
46
const
std::string value=
get_option
(option);
47
return
value.empty()?
false
:(std::stoi(value)!=0);
48
}
49
50
signed
int
optionst::get_signed_int_option
(
const
std::string &option)
const
51
{
52
const
std::string value=
get_option
(option);
53
return
value.empty()?0:std::stoi(value);
54
}
55
56
unsigned
int
optionst::get_unsigned_int_option
(
const
std::string &option)
const
57
{
58
const
std::string value=
get_option
(option);
59
return
value.empty()?0:
safe_string2unsigned
(value);
60
}
61
62
bool
optionst::is_set
(
const
std::string &option)
const
63
{
64
return
option_map
.find(option) !=
option_map
.end();
65
}
66
67
const
std::string
optionst::get_option
(
const
std::string &option)
const
68
{
69
option_mapt::const_iterator it=
70
option_map
.find(option);
71
72
if
(it==
option_map
.end())
73
return
std::string();
74
else
if
(it->second.empty())
75
return
std::string();
76
else
77
return
it->second.front();
78
}
79
80
const
optionst::value_listt
&
optionst::get_list_option
(
81
const
std::string &option)
const
82
{
83
option_mapt::const_iterator it=
84
option_map
.find(option);
85
86
if
(it==
option_map
.end())
87
return
empty_list
;
88
else
89
return
it->second;
90
}
91
93
json_objectt
optionst::to_json
()
const
94
{
95
return
make_range
(
option_map
)
96
.map([](
const
std::pair<std::string, value_listt> &option_pair) {
97
return
std::pair<std::string, json_arrayt>{
98
option_pair.first,
99
make_range
(option_pair.second).map(
constructor_of<json_stringt>
())};
100
});
101
}
102
104
xmlt
optionst::to_xml
()
const
105
{
106
xmlt
xml_options(
"options"
);
107
for
(
const
auto
&option_pair :
option_map
)
108
{
109
xmlt
&xml_option = xml_options.
new_element
(
"option"
);
110
xml_option.
set_attribute
(
"name"
, option_pair.first);
111
for
(
const
auto
&value : option_pair.second)
112
{
113
xmlt
&xml_value = xml_option.
new_element
(
"value"
);
114
xml_value.
data
= value;
115
}
116
}
117
return
xml_options;
118
}
119
121
void
optionst::output
(std::ostream &out)
const
122
{
123
for
(
const
auto
&option_pair :
option_map
)
124
{
125
out << option_pair.first <<
": "
;
126
bool
first =
true
;
127
for
(
const
auto
&value : option_pair.second)
128
{
129
if
(first)
130
first =
false
;
131
else
132
out <<
", "
;
133
out <<
'"'
<< value <<
'"'
;
134
}
135
out <<
"\n"
;
136
}
137
}
json_objectt
Definition
json.h:298
optionst::get_unsigned_int_option
unsigned int get_unsigned_int_option(const std::string &option) const
Definition
options.cpp:56
optionst::is_set
bool is_set(const std::string &option) const
N.B. opts.is_set("foo") does not imply opts.get_bool_option("foo").
Definition
options.cpp:62
optionst::to_json
json_objectt to_json() const
Returns the options as JSON key value pairs.
Definition
options.cpp:93
optionst::empty_list
const value_listt empty_list
Definition
options.h:67
optionst::option_map
option_mapt option_map
Definition
options.h:66
optionst::get_bool_option
bool get_bool_option(const std::string &option) const
Definition
options.cpp:44
optionst::set_option
void set_option(const std::string &option, const bool value)
Definition
options.cpp:28
optionst::get_option
const std::string get_option(const std::string &option) const
Definition
options.cpp:67
optionst::to_xml
xmlt to_xml() const
Returns the options in XML format.
Definition
options.cpp:104
optionst::output
void output(std::ostream &out) const
Outputs the options to out.
Definition
options.cpp:121
optionst::get_signed_int_option
signed int get_signed_int_option(const std::string &option) const
Definition
options.cpp:50
optionst::get_list_option
const value_listt & get_list_option(const std::string &option) const
Definition
options.cpp:80
optionst::value_listt
std::list< std::string > value_listt
Definition
options.h:25
xmlt
Definition
xml.h:21
xmlt::new_element
xmlt & new_element(const std::string &key)
Definition
xml.h:95
xmlt::set_attribute
void set_attribute(const std::string &attribute, unsigned value)
Definition
xml.cpp:198
xmlt::data
std::string data
Definition
xml.h:39
constructor_of.h
constructor_of
constexpr constructor_oft< constructedt > constructor_of()
Returns a functor which constructs type constructedt.
Definition
constructor_of.h:30
json.h
options.h
Options.
range.h
Ranges: pair of begin and end iterators, which can be initialized from containers,...
make_range
ranget< iteratort > make_range(iteratort begin, iteratort end)
Definition
range.h:522
safe_string2unsigned
unsigned safe_string2unsigned(const std::string &str, int base)
Definition
string2int.cpp:16
string2int.h
xml.h
util
options.cpp
Generated by
1.17.0