Skip to content

Commit

Permalink
include/nutconf.hpp, common/nutconf.cpp: introduce GenericConfigurati…
Browse files Browse the repository at this point in the history
…on::getFlag() and setFlag() methods [networkupstools#2294]

Signed-off-by: Jim Klimov <[email protected]>
  • Loading branch information
jimklimov committed Apr 25, 2024
1 parent e461d14 commit 3b0c514
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
26 changes: 26 additions & 0 deletions common/nutconf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
57 changes: 57 additions & 0 deletions include/nutconf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit 3b0c514

Please sign in to comment.