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
00026 #include <map>
00027 #include <string>
00028 #include <sstream>
00029
00030 #include <boost/format.hpp>
00031
00032 namespace sbuild
00033 {
00034
00038 class environment : public std::map<std::string, std::string>
00039 {
00040 public:
00041 using std::map<std::string, std::string>::value_type;
00042
00044 environment ();
00045
00051 environment (char **environment);
00052
00054 ~environment ();
00055
00063 void
00064 add (char **environment);
00065
00072 void
00073 add (environment const& environment);
00074
00081 void
00082 add (value_type const& value);
00083
00091 void
00092 add (std::string const& name,
00093 std::string const& value)
00094 {
00095 add(std::make_pair(name, value));
00096 }
00097
00105 template<typename T>
00106 void
00107 add (std::string const& name,
00108 T const& value)
00109 {
00110 std::ostringstream varstring;
00111 varstring.imbue(std::locale("C"));
00112 varstring << std::boolalpha << value;
00113 add(std::make_pair(name, varstring.str()));
00114 }
00115
00123 void
00124 add (std::string const& value);
00125
00133 void
00134 remove (char **environment);
00135
00142 void
00143 remove (environment const& environment);
00144
00151 void
00152 remove (std::string const& value);
00153
00160 void
00161 remove (value_type const& value);
00162
00171 template <typename T>
00172 bool
00173 get (std::string const& name,
00174 T& value)
00175 {
00176 log_debug(DEBUG_INFO) << "Getting environment variable=" << name
00177 << std::endl;
00178 iterator pos = find(name);
00179 if (pos != end())
00180 {
00181 try
00182 {
00183 parse_value(pos->second, value);
00184 return true;
00185 }
00186 catch (parse_error const& e)
00187 {
00188 log_warning() << boost::format("%1%: %2%\n")
00189 % name % e.what();
00190 return false;
00191 }
00192 }
00193 log_debug(DEBUG_NOTICE) << "name not found: " << name << std::endl;
00194 return false;
00195 }
00196
00204 char **
00205 get_strv () const;
00206
00213 template <typename T>
00214 environment&
00215 operator += (T& rhs)
00216 {
00217 add(rhs);
00218 return *this;
00219 }
00220
00227 template <typename T>
00228 environment&
00229 operator -= (T& rhs)
00230 {
00231 remove(rhs);
00232 return *this;
00233 }
00234
00242 template <typename T>
00243 friend environment
00244 operator + (environment const& lhs,
00245 T const& rhs)
00246 {
00247 environment ret(lhs);
00248 ret += rhs;
00249 return ret;
00250 }
00251
00259 template <typename T>
00260 friend environment
00261 operator - (environment const& lhs,
00262 T const& rhs)
00263 {
00264 environment ret(lhs);
00265 ret -= rhs;
00266 return ret;
00267 }
00268
00276 template <class charT, class traits>
00277 friend
00278 std::basic_ostream<charT,traits>&
00279 operator << (std::basic_ostream<charT,traits>& stream,
00280 environment const& rhs)
00281 {
00282 for (environment::const_iterator pos = rhs.begin();
00283 pos != rhs.end();
00284 ++pos)
00285 {
00286 stream << pos->first << '=' << pos->second << '\n';
00287 }
00288
00289 return stream;
00290 }
00291 };
00292
00293 }
00294
00295 #endif
00296
00297
00298
00299
00300
00301