From e4483eca01b48b943cd0461e24a74ae1a3139ed4 Mon Sep 17 00:00:00 2001 From: Nicolas James Date: Wed, 12 Feb 2025 21:57:46 +1100 Subject: Update to most recent version (old initial commit) --- src/client/settings.cc | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) (limited to 'src/client/settings.cc') diff --git a/src/client/settings.cc b/src/client/settings.cc index 8e2d0c9..3253f60 100644 --- a/src/client/settings.cc +++ b/src/client/settings.cc @@ -9,7 +9,7 @@ const char* const config_name = "blockgame_linux.conf"; namespace client { namespace settings { -static std::string get_settings_file() noexcept { +static std::string get_settings_file() { std::filesystem::create_directories(home_dir + ::config_dir); std::ifstream in{home_dir + ::config_dir + config_name, std::fstream::in | std::fstream::app}; @@ -20,7 +20,7 @@ static std::string get_settings_file() noexcept { return file.ends_with('\n') ? file : file + '\n'; } -static void set_settings_file(const std::string_view contents) noexcept { +static void set_settings_file(const std::string_view contents) { std::filesystem::create_directories(home_dir + ::config_dir); std::ofstream out{home_dir + ::config_dir + ::config_name, std::fstream::out | std::fstream::trunc}; @@ -31,7 +31,7 @@ static void set_settings_file(const std::string_view contents) noexcept { // Heading maps setting to headings (ie, "camera -> {map with fov, 100.0f}). using value_map = std::unordered_map; using setting_map = std::unordered_map; -static setting_map& get_settings() noexcept { +static setting_map& get_settings() { static setting_map ret = []() -> setting_map { setting_map settings{}; @@ -50,8 +50,9 @@ static setting_map& get_settings() noexcept { const size_t split_pos = line.find_first_of(':'); if (split_pos == std::string::npos) { - shared::print::warn("client: failed to parse line in settings " - "file, consider manual intervention\n"); + shared::print::warn + << "client: failed to parse line in settings file, " + "consider manual intervention\n"; continue; } @@ -72,10 +73,21 @@ static setting_map& get_settings() noexcept { return ret; } -// TODO fix this bit of code duplication -std::string -get_setting_str(const std::pair loc, - const std::string default_value) noexcept { +std::optional maybe_get_setting_str(const setting_pair_t& loc) { + const auto& [heading, name] = loc; + + setting_map& settings = get_settings(); + value_map& values = settings[heading]; + + const auto it = values.find(name); + if (it == std::end(values)) { + return std::nullopt; + } + return it->second; +} + +std::string get_setting_str(const setting_pair_t& loc, + const std::string default_value) { const auto& [heading, name] = loc; setting_map& settings = get_settings(); @@ -89,9 +101,7 @@ get_setting_str(const std::pair loc, return find_it->second; } -void set_setting_str( - const std::pair loc, - const std::string& value) noexcept { +void set_setting_str(const setting_pair_t& loc, const std::string& value) { const auto& [heading, name] = loc; setting_map& settings = get_settings(); @@ -105,7 +115,7 @@ void set_setting_str( find_it->second = value; } -void save() noexcept { +void save() { std::string contents; const setting_map& settings = get_settings(); -- cgit v1.2.3