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

Side by Side Diff: base/environment.cc

Issue 1852433005: Convert //base to use std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase after r384946 Created 4 years, 8 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
« no previous file with comments | « base/environment.h ('k') | base/environment_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 // Note that the variable may be defined but empty. 62 // Note that the variable may be defined but empty.
63 if (result) 63 if (result)
64 *result = env_value; 64 *result = env_value;
65 return true; 65 return true;
66 #elif defined(OS_WIN) 66 #elif defined(OS_WIN)
67 DWORD value_length = ::GetEnvironmentVariable( 67 DWORD value_length = ::GetEnvironmentVariable(
68 UTF8ToWide(variable_name).c_str(), NULL, 0); 68 UTF8ToWide(variable_name).c_str(), NULL, 0);
69 if (value_length == 0) 69 if (value_length == 0)
70 return false; 70 return false;
71 if (result) { 71 if (result) {
72 scoped_ptr<wchar_t[]> value(new wchar_t[value_length]); 72 std::unique_ptr<wchar_t[]> value(new wchar_t[value_length]);
73 ::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), value.get(), 73 ::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), value.get(),
74 value_length); 74 value_length);
75 *result = WideToUTF8(value.get()); 75 *result = WideToUTF8(value.get());
76 } 76 }
77 return true; 77 return true;
78 #else 78 #else
79 #error need to port 79 #error need to port
80 #endif 80 #endif
81 } 81 }
82 82
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 // An additional null marks the end of the list. We always need a double-null 177 // An additional null marks the end of the list. We always need a double-null
178 // in case nothing was added above. 178 // in case nothing was added above.
179 if (result.empty()) 179 if (result.empty())
180 result.push_back(0); 180 result.push_back(0);
181 result.push_back(0); 181 result.push_back(0);
182 return result; 182 return result;
183 } 183 }
184 184
185 #elif defined(OS_POSIX) 185 #elif defined(OS_POSIX)
186 186
187 scoped_ptr<char*[]> AlterEnvironment(const char* const* const env, 187 std::unique_ptr<char* []> AlterEnvironment(const char* const* const env,
188 const EnvironmentMap& changes) { 188 const EnvironmentMap& changes) {
189 std::string value_storage; // Holds concatenated null-terminated strings. 189 std::string value_storage; // Holds concatenated null-terminated strings.
190 std::vector<size_t> result_indices; // Line indices into value_storage. 190 std::vector<size_t> result_indices; // Line indices into value_storage.
191 191
192 // First build up all of the unchanged environment strings. These are 192 // First build up all of the unchanged environment strings. These are
193 // null-terminated of the form "key=value". 193 // null-terminated of the form "key=value".
194 std::string key; 194 std::string key;
195 for (size_t i = 0; env[i]; i++) { 195 for (size_t i = 0; env[i]; i++) {
196 size_t line_length = ParseEnvLine(env[i], &key); 196 size_t line_length = ParseEnvLine(env[i], &key);
197 197
198 // Keep only values not specified in the change vector. 198 // Keep only values not specified in the change vector.
(...skipping 12 matching lines...) Expand all
211 value_storage.append(i->first); 211 value_storage.append(i->first);
212 value_storage.push_back('='); 212 value_storage.push_back('=');
213 value_storage.append(i->second); 213 value_storage.append(i->second);
214 value_storage.push_back(0); 214 value_storage.push_back(0);
215 } 215 }
216 } 216 }
217 217
218 size_t pointer_count_required = 218 size_t pointer_count_required =
219 result_indices.size() + 1 + // Null-terminated array of pointers. 219 result_indices.size() + 1 + // Null-terminated array of pointers.
220 (value_storage.size() + sizeof(char*) - 1) / sizeof(char*); // Buffer. 220 (value_storage.size() + sizeof(char*) - 1) / sizeof(char*); // Buffer.
221 scoped_ptr<char*[]> result(new char*[pointer_count_required]); 221 std::unique_ptr<char* []> result(new char*[pointer_count_required]);
222 222
223 // The string storage goes after the array of pointers. 223 // The string storage goes after the array of pointers.
224 char* storage_data = reinterpret_cast<char*>( 224 char* storage_data = reinterpret_cast<char*>(
225 &result.get()[result_indices.size() + 1]); 225 &result.get()[result_indices.size() + 1]);
226 if (!value_storage.empty()) 226 if (!value_storage.empty())
227 memcpy(storage_data, value_storage.data(), value_storage.size()); 227 memcpy(storage_data, value_storage.data(), value_storage.size());
228 228
229 // Fill array of pointers at the beginning of the result. 229 // Fill array of pointers at the beginning of the result.
230 for (size_t i = 0; i < result_indices.size(); i++) 230 for (size_t i = 0; i < result_indices.size(); i++)
231 result[i] = &storage_data[result_indices[i]]; 231 result[i] = &storage_data[result_indices[i]];
232 result[result_indices.size()] = 0; // Null terminator. 232 result[result_indices.size()] = 0; // Null terminator.
233 233
234 return result; 234 return result;
235 } 235 }
236 236
237 #endif // OS_POSIX 237 #endif // OS_POSIX
238 238
239 } // namespace base 239 } // namespace base
OLDNEW
« no previous file with comments | « base/environment.h ('k') | base/environment_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698