sbuild  1.6.10
sbuild-environment.h
1 /* Copyright © 2005-2007 Roger Leigh <rleigh@debian.org>
2  *
3  * schroot is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * schroot is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  *********************************************************************/
18 
19 #ifndef SBUILD_ENVIRONMENT_H
20 #define SBUILD_ENVIRONMENT_H
21 
22 #include <sbuild/sbuild-log.h>
23 #include <sbuild/sbuild-parse-value.h>
24 #include <sbuild/sbuild-regex.h>
25 
26 #include <map>
27 #include <string>
28 #include <sstream>
29 
30 #include <boost/format.hpp>
31 
32 namespace sbuild
33 {
34 
38  class environment : public std::map<std::string, std::string>
39  {
40  public:
41  using std::map<std::string, std::string>::value_type;
42 
44  environment ();
45 
51  environment (char **environment);
52 
54  ~environment ();
55 
66  void
67  set_filter (regex const& filter);
68 
74  regex const&
75  get_filter () const;
76 
84  void
85  add (char **environment);
86 
93  void
94  add (environment const& environment);
95 
102  void
103  add (value_type const& value);
104 
112  void
113  add (std::string const& name,
114  std::string const& value)
115  {
116  add(std::make_pair(name, value));
117  }
118 
126  template<typename T>
127  void
128  add (std::string const& name,
129  T const& value)
130  {
131  std::ostringstream varstring;
132  varstring.imbue(std::locale::classic());
133  varstring << std::boolalpha << value;
134  add(std::make_pair(name, varstring.str()));
135  }
136 
144  void
145  add (std::string const& value);
146 
154  void
155  remove (char **environment);
156 
163  void
164  remove (environment const& environment);
165 
172  void
173  remove (std::string const& value);
174 
181  void
182  remove (value_type const& value);
183 
192  template <typename T>
193  bool
194  get (std::string const& name,
195  T& value) const
196  {
197  log_debug(DEBUG_INFO) << "Getting environment variable=" << name
198  << std::endl;
199  const_iterator pos = find(name);
200  if (pos != end())
201  {
202  try
203  {
204  parse_value(pos->second, value);
205  return true;
206  }
207  catch (parse_value_error const& e)
208  {
209  log_warning() << boost::format("%1%: %2%\n")
210  % name % e.what();
211  return false;
212  }
213  }
214  log_debug(DEBUG_NOTICE) << "name not found: " << name << std::endl;
215  return false;
216  }
217 
225  char **
226  get_strv () const;
227 
234  template <typename T>
235  environment&
236  operator += (T const& rhs)
237  {
238  add(rhs);
239  return *this;
240  }
241 
248  template <typename T>
249  environment&
250  operator -= (T const& rhs)
251  {
252  remove(rhs);
253  return *this;
254  }
255 
263  template <typename T>
264  friend environment
265  operator + (environment const& lhs,
266  T const& rhs)
267  {
268  environment ret(lhs);
269  ret += rhs;
270  return ret;
271  }
272 
280  template <typename T>
281  friend environment
282  operator - (environment const& lhs,
283  T const& rhs)
284  {
285  environment ret(lhs);
286  ret -= rhs;
287  return ret;
288  }
289 
297  template <class charT, class traits>
298  friend
299  std::basic_ostream<charT,traits>&
300  operator << (std::basic_ostream<charT,traits>& stream,
301  environment const& rhs)
302  {
303  for (environment::const_iterator pos = rhs.begin();
304  pos != rhs.end();
305  ++pos)
306  {
307  stream << pos->first << '=' << pos->second << '\n';
308  }
309 
310  return stream;
311  }
312 
313  private:
316  };
317 
318 }
319 
320 #endif /* SBUILD_ENVIRONMENT_H */
321 
322 /*
323  * Local Variables:
324  * mode:C++
325  * End:
326  */
POSIX extended regular expression.
Definition: sbuild-regex.h:66
regex const & get_filter() const
Get environment filter.
Definition: sbuild-environment.cc:51
Debian source builder components.
Definition: sbuild-auth-null.h:24
environment & operator+=(T const &rhs)
Add variables to the environment.
Definition: sbuild-environment.h:236
environment()
The constructor.
Definition: sbuild-environment.cc:28
regex filter
Filter regex.
Definition: sbuild-environment.h:315
void set_filter(regex const &filter)
Set environment filter.
Definition: sbuild-environment.cc:45
Notification messages.
Definition: sbuild-log.h:31
char ** get_strv() const
Get the evironment variables as a string vector.
Definition: sbuild-environment.cc:159
std::ostream & log_debug(debug_level level)
Log a debug message.
Definition: sbuild-log.cc:110
Container of environment variables.
Definition: sbuild-environment.h:38
~environment()
The destructor.
Definition: sbuild-environment.cc:40
void add(std::string const &name, T const &value)
Add environment variable.
Definition: sbuild-environment.h:128
void add(char **environment)
Add environment variables.
Definition: sbuild-environment.cc:57
friend environment operator+(environment const &lhs, T const &rhs)
Add variables to the environment.
Definition: sbuild-environment.h:265
Parse error.
Definition: sbuild-parse-error.h:35
Informational messages.
Definition: sbuild-log.h:32
void parse_value(std::string const &value, bool &parsed_value)
Parse a boolean value.
Definition: sbuild-parse-value.cc:49
environment & operator-=(T const &rhs)
Remove variables from the environment.
Definition: sbuild-environment.h:250
friend environment operator-(environment const &lhs, T const &rhs)
Remove variables from the environment.
Definition: sbuild-environment.h:282
void add(std::string const &name, std::string const &value)
Add environment variable.
Definition: sbuild-environment.h:113
std::ostream & log_warning()
Log a warning message.
Definition: sbuild-log.cc:96