Added Util::Config::Node::ValueAsUnsigned() with support for base 10 and 16 ('0x' prefix)

This commit is contained in:
Bart Trzynadlowski 2016-07-10 04:25:41 +00:00
parent 331d7042f2
commit 52d6eac7e5
2 changed files with 15 additions and 27 deletions

View file

@ -71,6 +71,7 @@
#include <algorithm> #include <algorithm>
#include <fstream> #include <fstream>
#include <queue> #include <queue>
#include <cstdlib>
#include <iostream> #include <iostream>
namespace Util namespace Util
@ -79,6 +80,13 @@ namespace Util
{ {
Node Node::s_empty_node; Node Node::s_empty_node;
uint64_t Node::ValueAsUnsigned() const
{
if (m_value.find("0x") == 0 || m_value.find("0X") == 0)
return strtoull(m_value.c_str() + 2, 0, 16);
return strtoull(m_value.c_str(), 0, 10);
}
const Node &Node::operator[](const std::string &path) const const Node &Node::operator[](const std::string &path) const
{ {
const Node *e = this; const Node *e = this;
@ -171,18 +179,6 @@ namespace Util
parent.m_children[node->m_key] = node; parent.m_children[node->m_key] = node;
} }
/*
void Node::Clear()
{
m_next_sibling.reset();
m_first_child.reset();
m_last_child.reset();
m_children.clear();
m_key.clear();
m_value.clear();
}
*/
Node::Node() Node::Node()
{ {
//std::cout << "Created " << "<null>" << " (" << this << ")" << std::endl; //std::cout << "Created " << "<null>" << " (" << this << ")" << std::endl;
@ -301,14 +297,12 @@ namespace Util
size_t idx_equals = line.find_first_of('='); size_t idx_equals = line.find_first_of('=');
if (idx_equals == std::string::npos) if (idx_equals == std::string::npos)
{ {
//std::cerr << filename << ':' << line_num << ": Syntax error. No '=' found." << std::endl;
ErrorLog("%s:%d: Syntax error. No '=' found.", filename.c_str(), line_num); ErrorLog("%s:%d: Syntax error. No '=' found.", filename.c_str(), line_num);
return; return;
} }
std::string lvalue(TrimWhiteSpace(std::string(line.begin(), line.begin() + idx_equals))); std::string lvalue(TrimWhiteSpace(std::string(line.begin(), line.begin() + idx_equals)));
if (lvalue.empty()) if (lvalue.empty())
{ {
//std::cerr << filename << ':' << line_num << ": Syntax error. Setting name missing before '='." << std::endl;
ErrorLog("%s:%d: Syntax error. Setting name missing before '='.", filename.c_str(), line_num); ErrorLog("%s:%d: Syntax error. Setting name missing before '='.", filename.c_str(), line_num);
return; return;
} }
@ -321,7 +315,6 @@ namespace Util
rvalue = std::string(rvalue.begin() + idx_first_quote + 1, rvalue.begin() + idx_last_quote); rvalue = std::string(rvalue.begin() + idx_first_quote + 1, rvalue.begin() + idx_last_quote);
if (std::count(rvalue.begin(), rvalue.end(), '\"') != 0) if (std::count(rvalue.begin(), rvalue.end(), '\"') != 0)
{ {
//std::cerr << filename << ':' << line_num << ": Warning: Extraneous quotes present on line." << std::endl;
ErrorLog("%s:%d: Warning: Extraneous quotes present on line.", filename.c_str(), line_num); ErrorLog("%s:%d: Warning: Extraneous quotes present on line.", filename.c_str(), line_num);
} }
// In INI files, we do not allow multiple settings with the same key. If // In INI files, we do not allow multiple settings with the same key. If
@ -412,12 +405,6 @@ namespace Util
{ {
// INI semantics: take care to only create a single setting per key // INI semantics: take care to only create a single setting per key
merged.Set(key, value); merged.Set(key, value);
/*
if (merged[key].Empty())
merged.Add(key, value);
else
merged.Get(key).SetValue(value);
*/
} }
} }
// Merge in settings from section y // Merge in settings from section y
@ -428,12 +415,6 @@ namespace Util
if (it->IsLeaf()) if (it->IsLeaf())
{ {
merged.Set(key, value); merged.Set(key, value);
/*
if (merged[key].Empty())
merged.Add(key, value);
else
merged.Get(key).SetValue(value);
*/
} }
} }
return merged_ptr; return merged_ptr;

View file

@ -161,6 +161,8 @@ namespace Util
return m_value; return m_value;
} }
uint64_t ValueAsUnsigned() const;
inline void SetValue(const std::string &value) inline void SetValue(const std::string &value)
{ {
if (this != &s_empty_node) // not allowed to modify empty node if (this != &s_empty_node) // not allowed to modify empty node
@ -172,6 +174,11 @@ namespace Util
return m_key.empty(); return m_key.empty();
} }
inline bool Exists() const
{
return !Empty();
}
inline bool IsLeaf() const inline bool IsLeaf() const
{ {
return m_children.empty(); return m_children.empty();