1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-16 08:52:40 +01:00

Codechange: introduce GetEnv that returns optional based on std::getenv

This commit is contained in:
Rubidium
2025-05-03 11:44:34 +02:00
committed by rubidium42
parent 04a6a55e94
commit 96fd291693
4 changed files with 25 additions and 25 deletions

View File

@@ -851,3 +851,15 @@ public:
#endif /* defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN) */
#endif
/**
* Get the environment variable using std::getenv and when it is an empty string (or nullptr), return \c std::nullopt instead.
* @param variable The environment variable to read from.
* @return The environment value, or \c std::nullopt.
*/
std::optional<std::string_view> GetEnv(const char *variable)
{
auto val = std::getenv(variable);
if (val == nullptr || *val == '\0') return std::nullopt;
return val;
}