|
11 | 11 |
|
12 | 12 | #include "options.h"
|
13 | 13 |
|
| 14 | +#include "json.h" |
14 | 15 | #include "string2int.h"
|
| 16 | +#include "xml.h" |
15 | 17 |
|
16 | 18 | void optionst::set_option(const std::string &option,
|
17 | 19 | const std::string &value)
|
@@ -86,3 +88,52 @@ const optionst::value_listt &optionst::get_list_option(
|
86 | 88 | else
|
87 | 89 | return it->second;
|
88 | 90 | }
|
| 91 | + |
| 92 | +/// Returns the options as JSON key value pairs |
| 93 | +json_objectt optionst::to_json() const |
| 94 | +{ |
| 95 | + json_objectt json_options; |
| 96 | + for(const auto &option_pair : option_map) |
| 97 | + { |
| 98 | + json_arrayt &values = json_options[option_pair.first].make_array(); |
| 99 | + for(const auto &value : option_pair.second) |
| 100 | + values.push_back(json_stringt(value)); |
| 101 | + } |
| 102 | + return json_options; |
| 103 | +} |
| 104 | + |
| 105 | +/// Returns the options in XML format |
| 106 | +xmlt optionst::to_xml() const |
| 107 | +{ |
| 108 | + xmlt xml_options("options"); |
| 109 | + for(const auto &option_pair : option_map) |
| 110 | + { |
| 111 | + xmlt &xml_option = xml_options.new_element("option"); |
| 112 | + xml_option.set_attribute("name", option_pair.first); |
| 113 | + for(const auto &value : option_pair.second) |
| 114 | + { |
| 115 | + xmlt &xml_value = xml_option.new_element("value"); |
| 116 | + xml_value.data = value; |
| 117 | + } |
| 118 | + } |
| 119 | + return xml_options; |
| 120 | +} |
| 121 | + |
| 122 | +/// Outputs the options to `out` |
| 123 | +void optionst::output(std::ostream &out) const |
| 124 | +{ |
| 125 | + for(const auto &option_pair : option_map) |
| 126 | + { |
| 127 | + out << option_pair.first << ": "; |
| 128 | + bool first = true; |
| 129 | + for(const auto &value : option_pair.second) |
| 130 | + { |
| 131 | + if(first) |
| 132 | + first = false; |
| 133 | + else |
| 134 | + out << ", "; |
| 135 | + out << '"' << value << '"'; |
| 136 | + } |
| 137 | + out << "\n"; |
| 138 | + } |
| 139 | +} |
0 commit comments