Skip to content

Commit

Permalink
Add some basic checks for xml parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
HomesGH authored Oct 26, 2023
1 parent 43c1a2f commit 0f8092e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 41 deletions.
53 changes: 16 additions & 37 deletions src/utils/xmlfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,59 +532,39 @@ template<typename T> bool XMLfile::Node::getValue(T& value) const
{
if(m_xmlnode)
{
//value=T(m_xmlnode->value());
std::stringstream ss(m_xmlnode->value());
ss>>value;
ss >> value;
// Check if input has correct sign
if (std::is_unsigned_v<T>) {
if (ss.str().at(0) == '-') {
std::cerr << "ERROR parsing \"" << ss.str() << "\" to data type " << typeid(T).name() << " from tag \"<" << name() << ">\" in xml file" << std::endl;
std::cerr << "The tag contains a negative value but an unsigned value was expected." << std::endl;
Simulation::exit(1);
}
}
// Check if the entire string was consumed
if (!ss.eof() || ss.fail()) {
std::cerr << "ERROR parsing all chars of \"" << ss.str() << "\" from tag \"<" << name() << ">\" in xml file" << std::endl;
std::cerr << "This might be the result of using a float while an integer is expected." << std::endl;
Simulation::exit(1);
}
return true;
}
else
return false;
}


template<> bool XMLfile::Node::getValue<std::string>(std::string& value) const
{
if(m_xmlnode)
{
value=std::string(m_xmlnode->value());
return true;
return true;
}
else
return false;
}

template<> bool XMLfile::Node::getValue<int>(int& value) const
{
std::string v;
bool found=getValue(v);
if(found) value=atoi(v.c_str());
return found;
}

template<> bool XMLfile::Node::getValue<long>(long& value) const
{
std::string v;
bool found=getValue(v);
if(found) value=atol(v.c_str());
return found;
}

template<> bool XMLfile::Node::getValue<float>(float& value) const
{
std::string v;
bool found=getValue(v);
if(found) value=static_cast<float>(atof(v.c_str()));
return found;
}

template<> bool XMLfile::Node::getValue<double>(double& value) const
{
std::string v;
bool found=getValue(v);
if(found) value=atof(v.c_str());
return found;
}

template<> bool XMLfile::Node::getValue<bool>(bool& value) const
{
std::string v;
Expand All @@ -607,7 +587,6 @@ template<> bool XMLfile::Node::getValue<bool>(bool& value) const
return found;
}


std::string XMLfile::Node::value_string(std::string defaultvalue) const
{
std::string value(defaultvalue);
Expand Down
4 changes: 0 additions & 4 deletions src/utils/xmlfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -687,10 +687,6 @@ inline std::ostream& operator << (std::ostream& ostrm, const XMLfile& xmlfile)
/*
// explicit instantiation
template bool XMLfile::Node::getValue<std::string>(std::string& value)const;
template bool XMLfile::Node::getValue<int>(int& value)const;
template bool XMLfile::Node::getValue<long>(long& value)const;
template bool XMLfile::Node::getValue<float>(float& value)const;
template bool XMLfile::Node::getValue<double>(double& value)const;
template bool XMLfile::Node::getValue<bool>(bool& value)const;
*/

Expand Down

0 comments on commit 0f8092e

Please sign in to comment.