sbuild 1.4.23
|
00001 /* Copyright © 2005-2007 Roger Leigh <rleigh@debian.org> 00002 * 00003 * schroot is free software: you can redistribute it and/or modify it 00004 * under the terms of the GNU General Public License as published by 00005 * the Free Software Foundation, either version 3 of the License, or 00006 * (at your option) any later version. 00007 * 00008 * schroot is distributed in the hope that it will be useful, but 00009 * WITHOUT ANY WARRANTY; without even the implied warranty of 00010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 * General Public License for more details. 00012 * 00013 * You should have received a copy of the GNU General Public License 00014 * along with this program. If not, see 00015 * <http://www.gnu.org/licenses/>. 00016 * 00017 *********************************************************************/ 00018 00019 #ifndef SBUILD_ENVIRONMENT_H 00020 #define SBUILD_ENVIRONMENT_H 00021 00022 #include <sbuild/sbuild-log.h> 00023 #include <sbuild/sbuild-parse-value.h> 00024 #include <sbuild/sbuild-regex.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 00066 void 00067 set_filter (regex const& filter); 00068 00074 regex const& 00075 get_filter () const; 00076 00084 void 00085 add (char **environment); 00086 00093 void 00094 add (environment const& environment); 00095 00102 void 00103 add (value_type const& value); 00104 00112 void 00113 add (std::string const& name, 00114 std::string const& value) 00115 { 00116 add(std::make_pair(name, value)); 00117 } 00118 00126 template<typename T> 00127 void 00128 add (std::string const& name, 00129 T const& value) 00130 { 00131 std::ostringstream varstring; 00132 varstring.imbue(std::locale::classic()); 00133 varstring << std::boolalpha << value; 00134 add(std::make_pair(name, varstring.str())); 00135 } 00136 00144 void 00145 add (std::string const& value); 00146 00154 void 00155 remove (char **environment); 00156 00163 void 00164 remove (environment const& environment); 00165 00172 void 00173 remove (std::string const& value); 00174 00181 void 00182 remove (value_type const& value); 00183 00192 template <typename T> 00193 bool 00194 get (std::string const& name, 00195 T& value) const 00196 { 00197 log_debug(DEBUG_INFO) << "Getting environment variable=" << name 00198 << std::endl; 00199 const_iterator pos = find(name); 00200 if (pos != end()) 00201 { 00202 try 00203 { 00204 parse_value(pos->second, value); 00205 return true; 00206 } 00207 catch (parse_value_error const& e) 00208 { 00209 log_warning() << boost::format("%1%: %2%\n") 00210 % name % e.what(); 00211 return false; 00212 } 00213 } 00214 log_debug(DEBUG_NOTICE) << "name not found: " << name << std::endl; 00215 return false; 00216 } 00217 00225 char ** 00226 get_strv () const; 00227 00234 template <typename T> 00235 environment& 00236 operator += (T const& rhs) 00237 { 00238 add(rhs); 00239 return *this; 00240 } 00241 00248 template <typename T> 00249 environment& 00250 operator -= (T const& rhs) 00251 { 00252 remove(rhs); 00253 return *this; 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 <typename T> 00281 friend environment 00282 operator - (environment const& lhs, 00283 T const& rhs) 00284 { 00285 environment ret(lhs); 00286 ret -= rhs; 00287 return ret; 00288 } 00289 00297 template <class charT, class traits> 00298 friend 00299 std::basic_ostream<charT,traits>& 00300 operator << (std::basic_ostream<charT,traits>& stream, 00301 environment const& rhs) 00302 { 00303 for (environment::const_iterator pos = rhs.begin(); 00304 pos != rhs.end(); 00305 ++pos) 00306 { 00307 stream << pos->first << '=' << pos->second << '\n'; 00308 } 00309 00310 return stream; 00311 } 00312 00313 private: 00315 regex filter; 00316 }; 00317 00318 } 00319 00320 #endif /* SBUILD_ENVIRONMENT_H */ 00321 00322 /* 00323 * Local Variables: 00324 * mode:C++ 00325 * End: 00326 */