Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(561)

Side by Side Diff: base/environment.cc

Issue 2692273008: Hacky slashy (Closed)
Patch Set: wip Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <stddef.h> 7 #include <stddef.h>
8 8
9 #include <vector> 9 #include <vector>
10 10
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 const std::string& new_value) override { 49 const std::string& new_value) override {
50 return SetVarImpl(variable_name, new_value); 50 return SetVarImpl(variable_name, new_value);
51 } 51 }
52 52
53 bool UnSetVar(StringPiece variable_name) override { 53 bool UnSetVar(StringPiece variable_name) override {
54 return UnSetVarImpl(variable_name); 54 return UnSetVarImpl(variable_name);
55 } 55 }
56 56
57 private: 57 private:
58 bool GetVarImpl(StringPiece variable_name, std::string* result) { 58 bool GetVarImpl(StringPiece variable_name, std::string* result) {
59 #if defined(OS_POSIX) 59 #if defined(OS_POSIX) || defined(OS_FUCHSIA)
60 const char* env_value = getenv(variable_name.data()); 60 const char* env_value = getenv(variable_name.data());
61 if (!env_value) 61 if (!env_value)
62 return false; 62 return false;
63 // Note that the variable may be defined but empty. 63 // Note that the variable may be defined but empty.
64 if (result) 64 if (result)
65 *result = env_value; 65 *result = env_value;
66 return true; 66 return true;
67 #elif defined(OS_WIN) 67 #elif defined(OS_WIN)
68 DWORD value_length = 68 DWORD value_length =
69 ::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), nullptr, 0); 69 ::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), nullptr, 0);
70 if (value_length == 0) 70 if (value_length == 0)
71 return false; 71 return false;
72 if (result) { 72 if (result) {
73 std::unique_ptr<wchar_t[]> value(new wchar_t[value_length]); 73 std::unique_ptr<wchar_t[]> value(new wchar_t[value_length]);
74 ::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), value.get(), 74 ::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), value.get(),
75 value_length); 75 value_length);
76 *result = WideToUTF8(value.get()); 76 *result = WideToUTF8(value.get());
77 } 77 }
78 return true; 78 return true;
79 #else 79 #else
80 #error need to port 80 #error need to port
81 #endif 81 #endif
82 } 82 }
83 83
84 bool SetVarImpl(StringPiece variable_name, const std::string& new_value) { 84 bool SetVarImpl(StringPiece variable_name, const std::string& new_value) {
85 #if defined(OS_POSIX) 85 #if defined(OS_POSIX) || defined(OS_FUCHSIA)
86 // On success, zero is returned. 86 // On success, zero is returned.
87 return !setenv(variable_name.data(), new_value.c_str(), 1); 87 return !setenv(variable_name.data(), new_value.c_str(), 1);
88 #elif defined(OS_WIN) 88 #elif defined(OS_WIN)
89 // On success, a nonzero value is returned. 89 // On success, a nonzero value is returned.
90 return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), 90 return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(),
91 UTF8ToWide(new_value).c_str()); 91 UTF8ToWide(new_value).c_str());
92 #endif 92 #endif
93 } 93 }
94 94
95 bool UnSetVarImpl(StringPiece variable_name) { 95 bool UnSetVarImpl(StringPiece variable_name) {
96 #if defined(OS_POSIX) 96 #if defined(OS_POSIX) || defined(OS_FUCHSIA)
97 // On success, zero is returned. 97 // On success, zero is returned.
98 return !unsetenv(variable_name.data()); 98 return !unsetenv(variable_name.data());
99 #elif defined(OS_WIN) 99 #elif defined(OS_WIN)
100 // On success, a nonzero value is returned. 100 // On success, a nonzero value is returned.
101 return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), nullptr); 101 return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), nullptr);
102 #endif 102 #endif
103 } 103 }
104 }; 104 };
105 105
106 // Parses a null-terminated input string of an environment block. The key is 106 // Parses a null-terminated input string of an environment block. The key is
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 } 176 }
177 177
178 // An additional null marks the end of the list. We always need a double-null 178 // An additional null marks the end of the list. We always need a double-null
179 // in case nothing was added above. 179 // in case nothing was added above.
180 if (result.empty()) 180 if (result.empty())
181 result.push_back(0); 181 result.push_back(0);
182 result.push_back(0); 182 result.push_back(0);
183 return result; 183 return result;
184 } 184 }
185 185
186 #elif defined(OS_POSIX) 186 #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
187 187
188 std::unique_ptr<char* []> AlterEnvironment(const char* const* const env, 188 std::unique_ptr<char* []> AlterEnvironment(const char* const* const env,
189 const EnvironmentMap& changes) { 189 const EnvironmentMap& changes) {
190 std::string value_storage; // Holds concatenated null-terminated strings. 190 std::string value_storage; // Holds concatenated null-terminated strings.
191 std::vector<size_t> result_indices; // Line indices into value_storage. 191 std::vector<size_t> result_indices; // Line indices into value_storage.
192 192
193 // First build up all of the unchanged environment strings. These are 193 // First build up all of the unchanged environment strings. These are
194 // null-terminated of the form "key=value". 194 // null-terminated of the form "key=value".
195 std::string key; 195 std::string key;
196 for (size_t i = 0; env[i]; i++) { 196 for (size_t i = 0; env[i]; i++) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 for (size_t i = 0; i < result_indices.size(); i++) 231 for (size_t i = 0; i < result_indices.size(); i++)
232 result[i] = &storage_data[result_indices[i]]; 232 result[i] = &storage_data[result_indices[i]];
233 result[result_indices.size()] = 0; // Null terminator. 233 result[result_indices.size()] = 0; // Null terminator.
234 234
235 return result; 235 return result;
236 } 236 }
237 237
238 #endif // OS_POSIX 238 #endif // OS_POSIX
239 239
240 } // namespace base 240 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698