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

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


nut::BoolInt GenericConfiguration::getBoolInt(
const std::string & section,
const std::string & entry,
nut::BoolInt val) const
{
ConfigParamList params;

if (!get(section, entry, params))
return val;

if (params.empty())
return val;

// TBD: What if there are multiple values?
nut::BoolInt bi(params.front());

return bi;
}


bool GenericConfiguration::str2bool(const std::string & str)
{
if ("true" == str) return true;
Expand Down
70 changes: 69 additions & 1 deletion include/nutconf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1162,13 +1162,81 @@ class GenericConfiguration : public BaseConfiguration, public Serialisable
return static_cast<T>(number);
}

/**
* \brief Configuration mixed boolean/int 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 BoolInt type for original
* values which have a boolean or integer-numeric meaning
* (or the default if not defined)
*/
nut::BoolInt getBoolInt(
const std::string & section,
const std::string & entry,
nut::BoolInt val = false) const;

/**
* \brief Global scope configuration mixed boolean/int option getter
*
* Value depends on original string representation of a setting.
*
* \param entry Entry name
* \param val Default value
*
* \return Configuration parameter as BoolInt type for original
* values which have a boolean or integer-numeric meaning
* (or the default if not defined)
*/
inline nut::BoolInt getBoolInt(const std::string & entry, nut::BoolInt val = false) const
{
return getBoolInt("", entry, val);
}

/**
* \brief Configuration mixed boolean/int option setter
*
* Input value types are auto-converted through BoolInt
* type for sanity checks (e.g. throw exceptions for
* invalid string contents) and are stored as strings
* internally.
*
* \param section Section name
* \param entry Entry name
* \param val Default value
*/
inline void setBoolInt(
const std::string & section,
const std::string & entry,
nut::BoolInt val = true)
{
setStr(section, entry, val);
}

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

/**
* \brief Resolve string as Boolean value
*
* \param str String
*
* \retval true IFF the string expresses a known true value
* \retval false otherwise
* \retval false otherwise (no errors emitted for bogus inputs)
*/
static bool str2bool(const std::string & str);

Expand Down

0 comments on commit 880023f

Please sign in to comment.