E-MailRelay
genvironment.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2023 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file genvironment.h
19///
20
21#ifndef G_ENVIRONMENT_H
22#define G_ENVIRONMENT_H
23
24#include "gdef.h"
25#include "gexception.h"
26#include <string>
27#include <vector>
28#include <map>
29
30namespace G
31{
32 class Environment ;
33}
34
35//| \class G::Environment
36/// Holds a set of environment variables and also provides static methods
37/// to wrap getenv() and putenv().
38///
40{
41public:
42 G_EXCEPTION( Error , tx("invalid environment variable") ) ;
43
44 static std::string get( const std::string & name , const std::string & default_ ) ;
45 ///< Returns the environment variable value or the given default.
46
47 static void put( const std::string & name , const std::string & value ) ;
48 ///< Sets the environment variable value.
49
50 static Environment minimal( bool sbin = false ) ;
51 ///< Returns a minimal, safe set of environment variables.
52
53 static Environment inherit() ;
54 ///< Returns an empty() environment, as if default constructed.
55 ///< This is syntactic sugar for the G::NewProcess interface.
56
57 explicit Environment( const std::map<std::string,std::string> & ) ;
58 ///< Constructor from a map.
59
60 ~Environment() = default ;
61 ///< Destructor.
62
63 void add( const std::string & name , const std::string & value ) ;
64 ///< Adds a variable to this set. Does nothing if already
65 ///< present.
66
67 bool contains( const std::string & name ) const ;
68 ///< Returns true if the given variable is in this set.
69
70 std::string value( const std::string & name , const std::string & default_ = {} ) const ;
71 ///< Returns the value of the given variable in this set.
72
73 void set( const std::string & name , const std::string & value ) ;
74 ///< Inserts or updates a variable in this set.
75
76 const char * ptr() const noexcept ;
77 ///< Returns a contiguous block of memory containing the
78 ///< null-terminated strings with an extra zero byte
79 ///< at the end.
80
81 char ** v() const noexcept ;
82 ///< Returns a null-terminated array of pointers.
83
84 bool empty() const noexcept ;
85 ///< Returns true if empty.
86
87 Environment( const Environment & ) ;
88 ///< Copy constructor.
89
90 Environment( Environment && ) noexcept ;
91 ///< Move constructor.
92
93 Environment & operator=( const Environment & ) ;
94 ///< Assigment operator.
95
96 Environment & operator=( Environment && ) noexcept ;
97 ///< Move assigment operator.
98
99 bool valid() const ;
100 ///< Returns true if the class invariants are
101 ///< satisfied. Used in testing.
102
103private:
104 Environment() ;
105 void swap( Environment & ) noexcept ;
106
107private:
108 using Map = std::map<std::string,std::string> ;
109 using List = std::vector<std::string> ;
110 static char * stringdup( const std::string & ) ;
111 void setup() ;
112 void setList() ;
113 void setPointers() ;
114 void setBlock() ;
115
116private:
117 Map m_map ;
118 std::vector<std::string> m_list ;
119 std::vector<char*> m_pointers ;
120 std::string m_block ;
121} ;
122
123inline
124bool G::Environment::empty() const noexcept
125{
126 return m_map.empty() ;
127}
128
129inline
131{
132 return {} ;
133}
134
135#endif
Holds a set of environment variables and also provides static methods to wrap getenv() and putenv().
Definition: genvironment.h:40
std::string value(const std::string &name, const std::string &default_={}) const
Returns the value of the given variable in this set.
bool valid() const
Returns true if the class invariants are satisfied.
static void put(const std::string &name, const std::string &value)
Sets the environment variable value.
static std::string get(const std::string &name, const std::string &default_)
Returns the environment variable value or the given default.
~Environment()=default
Destructor.
bool empty() const noexcept
Returns true if empty.
Definition: genvironment.h:124
static Environment inherit()
Returns an empty() environment, as if default constructed.
Definition: genvironment.h:130
static Environment minimal(bool sbin=false)
Returns a minimal, safe set of environment variables.
void add(const std::string &name, const std::string &value)
Adds a variable to this set.
const char * ptr() const noexcept
Returns a contiguous block of memory containing the null-terminated strings with an extra zero byte a...
void set(const std::string &name, const std::string &value)
Inserts or updates a variable in this set.
bool contains(const std::string &name) const
Returns true if the given variable is in this set.
char ** v() const noexcept
Returns a null-terminated array of pointers.
Low-level classes.
Definition: garg.h:30
constexpr const char * tx(const char *p)
A briefer alternative to G::gettext_noop().
Definition: ggettext.h:84
STL namespace.