diff options
author | 2021-01-01 12:18:29 -0500 | |
---|---|---|
committer | 2021-01-01 12:22:17 -0500 | |
commit | aa29519528cb3dbdbfac56819bea670ed8c56c5d (patch) | |
tree | bdaff6e127ed377a4fc84d3e8ee1b7a99f503d0b /src/lib/util/xmlfile.cpp | |
parent | 21fd9835451a5a7d7655964bfb7adb1ba9b8540f (diff) |
Further additions of std::string_view
- corefile.cpp, fileio.cpp: Change puts to take a std::string_view parameter
- rendlay.cpp: Use std::string_view instead of bare pointers in various functions
- vecstream.h: Add std::string_view conversion operator to obtain output buffer without needing to make it a C string with explicit null termination
- xmlfile.cpp: Add get_attribute_string_ptr method that distinguishes between empty strings and absent attributes without falling back to C strings
Diffstat (limited to 'src/lib/util/xmlfile.cpp')
-rw-r--r-- | src/lib/util/xmlfile.cpp | 34 |
1 files changed, 25 insertions, 9 deletions
diff --git a/src/lib/util/xmlfile.cpp b/src/lib/util/xmlfile.cpp index 2a1bedc7c2c..cfa6bbd8ff5 100644 --- a/src/lib/util/xmlfile.cpp +++ b/src/lib/util/xmlfile.cpp @@ -603,6 +603,19 @@ data_node::attribute_node const *data_node::get_attribute(const char *attribute) //------------------------------------------------- +// get_attribute_string_ptr - get a pointer to +// the string value of the specified attribute; +// if not found, return = nullptr +//------------------------------------------------- + +std::string const *data_node::get_attribute_string_ptr(const char *attribute) const +{ + attribute_node const *attr = get_attribute(attribute); + return attr ? &attr->value : nullptr; +} + + +//------------------------------------------------- // get_attribute_string - get the string // value of the specified attribute; if not // found, return = the provided default @@ -623,9 +636,10 @@ const char *data_node::get_attribute_string(const char *attribute, const char *d long long data_node::get_attribute_int(const char *attribute, long long defvalue) const { - char const *const string = get_attribute_string(attribute, nullptr); - if (!string) + attribute_node const *attr = get_attribute(attribute); + if (!attr) return defvalue; + std::string const &string = attr->value; std::istringstream stream; stream.imbue(std::locale::classic()); @@ -666,14 +680,16 @@ long long data_node::get_attribute_int(const char *attribute, long long defvalue data_node::int_format data_node::get_attribute_int_format(const char *attribute) const { - char const *const string = get_attribute_string(attribute, nullptr); - if (!string) + attribute_node const *attr = get_attribute(attribute); + if (!attr) return int_format::DECIMAL; - else if (string[0] == '$') + std::string const &string = attr->value; + + if (string[0] == '$') return int_format::HEX_DOLLAR; else if (string[0] == '0' && string[1] == 'x') return int_format::HEX_C; - if (string[0] == '#') + else if (string[0] == '#') return int_format::DECIMAL_HASH; else return int_format::DECIMAL; @@ -688,11 +704,11 @@ data_node::int_format data_node::get_attribute_int_format(const char *attribute) float data_node::get_attribute_float(const char *attribute, float defvalue) const { - char const *const string = get_attribute_string(attribute, nullptr); - if (!string) + attribute_node const *attr = get_attribute(attribute); + if (!attr) return defvalue; - std::istringstream stream(string); + std::istringstream stream(attr->value); stream.imbue(std::locale::classic()); float result; return (stream >> result) ? result : defvalue; |