0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-09 21:48:55 +02:00

ircd::util: Add environment iteration convenience tools.

This commit is contained in:
Jason Volk 2023-02-06 21:06:55 -08:00
parent bd960a206b
commit 0b9bb185de
3 changed files with 42 additions and 1 deletions

View file

@ -14,5 +14,14 @@
namespace ircd {
inline namespace util
{
using env_closure = closure_bool<std::function, string_view, string_view>;
// Iterate environment variables starting with prefix
bool for_each_env(const string_view &, const env_closure &);
// Iterate all environment variables
bool for_each_env(const env_closure &);
// Get one environment variable
string_view getenv(const string_view &);
}}

View file

@ -38,7 +38,6 @@ namespace ircd
#include "reentrance.h"
#include "enum.h"
#include "custom_ptr.h"
#include "env.h"
#include "va_rtti.h"
#include "unique_iterator.h"
#include "scope_count.h"
@ -61,6 +60,7 @@ namespace ircd
#include "pretty.h"
#include "hash.h"
#include "closure.h"
#include "env.h"
#include "test.h"
#include "boolean.h"
#include "maybe.h"

View file

@ -52,6 +52,38 @@ ircd::util::getenv(const string_view &key)
return var;
}
bool
ircd::util::for_each_env(const string_view &prefix,
const env_closure &closure)
{
return for_each_env([&prefix, &closure]
(const auto &key, const auto &val)
{
if(!startswith(key, prefix))
return true;
return closure(key, val);
});
}
bool
ircd::util::for_each_env(const env_closure &closure)
{
assert(environ);
for(char **env(environ); *env; ++env)
{
const auto &[key, val]
{
split(*env, '=')
};
if(!closure(key, val))
return false;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////
//
// util/pretty.h