From 718c237063b39ff6e445547de7211df78533a324 Mon Sep 17 00:00:00 2001 From: Bart Trzynadlowski Date: Wed, 10 Aug 2016 03:27:01 +0000 Subject: [PATCH] Added ValueAsBool(), ValueAsBoolWithDefault(), and ValueAsUnsignedWithDefault() --- Src/Util/NewConfig.cpp | 26 +++++++++++++++++++++++++- Src/Util/NewConfig.h | 3 +++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/Src/Util/NewConfig.cpp b/Src/Util/NewConfig.cpp index f3f8324..5e18264 100644 --- a/Src/Util/NewConfig.cpp +++ b/Src/Util/NewConfig.cpp @@ -80,13 +80,37 @@ namespace Util { Node Node::s_empty_node; + bool Node::ValueAsBool() const + { + const char *value = m_value.c_str(); + if (!value || !stricmp(value, "false") || !stricmp(value, "off") || !stricmp(value, "no")) + return false; + else if (!stricmp(value, "true") || !stricmp(value, "on") || !stricmp(value, "yes")) + return true; + return bool(ValueAsUnsigned()); + } + + bool Node::ValueAsBoolWithDefault(bool default_value) const + { + if (this == &s_empty_node) + return default_value; + return ValueAsBool(); + } + 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); } - + + uint64_t Node::ValueAsUnsignedWithDefault(uint64_t default_value) const + { + if (this == &s_empty_node) + return default_value; + return ValueAsUnsigned(); + } + const Node &Node::operator[](const std::string &path) const { const Node *e = this; diff --git a/Src/Util/NewConfig.h b/Src/Util/NewConfig.h index fa31c23..0453dee 100644 --- a/Src/Util/NewConfig.h +++ b/Src/Util/NewConfig.h @@ -161,7 +161,10 @@ namespace Util return m_value; } + bool ValueAsBool() const; + bool ValueAsBoolWithDefault(bool default_value) const; uint64_t ValueAsUnsigned() const; + uint64_t ValueAsUnsignedWithDefault(uint64_t default_value) const; inline void SetValue(const std::string &value) {