sbuild  1.7.1
environment.h
1 /* Copyright © 2005-2013 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/log.h>
23 #include <sbuild/parse-value.h>
24 #include <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 (const regex& filter);
68 
74  regex const&
75  get_filter () const;
76 
84  void
85  add (char **environment);
86 
93  void
94  add (const environment& environment);
95 
102  void
103  add (const value_type& value);
104 
112  void
113  add (const std::string& name,
114  const std::string& value)
115  {
116  add(std::make_pair(name, value));
117  }
118 
126  template<typename T>
127  void
128  add (const std::string& 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 (const std::string& value);
146 
154  void
155  remove (char **environment);
156 
163  void
164  remove (const environment& environment);
165 
172  void
173  remove (const std::string& value);
174 
181  void
182  remove (const value_type& value);
183 
192  template <typename T>
193  bool
194  get (const std::string& 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 (const parse_value_error& 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 + (const environment& 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 - (const environment& 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  const environment& rhs)
302  {
303  for (const auto& env : rhs)
304  {
305  stream << env.first << '=' << env.second << '\n';
306  }
307 
308  return stream;
309  }
310 
311  private:
314  };
315 
316 }
317 
318 #endif /* SBUILD_ENVIRONMENT_H */
319 
320 /*
321  * Local Variables:
322  * mode:C++
323  * End:
324  */