sbuild-environment.cc

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 #include <config.h>
00021 
00022 #include "sbuild-environment.h"
00023 
00024 using boost::format;
00025 using namespace sbuild;
00026 
00027 environment::environment ():
00028   std::map<std::string,std::string>()
00029 {
00030 }
00031 
00032 environment::environment (char **environment):
00033   std::map<std::string,std::string>()
00034 {
00035   add(environment);
00036 }
00037 
00038 environment::~environment ()
00039 {
00040 }
00041 
00042 void
00043 environment::add (char **environment)
00044 {
00045   if (environment)
00046     {
00047       for (char **ev = environment; ev != 0 && *ev != 0; ++ev)
00048         add(std::string(*ev));
00049     }
00050 }
00051 
00052 void
00053 environment::add (environment const& environment)
00054 {
00055   for (const_iterator pos = environment.begin();
00056        pos != environment.end();
00057        ++pos)
00058     add(*pos);
00059 }
00060 
00061 void
00062 environment::add (std::string const& value)
00063 {
00064   std::string::size_type pos = value.find('=');
00065   if (pos != std::string::npos && pos != 0)
00066     {
00067       std::string key = value.substr(0, pos);
00068       std::string val;
00069       if (pos < value.length())
00070         val = value.substr(pos + 1);
00071       add(std::make_pair(key, val));
00072     }
00073   else
00074     {
00075       add(std::make_pair(value, std::string()));
00076     }
00077 }
00078 
00079 void
00080 environment::add (value_type const& value)
00081 {
00082   remove(value);
00083   if (!value.second.empty())
00084     insert(value);
00085 }
00086 
00087 void
00088 environment::remove (char **environment)
00089 {
00090   if (environment)
00091     {
00092       for (char **ev = environment; ev != 0 && *ev != 0; ++ev)
00093         remove(std::string(*ev));
00094     }
00095 }
00096 
00097 void
00098 environment::remove (environment const& environment)
00099 {
00100   for (const_iterator pos = environment.begin();
00101        pos != environment.end();
00102        ++pos)
00103     remove(*pos);
00104 }
00105 
00106 void
00107 environment::remove (std::string const& value)
00108 {
00109   std::string::size_type pos = value.find('=');
00110   if (pos != std::string::npos && pos != 0)
00111     {
00112       std::string key = value.substr(0, pos);
00113       std::string val;
00114       if (pos < value.length())
00115         val = value.substr(pos + 1);
00116       remove(std::make_pair(key, val));
00117     }
00118   else
00119     {
00120       remove(std::make_pair(value, std::string()));
00121     }
00122 }
00123 
00124 void
00125 environment::remove (value_type const& value)
00126 {
00127   iterator pos = find(value.first);
00128   if (pos != end())
00129     erase(pos);
00130 }
00131 
00132 char **
00133 environment::get_strv () const
00134 {
00135   char **ret = new char *[size() + 1];
00136 
00137   size_type idx = 0;
00138   for (const_iterator pos = begin(); pos != end(); ++pos, ++idx)
00139     {
00140       std::string envitem = pos->first + "=" + pos->second;
00141       ret[idx] = new char[envitem.length() + 1];
00142       std::strcpy(ret[idx], envitem.c_str());
00143     }
00144   ret[size()] = 0;
00145 
00146   return ret;
00147 }

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