16 #include <yaml-cpp/yaml.h> 17 #include <boost/filesystem.hpp> 18 #include <boost/filesystem/fstream.hpp> 37 std::initializer_list<const char *> keys) {
38 assert(keys.size() > 0);
39 for (
auto key : keys) {
41 node.reset(node[key]);
54 std::vector<std::string> to_remove(root.size());
57 if ((
n.second.IsMap() ||
n.second.IsSequence()) &&
n.second.size() == 0) {
58 to_remove.emplace_back(
n.first.Scalar());
61 for (
const auto &key : to_remove) {
75 YAML::Node
operator|=(YAML::Node a,
const YAML::Node &b) {
78 a[n0.first.Scalar()] |= n0.second;
94 const auto file_path = path / filename;
95 if (!bf::exists(file_path)) {
98 "', but the file does not exist.");
101 throw std::runtime_error(
102 "The configuration file has CR LF line endings. Please use LF " 106 root_node_ = YAML::LoadFile(file_path.native());
107 }
catch (YAML::ParserException &e) {
108 if (e.msg ==
"illegal map value" || e.msg ==
"end of map not found") {
109 const auto line = std::to_string(e.mark.line + 1);
110 throw ParseError(
"YAML parse error at\n" + file_path.native() +
':' +
111 line +
": " + e.msg +
112 " (check that the indentation of map keys matches)");
121 }
catch (YAML::ParserException &e) {
122 if (e.msg ==
"illegal map value" || e.msg ==
"end of map not found") {
123 const auto line = std::to_string(e.mark.line + 1);
124 throw ParseError(
"YAML parse error in:\n" + yaml +
"\nat line " + line +
126 " (check that the indentation of map keys matches)");
133 std::vector<std::string> r;
135 r.emplace_back(i.first.Scalar());
141 std::initializer_list<const char *> keys) {
142 assert(keys.size() > 0);
144 auto keyIt = begin(keys);
146 for (; i < keys.size() - 1; ++i, ++keyIt) {
151 node.reset(node[*keyIt]);
153 const auto r = node[*keyIt];
155 return {r, keys.begin()[keys.size() - 1]};
159 std::initializer_list<const char *> keys)
const {
164 std::vector<std::string> to_remove;
166 if (i.first.Scalar() != key) {
167 to_remove.push_back(i.first.Scalar());
170 for (
auto i : to_remove) {
171 root_node_.remove(i);
176 std::initializer_list<const char *> keys)
const {
178 return n.IsDefined();
183 return n.IsDefined() && (!
n.IsNull());
std::string to_string() const
Returns a YAML string of the current tree.
Thrown if the file does not exist.
Return type of Configuration::take that automatically determines the target type. ...
YAML::Node operator|=(YAML::Node a, const YAML::Node &b)
Merge two YAML::Nodes.
std::string unused_values_report() const
Returns a string listing the key/value pairs that have not been taken yet.
Configuration(const bf::path &path)
Reads config.yaml from the specified path.
YAML::Node find_node_at(YAML::Node node, std::initializer_list< const char * > keys)
Finds a node, copies its structure and replaces the previous keys by the newly provided keys...
Value read(std::initializer_list< const char * > keys) const
Additional interface for SMASH to read configuration values without removing them.
Interface to the SMASH configuration files.
YAML::Node remove_empty_maps(YAML::Node root)
Removes all empty maps of a YAML::Node.
Thrown for YAML parse errors.
bool has_value(std::initializer_list< const char * > keys) const
Returns whether there is a non-empty value behind the requested keys.
std::vector< std::string > list_upmost_nodes()
Lists all YAML::Nodes from the configuration setup.
bool has_value_including_empty(std::initializer_list< const char * > keys) const
Returns if there is a (maybe empty) value behind the requested keys.
void merge_yaml(const std::string &yaml)
Merge the configuration in yaml into the existing tree.
Value take(std::initializer_list< const char * > keys)
The default interface for SMASH to read configuration values.
void remove_all_but(const std::string &key)
Removes all entries in the map except for key.
bool has_crlf_line_ending(const std::string in)
Check if a line in the string ends with \r\n.
YAML::Node root_node_
the general_config.yaml contents - fully parsed
std::string read_all(std::istream &&input)
Utility function to read a complete input stream (e.g.