Expand reader getAttribute calls to accept a default value

This commit is contained in:
bgbsww
2024-07-17 21:07:02 -04:00
committed by Chris Hennes
parent df8315d747
commit 3efba18ad4
2 changed files with 32 additions and 39 deletions

View File

@@ -109,52 +109,35 @@ unsigned int Base::XMLReader::getAttributeCount() const
return static_cast<unsigned int>(AttrMap.size());
}
long Base::XMLReader::getAttributeAsInteger(const char* AttrName) const
long Base::XMLReader::getAttributeAsInteger(const char* AttrName, const char* defaultValue) const
{
AttrMapType::const_iterator pos = AttrMap.find(AttrName);
if (pos != AttrMap.end()) {
return atol(pos->second.c_str());
}
// wrong name, use hasAttribute if not sure!
std::ostringstream msg;
msg << "XML Attribute: \"" << AttrName << "\" not found";
throw Base::XMLAttributeError(msg.str());
const int base = 10;
return strtol(getAttribute(AttrName, defaultValue), nullptr, base);
}
unsigned long Base::XMLReader::getAttributeAsUnsigned(const char* AttrName) const
unsigned long Base::XMLReader::getAttributeAsUnsigned(const char* AttrName,
const char* defaultValue) const
{
AttrMapType::const_iterator pos = AttrMap.find(AttrName);
if (pos != AttrMap.end()) {
return strtoul(pos->second.c_str(), nullptr, 10);
}
// wrong name, use hasAttribute if not sure!
std::ostringstream msg;
msg << "XML Attribute: \"" << AttrName << "\" not found";
throw Base::XMLAttributeError(msg.str());
const int base = 10;
return strtoul(getAttribute(AttrName, defaultValue), nullptr, base);
}
double Base::XMLReader::getAttributeAsFloat(const char* AttrName) const
double Base::XMLReader::getAttributeAsFloat(const char* AttrName, const char* defaultValue) const
{
AttrMapType::const_iterator pos = AttrMap.find(AttrName);
if (pos != AttrMap.end()) {
return atof(pos->second.c_str());
}
// wrong name, use hasAttribute if not sure!
std::ostringstream msg;
msg << "XML Attribute: \"" << AttrName << "\" not found";
throw Base::XMLAttributeError(msg.str());
return strtod(getAttribute(AttrName, defaultValue), nullptr);
}
const char* Base::XMLReader::getAttribute(const char* AttrName) const
const char* Base::XMLReader::getAttribute(const char* AttrName, // NOLINT
const char* defaultValue) const // NOLINT
{
AttrMapType::const_iterator pos = AttrMap.find(AttrName);
auto pos = AttrMap.find(AttrName);
if (pos != AttrMap.end()) {
return pos->second.c_str();
}
if (defaultValue) {
return defaultValue;
}
// wrong name, use hasAttribute if not sure!
std::ostringstream msg;
msg << "XML Attribute: \"" << AttrName << "\" not found";

View File

@@ -217,13 +217,23 @@ public:
unsigned int getAttributeCount() const;
/// check if the read element has a special attribute
bool hasAttribute(const char* AttrName) const;
/// return the named attribute as an integer (does type checking)
long getAttributeAsInteger(const char* AttrName) const;
unsigned long getAttributeAsUnsigned(const char* AttrName) const;
/// return the named attribute as a double floating point (does type checking)
double getAttributeAsFloat(const char* AttrName) const;
/// return the named attribute as a double floating point (does type checking)
const char* getAttribute(const char* AttrName) const;
/// return the named attribute as an integer (does type checking); if missing return
/// defaultValue
long getAttributeAsInteger(const char* AttrName, const char* defaultValue = nullptr) const;
/// return the named attribute as unsigned integer (does type checking); if missing return
/// defaultValue
unsigned long getAttributeAsUnsigned(const char* AttrName,
const char* defaultValue = nullptr) const;
/// return the named attribute as a double floating point (does type checking); if missing
/// return defaultValue
double getAttributeAsFloat(const char* AttrName, const char* defaultValue = nullptr) const;
/// return the named attribute as a double floating point (does type checking); if missing
/// return defaultValue
const char* getAttribute(const char* AttrName, const char* defaultValue = nullptr) const;
//@}
/** @name additional file reading */