preCICE v3.1.1
Loading...
Searching...
No Matches
ValueParser.cpp
Go to the documentation of this file.
1#include "xml/ValueParser.hpp"
2#include <boost/algorithm/string/constants.hpp>
3#include <boost/algorithm/string/split.hpp>
4#include <ostream>
5#include <sstream>
6#include <stdexcept>
7#include <vector>
8
9namespace precice::xml {
10
11namespace {
12constexpr static const char *PARSING_LOCALE = "en_US.UTF-8";
13
14double parseDouble(const std::string &rawValue)
15{
16 std::istringstream iss{rawValue};
17 try {
18 iss.imbue(std::locale(PARSING_LOCALE));
19 } catch (...) {
20 }
21 double value;
22 iss >> value;
23 if (!iss.eof()) {
24 throw std::runtime_error{"Could not fully parse value \"" + rawValue + "\" as a double."};
25 }
26 return value;
27}
28} // namespace
29
30void readValueSpecific(const std::string &rawValue, double &value)
31{
32 if (rawValue.find('/') != std::string::npos) {
33 std::string left = rawValue.substr(0, rawValue.find('/'));
34 std::string right = rawValue.substr(rawValue.find('/') + 1, rawValue.size() - rawValue.find('/') - 1);
35
36 value = parseDouble(left) / parseDouble(right);
37 } else {
38 value = parseDouble(rawValue);
39 }
40}
41
42void readValueSpecific(const std::string &rawValue, int &value)
43{
45 try {
47 } catch (...) {
48 }
49 iss >> value;
50 if (!iss.eof()) {
51 throw std::runtime_error{"Could not fully parse value \"" + rawValue + "\" as an int."};
52 }
53}
54
55void readValueSpecific(const std::string &rawValue, Eigen::VectorXd &value)
56{
58 boost::split(
59 components, rawValue, [](char c) { return c == ';'; }, boost::algorithm::token_compress_on);
60 const int size = components.size();
61 if (size < 2 || size > 3) {
62 throw std::runtime_error{"The value \"" + rawValue + "\" is not a 2D or 3D vector."};
63 }
64
65 Eigen::VectorXd vec(size);
66 for (int i = 0; i != size; ++i) {
67 vec(i) = parseDouble(components[i]);
68 }
69 value = vec;
70}
71
72} // namespace precice::xml
contains the XML configuration parser.
void readValueSpecific(const std::string &rawValue, double &value)