From: Joel Rosdahl Date: Thu, 7 Nov 2024 20:50:52 +0000 (+0100) Subject: enhance: Make util::getenv_path differentiate between unset and empty X-Git-Tag: v4.11~51 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=fa7f9407822b3be43a1f4d219ac2d6c07e26dc1a;p=thirdparty%2Fccache.git enhance: Make util::getenv_path differentiate between unset and empty --- diff --git a/src/ccache/util/environment.cpp b/src/ccache/util/environment.cpp index f3c01009..5987bd1f 100644 --- a/src/ccache/util/environment.cpp +++ b/src/ccache/util/environment.cpp @@ -85,20 +85,20 @@ expand_environment_variables(const std::string& str) #ifdef _WIN32 -static std::wstring +static std::optional wide_getenv(const char* name) { std::vector wname(strlen(name) + 1); size_t n = mbstowcs(wname.data(), name, wname.size()); if (n == static_cast(-1)) { - return {}; + return std::nullopt; } std::vector value(1024); auto len = GetEnvironmentVariableW(wname.data(), value.data(), value.size()); if (len == 0) { // Variable not set. - return {}; + return std::nullopt; } if (len >= value.size()) { // len is the number of needed characters including the terminating null. @@ -109,22 +109,23 @@ wide_getenv(const char* name) return std::wstring(value.data(), len); } -fs::path +std::optional getenv_path(const char* name) { - return fs::path(wide_getenv(name)); + auto value = wide_getenv(name); + return value ? std::optional(fs::path(*value)) : std::nullopt; } std::vector getenv_path_list(const char* name) { - std::wstring value = wide_getenv(name); - if (value.empty()) { + auto value = wide_getenv(name); + if (!value) { return {}; } std::vector result; - std::wstring_view view(value); + std::wstring_view view(*value); size_t left = 0; while (left < view.size()) { size_t right = view.find(';', left); @@ -145,11 +146,11 @@ getenv_path_list(const char* name) #else // _WIN32 -fs::path +std::optional getenv_path(const char* name) { const char* value = getenv(name); - return value ? value : ""; + return value ? std::optional(value) : std::nullopt; } std::vector diff --git a/src/ccache/util/environment.hpp b/src/ccache/util/environment.hpp index 252fea9e..a81cfbc2 100644 --- a/src/ccache/util/environment.hpp +++ b/src/ccache/util/environment.hpp @@ -21,6 +21,7 @@ #include #include +#include #include #include @@ -32,7 +33,7 @@ tl::expected expand_environment_variables(const std::string& str); // Get value of environment variable `name` as a path. -std::filesystem::path getenv_path(const char* name); +std::optional getenv_path(const char* name); // Get value of environment variable `name` as a vector of paths where the value // is delimited by ';' on Windows and ':' on other systems..