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 "sbuild-log.h"
00028 #include "sbuild-parse-value.h"
00029 
00030 namespace sbuild
00031 {
00032 
00036   class environment : public std::map<std::string, std::string>
00037   {
00038   public:
00039     using std::map<std::string, std::string>::value_type;
00040 
00042     environment ();
00043 
00049     environment (char **environment);
00050 
00052     ~environment ();
00053 
00061     void
00062     add (char **environment);
00063 
00070     void
00071     add (environment const& environment);
00072 
00079     void
00080     add (value_type const& value);
00081 
00089     void
00090     add (std::string const& name,
00091          std::string const& value)
00092     {
00093       add(std::make_pair(name, value));
00094     }
00095 
00103     template<typename T>
00104     void
00105     add (std::string const& name,
00106          T const&           value)
00107     {
00108       std::ostringstream varstring;
00109       varstring.imbue(std::locale("C"));
00110       varstring << std::boolalpha << value;
00111       add(std::make_pair(name, varstring.str()));
00112     }
00113 
00121     void
00122     add (std::string const& value);
00123 
00131     void
00132     remove (char **environment);
00133 
00140     void
00141     remove (environment const& environment);
00142 
00149     void
00150     remove (std::string const& value);
00151 
00158     void
00159     remove (value_type const& value);
00160 
00169     template <typename T>
00170     bool
00171     get (std::string const& name,
00172          T&                 value)
00173     {
00174       log_debug(DEBUG_INFO) << "Getting environment variable=" << name
00175                             << std::endl;
00176       iterator pos = find(name);
00177       if (pos != end())
00178         return parse_value(pos->second, value);
00179       log_debug(DEBUG_NOTICE) << "name not found: " << name << std::endl;
00180       return false;
00181     }
00182 
00190     char **
00191     get_strv () const;
00192 
00199     template <typename T>
00200     environment&
00201     operator += (T& rhs)
00202     {
00203       add(rhs);
00204       return *this;
00205     }
00206 
00213     template <typename T>
00214     environment&
00215     operator -= (T& rhs)
00216     {
00217       remove(rhs);
00218       return *this;
00219     }
00220 
00228     template <typename T>
00229     friend environment
00230     operator + (environment const& lhs,
00231                 T const&           rhs)
00232     {
00233       environment ret(lhs);
00234       ret += rhs;
00235       return ret;
00236     }
00237 
00245     template <typename T>
00246     friend environment
00247     operator - (environment const& lhs,
00248                 T const&           rhs)
00249     {
00250       environment ret(lhs);
00251       ret -= rhs;
00252       return ret;
00253     }
00254 
00262     template <class charT, class traits>
00263     friend
00264     std::basic_ostream<charT,traits>&
00265     operator << (std::basic_ostream<charT,traits>& stream,
00266                  environment const& rhs)
00267     {
00268       for (environment::const_iterator pos = rhs.begin();
00269            pos != rhs.end();
00270            ++pos)
00271         {
00272           stream << pos->first << '=' << pos->second << '\n';
00273         }
00274 
00275       return stream;
00276     }
00277   };
00278 
00279 }
00280 
00281 #endif /* SBUILD_ENVIRONMENT_H */
00282 
00283 /*
00284  * Local Variables:
00285  * mode:C++
00286  * End:
00287  */

Generated on Sun Mar 19 12:07:48 2006 for schroot by  doxygen 1.4.6