[nostalgia/studio] Fix config path for Windows
This commit is contained in:
parent
dac33e63fd
commit
db3e9c5d93
@ -7,6 +7,7 @@
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
#include <ox/fs/fs.hpp>
|
||||
#include <ox/model/typenamecatcher.hpp>
|
||||
#include <ox/oc/oc.hpp>
|
||||
#include <ox/std/buffer.hpp>
|
||||
@ -29,8 +30,9 @@ constexpr auto ConfigDir = [] {
|
||||
case ox::OS::NetBSD:
|
||||
case ox::OS::OpenBSD:
|
||||
return "{}/.config/{}";
|
||||
case ox::OS::BareMetal:
|
||||
case ox::OS::Windows:
|
||||
return "{}/AppData/Local/{}";
|
||||
case ox::OS::BareMetal:
|
||||
return "";
|
||||
}
|
||||
}();
|
||||
@ -38,7 +40,7 @@ constexpr auto ConfigDir = [] {
|
||||
template<typename T>
|
||||
ox::Result<T> readConfig(core::Context *ctx, const char *name) noexcept {
|
||||
oxAssert(ox_strcmp(name, ""), "Config type has no TypeName");
|
||||
const auto homeDir = std::getenv("HOME");
|
||||
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);
|
||||
@ -67,28 +69,21 @@ ox::Result<T> readConfig(core::Context *ctx) noexcept {
|
||||
template<typename T>
|
||||
ox::Error writeConfig(core::Context *ctx, const auto &name, T *data) noexcept {
|
||||
oxAssert(ox_strcmp(name, ""), "Config type has no TypeName");
|
||||
const auto homeDir = std::getenv("HOME");
|
||||
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::error_code ec;
|
||||
std::filesystem::create_directory(configPath, ec);
|
||||
if (ec) {
|
||||
oxErrf("Could not create config directory: {}\n", ec.message());
|
||||
}
|
||||
std::ofstream 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");
|
||||
const auto path = ox::sfmt("{}.json", name).toStdString();
|
||||
ox::PassThroughFS fs(configPath.c_str());
|
||||
if (auto err = fs.mkdir(configPath.c_str(), true)) {
|
||||
oxErrf("Could not create config directory: {}\n", toStr(err));
|
||||
return err;
|
||||
}
|
||||
oxRequireM(buff, ox::writeOC(data));
|
||||
buff.back().value = '\n';
|
||||
try {
|
||||
file.write(buff.data(), static_cast<ox::Signed<decltype(buff.size())>>(buff.size()));
|
||||
return OxError(0);
|
||||
} catch (const std::ios_base::failure &e) {
|
||||
oxErrf("Could not read config file: {}\n", e.what());
|
||||
if (auto err = fs.write(path.c_str(), buff.data(), buff.size())) {
|
||||
oxErrf("Could not read config file: {}\n", toStr(err));
|
||||
return OxError(2, "Could not read config file");
|
||||
}
|
||||
return OxError(0);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
Loading…
Reference in New Issue
Block a user