aboutsummaryrefslogtreecommitdiff
path: root/src/settings.h.in
diff options
context:
space:
mode:
Diffstat (limited to 'src/settings.h.in')
-rw-r--r--src/settings.h.in63
1 files changed, 51 insertions, 12 deletions
diff --git a/src/settings.h.in b/src/settings.h.in
index 605288e..51b2bd0 100644
--- a/src/settings.h.in
+++ b/src/settings.h.in
@@ -1,13 +1,25 @@
+/*
+ * This file is part of smolbote. It's copyrighted by the contributors recorded
+ * in the version control history of the file, available from its original
+ * location: https://neueland.iserlohn-fortress.net/cgit/smolbote
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
#pragma once
-#include <QStandardPaths>
#include <configuration.h>
#include <fstream>
+#include <cstdlib>
+#include <filesystem>
+#include <array>
+#include <spdlog/spdlog.h>
inline auto init_conf(const std::string &path)
{
+ namespace fs = std::filesystem;
struct {
- std::string path;
+ std::optional<fs::path> path;
std::unique_ptr<Configuration> ptr;
} conf;
@@ -15,17 +27,44 @@ inline auto init_conf(const std::string &path)
@__DEFAULT_CFG__
});
- conf.path = path.empty() ? conf.ptr->value<std::string>("poi.cfg.path").value() : path;
- if(conf.path.front() == '~') {
- conf.path.replace(0, 1, QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString());
- };
+ conf.path = [&]() -> std::optional<fs::path> {
+ if(!path.empty()) {
+ return fs::path(path);
+ }
- std::fstream fs;
- fs.open(conf.path, std::fstream::in);
- if(fs.is_open()) {
- conf.ptr->read(fs);
- fs.close();
- }
+ // 1000: invalid; 0: $HOME; 1: $XDG_CONFIG_HOME
+ std::array<std::optional<fs::path>, 2> home = { std::nullopt, std::nullopt };
+ if(const char* usr = std::getenv("HOME"))
+ home[0] = fs::path(usr);
+ if(const char* xdg = std::getenv("XDG_CONFIG_HOME"))
+ home[1] = fs::path(xdg);
+
+ for(const auto &pair : {
+ @__CONFIG_PATHS__
+ }) {
+ if(pair.first < 0) {
+ auto p = fs::path(pair.second);
+ spdlog::debug("Trying {}", p.c_str());
+ if(fs::exists(p))
+ return p;
+ } else if(home[static_cast<std::size_t>(pair.first)]) {
+ auto p = home[static_cast<std::size_t>(pair.first)].value() / pair.second;
+ spdlog::debug("Trying {}", p.c_str());
+ if(fs::exists(p))
+ return p;
+ }
+ }
+ return std::nullopt;
+
+ }();
+
+ if(conf.path) {
+ std::fstream file(conf.path.value(), std::fstream::in);
+ if(file.is_open()) {
+ conf.ptr->read(file);
+ file.close();
+ }
+ }
return conf;
}