This source file includes following definitions.
- GetVar
- SetVar
- UnSetVar
- GetVarImpl
- SetVarImpl
- UnSetVarImpl
- ParseEnvLine
- Create
- HasVar
- AlterEnvironment
- AlterEnvironment
#include "base/environment.h"
#include <vector>
#include "base/strings/string_piece.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#if defined(OS_POSIX)
#include <stdlib.h>
#elif defined(OS_WIN)
#include <windows.h>
#endif
namespace base {
namespace {
class EnvironmentImpl : public base::Environment {
public:
virtual bool GetVar(const char* variable_name,
std::string* result) OVERRIDE {
if (GetVarImpl(variable_name, result))
return true;
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 GetVarImpl(alternate_case_var.c_str(), result);
}
virtual bool SetVar(const char* variable_name,
const std::string& new_value) OVERRIDE {
return SetVarImpl(variable_name, new_value);
}
virtual bool UnSetVar(const char* variable_name) OVERRIDE {
return UnSetVarImpl(variable_name);
}
private:
bool GetVarImpl(const char* variable_name, std::string* result) {
#if defined(OS_POSIX)
const char* env_value = getenv(variable_name);
if (!env_value)
return false;
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_ptr<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
}
bool SetVarImpl(const char* variable_name, const std::string& new_value) {
#if defined(OS_POSIX)
return !setenv(variable_name, new_value.c_str(), 1);
#elif defined(OS_WIN)
return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(),
UTF8ToWide(new_value).c_str());
#endif
}
bool UnSetVarImpl(const char* variable_name) {
#if defined(OS_POSIX)
return !unsetenv(variable_name);
#elif defined(OS_WIN)
return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), NULL);
#endif
}
};
size_t ParseEnvLine(const NativeEnvironmentString::value_type* input,
NativeEnvironmentString* key) {
size_t cur = 0;
while (input[cur] && input[cur] != '=')
cur++;
*key = NativeEnvironmentString(&input[0], cur);
while (input[cur])
cur++;
return cur + 1;
}
}
namespace env_vars {
#if defined(OS_POSIX)
const char kHome[] = "HOME";
#endif
}
Environment::~Environment() {}
Environment* Environment::Create() {
return new EnvironmentImpl();
}
bool Environment::HasVar(const char* variable_name) {
return GetVar(variable_name, NULL);
}
#if defined(OS_WIN)
string16 AlterEnvironment(const wchar_t* env,
const EnvironmentMap& changes) {
string16 result;
size_t cur_env = 0;
string16 key;
while (env[cur_env]) {
const wchar_t* line = &env[cur_env];
size_t line_length = ParseEnvLine(line, &key);
EnvironmentMap::const_iterator found_change = changes.find(key);
if (found_change == changes.end())
result.append(line, line_length);
cur_env += line_length;
}
for (EnvironmentMap::const_iterator i = changes.begin();
i != changes.end(); ++i) {
if (!i->second.empty()) {
result.append(i->first);
result.push_back('=');
result.append(i->second);
result.push_back(0);
}
}
if (result.empty())
result.push_back(0);
result.push_back(0);
return result;
}
#elif defined(OS_POSIX)
scoped_ptr<char*[]> AlterEnvironment(const char* const* const env,
const EnvironmentMap& changes) {
std::string value_storage;
std::vector<size_t> result_indices;
std::string key;
for (size_t i = 0; env[i]; i++) {
size_t line_length = ParseEnvLine(env[i], &key);
EnvironmentMap::const_iterator found_change = changes.find(key);
if (found_change == changes.end()) {
result_indices.push_back(value_storage.size());
value_storage.append(env[i], line_length);
}
}
for (EnvironmentMap::const_iterator i = changes.begin();
i != changes.end(); ++i) {
if (!i->second.empty()) {
result_indices.push_back(value_storage.size());
value_storage.append(i->first);
value_storage.push_back('=');
value_storage.append(i->second);
value_storage.push_back(0);
}
}
size_t pointer_count_required =
result_indices.size() + 1 +
(value_storage.size() + sizeof(char*) - 1) / sizeof(char*);
scoped_ptr<char*[]> result(new char*[pointer_count_required]);
char* storage_data = reinterpret_cast<char*>(
&result.get()[result_indices.size() + 1]);
if (!value_storage.empty())
memcpy(storage_data, value_storage.data(), value_storage.size());
for (size_t i = 0; i < result_indices.size(); i++)
result[i] = &storage_data[result_indices[i]];
result[result_indices.size()] = 0;
return result.Pass();
}
#endif
}