E-MailRelay
genvironment.cpp
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.cpp
19///
20
21#include "gdef.h"
22#include "genvironment.h"
23#include "gstr.h"
24#include <algorithm>
25#include <stdexcept>
26
28{
29 setup() ;
30}
31
32#ifndef G_LIB_SMALL
33G::Environment::Environment( const std::map<std::string,std::string> & map ) :
34 m_map(map)
35{
36 setup() ;
37}
38#endif
39
41 m_map(other.m_map)
42{
43 setup() ;
44}
45
47 m_map(std::move(other.m_map)) ,
48 m_list(std::move(other.m_list)) ,
49 m_pointers(std::move(other.m_pointers)) ,
50 m_block(std::move(other.m_block))
51{
52}
53
54void G::Environment::swap( Environment & other ) noexcept
55{
56 m_map.swap( other.m_map ) ;
57 m_list.swap( other.m_list ) ;
58 m_pointers.swap( other.m_pointers ) ;
59 std::swap( m_block , other.m_block ) ;
60}
61
62#ifndef G_LIB_SMALL
64{
65 return
66 m_map.size() == m_list.size() &&
67 (m_list.size()+1U) == m_pointers.size() &&
68 ( m_list.empty() || m_pointers.at(0U) == m_list.at(0U).c_str() ) ;
69}
70#endif
71
73{
74 Environment(other).swap( *this ) ;
75 return *this ;
76}
77
78#ifndef G_LIB_SMALL
80{
81 Environment(std::move(other)).swap( *this ) ;
82 return *this ;
83}
84#endif
85
86void G::Environment::setup()
87{
88 setList() ;
89 setPointers() ;
90 setBlock() ;
91}
92
93void G::Environment::setList()
94{
95 m_list.clear() ;
96 m_list.reserve( m_map.size() ) ;
97 StringArray keys = Str::keys( m_map ) ;
98 std::sort( keys.begin() , keys.end() ) ;
99 for( const auto & key : keys )
100 {
101 m_list.push_back( key + "=" + (*m_map.find(key)).second ) ;
102 }
103}
104
105void G::Environment::setPointers()
106{
107 m_pointers.clear() ;
108 m_pointers.reserve( m_list.size() + 1U ) ;
109 for( const auto & s : m_list )
110 m_pointers.push_back( const_cast<char*>(s.c_str()) ) ;
111 m_pointers.push_back( nullptr ) ;
112}
113
114void G::Environment::setBlock()
115{
116 std::size_t n = 0U ;
117 for( auto & s : m_list )
118 n += (s.size()+1U) ;
119 m_block.reserve( n + 1U ) ;
120 for( auto & s : m_list )
121 {
122 m_block.append( s ) ;
123 m_block.append( 1U , '\0' ) ;
124 }
125 m_block.append( 1U , '\0' ) ;
126}
127
128void G::Environment::add( const std::string & name , const std::string & value )
129{
130 if( name.find('=') != std::string::npos )
131 throw Error( name ) ;
132 m_map.insert( std::make_pair(name,value) ) ;
133 setup() ;
134}
135
136#ifndef G_LIB_SMALL
137void G::Environment::set( const std::string & name , const std::string & value )
138{
139 m_map[name] = value ;
140 setup() ;
141}
142#endif
143
144char ** G::Environment::v() const noexcept
145{
146 return const_cast<char**>(&m_pointers[0]) ;
147}
148
149#ifndef G_LIB_SMALL
150const char * G::Environment::ptr() const noexcept
151{
152 return m_block.data() ;
153}
154#endif
155
156bool G::Environment::contains( const std::string & name ) const
157{
158 return m_map.find(name) != m_map.end() ;
159}
160
161#ifndef G_LIB_SMALL
162std::string G::Environment::value( const std::string & name , const std::string & default_ ) const
163{
164 return contains(name) ? (*m_map.find(name)).second : default_ ;
165}
166#endif
167
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.
Environment(const std::map< std::string, std::string > &)
Constructor from a map.
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.
Environment & operator=(const Environment &)
Assigment operator.
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.
static StringArray keys(const StringMap &string_map)
Extracts the keys from a map of strings.
Definition: gstr.cpp:1259
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstringarray.h:30