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/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> |
(...skipping 26 matching lines...) Expand all Loading... |
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 GetEnvImpl(alternate_case_var.c_str(), result); |
41 } | 41 } |
42 | 42 |
43 virtual bool SetEnv(const char* variable_name, const std::string& new_value) { | 43 virtual bool SetEnv(const char* variable_name, const std::string& new_value) { |
44 return SetEnvImpl(variable_name, new_value); | 44 return SetEnvImpl(variable_name, new_value); |
45 } | 45 } |
46 | 46 |
| 47 virtual bool UnSetEnv(const char* variable_name) { |
| 48 return UnSetEnvImpl(variable_name); |
| 49 } |
| 50 |
47 private: | 51 private: |
48 bool GetEnvImpl(const char* variable_name, std::string* result) { | 52 bool GetEnvImpl(const char* variable_name, std::string* result) { |
49 #if defined(OS_POSIX) | 53 #if defined(OS_POSIX) |
50 const char* env_value = getenv(variable_name); | 54 const char* env_value = getenv(variable_name); |
51 if (!env_value) | 55 if (!env_value) |
52 return false; | 56 return false; |
53 // Note that the variable may be defined but empty. | 57 // Note that the variable may be defined but empty. |
54 if (result) | 58 if (result) |
55 *result = env_value; | 59 *result = env_value; |
56 return true; | 60 return true; |
(...skipping 17 matching lines...) Expand all Loading... |
74 bool SetEnvImpl(const char* variable_name, const std::string& new_value) { | 78 bool SetEnvImpl(const char* variable_name, const std::string& new_value) { |
75 #if defined(OS_POSIX) | 79 #if defined(OS_POSIX) |
76 // On success, zero is returned. | 80 // On success, zero is returned. |
77 return setenv(variable_name, new_value.c_str(), 1) == 0; | 81 return setenv(variable_name, new_value.c_str(), 1) == 0; |
78 #elif defined(OS_WIN) | 82 #elif defined(OS_WIN) |
79 // On success, a nonzero is returned. | 83 // On success, a nonzero is returned. |
80 return ::SetEnvironmentVariable(ASCIIToWide(variable_name).c_str(), | 84 return ::SetEnvironmentVariable(ASCIIToWide(variable_name).c_str(), |
81 ASCIIToWide(new_value).c_str()) != 0; | 85 ASCIIToWide(new_value).c_str()) != 0; |
82 #endif | 86 #endif |
83 } | 87 } |
| 88 |
| 89 bool UnSetEnvImpl(const char* variable_name) { |
| 90 #if defined(OS_POSIX) |
| 91 // On success, zero is returned. |
| 92 return unsetenv(variable_name) == 0; |
| 93 #elif defined(OS_WIN) |
| 94 // On success, a nonzero is returned. |
| 95 return ::SetEnvironmentVariable(ASCIIToWide(variable_name).c_str(), |
| 96 NULL) != 0; |
| 97 #endif |
| 98 } |
84 }; | 99 }; |
85 | 100 |
86 } // namespace | 101 } // namespace |
87 | 102 |
88 namespace base { | 103 namespace base { |
89 | 104 |
90 namespace env_vars { | 105 namespace env_vars { |
91 | 106 |
92 #if defined(OS_POSIX) | 107 #if defined(OS_POSIX) |
93 // On Posix systems, this variable contains the location of the user's home | 108 // On Posix systems, this variable contains the location of the user's home |
94 // directory. (e.g, /home/username/). | 109 // directory. (e.g, /home/username/). |
95 const char kHome[] = "HOME"; | 110 const char kHome[] = "HOME"; |
96 #endif | 111 #endif |
97 | 112 |
98 } // namespace env_vars | 113 } // namespace env_vars |
99 | 114 |
100 EnvVarGetter::~EnvVarGetter() {} | 115 EnvVarGetter::~EnvVarGetter() {} |
101 | 116 |
102 bool EnvVarGetter::HasEnv(const char* variable_name) { | |
103 return GetEnv(variable_name, NULL); | |
104 } | |
105 | |
106 // static | 117 // static |
107 EnvVarGetter* EnvVarGetter::Create() { | 118 EnvVarGetter* EnvVarGetter::Create() { |
108 return new EnvVarGetterImpl(); | 119 return new EnvVarGetterImpl(); |
109 } | 120 } |
110 | 121 |
| 122 bool EnvVarGetter::HasEnv(const char* variable_name) { |
| 123 return GetEnv(variable_name, NULL); |
| 124 } |
| 125 |
111 } // namespace base | 126 } // namespace base |
OLD | NEW |