preCICE
v3.1.2
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
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
ValueParser.hpp
std::istringstream
std::string
precice::xml::XMLAttribute
Definition
XMLAttribute.hpp:20
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
sstream
stdexcept
vector