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
00042 class environment : public std::map<std::string, std::string>
00043 {
00044 public:
00045 using std::map<std::string, std::string>::value_type;
00046
00048 environment ();
00049
00055 environment (char **environment);
00056
00058 ~environment ();
00059
00067 void
00068 add (char **environment);
00069
00076 void
00077 add (environment const& environment);
00078
00085 void
00086 add (value_type const& value);
00087
00095 void
00096 add (std::string const& name,
00097 std::string const& value)
00098 {
00099 add(std::make_pair(name, value));
00100 }
00101
00109 template<typename T>
00110 void
00111 add (std::string const& name,
00112 T const& value)
00113 {
00114 std::ostringstream varstring;
00115 varstring.imbue(std::locale::classic());
00116 varstring << std::boolalpha << value;
00117 add(std::make_pair(name, varstring.str()));
00118 }
00119
00127 void
00128 add (std::string const& value);
00129
00137 void
00138 remove (char **environment);
00139
00146 void
00147 remove (environment const& environment);
00148
00155 void
00156 remove (std::string const& value);
00157
00164 void
00165 remove (value_type const& value);
00166
00175 template <typename T>
00176 bool
00177 get (std::string const& name,
00178 T& value)
00179 {
00180 log_debug(DEBUG_INFO) << "Getting environment variable=" << name
00181 << std::endl;
00182 iterator pos = find(name);
00183 if (pos != end())
00184 {
00185 try
00186 {
00187 parse_value(pos->second, value);
00188 return true;
00189 }
00190 catch (parse_value_error const& e)
00191 {
00192 log_warning() << boost::format("%1%: %2%\n")
00193 % name % e.what();
00194 return false;
00195 }
00196 }
00197 log_debug(DEBUG_NOTICE) << "name not found: " << name << std::endl;
00198 return false;
00199 }
00200
00208 char **
00209 get_strv () const;
00210
00217 template <typename T>
00218 environment&
00219 operator += (T& rhs)
00220 {
00221 add(rhs);
00222 return *this;
00223 }
00224
00231 template <typename T>
00232 environment&
00233 operator -= (T& rhs)
00234 {
00235 remove(rhs);
00236 return *this;
00237 }
00238
00246 template <typename T>
00247 friend environment
00248 operator + (environment const& lhs,
00249 T const& rhs)
00250 {
00251 environment ret(lhs);
00252 ret += rhs;
00253 return ret;
00254 }
00255
00263 template <typename T>
00264 friend environment
00265 operator - (environment const& lhs,
00266 T const& rhs)
00267 {
00268 environment ret(lhs);
00269 ret -= rhs;
00270 return ret;
00271 }
00272
00280 template <class charT, class traits>
00281 friend
00282 std::basic_ostream<charT,traits>&
00283 operator << (std::basic_ostream<charT,traits>& stream,
00284 environment const& rhs)
00285 {
00286 for (environment::const_iterator pos = rhs.begin();
00287 pos != rhs.end();
00288 ++pos)
00289 {
00290 stream << pos->first << '=' << pos->second << '\n';
00291 }
00292
00293 return stream;
00294 }
00295 };
00296
00297 }
00298
00299 #endif
00300
00301
00302
00303
00304
00305