CBMC
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 
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 
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 
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 }
unsigned int get_unsigned_int_option(const std::string &option) const
Definition: options.cpp:56
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
json_objectt to_json() const
Returns the options as JSON key value pairs.
Definition: options.cpp:93
const value_listt empty_list
Definition: options.h:67
option_mapt option_map
Definition: options.h:66
bool get_bool_option(const std::string &option) const
Definition: options.cpp:44
void set_option(const std::string &option, const bool value)
Definition: options.cpp:28
const std::string get_option(const std::string &option) const
Definition: options.cpp:67
xmlt to_xml() const
Returns the options in XML format.
Definition: options.cpp:104
void output(std::ostream &out) const
Outputs the options to out
Definition: options.cpp:121
signed int get_signed_int_option(const std::string &option) const
Definition: options.cpp:50
const value_listt & get_list_option(const std::string &option) const
Definition: options.cpp:80
std::list< std::string > value_listt
Definition: options.h:25
Definition: xml.h:21
void set_attribute(const std::string &attribute, unsigned value)
Definition: xml.cpp:198
xmlt & new_element(const std::string &key)
Definition: xml.h:95
std::string data
Definition: xml.h:39
Options.
Ranges: pair of begin and end iterators, which can be initialized from containers,...
ranget< iteratort > make_range(iteratort begin, iteratort end)
Definition: range.h:522
unsigned safe_string2unsigned(const std::string &str, int base)
Definition: string2int.cpp:16
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.