From 3b0c514494dd538c127ccb66a3a67b1beb878667 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Thu, 25 Apr 2024 11:26:34 +0200 Subject: [PATCH] include/nutconf.hpp, common/nutconf.cpp: introduce GenericConfiguration::getFlag() and setFlag() methods [#2294] Signed-off-by: Jim Klimov --- common/nutconf.cpp | 26 +++++++++++++++++++++ include/nutconf.hpp | 57 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/common/nutconf.cpp b/common/nutconf.cpp index 9c18ec5058..eee78b9bca 100644 --- a/common/nutconf.cpp +++ b/common/nutconf.cpp @@ -1025,6 +1025,32 @@ void GenericConfiguration::setStr( } +bool GenericConfiguration::getFlag( + const std::string & section, + const std::string & entry) const +{ + ConfigParamList params; + + if (!get(section, entry, params)) + return false; + + // Flag - if exists then "true" + return true; +} + + +void GenericConfiguration::setFlag( + const std::string & section, + const std::string & entry) +{ + ConfigParamList param; + + param.push_back("true"); + + set(section, entry, param); +} + + long long int GenericConfiguration::getInt( const std::string & section, const std::string & entry, diff --git a/include/nutconf.hpp b/include/nutconf.hpp index 30db399f98..f878b5eda8 100644 --- a/include/nutconf.hpp +++ b/include/nutconf.hpp @@ -907,6 +907,63 @@ class GenericConfiguration : public BaseConfiguration, public Serialisable setStr("", entry, value); } + /** + * \brief Configuration flag getter + * + * False is returned if the section or entry doesn't exist. + * If a flag exists in configuration (any value is ignored), + * it is effectively True. + * + * \param section Section name + * \param entry Entry name + * + * \return Configuration parameter as boolean + */ + bool getFlag( + const std::string & section, + const std::string & entry) const; + + /** + * \brief Global scope configuration flag getter + * + * False is returned if the entry doesn't exist. + * If a flag exists in configuration (any value is ignored), + * it is effectively True. + * + * \param entry Entry name + * + * \return Configuration parameter as boolean + */ + inline bool getFlag(const std::string & entry) const + { + return getFlag("", entry); + } + + /** + * \brief Configuration flag setter (mentioned == true) + * + * Note: to unset a flag, just use remove() method. + * + * \param section Section name + * \param entry Entry name + */ + void setFlag( + const std::string & section, + const std::string & entry); + + /** + * \brief Global scope configuration flag setter (mentioned == true) + * + * Note: to unset a flag, just use remove() method. + * + * \param entry Entry name + */ + inline void setFlag( + const std::string & entry) + { + setFlag("", entry); + } + /** * \brief Configuration number getter *