sbuild-environment.h

Go to the documentation of this file.
00001 /* Copyright © 2005-2006  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 2 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, write to the Free Software
00015  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00016  * MA  02111-1307  USA
00017  *
00018  *********************************************************************/
00019 
00020 #ifndef SBUILD_ENVIRONMENT_H
00021 #define SBUILD_ENVIRONMENT_H
00022 
00023 #include <map>
00024 #include <string>
00025 #include <sstream>
00026 
00027 #include <boost/format.hpp>
00028 
00029 #include "sbuild-log.h"
00030 #include "sbuild-parse-value.h"
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               value = static_cast<T const&>(parse_value(pos->second));
00184               return true;
00185             }
00186           catch (parse_value::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 /* SBUILD_ENVIRONMENT_H */
00296 
00297 /*
00298  * Local Variables:
00299  * mode:C++
00300  * End:
00301  */

Generated on Sat Jun 10 10:05:12 2006 for schroot by  doxygen 1.4.6