[24167] | 1 | /* Copyright © 2005-2008 Roger Leigh <rleigh@debian.org> |
---|
| 2 | * |
---|
| 3 | * schroot is free software: you can redistribute it and/or modify it |
---|
| 4 | * under the terms of the GNU General Public License as published by |
---|
| 5 | * the Free Software Foundation, either version 3 of the License, or |
---|
| 6 | * (at your option) any later version. |
---|
| 7 | * |
---|
| 8 | * schroot is distributed in the hope that it will be useful, but |
---|
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 11 | * General Public License for more details. |
---|
| 12 | * |
---|
| 13 | * You should have received a copy of the GNU General Public License |
---|
| 14 | * along with this program. If not, see |
---|
| 15 | * <http://www.gnu.org/licenses/>. |
---|
| 16 | * |
---|
| 17 | *********************************************************************/ |
---|
| 18 | |
---|
| 19 | #include <config.h> |
---|
| 20 | |
---|
| 21 | #include "sbuild-environment.h" |
---|
| 22 | |
---|
| 23 | using boost::format; |
---|
| 24 | using namespace sbuild; |
---|
| 25 | |
---|
| 26 | environment::environment (): |
---|
| 27 | std::map<std::string,std::string>(), |
---|
| 28 | filter() |
---|
| 29 | { |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | environment::environment (char **environment): |
---|
| 33 | std::map<std::string,std::string>() |
---|
| 34 | { |
---|
| 35 | add(environment); |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | environment::~environment () |
---|
| 39 | { |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | void |
---|
| 43 | environment::set_filter (regex const& filter) |
---|
| 44 | { |
---|
| 45 | this->filter = filter; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | regex const& |
---|
| 49 | environment::get_filter () const |
---|
| 50 | { |
---|
| 51 | return this->filter; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | void |
---|
| 55 | environment::add (char **environment) |
---|
| 56 | { |
---|
| 57 | if (environment) |
---|
| 58 | { |
---|
| 59 | for (char **ev = environment; ev != 0 && *ev != 0; ++ev) |
---|
| 60 | add(std::string(*ev)); |
---|
| 61 | } |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | void |
---|
| 65 | environment::add (environment const& environment) |
---|
| 66 | { |
---|
| 67 | for (const_iterator pos = environment.begin(); |
---|
| 68 | pos != environment.end(); |
---|
| 69 | ++pos) |
---|
| 70 | add(*pos); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | void |
---|
| 74 | environment::add (std::string const& value) |
---|
| 75 | { |
---|
| 76 | std::string::size_type pos = value.find('='); |
---|
| 77 | if (pos != std::string::npos && pos != 0) |
---|
| 78 | { |
---|
| 79 | std::string key = value.substr(0, pos); |
---|
| 80 | std::string val; |
---|
| 81 | if (pos < value.length()) |
---|
| 82 | val = value.substr(pos + 1); |
---|
| 83 | add(std::make_pair(key, val)); |
---|
| 84 | } |
---|
| 85 | else |
---|
| 86 | { |
---|
| 87 | add(std::make_pair(value, std::string())); |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | void |
---|
| 92 | environment::add (value_type const& value) |
---|
| 93 | { |
---|
| 94 | remove(value); |
---|
| 95 | if (!value.first.empty() && !value.second.empty()) |
---|
| 96 | { |
---|
| 97 | if (this->filter.str().empty() || |
---|
| 98 | !regex_search(value.first, this->filter)) |
---|
| 99 | { |
---|
| 100 | insert(value); |
---|
| 101 | log_debug(DEBUG_NOTICE) << "Inserted into environment: " |
---|
| 102 | << value.first << '=' << value.second |
---|
| 103 | << std::endl; |
---|
| 104 | } |
---|
| 105 | else |
---|
| 106 | log_debug(DEBUG_INFO) << "Filtered from environment: " << value.first |
---|
| 107 | << std::endl; |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | void |
---|
| 112 | environment::remove (char **environment) |
---|
| 113 | { |
---|
| 114 | if (environment) |
---|
| 115 | { |
---|
| 116 | for (char **ev = environment; ev != 0 && *ev != 0; ++ev) |
---|
| 117 | remove(std::string(*ev)); |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | void |
---|
| 122 | environment::remove (environment const& environment) |
---|
| 123 | { |
---|
| 124 | for (const_iterator pos = environment.begin(); |
---|
| 125 | pos != environment.end(); |
---|
| 126 | ++pos) |
---|
| 127 | remove(*pos); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | void |
---|
| 131 | environment::remove (std::string const& value) |
---|
| 132 | { |
---|
| 133 | std::string::size_type pos = value.find('='); |
---|
| 134 | if (pos != std::string::npos && pos != 0) |
---|
| 135 | { |
---|
| 136 | std::string key = value.substr(0, pos); |
---|
| 137 | std::string val; |
---|
| 138 | if (pos < value.length()) |
---|
| 139 | val = value.substr(pos + 1); |
---|
| 140 | remove(std::make_pair(key, val)); |
---|
| 141 | } |
---|
| 142 | else |
---|
| 143 | { |
---|
| 144 | remove(std::make_pair(value, std::string())); |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | void |
---|
| 149 | environment::remove (value_type const& value) |
---|
| 150 | { |
---|
| 151 | iterator pos = find(value.first); |
---|
| 152 | if (pos != end()) |
---|
| 153 | erase(pos); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | char ** |
---|
| 157 | environment::get_strv () const |
---|
| 158 | { |
---|
| 159 | char **ret = new char *[size() + 1]; |
---|
| 160 | |
---|
| 161 | size_type idx = 0; |
---|
| 162 | for (const_iterator pos = begin(); pos != end(); ++pos, ++idx) |
---|
| 163 | { |
---|
| 164 | std::string envitem = pos->first + "=" + pos->second; |
---|
| 165 | ret[idx] = new char[envitem.length() + 1]; |
---|
| 166 | std::strcpy(ret[idx], envitem.c_str()); |
---|
| 167 | } |
---|
| 168 | ret[size()] = 0; |
---|
| 169 | |
---|
| 170 | return ret; |
---|
| 171 | } |
---|