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

Side by Side Diff: base/env_var.cc

Issue 2843048: base: Add SetEnv() to EnvVarGetter class and get rid of the some ifdefs. (Closed) Base URL: git://git.chromium.org/chromium.git
Patch Set: filename Created 10 years, 5 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/env_var.h ('k') | base/xdg_util_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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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/env_var.h" 5 #include "base/env_var.h"
6 6
7 #if defined(OS_POSIX) 7 #if defined(OS_POSIX)
8 #include <stdlib.h> 8 #include <stdlib.h>
9 #elif defined(OS_WIN) 9 #elif defined(OS_WIN)
10 #include <windows.h> 10 #include <windows.h>
11 #endif 11 #endif
12 12
13 #include "base/string_util.h" 13 #include "base/string_util.h"
14 14
15 #if defined(OS_WIN) 15 #if defined(OS_WIN)
16 #include "base/scoped_ptr.h" 16 #include "base/scoped_ptr.h"
17 #include "base/utf_string_conversions.h" 17 #include "base/utf_string_conversions.h"
18 #endif 18 #endif
19 19
20 namespace { 20 namespace {
21 21
22 class EnvVarGetterImpl 22 class EnvVarGetterImpl : public base::EnvVarGetter {
23 : public base::EnvVarGetter {
24 public: 23 public:
25 virtual bool GetEnv(const char* variable_name, std::string* result) { 24 virtual bool GetEnv(const char* variable_name, std::string* result) {
26 if (GetEnvImpl(variable_name, result)) 25 if (GetEnvImpl(variable_name, result))
27 return true; 26 return true;
28 27
29 // Some commonly used variable names are uppercase while others 28 // Some commonly used variable names are uppercase while others
30 // are lowercase, which is inconsistent. Let's try to be helpful 29 // are lowercase, which is inconsistent. Let's try to be helpful
31 // and look for a variable name with the reverse case. 30 // and look for a variable name with the reverse case.
32 // I.e. HTTP_PROXY may be http_proxy for some users/systems. 31 // I.e. HTTP_PROXY may be http_proxy for some users/systems.
33 char first_char = variable_name[0]; 32 char first_char = variable_name[0];
34 std::string alternate_case_var; 33 std::string alternate_case_var;
35 if (first_char >= 'a' && first_char <= 'z') 34 if (first_char >= 'a' && first_char <= 'z')
36 alternate_case_var = StringToUpperASCII(std::string(variable_name)); 35 alternate_case_var = StringToUpperASCII(std::string(variable_name));
37 else if (first_char >= 'A' && first_char <= 'Z') 36 else if (first_char >= 'A' && first_char <= 'Z')
38 alternate_case_var = StringToLowerASCII(std::string(variable_name)); 37 alternate_case_var = StringToLowerASCII(std::string(variable_name));
39 else 38 else
40 return false; 39 return false;
41 return GetEnvImpl(alternate_case_var.c_str(), result); 40 return GetEnvImpl(alternate_case_var.c_str(), result);
42 } 41 }
42
43 virtual void SetEnv(const char* variable_name, const std::string& new_value) {
44 SetEnvImpl(variable_name, new_value);
45 }
46
43 private: 47 private:
44 bool GetEnvImpl(const char* variable_name, std::string* result) { 48 bool GetEnvImpl(const char* variable_name, std::string* result) {
45 #if defined(OS_POSIX) 49 #if defined(OS_POSIX)
46 const char* env_value = getenv(variable_name); 50 const char* env_value = getenv(variable_name);
47 if (!env_value) 51 if (!env_value)
48 return false; 52 return false;
49 // Note that the variable may be defined but empty. 53 // Note that the variable may be defined but empty.
50 if (result) 54 if (result)
51 *result = env_value; 55 *result = env_value;
52 return true; 56 return true;
53 #elif defined(OS_WIN) 57 #elif defined(OS_WIN)
54 DWORD value_length = ::GetEnvironmentVariable( 58 DWORD value_length = ::GetEnvironmentVariable(
55 UTF8ToWide(variable_name).c_str(), NULL, 0); 59 UTF8ToWide(variable_name).c_str(), NULL, 0);
56 if (value_length == 0) 60 if (value_length == 0)
57 return false; 61 return false;
58 if (result) { 62 if (result) {
59 scoped_array<wchar_t> value(new wchar_t[value_length]); 63 scoped_array<wchar_t> value(new wchar_t[value_length]);
60 ::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), value.get(), 64 ::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), value.get(),
61 value_length); 65 value_length);
62 *result = WideToUTF8(value.get()); 66 *result = WideToUTF8(value.get());
63 } 67 }
64 return true; 68 return true;
65 #else 69 #else
66 #error need to port 70 #error need to port
67 #endif 71 #endif
68 } 72 }
73
74 void SetEnvImpl(const char* variable_name, const std::string& new_value) {
75 #if defined(OS_POSIX)
76 setenv(variable_name, new_value.c_str(), 1);
77 #elif defined(OS_WIN)
78 ::SetEnvironmentVariable(ASCIIToWide(variable_name).c_str(),
79 ASCIIToWide(new_value).c_str());
80 #endif
81 }
69 }; 82 };
70 83
71 } // namespace 84 } // namespace
72 85
73 namespace base { 86 namespace base {
74 87
75 // static 88 // static
76 EnvVarGetter* EnvVarGetter::Create() { 89 EnvVarGetter* EnvVarGetter::Create() {
77 return new EnvVarGetterImpl(); 90 return new EnvVarGetterImpl();
78 } 91 }
79 92
80 } // namespace base 93 } // namespace base
OLDNEW
« no previous file with comments | « base/env_var.h ('k') | base/xdg_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698