21#ifndef G_ENVIRONMENT_H
22#define G_ENVIRONMENT_H
44 G_EXCEPTION( Error ,
tx(
"invalid environment variable") )
46 static std::string
get(
const std::string & name ,
const std::string & default_ ) ;
52 static void put(
const std::string & name ,
const std::string &
value ) ;
62 explicit Environment(
const std::map<std::string,std::string> & ) ;
65 bool contains(
const std::string & name )
const ;
68 std::string
value(
const std::string & name ,
const std::string & default_ = {} )
const ;
71 bool add( std::string_view key , std::string_view
value ) ;
74 std::string
block()
const ;
79 std::wstring
block( std::wstring (*)(std::string_view) )
const ;
84 bool empty()
const noexcept ;
87 static std::vector<char*>
array(
const std::string &
block ) ;
91 static std::vector<char*>
array( std::string && envblock ) = delete ;
94 std::map<std::string,std::string>
map()
const ;
98 using Map = std::map<std::string,std::string> ;
99 using List = std::vector<std::string> ;
102 static void sanitise( Map & ) ;
117 return m_map.empty() ;
Holds a set of environment variables and also provides static methods to wrap getenv() and putenv().
std::map< std::string, std::string > map() const
Returns the environment as a map.
std::string value(const std::string &name, const std::string &default_={}) const
Returns the value of the given variable in this set.
static std::vector< char * > array(std::string &&envblock)=delete
Deleted overload.
static void put(const std::string &name, const std::string &value)
Sets the environment variable value.
Environment(const std::map< std::string, std::string > &)
Constructor from a map.
static std::string get(const std::string &name, const std::string &default_)
Returns the environment variable value or the given default.
bool empty() const noexcept
Returns true if empty.
std::string block() const
Returns a contiguous block of memory containing the null-terminated strings with an extra zero byte a...
static Environment inherit()
Returns an empty() environment, as if default constructed.
static G::Path getPath(const std::string &name, const G::Path &={})
Returns the environment variable value as a G::Path object.
static Environment minimal(bool sbin=false)
Returns a minimal, safe set of environment variables.
bool contains(const std::string &name) const
Returns true if the given variable is in this set.
static std::vector< char * > array(const std::string &block)
Returns a pointer array pointing into the given block(), with const-casts.
bool add(std::string_view key, std::string_view value)
Adds an environment variable. Returns false if invalid.
A Path object represents a file system path.
constexpr const char * tx(const char *p) noexcept
A briefer alternative to G::gettext_noop().