From 96fd2916933df7ed05accf68a31a91c7ce192194 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sat, 3 May 2025 11:44:34 +0200 Subject: [PATCH] Codechange: introduce GetEnv that returns optional based on std::getenv --- src/fileio.cpp | 14 ++++++-------- src/network/core/config.cpp | 22 +++++----------------- src/string.cpp | 12 ++++++++++++ src/string_func.h | 2 ++ 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/fileio.cpp b/src/fileio.cpp index 6d356983f4..c7bfd345ac 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -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()) { diff --git a/src/network/core/config.cpp b/src/network/core/config.cpp index 2ff06a6312..fbadb6818f 100644 --- a/src/network/core/config.cpp +++ b/src/network/core/config.cpp @@ -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/"); } diff --git a/src/string.cpp b/src/string.cpp index e28cc67f5f..ba35223567 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -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 GetEnv(const char *variable) +{ + auto val = std::getenv(variable); + if (val == nullptr || *val == '\0') return std::nullopt; + return val; +} diff --git a/src/string_func.h b/src/string_func.h index 455d5547b5..2150e5cd9a 100644 --- a/src/string_func.h +++ b/src/string_func.h @@ -167,4 +167,6 @@ inline bool IsWhitespace(char32_t c) #include #endif +std::optional GetEnv(const char *variable); + #endif /* STRING_FUNC_H */