forked from openmc-dev/openmc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxml_interface.cpp
59 lines (49 loc) · 1.48 KB
/
xml_interface.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "openmc/xml_interface.h"
#include <algorithm> // for transform
#include <sstream>
#include "openmc/error.h"
namespace openmc {
std::string
get_node_value(pugi::xml_node node, const char* name, bool lowercase,
bool strip)
{
// Search for either an attribute or child tag and get the data as a char*.
const pugi::char_t* value_char;
if (node.attribute(name)) {
value_char = node.attribute(name).value();
} else if (node.child(name)) {
value_char = node.child_value(name);
} else {
std::stringstream err_msg;
err_msg << "Node \"" << name << "\" is not a member of the \""
<< node.name() << "\" XML node";
fatal_error(err_msg);
}
std::string value {value_char};
// Convert to lower-case if needed
if (lowercase) {
std::transform(value.begin(), value.end(), value.begin(), ::tolower);
}
// Strip leading/trailing whitespace if needed
if (strip) {
value.erase(0, value.find_first_not_of(" \t\r\n"));
value.erase(value.find_last_not_of(" \t\r\n") + 1);
}
return value;
}
bool
get_node_value_bool(pugi::xml_node node, const char* name)
{
if (node.attribute(name)) {
return node.attribute(name).as_bool();
} else if (node.child(name)) {
return node.child(name).text().as_bool();
} else {
std::stringstream err_msg;
err_msg << "Node \"" << name << "\" is not a member of the \""
<< node.name() << "\" XML node";
fatal_error(err_msg);
}
return false;
}
} // namespace openmc