MaMiCo 1.2
Loading...
Searching...
No Matches
ParseConfiguration.h
1// Copyright (C) 2015 Technische Universitaet Muenchen
2// This file is part of the Mamico project. For conditions of distribution
3// and use, please see the copyright notice in Mamico's main folder, or at
4// www5.in.tum.de/mamico
5#ifndef _TARCH_CONFIGURATION_PARSECONFIGURATION_H_
6#define _TARCH_CONFIGURATION_PARSECONFIGURATION_H_
7#include "tarch/la/Vector.h"
8#include "tarch/tinyxml2/tinyxml2.h"
9#include <cstdlib>
10#include <filesystem>
11#include <iostream>
12#include <sstream>
13
14namespace tarch {
15namespace configuration {
16
21public:
26 class XMLConfiguration {
27 private:
28 XMLConfiguration(tinyxml2::XMLNode* _root, tinyxml2::XMLError _error) : root(_root), error(_error) {}
29
30 public:
35
39 tinyxml2::XMLError const error;
40
45 static XMLConfiguration load(const std::string filename) {
47 tinyxml2::XMLError error = doc->LoadFile(filename.c_str());
48 tinyxml2::XMLNode* root = doc->FirstChildElement("scenario-configuration");
49 if (root == NULL) {
50 root = doc;
51 std::cout << "No root node <scenario-configuration> found in configuration file. (Using legacy format without XML root node.)" << std::endl;
52 }
53 return XMLConfiguration(root, error);
54 }
55
56 ~XMLConfiguration() { delete root->GetDocument(); }
57 };
58
63 template <class Configuration> static void parseConfiguration(const std::string filename, const std::string topleveltag, Configuration& config) {
64 XMLConfiguration xmlConfig = XMLConfiguration::load(filename);
65 tinyxml2::XMLElement* node = xmlConfig.root->FirstChildElement(topleveltag.c_str());
66 if (node == NULL) {
67 std::cout << "Could not read input file " << filename << " (missing or invalid)" << std::endl;
68 exit(EXIT_FAILURE);
69 }
70 config.parseSubtag(node);
71 }
72
79 static void readDoubleMandatory(double& storage, tinyxml2::XMLElement* node, std::string tag) {
80 double value;
81 if (node->QueryDoubleAttribute(tag.c_str(), &value) != tinyxml2::XML_SUCCESS) {
82 std::cout << "Error while reading mandatory argument " << tag << " of XML element " << node->Name() << std::endl;
83 exit(EXIT_FAILURE);
84 } else {
85 storage = value;
86 }
87 }
88
96 static void readDoubleOptional(double& storage, tinyxml2::XMLElement* node, std::string tag) {
97 double value;
98 int result = node->QueryDoubleAttribute(tag.c_str(), &value);
99 if (result == tinyxml2::XML_NO_ATTRIBUTE) {
100 // nop
101 } else if (result == tinyxml2::XML_WRONG_ATTRIBUTE_TYPE) {
102 std::cout << "Error while reading optional argument " << tag << " of XML element " << node->Name() << std::endl;
103 exit(EXIT_FAILURE);
104 } else {
105 storage = value;
106 }
107 }
108
115 static void readIntMandatory(int& storage, tinyxml2::XMLElement* node, std::string tag) {
116 int value;
117 if (node->QueryIntAttribute(tag.c_str(), &value) != tinyxml2::XML_SUCCESS) {
118 std::cout << "Error while reading mandatory argument " << tag << " of XML element " << node->Name() << std::endl;
119 exit(EXIT_FAILURE);
120 } else {
121 storage = value;
122 }
123 }
124
132 static void readIntOptional(int& storage, tinyxml2::XMLElement* node, std::string tag) {
133 int value;
134 int result = node->QueryIntAttribute(tag.c_str(), &value);
135 if (result == tinyxml2::XML_NO_ATTRIBUTE) {
136 // nop
137 } else if (result == tinyxml2::XML_WRONG_ATTRIBUTE_TYPE) {
138 std::cout << "Error while reading optional argument " << tag << " of XML element " << node->Name() << std::endl;
139 exit(EXIT_FAILURE);
140 } else {
141 storage = value;
142 }
143 }
144
151 static void readBoolMandatory(bool& storage, tinyxml2::XMLElement* node, std::string tag) {
152 const char* myTextChar = node->Attribute(tag.c_str());
153 if (myTextChar == NULL) {
154 std::cout << "Error: mandatory bool " << tag << " could not be found!" << std::endl;
155 exit(EXIT_FAILURE);
156 }
157 std::string myText(myTextChar);
158 if (myText == "yes") {
159 storage = true;
160 } else if (myText == "no") {
161 storage = false;
162 } else {
163 std::cout << "Error while reading bool optional argument: Argument can "
164 "only be yes or no!"
165 << std::endl;
166 exit(EXIT_FAILURE);
167 }
168 }
169
177 static void readBoolOptional(bool& storage, tinyxml2::XMLElement* node, std::string tag) {
178 const char* myTextChar = node->Attribute(tag.c_str());
179 if (myTextChar == NULL) {
180 return;
181 }
182 std::string myText(myTextChar);
183 if (myText == "yes") {
184 storage = true;
185 } else if (myText == "no") {
186 storage = false;
187 } else {
188 std::cout << "Error while reading bool optional argument: Argument can "
189 "only be yes or no!"
190 << std::endl;
191 exit(EXIT_FAILURE);
192 }
193 }
194
201 static void readStringMandatory(std::string& storage, tinyxml2::XMLElement* node, std::string tag) {
202 const char* myText = node->Attribute(tag.c_str());
203 if (myText == NULL) {
204 std::cout << "Error while reading mandatory argument " << tag << " of XML element " << node->Name() << std::endl;
205 exit(EXIT_FAILURE);
206 } else {
207 storage = std::string(myText);
208 }
209 }
210
218 static void readStringOptional(std::string& storage, tinyxml2::XMLElement* node, std::string tag) {
219 const char* myText = node->Attribute(tag.c_str());
220 if (myText != NULL) {
221 storage = std::string(myText);
222 }
223 }
224
232 template <unsigned int size, class T> static void readVector(tarch::la::Vector<size, T>& result, const char* myText) {
233 std::string input(myText);
234 for (unsigned int i = 0; i < size; i++) {
235 // search for first non-whitespace entry in this vector entry
236 std::size_t first = input.find_first_not_of(" ");
237 // search for end of this vector component, typically denoted by ";"
238 // -> if this is the last entry, then npos is accepted as well
239 std::size_t last = input.find_first_of(";");
240 // for debugging
241 // std::cout << first << ", " << last << std::endl;
242 if ((i == size - 1) && (last == std::string::npos)) {
243 last = input.size();
244 }
245
246 std::stringstream ss(input.substr(first, last - first));
247 // for debugging
248 // std::cout << ss.str() << std::endl;
249 ss >> result[i];
250 if (i < size - 1) {
251 input = input.substr(last + 1, input.size() - last - 1);
252 }
253 }
254 }
255
264 template <unsigned int size, class T> static void readVectorMandatory(tarch::la::Vector<size, T>& result, tinyxml2::XMLElement* node, std::string tag) {
265 const char* myText = node->Attribute(tag.c_str());
266 if (myText == NULL) {
267 std::cout << "Error while reading mandatory argument " << tag << " of XML element " << node->Name() << std::endl;
268 exit(EXIT_FAILURE);
269 }
270 readVector<size, T>(result, myText);
271 }
272
282 template <unsigned int size, class T> static void readVectorOptional(tarch::la::Vector<size, T>& result, tinyxml2::XMLElement* node, std::string tag) {
283 const char* myText = node->Attribute(tag.c_str());
284 if (myText != NULL) {
285 readVector<size, T>(result, myText);
286 }
287 }
288};
289} // namespace configuration
290} // namespace tarch
291#endif
Definition Configuration.h:22
virtual void parseSubtag(tinyxml2::XMLElement *node)=0
static XMLConfiguration load(const std::string filename)
Definition ParseConfiguration.h:45
tinyxml2::XMLError const error
Definition ParseConfiguration.h:39
tinyxml2::XMLNode *const root
Definition ParseConfiguration.h:34
Definition ParseConfiguration.h:20
static void readBoolMandatory(bool &storage, tinyxml2::XMLElement *node, std::string tag)
Definition ParseConfiguration.h:151
static void readStringMandatory(std::string &storage, tinyxml2::XMLElement *node, std::string tag)
Definition ParseConfiguration.h:201
static void readVectorMandatory(tarch::la::Vector< size, T > &result, tinyxml2::XMLElement *node, std::string tag)
Definition ParseConfiguration.h:264
static void readStringOptional(std::string &storage, tinyxml2::XMLElement *node, std::string tag)
Definition ParseConfiguration.h:218
static void readIntOptional(int &storage, tinyxml2::XMLElement *node, std::string tag)
Definition ParseConfiguration.h:132
static void parseConfiguration(const std::string filename, const std::string topleveltag, Configuration &config)
Definition ParseConfiguration.h:63
static void readIntMandatory(int &storage, tinyxml2::XMLElement *node, std::string tag)
Definition ParseConfiguration.h:115
static void readVectorOptional(tarch::la::Vector< size, T > &result, tinyxml2::XMLElement *node, std::string tag)
Definition ParseConfiguration.h:282
static void readDoubleOptional(double &storage, tinyxml2::XMLElement *node, std::string tag)
Definition ParseConfiguration.h:96
static void readDoubleMandatory(double &storage, tinyxml2::XMLElement *node, std::string tag)
Definition ParseConfiguration.h:79
static void readVector(tarch::la::Vector< size, T > &result, const char *myText)
Definition ParseConfiguration.h:232
static void readBoolOptional(bool &storage, tinyxml2::XMLElement *node, std::string tag)
Definition ParseConfiguration.h:177
Definition Vector.h:24
Definition tinyxml2.h:1721
XMLError LoadFile(const char *filename)
Definition tinyxml2.h:1268
const char * Attribute(const char *name, const char *value=0) const
XMLError QueryDoubleAttribute(const char *name, double *value) const
See QueryIntAttribute()
Definition tinyxml2.h:1390
XMLError QueryIntAttribute(const char *name, int *value) const
Definition tinyxml2.h:1346
const char * Name() const
Get the name of an element (which is the Value() of the node.)
Definition tinyxml2.h:1272
Definition tinyxml2.h:672
const XMLElement * FirstChildElement(const char *name=0) const
const XMLDocument * GetDocument() const
Get the XMLDocument that owns this XMLNode.
Definition tinyxml2.h:678
Definition Configuration.h:13
Definition Configuration.h:11