OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/env_var.h" |
| 6 |
| 7 #if defined(OS_POSIX) |
| 8 #include <stdlib.h> |
| 9 #elif defined(OS_WIN) |
| 10 #include <windows.h> |
| 11 #endif |
| 12 |
| 13 #include "base/string_util.h" |
| 14 |
| 15 #if defined(OS_WIN) |
| 16 #include "base/scoped_ptr.h" |
| 17 #include "base/utf_string_conversions.h" |
| 18 #endif |
| 19 |
| 20 namespace { |
| 21 |
| 22 class EnvVarGetterImpl |
| 23 : public base::EnvVarGetter { |
| 24 public: |
| 25 virtual bool GetEnv(const char* variable_name, std::string* result) { |
| 26 if (GetEnvImpl(variable_name, result)) |
| 27 return true; |
| 28 |
| 29 // Some commonly used variable names are uppercase while others |
| 30 // are lowercase, which is inconsistent. Let's try to be helpful |
| 31 // and look for a variable name with the reverse case. |
| 32 // I.e. HTTP_PROXY may be http_proxy for some users/systems. |
| 33 char first_char = variable_name[0]; |
| 34 std::string alternate_case_var; |
| 35 if (first_char >= 'a' && first_char <= 'z') |
| 36 alternate_case_var = StringToUpperASCII(std::string(variable_name)); |
| 37 else if (first_char >= 'A' && first_char <= 'Z') |
| 38 alternate_case_var = StringToLowerASCII(std::string(variable_name)); |
| 39 else |
| 40 return false; |
| 41 return GetEnvImpl(alternate_case_var.c_str(), result); |
| 42 } |
| 43 private: |
| 44 bool GetEnvImpl(const char* variable_name, std::string* result) { |
| 45 #if defined(OS_POSIX) |
| 46 const char* env_value = getenv(variable_name); |
| 47 if (!env_value) |
| 48 return false; |
| 49 // Note that the variable may be defined but empty. |
| 50 if (result) |
| 51 *result = env_value; |
| 52 return true; |
| 53 #elif defined(OS_WIN) |
| 54 DWORD value_length = ::GetEnvironmentVariable( |
| 55 UTF8ToWide(variable_name).c_str(), NULL, 0); |
| 56 if (value_length == 0) |
| 57 return false; |
| 58 if (result) { |
| 59 scoped_array<wchar_t> value(new wchar_t[value_length]); |
| 60 ::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), value.get(), |
| 61 value_length); |
| 62 *result = WideToUTF8(value.get()); |
| 63 } |
| 64 return true; |
| 65 #else |
| 66 #error need to port |
| 67 #endif |
| 68 } |
| 69 }; |
| 70 |
| 71 } // namespace |
| 72 |
| 73 namespace base { |
| 74 |
| 75 // static |
| 76 EnvVarGetter* EnvVarGetter::Create() { |
| 77 return new EnvVarGetterImpl(); |
| 78 } |
| 79 |
| 80 } // namespace base |
OLD | NEW |