// Copyright (c) 2012 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.
class EnvironmentImpl : public Environment { public: bool GetVar(StringPiece variable_name, std::string* result) override { if (GetVarImpl(variable_name, result)) returntrue;
// 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 (IsAsciiLower(first_char))
alternate_case_var = ToUpperASCII(variable_name); elseif (IsAsciiUpper(first_char))
alternate_case_var = ToLowerASCII(variable_name); else returnfalse; return GetVarImpl(alternate_case_var, result);
}
private: bool GetVarImpl(StringPiece variable_name, std::string* result) { #ifdefined(OS_WIN)
DWORD value_length =
::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), nullptr, 0); if (value_length == 0) returnfalse; if (result) {
std::unique_ptr<wchar_t[]> value(newwchar_t[value_length]);
::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), value.get(),
value_length);
*result = WideToUTF8(value.get());
} returntrue; #elifdefined(OS_POSIX) || defined(OS_FUCHSIA) constchar* env_value = getenv(variable_name.data()); if (!env_value) returnfalse; // Note that the variable may be defined but empty. if (result)
*result = env_value; returntrue; #endif
}
bool SetVarImpl(StringPiece variable_name, const std::string& new_value) { #ifdefined(OS_WIN) // On success, a nonzero value is returned. return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(),
UTF8ToWide(new_value).c_str()); #elifdefined(OS_POSIX) || defined(OS_FUCHSIA) // On success, zero is returned. return !setenv(variable_name.data(), new_value.c_str(), 1); #endif
}
bool UnSetVarImpl(StringPiece variable_name) { #ifdefined(OS_WIN) // On success, a nonzero value is returned. return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), nullptr); #elifdefined(OS_POSIX) || defined(OS_FUCHSIA) // On success, zero is returned. return !unsetenv(variable_name.data()); #endif
}
};
} // namespace
namespace env_vars {
#ifdefined(OS_POSIX) || defined(OS_FUCHSIA) // On Posix systems, this variable contains the location of the user's home // directory. (e.g, /home/username/). constchar kHome[] = "HOME"; #endif
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.