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 <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 /* SBUILD_ENVIRONMENT_H */
00300 
00301 /*
00302  * Local Variables:
00303  * mode:C++
00304  * End:
00305  */

Generated on Sat Jan 27 16:11:03 2007 for schroot by  doxygen 1.5.1