OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/environment.h" | 5 #include "base/environment.h" |
6 | 6 |
7 #include <map> | |
viettrungluu
2013/08/09 19:27:11
I don't think this include is needed (given that i
| |
8 | |
9 #include "base/strings/string_piece.h" | |
10 #include "base/strings/string_util.h" | |
11 #include "base/strings/utf_string_conversions.h" | |
12 | |
7 #if defined(OS_POSIX) | 13 #if defined(OS_POSIX) |
8 #include <stdlib.h> | 14 #include <stdlib.h> |
9 #elif defined(OS_WIN) | 15 #elif defined(OS_WIN) |
10 #include <windows.h> | 16 #include <windows.h> |
11 #endif | 17 #endif |
12 | 18 |
13 #include "base/strings/string_util.h" | 19 namespace base { |
14 | |
15 #if defined(OS_WIN) | |
16 #include "base/memory/scoped_ptr.h" | |
17 #include "base/strings/utf_string_conversions.h" | |
18 #endif | |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 class EnvironmentImpl : public base::Environment { | 23 class EnvironmentImpl : public base::Environment { |
23 public: | 24 public: |
24 virtual bool GetVar(const char* variable_name, | 25 virtual bool GetVar(const char* variable_name, |
25 std::string* result) OVERRIDE { | 26 std::string* result) OVERRIDE { |
26 if (GetVarImpl(variable_name, result)) | 27 if (GetVarImpl(variable_name, result)) |
27 return true; | 28 return true; |
28 | 29 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 #if defined(OS_POSIX) | 93 #if defined(OS_POSIX) |
93 // On success, zero is returned. | 94 // On success, zero is returned. |
94 return !unsetenv(variable_name); | 95 return !unsetenv(variable_name); |
95 #elif defined(OS_WIN) | 96 #elif defined(OS_WIN) |
96 // On success, a nonzero value is returned. | 97 // On success, a nonzero value is returned. |
97 return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), NULL); | 98 return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), NULL); |
98 #endif | 99 #endif |
99 } | 100 } |
100 }; | 101 }; |
101 | 102 |
103 // Parses a null-terminated input string of an environment block. The key is | |
104 // placed into the given string, and the total length of the line, including | |
105 // the terminating null, is returned. | |
106 size_t ParseEnvLine(const NativeEnvironmentString::value_type* input, | |
107 NativeEnvironmentString* key) { | |
108 // Skip to the equals or end of the string, this is the key. | |
109 size_t cur = 0; | |
110 while (input[cur] && input[cur] != '=') | |
111 cur++; | |
112 *key = NativeEnvironmentString(&input[0], cur); | |
113 | |
114 // Now just skip to the end of the string. | |
115 while (input[cur]) | |
116 cur++; | |
117 return cur + 1; | |
118 } | |
119 | |
102 } // namespace | 120 } // namespace |
103 | 121 |
104 namespace base { | |
105 | |
106 namespace env_vars { | 122 namespace env_vars { |
107 | 123 |
108 #if defined(OS_POSIX) | 124 #if defined(OS_POSIX) |
109 // On Posix systems, this variable contains the location of the user's home | 125 // On Posix systems, this variable contains the location of the user's home |
110 // directory. (e.g, /home/username/). | 126 // directory. (e.g, /home/username/). |
111 const char kHome[] = "HOME"; | 127 const char kHome[] = "HOME"; |
112 #endif | 128 #endif |
113 | 129 |
114 } // namespace env_vars | 130 } // namespace env_vars |
115 | 131 |
116 Environment::~Environment() {} | 132 Environment::~Environment() {} |
117 | 133 |
118 // static | 134 // static |
119 Environment* Environment::Create() { | 135 Environment* Environment::Create() { |
120 return new EnvironmentImpl(); | 136 return new EnvironmentImpl(); |
121 } | 137 } |
122 | 138 |
123 bool Environment::HasVar(const char* variable_name) { | 139 bool Environment::HasVar(const char* variable_name) { |
124 return GetVar(variable_name, NULL); | 140 return GetVar(variable_name, NULL); |
125 } | 141 } |
126 | 142 |
143 #if defined(OS_WIN) | |
144 | |
145 string16 AlterEnvironment(const wchar_t* env, | |
146 const EnvironmentMap& changes) { | |
147 string16 result; | |
148 | |
149 // First copy all unmodified values to the output. | |
150 size_t cur_env = 0; | |
151 string16 key; | |
152 while (env[cur_env]) { | |
153 const wchar_t* line = &env[cur_env]; | |
154 size_t line_length = ParseEnvLine(line, &key); | |
155 | |
156 // Keep only values not specified in the change vector. | |
157 EnvironmentMap::const_iterator found_change = changes.find(key); | |
158 if (found_change == changes.end()) | |
159 result.append(line, line_length); | |
160 | |
161 cur_env += line_length; | |
162 } | |
163 | |
164 // Now append all modified and new values. | |
165 for (EnvironmentMap::const_iterator i = changes.begin(); | |
166 i != changes.end(); ++i) { | |
167 if (!i->second.empty()) | |
168 result.append(i->first); | |
169 result.push_back('='); | |
170 result.append(i->second); | |
171 result.push_back(0); | |
172 } | |
173 } | |
174 | |
175 // An additional null marks the end of the list. | |
176 result.push_back(0); | |
177 return result; | |
178 } | |
179 | |
180 #elif defined(OS_POSIX) | |
181 | |
182 scoped_ptr<char*[]> AlterEnvironment(const char* const* const env, | |
183 const EnvironmentMap& changes) { | |
184 std::string value_storage; // Holds concatenated null-terminated strings. | |
185 std::vector<size_t> result_indices; // Line indices into value_storage. | |
186 | |
187 // First build up all of the unchanged environment strings. These are | |
188 // null-terminated of the form "key=value". | |
189 std::string key; | |
190 for (size_t i = 0; env[i]; i++) { | |
191 size_t line_length = ParseEnvLine(env[i], &key); | |
192 | |
193 // Keep only values not specified in the change vector. | |
194 EnvironmentMap::const_iterator found_change = changes.find(key); | |
195 if (found_change == changes.end()) { | |
196 result_indices.push_back(value_storage.size()); | |
197 value_storage.append(env[i], line_length); | |
198 } | |
199 } | |
200 | |
201 // Now append all modified and new values. | |
202 for (EnvironmentMap::const_iterator i = changes.begin(); | |
203 i != changes.end(); ++i) { | |
204 if (!i->second.empty()) { | |
205 result_indices.push_back(value_storage.size()); | |
206 value_storage.append(i->first); | |
207 value_storage.push_back('='); | |
208 value_storage.append(i->second); | |
209 value_storage.push_back(0); | |
210 } | |
211 } | |
212 | |
213 // The returned memory needs enough room for the pointer array with a null | |
214 // terminator, plus the storage for the strings. | |
215 char* buffer = new char[ | |
216 (result_indices.size() + 1) * sizeof(char*) + value_storage.size()]; | |
217 scoped_ptr<char*[]> result(reinterpret_cast<char**>(buffer)); | |
viettrungluu
2013/08/09 19:27:11
Hrm, putting a char[] into a scoped_ptr<char*[]> s
brettw
2013/08/09 20:08:56
Done.
| |
218 | |
219 // The string storage goes after the array of pointers. | |
220 char* storage_data = reinterpret_cast<char*>( | |
221 &result.get()[result_indices.size() + 1]); | |
222 memcpy(storage_data, value_storage.c_str(), value_storage.size()); | |
viettrungluu
2013/08/09 19:27:11
c_str -> data
brettw
2013/08/09 20:08:56
I did this change but I added an if(). I did the o
| |
223 | |
224 // Fill array of pointers at the beginning of the result. | |
225 for (size_t i = 0; i < result_indices.size(); i++) | |
226 result[i] = &storage_data[result_indices[i]]; | |
227 result[result_indices.size()] = 0; // Null terminator. | |
228 | |
229 return result.Pass(); | |
230 } | |
231 | |
232 #endif // OS_POSIX | |
233 | |
127 } // namespace base | 234 } // namespace base |
OLD | NEW |