00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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 }