preCICE
v3.2.0
Loading...
Searching...
No Matches
src
xml
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
9
namespace
precice::xml
{
10
11
namespace
{
12
constexpr
static
const
char
*PARSING_LOCALE =
"en_US.UTF-8"
;
13
14
double
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
30
void
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
42
void
readValueSpecific
(
const
std::string
&rawValue,
int
&value)
43
{
44
std::istringstream
iss{rawValue};
45
try
{
46
iss.
imbue
(
std::locale
(PARSING_LOCALE));
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
55
void
readValueSpecific
(
const
std::string
&rawValue, Eigen::VectorXd &value)
56
{
57
std::vector<std::string>
components;
58
boost::split(
59
components, rawValue, [](
char
c) {
return
c ==
';'
; }, boost::algorithm::token_compress_on);
60
const
int
size = components.
size
();
61
62
Eigen::VectorXd vec(size);
63
for
(
int
i = 0; i != size; ++i) {
64
vec(i) = parseDouble(components[i]);
65
}
66
value = vec;
67
}
68
69
}
// namespace precice::xml
ValueParser.hpp
std::istringstream
std::string
std::istringstream::eof
T eof(T... args)
std::string::find
T find(T... args)
std::istringstream::imbue
T imbue(T... args)
std::locale
precice::xml
contains the XML configuration parser.
Definition
CommunicationConfiguration.hpp:9
precice::xml::readValueSpecific
void readValueSpecific(const std::string &rawValue, double &value)
Definition
ValueParser.cpp:30
ostream
std::runtime_error
std::string::size
T size(T... args)
sstream
stdexcept
std::string::substr
T substr(T... args)
vector