00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef SBUILD_ENVIRONMENT_H
00021 #define SBUILD_ENVIRONMENT_H
00022
00023 #include <sbuild/sbuild-log.h>
00024 #include <sbuild/sbuild-parse-value.h>
00025 #include <sbuild/sbuild-regex.h>
00026
00027 #include <map>
00028 #include <string>
00029 #include <sstream>
00030
00031 #include <boost/format.hpp>
00032
00033 namespace sbuild
00034 {
00035
00039 class environment : public std::map<std::string, std::string>
00040 {
00041 public:
00042 using std::map<std::string, std::string>::value_type;
00043
00045 environment ();
00046
00052 environment (char **environment);
00053
00055 ~environment ();
00056
00067 void
00068 set_filter (regex const& filter);
00069
00075 regex const&
00076 get_filter () const;
00077
00085 void
00086 add (char **environment);
00087
00094 void
00095 add (environment const& environment);
00096
00103 void
00104 add (value_type const& value);
00105
00113 void
00114 add (std::string const& name,
00115 std::string const& value)
00116 {
00117 add(std::make_pair(name, value));
00118 }
00119
00127 template<typename T>
00128 void
00129 add (std::string const& name,
00130 T const& value)
00131 {
00132 std::ostringstream varstring;
00133 varstring.imbue(std::locale::classic());
00134 varstring << std::boolalpha << value;
00135 add(std::make_pair(name, varstring.str()));
00136 }
00137
00145 void
00146 add (std::string const& value);
00147
00155 void
00156 remove (char **environment);
00157
00164 void
00165 remove (environment const& environment);
00166
00173 void
00174 remove (std::string const& value);
00175
00182 void
00183 remove (value_type const& value);
00184
00193 template <typename T>
00194 bool
00195 get (std::string const& name,
00196 T& value)
00197 {
00198 log_debug(DEBUG_INFO) << "Getting environment variable=" << name
00199 << std::endl;
00200 iterator pos = find(name);
00201 if (pos != end())
00202 {
00203 try
00204 {
00205 parse_value(pos->second, value);
00206 return true;
00207 }
00208 catch (parse_value_error const& e)
00209 {
00210 log_warning() << boost::format("%1%: %2%\n")
00211 % name % e.what();
00212 return false;
00213 }
00214 }
00215 log_debug(DEBUG_NOTICE) << "name not found: " << name << std::endl;
00216 return false;
00217 }
00218
00226 char **
00227 get_strv () const;
00228
00235 template <typename T>
00236 environment&
00237 operator += (T const& rhs)
00238 {
00239 add(rhs);
00240 return *this;
00241 }
00242
00249 template <typename T>
00250 environment&
00251 operator -= (T const& rhs)
00252 {
00253 remove(rhs);
00254 return *this;
00255 }
00256
00264 template <typename T>
00265 friend environment
00266 operator + (environment const& lhs,
00267 T const& rhs)
00268 {
00269 environment ret(lhs);
00270 ret += rhs;
00271 return ret;
00272 }
00273
00281 template <typename T>
00282 friend environment
00283 operator - (environment const& lhs,
00284 T const& rhs)
00285 {
00286 environment ret(lhs);
00287 ret -= rhs;
00288 return ret;
00289 }
00290
00298 template <class charT, class traits>
00299 friend
00300 std::basic_ostream<charT,traits>&
00301 operator << (std::basic_ostream<charT,traits>& stream,
00302 environment const& rhs)
00303 {
00304 for (environment::const_iterator pos = rhs.begin();
00305 pos != rhs.end();
00306 ++pos)
00307 {
00308 stream << pos->first << '=' << pos->second << '\n';
00309 }
00310
00311 return stream;
00312 }
00313
00314 private:
00316 regex filter;
00317 };
00318
00319 }
00320
00321 #endif
00322
00323
00324
00325
00326
00327