[nostalgia/studio] Cleanup

This commit is contained in:
2023-02-01 22:42:07 -06:00
parent fad8837ad1
commit fea6a0764c
6 changed files with 47 additions and 54 deletions
+14 -43
View File
@@ -4,9 +4,6 @@
#pragma once
#include <filesystem>
#include <fstream>
#include <ox/fs/fs.hpp>
#include <ox/model/typenamecatcher.hpp>
#include <ox/oc/oc.hpp>
@@ -20,44 +17,20 @@
namespace nostalgia::studio {
constexpr auto ConfigDir = [] {
switch (ox::defines::OS) {
case ox::OS::Darwin:
return "{}/Library/Preferences/{}";
case ox::OS::DragonFlyBSD:
case ox::OS::FreeBSD:
case ox::OS::Linux:
case ox::OS::NetBSD:
case ox::OS::OpenBSD:
return "{}/.config/{}";
case ox::OS::Windows:
return R"({}/AppData/Local/{})";
case ox::OS::BareMetal:
return "";
}
}();
[[nodiscard]]
ox::String configPath(const core::Context *ctx) noexcept;
template<typename T>
ox::Result<T> readConfig(core::Context *ctx, ox::CRStringView name) noexcept {
oxAssert(ox_strcmp(name, ""), "Config type has no TypeName");
const auto homeDir = std::getenv(ox::defines::OS == ox::OS::Windows ? "USERPROFILE" : "HOME");
const auto configPath = ox::sfmt(ConfigDir, homeDir, ctx->appName).toStdString();
const auto path = ox::sfmt("{}/{}.json", configPath, name).toStdString();
std::ifstream file(path, std::ios::binary | std::ios::ate);
if (!file.good()) {
oxErrf("Could not find config file: {}\n", path);
return OxError(1, "Could not find config file");
}
try {
const auto size = file.tellg();
file.seekg(0, std::ios::beg);
ox::Buffer buff(static_cast<std::size_t>(size));
file.read(buff.data(), size);
return ox::readOC<T>(buff);
} catch (const std::ios_base::failure &e) {
oxErrf("Could not read config file: {}\n", e.what());
return OxError(2, "Could not read config file");
const auto path = ox::sfmt("/{}.json", name);
ox::PassThroughFS fs(configPath(ctx));
const auto [buff, err] = fs.read(path);
if (err) {
oxErrf("Could not read config file: {}\n", toStr(err));
return err;
}
return ox::readOC<T>(buff);
}
template<typename T>
@@ -67,19 +40,17 @@ ox::Result<T> readConfig(core::Context *ctx) noexcept {
}
template<typename T>
ox::Error writeConfig(core::Context *ctx, const auto &name, T *data) noexcept {
ox::Error writeConfig(core::Context *ctx, ox::CRStringView name, T *data) noexcept {
oxAssert(ox_strcmp(name, ""), "Config type has no TypeName");
const auto homeDir = std::getenv(ox::defines::OS == ox::OS::Windows ? "USERPROFILE" : "HOME");
const auto configPath = ox::sfmt(ConfigDir, homeDir, ctx->appName);
const auto path = ox::sfmt("{}.json", name);
ox::PassThroughFS fs(configPath);
if (auto err = fs.mkdir("/", true)) {
const auto path = ox::sfmt("/{}.json", name);
ox::PassThroughFS fs(configPath(ctx));
if (const auto err = fs.mkdir("/", true)) {
oxErrf("Could not create config directory: {}\n", toStr(err));
return err;
}
oxRequireM(buff, ox::writeOC(data));
buff.back().value = '\n';
if (auto err = fs.write(path, buff.data(), buff.size())) {
if (const auto err = fs.write(path, buff.data(), buff.size())) {
oxErrf("Could not read config file: {}\n", toStr(err));
return OxError(2, "Could not read config file");
}