forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move EnvironmentVariableGetter from base/linux_util.h to base/env_var…
….h and rename it EnvVarGetter. Label base::SysInfo::{Get,Has}EnvVar as deprecated. BUG=none TEST=none Review URL: http://codereview.chromium.org/1606007 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@43559 0039d316-1c4b-4281-b951-d872f2087c98
- Loading branch information
thestig@chromium.org
committed
Apr 3, 2010
1 parent
d5c8101
commit 9bc8cff
Showing
19 changed files
with
242 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (c) 2010 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "base/env_var.h" | ||
|
||
#if defined(OS_POSIX) | ||
#include <stdlib.h> | ||
#elif defined(OS_WIN) | ||
#include <windows.h> | ||
#endif | ||
|
||
#include "base/string_util.h" | ||
|
||
#if defined(OS_WIN) | ||
#include "base/scoped_ptr.h" | ||
#include "base/utf_string_conversions.h" | ||
#endif | ||
|
||
namespace { | ||
|
||
class EnvVarGetterImpl | ||
: public base::EnvVarGetter { | ||
public: | ||
virtual bool GetEnv(const char* variable_name, std::string* result) { | ||
if (GetEnvImpl(variable_name, result)) | ||
return true; | ||
|
||
// Some commonly used variable names are uppercase while others | ||
// are lowercase, which is inconsistent. Let's try to be helpful | ||
// and look for a variable name with the reverse case. | ||
// I.e. HTTP_PROXY may be http_proxy for some users/systems. | ||
char first_char = variable_name[0]; | ||
std::string alternate_case_var; | ||
if (first_char >= 'a' && first_char <= 'z') | ||
alternate_case_var = StringToUpperASCII(std::string(variable_name)); | ||
else if (first_char >= 'A' && first_char <= 'Z') | ||
alternate_case_var = StringToLowerASCII(std::string(variable_name)); | ||
else | ||
return false; | ||
return GetEnvImpl(alternate_case_var.c_str(), result); | ||
} | ||
private: | ||
bool GetEnvImpl(const char* variable_name, std::string* result) { | ||
#if defined(OS_POSIX) | ||
const char* env_value = getenv(variable_name); | ||
if (!env_value) | ||
return false; | ||
// Note that the variable may be defined but empty. | ||
if (result) | ||
*result = env_value; | ||
return true; | ||
#elif defined(OS_WIN) | ||
DWORD value_length = ::GetEnvironmentVariable( | ||
UTF8ToWide(variable_name).c_str(), NULL, 0); | ||
if (value_length == 0) | ||
return false; | ||
if (result) { | ||
scoped_array<wchar_t> value(new wchar_t[value_length]); | ||
::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), value.get(), | ||
value_length); | ||
*result = WideToUTF8(value.get()); | ||
} | ||
return true; | ||
#else | ||
#error need to port | ||
#endif | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
namespace base { | ||
|
||
// static | ||
EnvVarGetter* EnvVarGetter::Create() { | ||
return new EnvVarGetterImpl(); | ||
} | ||
|
||
} // namespace base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (c) 2010 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef BASE_ENV_VAR_H_ | ||
#define BASE_ENV_VAR_H_ | ||
|
||
#include <string> | ||
|
||
#include "base/basictypes.h" | ||
|
||
namespace base { | ||
|
||
// These are used to derive mocks for unittests. | ||
class EnvVarGetter { | ||
public: | ||
virtual ~EnvVarGetter() {} | ||
// Gets an environment variable's value and stores it in |result|. | ||
// Returns false if the key is unset. | ||
virtual bool GetEnv(const char* variable_name, std::string* result) = 0; | ||
|
||
// Syntactic sugar for GetEnv(variable_name, NULL); | ||
virtual bool HasEnv(const char* variable_name) { | ||
return GetEnv(variable_name, NULL); | ||
} | ||
|
||
// Create an instance of EnvVarGetter | ||
static EnvVarGetter* Create(); | ||
}; | ||
|
||
} // namespace base | ||
|
||
#endif // BASE_ENV_VAR_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.