29#ifndef OLB_IO_XML_READER_H
30#define OLB_IO_XML_READER_H
49template <
class T,
unsigned DIM>
class ADf;
72 template <
typename T>
bool read(T& value,
bool verboseOn =
true,
bool exitIfMissing=
false)
const;
73 template <
typename T,
unsigned DIM>
bool read(
util::ADf<T,DIM>& value,
bool verboseOn =
true,
bool exitIfMissing=
false)
const;
74 template <
typename T>
bool read(std::vector<T>& value,
bool verboseOn =
true,
bool exitIfMissing=
false)
const;
75 template <
typename T> T
get(
bool verboseOn =
true,
bool exitIfMissing=
false)
const;
78 template<
typename ParameterType>
80 std::string name_parameter_2, std::string name_parameter_3,
81 ParameterType& var,
bool defaultAvailable =
true,
bool exitIfMissing =
false,
bool showWarning =
true)
const;
83 template<
typename ParameterType>
85 std::string name_parameter_2, std::string name_parameter_3, std::string name_parameter_4,
86 ParameterType& var,
bool defaultAvailable =
true,
bool exitIfMissing =
false,
bool showWarning =
true)
const;
93 std::vector<XMLreader*>::const_iterator
begin()
const;
98 std::vector<XMLreader*>::const_iterator
end()
const;
106 std::string
getAttribute(
const std::string& aName)
const;
112 void mainProcessorIni(tinyxml2::XMLNode* pParent);
117 mutable bool _warningsOn;
130template <
typename T,
unsigned DIM>
133 std::stringstream valueStr(_text);
135 if (!(valueStr >> tmp)) {
147bool XMLreader::read(std::vector<T>& values,
bool verboseOn,
bool exitIfMissing )
const
149 std::stringstream multiValueStr(_text);
151 std::vector<T> tmp(values);
152 while (multiValueStr>>word) {
153 std::stringstream valueStr(word);
155 if (!(valueStr >> value)) {
162 tmp.push_back(value);
171 std::stringstream valueStr(_text);
173 if (!(valueStr >> tmp)) {
182template<
typename ParameterType>
184 std::string name_parameter_2, std::string name_parameter_3,
185 ParameterType& var,
bool defaultAvailable,
bool exitIfMissing,
bool showWarning)
const
189 if (name_parameter_3 ==
"") {
191 _output.
parameterReading({name_parameter_1, name_parameter_2}, var, defaultAvailable, exitIfMissing, showWarning);
197 if (!(*
this)[name_parameter_1][name_parameter_2][name_parameter_3].
read<ParameterType>(var,
false)) {
198 _output.
parameterReading({name_parameter_1, name_parameter_2, name_parameter_3}, var, defaultAvailable, exitIfMissing, showWarning);
207template<
typename ParameterType>
209 std::string name_parameter_2, std::string name_parameter_3, std::string name_parameter_4,
210 ParameterType& var,
bool defaultAvailable,
bool exitIfMissing,
bool showWarning)
const
214 if (name_parameter_3 ==
"") {
216 _output.
parameterReading({name_parameter_1, name_parameter_2}, var, defaultAvailable, exitIfMissing, showWarning);
221 else if(name_parameter_4 ==
""){
222 if (!(*
this)[name_parameter_1][name_parameter_2][name_parameter_3].
read<ParameterType>(var,
false)) {
223 _output.
parameterReading({name_parameter_1, name_parameter_2, name_parameter_3}, var, defaultAvailable, exitIfMissing, showWarning);
229 if (!(*
this)[name_parameter_1][name_parameter_2][name_parameter_3][name_parameter_4].
read<ParameterType>(var,
false)) {
231 var, defaultAvailable, exitIfMissing, showWarning);
242XMLreader::XMLreader()
244 _name =
"XML node not found";
249XMLreader::XMLreader( tinyxml2::XMLNode* pParent,
OutputChannel outputChannel) : _output(outputChannel)
251 _outputChannel = outputChannel;
254 mainProcessorIni(pParent);
258 : _output(outputChannel), _outputChannel(outputChannel), _warningsOn(true)
260 tinyxml2::XMLDocument doc(
true, tinyxml2::COLLAPSE_WHITESPACE);
261 bool loadOK = (doc.LoadFile(fName.c_str()) == tinyxml2::XML_SUCCESS);
263 mainProcessorIni(&doc);
268 for (
unsigned int iNode=0; iNode<
_children.size(); ++iNode) {
273void XMLreader::mainProcessorIni(tinyxml2::XMLNode* pParent)
276 assert(pParent !=
nullptr);
278 tinyxml2::XMLElement* element = pParent->ToElement();
279 if (element ==
nullptr && pParent->ToDocument() ==
nullptr) {
284 if (pParent->ToDocument() !=
nullptr) {
286 element = pParent->FirstChildElement();
287 if (element ==
nullptr)
return;
290 _name = element->Name();
293 const tinyxml2::XMLAttribute* attr = element->FirstAttribute();
294 while (attr !=
nullptr) {
301 for (tinyxml2::XMLNode* pChild = element->FirstChild(); pChild !=
nullptr; pChild = pChild->NextSibling()) {
302 if (pChild->ToElement()) {
305 else if (pChild->ToText()) {
306 _text = pChild->ToText()->Value();
313 for (
unsigned int iNode=0; iNode<
_children.size(); ++iNode) {
344 _warningsOn = warnings;
345 for (
unsigned int iNode=0; iNode<
_children.size(); ++iNode) {
346 _children[iNode]->setWarningsOn(warnings);
354 std::stringstream valueStr(_text);
358 std::transform(word.begin(), word.end(), word.begin(), ::tolower);
359 if (!word.compare(
"true") || (word==
"1")) {
363 else if (!word.compare(
"false") || (word==
"0")) {
369 std::stringstream ss;
370 ss << ( value ?
"true" :
"false" );
381 std::stringstream valueStr(_text);
383 if (!(valueStr >> tmp)) {
384 std::stringstream ss;
395bool XMLreader::read<std::size_t>(std::size_t& value,
bool verboseOn,
bool exitIfMissing)
const
397 std::stringstream valueStr(_text);
398 std::size_t tmp = std::size_t();
399 if (!(valueStr >> tmp)) {
400 std::stringstream ss;
402 _output.printWarning(_name,
"std::size_t", ss.str(), verboseOn, exitIfMissing);
413 std::stringstream valueStr(_text);
414 double tmp = double();
415 if (!(valueStr >> tmp)) {
427 std::stringstream valueStr(_text);
429 if (!(valueStr >> tmp)) {
430 _output.
printWarning(_name,
"long double", std::to_string(value), verboseOn, exitIfMissing);
433 value = std::stold(tmp);
440 std::stringstream valueStr(_text);
442 if (!(valueStr >> tmp)) {
443 std::stringstream ss;
453bool XMLreader::read<std::string>(std::string& entry,
bool verboseOn,
bool exitIfMissing)
const
455 if (_name ==
"XML node not found") {
458 std::stringstream valueStr(_text);
459 std::string tmp = std::string();
460 if (!(valueStr >> tmp)) {
461 std::stringstream ss;
463 _output.printWarning(_name,
"string", ss.str(), verboseOn, exitIfMissing);
473 std::map<std::string, std::string>::const_iterator it =
_attributes.find(aName);
475 return "Attribute not found.";
484 std::vector<S> values(nData, S());
485 std::stringstream extstr(reader.
getAttribute(attrName));
486 for (
auto& valueI: values) {
class for marking output with some text
void loadFile(bool loadOK, std::string fName) const
void parameterReading(std::vector< std::string > parameters, ParameterType &var, bool defaultAvailable, bool exitIfMissing, bool showWarning) const
void printWarning(std::string name, std::string typeName, std::string value, bool verboseOn, bool exitIfMissing) const
print warning if verbose mode is on and exit, if exItMissing is true
void readValue(bool warningsOn, std::string name, std::string fName) const
std::string getAttribute(const std::string &aName) const
std::vector< XMLreader * > _children
friend class XMLreaderOutput
std::vector< XMLreader * >::const_iterator begin() const
Returns an iterator.begin() of the child XMLreader This means an iterator to the next level on an XML...
std::string getName() const
return the name of the element
T get(bool verboseOn=true, bool exitIfMissing=false) const
XMLreaderOutput _output
handling all the output for the XMLreader
bool readOrWarn(std::string name_parameter_1, std::string name_parameter_2, std::string name_parameter_3, ParameterType &var, bool defaultAvailable=true, bool exitIfMissing=false, bool showWarning=true) const
This wrapper function reads the given parameter from the "type_parameter" and "name_parameter_1" or "...
void setWarningsOn(bool warnings) const
switch warnings on/off
std::vector< XMLreader * >::const_iterator end() const
Returns an iterator.end() of the child XMLreader This means an iterator to the next level on an XML t...
bool read(T &value, bool verboseOn=true, bool exitIfMissing=false) const
Read a value from the xml file.
XMLreader const & operator[](std::string name) const
XMLreader(tinyxml2::XMLNode *pParent, OutputChannel outputChannel=OutputChannel::ERRCHANNEL)
Constructs a new XMLreader from another XMLreader.
std::map< std::string, std::string > _attributes
std::string getText() const
return the text of the element
Definition of a description of a algoritmic differentiation data type using the forward method.
Top level namespace for all of OpenLB.
std::vector< S > getDataFromTag(const XMLreader &reader, std::string attrName, int nData)
Helper Function to retrieve nData-dimensional std::vector of type S from space separated tag.