From 92b3c2dcff3e85ad3d455f6ab845d9a97d3b525b Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 7 Dec 2020 12:22:15 +0200 Subject: Rewrite meson build scripts into cmakelists --- src/autogen/settings.h.in | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/autogen/settings.h.in (limited to 'src/autogen/settings.h.in') diff --git a/src/autogen/settings.h.in b/src/autogen/settings.h.in new file mode 100644 index 0000000..51b2bd0 --- /dev/null +++ b/src/autogen/settings.h.in @@ -0,0 +1,70 @@ +/* + * 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 +#include +#include +#include +#include +#include + +inline auto init_conf(const std::string &path) +{ + namespace fs = std::filesystem; + struct { + std::optional path; + std::unique_ptr ptr; + } conf; + + conf.ptr = std::make_unique>>({ + @__DEFAULT_CFG__ + }); + + conf.path = [&]() -> std::optional { + if(!path.empty()) { + return fs::path(path); + } + + // 1000: invalid; 0: $HOME; 1: $XDG_CONFIG_HOME + std::array, 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(pair.first)]) { + auto p = home[static_cast(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; +} -- cgit v1.2.1