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::getBool() and setBool() method set [networkupstools#2294]

Signed-off-by: Jim Klimov <[email protected]>
  • Loading branch information
jimklimov committed Apr 25, 2024
1 parent 3b0c514 commit ed7ff59
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
12 changes: 12 additions & 0 deletions common/nutconf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,18 @@ void GenericConfiguration::setStr(
}


bool GenericConfiguration::getBool(
const std::string & section,
const std::string & entry,
bool val) const
{
std::string s = getStr(section, entry);
if (s.empty())
return val;
return str2bool(s);
}


bool GenericConfiguration::getFlag(
const std::string & section,
const std::string & entry) const
Expand Down
110 changes: 110 additions & 0 deletions include/nutconf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,116 @@ class GenericConfiguration : public BaseConfiguration, public Serialisable
setFlag("", entry);
}

/**
* \brief Configuration boolean option getter
*
* Value depends on original string representation of a setting.
*
* \param section Section name
* \param entry Entry name
* \param val Default value
*
* \return Configuration parameter as boolean (or the default if not defined)
*/
bool getBool(
const std::string & section,
const std::string & entry,
bool val = false) const;

/**
* \brief Configuration boolean option getter
*
* Value depends on original string representation of a setting.
*
* \param section Section name
* \param entry Entry name
* \param val Default value
*
* \return Configuration parameter as boolean (or the default if not defined)
*/
// Avoid error: implicit conversion turns string literal
// into bool: 'const char[7]' to 'bool'
bool getBool(
const std::string & section,
const char * entry,
bool val = false) const
{
return getBool(section, std::string(entry), val);
}

/**
* \brief Global scope configuration boolean option getter
*
* Value depends on original string representation of a setting.
*
* \param entry Entry name
* \param val Default value
*
* \return Configuration parameter as boolean (or the default if not defined)
*/
inline bool getBool(const std::string & entry, bool val = false) const
{
return getBool("", entry, val);
}

/**
* \brief Configuration boolean option setter
*
* \param section Section name
* \param entry Entry name
* \param val Default value
*/
inline void setBool(
const std::string & section,
const std::string & entry,
bool val = true)
{
setStr(section, entry, bool2str(val));
}

/**
* \brief Global scope configuration boolean option setter
*
* \param entry Entry name
* \param val Default value
*/
inline void setBool(
const std::string & entry,
bool val = true)
{
setBool("", entry, val);
}

/**
* \brief Configuration boolean option setter from a string
*
* \param section Section name
* \param entry Entry name
* \param val Default value
*/
inline void setBool(
const std::string & section,
const std::string & entry,
const std::string & val = "true")
{
// Normalize:
bool b = str2bool(val);
setStr(section, entry, bool2str(b));
}

/**
* \brief Global scope configuration boolean option setter from a string
*
* \param entry Entry name
* \param val Default value
*/
inline void setBool(
const std::string & entry,
const std::string & val = "true")
{
setBool("", entry, val);
}

/**
* \brief Configuration number getter
*
Expand Down

0 comments on commit ed7ff59

Please sign in to comment.