mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-22 02:27:01 +08:00
chore: add clang-format and pre-commit configuration
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include <Cubed/config.hpp>
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
#include <Cubed/tools/log.hpp>
|
||||
#include "Cubed/config.hpp"
|
||||
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
#include "Cubed/tools/log.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
@@ -10,22 +11,16 @@ using namespace std::string_view_literals;
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
Config::Config() {
|
||||
load_or_create_config();
|
||||
}
|
||||
Config::Config() { load_or_create_config(); }
|
||||
|
||||
Config::~Config() {
|
||||
save_to_file();
|
||||
}
|
||||
Config::~Config() { save_to_file(); }
|
||||
|
||||
Config& Config::get() {
|
||||
static Config instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
toml::table& Config::table() {
|
||||
return m_tbl;
|
||||
}
|
||||
toml::table& Config::table() { return m_tbl; }
|
||||
|
||||
void Config::create_config() {
|
||||
static constexpr auto SOURCE = R"(
|
||||
@@ -60,26 +55,25 @@ void Config::create_config() {
|
||||
std::abort();
|
||||
}
|
||||
Logger::info("Create New Config File Success");
|
||||
|
||||
}
|
||||
|
||||
void Config::load_or_create_config() {
|
||||
fs::path config_path {CONGIF_PATH};
|
||||
fs::path config_path{CONGIF_PATH};
|
||||
if (!fs::is_regular_file(config_path)) {
|
||||
create_config();
|
||||
} else try {
|
||||
m_tbl = toml::parse_file(config_path.string());
|
||||
} catch (const toml::parse_error& err) {
|
||||
Logger::error("Load Config Error: \"{}\"", err.what());
|
||||
create_config();
|
||||
}
|
||||
|
||||
Logger::info("Load Config File Success");
|
||||
} else
|
||||
try {
|
||||
m_tbl = toml::parse_file(config_path.string());
|
||||
} catch (const toml::parse_error& err) {
|
||||
Logger::error("Load Config Error: \"{}\"", err.what());
|
||||
create_config();
|
||||
}
|
||||
|
||||
Logger::info("Load Config File Success");
|
||||
}
|
||||
|
||||
void Config::save_to_file() {
|
||||
fs::path config_path {CONGIF_PATH};
|
||||
fs::path config_path{CONGIF_PATH};
|
||||
std::ofstream file{config_path};
|
||||
file << m_tbl;
|
||||
Logger::info("Save File Success");
|
||||
@@ -121,4 +115,4 @@ toml::node_view<toml::node> Config::val_view(std::string_view key) {
|
||||
return view;
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace Cubed
|
||||
Reference in New Issue
Block a user