1
0
Fork 0

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

pull/14191/head
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

@ -733,8 +733,8 @@ static std::string GetHomeDir()
find_directory(B_USER_SETTINGS_DIRECTORY, &path);
return std::string(path.Path());
#else
const char *home_env = std::getenv("HOME"); // Stack var, shouldn't be freed
if (home_env != nullptr) return std::string(home_env);
auto home_env = GetEnv("HOME"); // Stack var, shouldn't be freed
if (home_env.has_value()) return std::string(*home_env);
const struct passwd *pw = getpwuid(getuid());
if (pw != nullptr) return std::string(pw->pw_dir);
@ -751,9 +751,8 @@ void DetermineBasePaths(std::string_view exe)
std::string tmp;
const std::string homedir = GetHomeDir();
#ifdef USE_XDG
const char *xdg_data_home = std::getenv("XDG_DATA_HOME");
if (xdg_data_home != nullptr) {
tmp = xdg_data_home;
if (auto xdg_data_home = GetEnv("XDG_DATA_HOME"); xdg_data_home.has_value()) {
tmp = *xdg_data_home;
tmp += PATHSEP;
tmp += PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
AppendPathSeparator(tmp);
@ -882,9 +881,8 @@ void DeterminePaths(std::string_view exe, bool only_local_path)
#ifdef USE_XDG
std::string config_home;
std::string homedir = GetHomeDir();
const char *xdg_config_home = std::getenv("XDG_CONFIG_HOME");
if (xdg_config_home != nullptr) {
config_home = xdg_config_home;
if (auto xdg_config_home = GetEnv("XDG_CONFIG_HOME"); xdg_config_home.has_value()) {
config_home = *xdg_config_home;
config_home += PATHSEP;
config_home += PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
} else if (!homedir.empty()) {

View File

@ -15,18 +15,6 @@
#include "../../safeguards.h"
/**
* Get the environment variable using std::getenv and when it is an empty string (or nullptr), return a fallback value instead.
* @param variable The environment variable to read from.
* @param fallback The fallback in case the environment variable is not set.
* @return The environment value, or when that does not exist the given fallback value.
*/
static std::string_view GetEnv(const char *variable, std::string_view fallback)
{
const char *value = std::getenv(variable);
return StrEmpty(value) ? fallback : value;
}
/**
* Get the connection string for the game coordinator from the environment variable OTTD_COORDINATOR_CS,
* or when it has not been set a hard coded default DNS hostname of the production server.
@ -34,7 +22,7 @@ static std::string_view GetEnv(const char *variable, std::string_view fallback)
*/
std::string_view NetworkCoordinatorConnectionString()
{
return GetEnv("OTTD_COORDINATOR_CS", "coordinator.openttd.org");
return GetEnv("OTTD_COORDINATOR_CS").value_or("coordinator.openttd.org");
}
/**
@ -44,7 +32,7 @@ std::string_view NetworkCoordinatorConnectionString()
*/
std::string_view NetworkStunConnectionString()
{
return GetEnv("OTTD_STUN_CS", "stun.openttd.org");
return GetEnv("OTTD_STUN_CS").value_or("stun.openttd.org");
}
/**
@ -54,7 +42,7 @@ std::string_view NetworkStunConnectionString()
*/
std::string_view NetworkContentServerConnectionString()
{
return GetEnv("OTTD_CONTENT_SERVER_CS", "content.openttd.org");
return GetEnv("OTTD_CONTENT_SERVER_CS").value_or("content.openttd.org");
}
/**
@ -64,7 +52,7 @@ std::string_view NetworkContentServerConnectionString()
*/
std::string_view NetworkContentMirrorUriString()
{
return GetEnv("OTTD_CONTENT_MIRROR_URI", "https://binaries.openttd.org/bananas");
return GetEnv("OTTD_CONTENT_MIRROR_URI").value_or("https://binaries.openttd.org/bananas");
}
/**
@ -74,5 +62,5 @@ std::string_view NetworkContentMirrorUriString()
*/
std::string_view NetworkSurveyUriString()
{
return GetEnv("OTTD_SURVEY_URI", "https://survey-participate.openttd.org/");
return GetEnv("OTTD_SURVEY_URI").value_or("https://survey-participate.openttd.org/");
}

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;
}

View File

@ -167,4 +167,6 @@ inline bool IsWhitespace(char32_t c)
#include <sys/param.h>
#endif
std::optional<std::string_view> GetEnv(const char *variable);
#endif /* STRING_FUNC_H */