aboutsummaryrefslogtreecommitdiff
path: root/src/client/settings.cc
diff options
context:
space:
mode:
authorNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-12 21:57:46 +1100
committerNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-12 21:57:46 +1100
commite4483eca01b48b943cd0461e24a74ae1a3139ed4 (patch)
treeed58c3c246e3af1af337697695d780aa31f6ad9a /src/client/settings.cc
parent1cc08c51eb4b0f95c30c0a98ad1fc5ad3459b2df (diff)
Update to most recent version (old initial commit)
Diffstat (limited to 'src/client/settings.cc')
-rw-r--r--src/client/settings.cc36
1 files changed, 23 insertions, 13 deletions
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<std::string, std::string>;
using setting_map = std::unordered_map<std::string, value_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<const std::string&, const std::string&> loc,
- const std::string default_value) noexcept {
+std::optional<std::string> 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<const std::string&, const std::string&> loc,
return find_it->second;
}
-void set_setting_str(
- const std::pair<const std::string&, const std::string&> 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();