OLD | NEW |
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/environment.h" | 5 #include "base/environment.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 EnvironmentImpl : public base::Environment { | 22 class EnvironmentImpl : public base::Environment { |
23 public: | 23 public: |
24 virtual bool GetEnv(const char* variable_name, std::string* result) { | 24 virtual bool GetVar(const char* variable_name, std::string* result) { |
25 if (GetEnvImpl(variable_name, result)) | 25 if (GetVarImpl(variable_name, result)) |
26 return true; | 26 return true; |
27 | 27 |
28 // Some commonly used variable names are uppercase while others | 28 // Some commonly used variable names are uppercase while others |
29 // are lowercase, which is inconsistent. Let's try to be helpful | 29 // are lowercase, which is inconsistent. Let's try to be helpful |
30 // and look for a variable name with the reverse case. | 30 // and look for a variable name with the reverse case. |
31 // 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. |
32 char first_char = variable_name[0]; | 32 char first_char = variable_name[0]; |
33 std::string alternate_case_var; | 33 std::string alternate_case_var; |
34 if (first_char >= 'a' && first_char <= 'z') | 34 if (first_char >= 'a' && first_char <= 'z') |
35 alternate_case_var = StringToUpperASCII(std::string(variable_name)); | 35 alternate_case_var = StringToUpperASCII(std::string(variable_name)); |
36 else if (first_char >= 'A' && first_char <= 'Z') | 36 else if (first_char >= 'A' && first_char <= 'Z') |
37 alternate_case_var = StringToLowerASCII(std::string(variable_name)); | 37 alternate_case_var = StringToLowerASCII(std::string(variable_name)); |
38 else | 38 else |
39 return false; | 39 return false; |
40 return GetEnvImpl(alternate_case_var.c_str(), result); | 40 return GetVarImpl(alternate_case_var.c_str(), result); |
41 } | 41 } |
42 | 42 |
43 virtual bool SetVar(const char* variable_name, const std::string& new_value) { | 43 virtual bool SetVar(const char* variable_name, const std::string& new_value) { |
44 return SetVarImpl(variable_name, new_value); | 44 return SetVarImpl(variable_name, new_value); |
45 } | 45 } |
46 | 46 |
47 virtual bool UnSetVar(const char* variable_name) { | 47 virtual bool UnSetVar(const char* variable_name) { |
48 return UnSetVarImpl(variable_name); | 48 return UnSetVarImpl(variable_name); |
49 } | 49 } |
50 | 50 |
51 private: | 51 private: |
52 bool GetEnvImpl(const char* variable_name, std::string* result) { | 52 bool GetVarImpl(const char* variable_name, std::string* result) { |
53 #if defined(OS_POSIX) | 53 #if defined(OS_POSIX) |
54 const char* env_value = getenv(variable_name); | 54 const char* env_value = getenv(variable_name); |
55 if (!env_value) | 55 if (!env_value) |
56 return false; | 56 return false; |
57 // Note that the variable may be defined but empty. | 57 // Note that the variable may be defined but empty. |
58 if (result) | 58 if (result) |
59 *result = env_value; | 59 *result = env_value; |
60 return true; | 60 return true; |
61 #elif defined(OS_WIN) | 61 #elif defined(OS_WIN) |
62 DWORD value_length = ::GetEnvironmentVariable( | 62 DWORD value_length = ::GetEnvironmentVariable( |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 } // namespace env_vars | 113 } // namespace env_vars |
114 | 114 |
115 Environment::~Environment() {} | 115 Environment::~Environment() {} |
116 | 116 |
117 // static | 117 // static |
118 Environment* Environment::Create() { | 118 Environment* Environment::Create() { |
119 return new EnvironmentImpl(); | 119 return new EnvironmentImpl(); |
120 } | 120 } |
121 | 121 |
122 bool Environment::HasVar(const char* variable_name) { | 122 bool Environment::HasVar(const char* variable_name) { |
123 return GetEnv(variable_name, NULL); | 123 return GetVar(variable_name, NULL); |
124 } | 124 } |
125 | 125 |
126 } // namespace base | 126 } // namespace base |
OLD | NEW |