#ifdef _WIN32
-static std::wstring
+static std::optional<std::wstring>
wide_getenv(const char* name)
{
std::vector<wchar_t> wname(strlen(name) + 1);
size_t n = mbstowcs(wname.data(), name, wname.size());
if (n == static_cast<size_t>(-1)) {
- return {};
+ return std::nullopt;
}
std::vector<wchar_t> 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.
return std::wstring(value.data(), len);
}
-fs::path
+std::optional<fs::path>
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<fs::path>
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<fs::path> 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);
#else // _WIN32
-fs::path
+std::optional<fs::path>
getenv_path(const char* name)
{
const char* value = getenv(name);
- return value ? value : "";
+ return value ? std::optional(value) : std::nullopt;
}
std::vector<fs::path>
#include <tl/expected.hpp>
#include <filesystem>
+#include <optional>
#include <string>
#include <vector>
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<std::filesystem::path> 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..