File: sbuild-environment.h

package info (click to toggle)
schroot 1.6.4-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,684 kB
  • sloc: cpp: 21,427; sh: 12,516; makefile: 829; ansic: 231; sed: 16
file content (326 lines) | stat: -rw-r--r-- 8,132 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* Copyright © 2005-2007  Roger Leigh <rleigh@debian.org>
 *
 * schroot is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * schroot is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 *********************************************************************/

#ifndef SBUILD_ENVIRONMENT_H
#define SBUILD_ENVIRONMENT_H

#include <sbuild/sbuild-log.h>
#include <sbuild/sbuild-parse-value.h>
#include <sbuild/sbuild-regex.h>

#include <map>
#include <string>
#include <sstream>

#include <boost/format.hpp>

namespace sbuild
{

  /**
   * Container of environment variables.
   */
  class environment : public std::map<std::string, std::string>
  {
  public:
    using std::map<std::string, std::string>::value_type;

    /// The constructor.
    environment ();

    /**
     * The constructor.
     *
     * @param environment the environment to set.
     */
    environment (char **environment);

    /// The destructor.
    ~environment ();

    /**
     * Set environment filter.  If the environment variable name
     * matches the regex when add() is called, addition will be .
     *
     * The default filter is to allow all strings.
     *
     * If the regex contains errors, an exception will be thrown.
     *
     * @param filter the filter regex.
     */
    void
    set_filter (regex const& filter);

    /**
     * Get environment filter.
     *
     * @returns the filter regex.
     */
    regex const&
    get_filter () const;

    /**
     * Add environment variables.  Any existing variables sharing the
     * name of a new value will be replaced.
     *
     * @param environment the environment variables to add.  This is a
     * null-terminated array of pointers to char.
     */
    void
    add (char **environment);

    /**
     * Add environment variables.  Any existing variables sharing the
     * name of a new value will be replaced.
     *
     * @param environment the environment variables to add.
     */
    void
    add (environment const& environment);

    /**
     * Add environment variable.  Any existing variable sharing the
     * name will be replaced.
     *
     * @param value the environment variable to add.
     */
    void
    add (value_type const& value);

    /**
     * Add environment variable.  Any existing variable sharing the
     * name will be replaced.
     *
     * @param name the environment variable name
     * @param value the environment variable value to add.
     */
    void
    add (std::string const& name,
	 std::string const& value)
    {
      add(std::make_pair(name, value));
    }

    /**
     * Add environment variable.  Any existing variable sharing the
     * name will be replaced.
     *
     * @param name the environment variable name
     * @param value the environment variable value to add.
     */
    template<typename T>
    void
    add (std::string const& name,
	 T const&           value)
    {
      std::ostringstream varstring;
      varstring.imbue(std::locale::classic());
      varstring << std::boolalpha << value;
      add(std::make_pair(name, varstring.str()));
    }

    /**
     * Add environment variable.  Any existing variable sharing the
     * name will be replaced.
     *
     * @param value the environment variable to add.  This is a string
     * in the form key=value.
     */
    void
    add (std::string const& value);

    /**
     * Remove environment variables.  Any variables sharing the names
     * of a specified value will be removed.
     *
     * @param environment the environment variables to remove.  This
     * is a null-terminated array of pointers to char.
     */
    void
    remove (char **environment);

    /**
     * Remove environment variables.  Any variables sharing the names
     * of a specified value will be removed.
     *
     * @param environment the environment variables to remove.
     */
    void
    remove (environment const& environment);

    /**
     * Remove environment variable.  Any variable sharing the name
     * of the specified value will be removed.
     *
     * @param value the environment variable to remove.
     */
    void
    remove (std::string const& value);

    /**
     * Remove environment variable.  Any variable sharing the name
     * of the specified value will be removed.
     *
     * @param value the environment variable to remove.
     */
    void
    remove (value_type const& value);

    /**
     * Get the value of an environment variable.
     *
     * @param name the name of the environment variable.
     * @param value the variable to store the value in on success.
     * @returns true on success, false if the variable does not exist,
     * or there is a parse error.
     */
    template <typename T>
    bool
    get (std::string const& name,
	 T&                 value) const
    {
      log_debug(DEBUG_INFO) << "Getting environment variable=" << name
			    << std::endl;
      const_iterator pos = find(name);
      if (pos != end())
	{
	  try
	    {
	      parse_value(pos->second, value);
	      return true;
	    }
	  catch (parse_value_error const& e)
	    {
	      log_warning() << boost::format("%1%: %2%\n")
		% name % e.what();
	      return false;
	    }
	}
      log_debug(DEBUG_NOTICE) << "name not found: " << name << std::endl;
      return false;
    }

    /**
     * Get the evironment variables as a string vector.  This form is
     * suitable for use as an envp argument with execve, for example.
     *
     * @returns a newly-allocated string vector.  This is allocated
     * with new, and should be freed with strv_delete().
     */
    char **
    get_strv () const;

    /**
     * Add variables to the environment.
     *
     * @param rhs the values to add.
     * @returns the modified environment.
     */
    template <typename T>
    environment&
    operator += (T const& rhs)
    {
      add(rhs);
      return *this;
    }

    /**
     * Remove variables from the environment.
     *
     * @param rhs the values to remove.
     * @returns the modified environment.
     */
    template <typename T>
    environment&
    operator -= (T const& rhs)
    {
      remove(rhs);
      return *this;
    }

    /**
     * Add variables to the environment.
     *
     * @param lhs the environment to add to.
     * @param rhs the values to add.
     * @returns the new environment.
     */
    template <typename T>
    friend environment
    operator + (environment const& lhs,
		T const&           rhs)
    {
      environment ret(lhs);
      ret += rhs;
      return ret;
    }

    /**
     * Remove variables from the environment.
     *
     * @param lhs the environment to remove from.
     * @param rhs the values to remove.
     * @returns the new environment.
     */
    template <typename T>
    friend environment
    operator - (environment const& lhs,
		T const&           rhs)
    {
      environment ret(lhs);
      ret -= rhs;
      return ret;
    }

    /**
     * Output the environment to an ostream.
     *
     * @param stream the stream to output to.
     * @param rhs the environment to output.
     * @returns the stream.
     */
    template <class charT, class traits>
    friend
    std::basic_ostream<charT,traits>&
    operator << (std::basic_ostream<charT,traits>& stream,
		 environment const& rhs)
    {
      for (environment::const_iterator pos = rhs.begin();
	   pos != rhs.end();
	   ++pos)
	{
	  stream << pos->first << '=' << pos->second << '\n';
	}

      return stream;
    }

  private:
    /// Filter regex.
    regex filter;
  };

}

#endif /* SBUILD_ENVIRONMENT_H */

/*
 * Local Variables:
 * mode:C++
 * End:
 */