looper/options.cpp
Zachary Hall 27113f6443
Some checks failed
Build / build-gentoo (push) Failing after 22s
Build / download-system-deps (push) Successful in 3m16s
Build / get-source-code (push) Successful in 12m8s
Build / build-appimage (push) Successful in 3m58s
Build / build-android (push) Failing after 2m59s
Build / build-windows (push) Failing after 7m22s
WTF - Asserting the return value of a function causes it to not run?!
2024-12-08 09:55:27 -08:00

86 lines
2.5 KiB
C++

#include "thirdparty/toml.hpp"
#include "util.hpp"
#include <filesystem>
#include <sstream>
#ifdef __ANDROID__
#include <SDL.h>
#endif
#include <optional>
#include "web_functions.hpp"
using namespace std::filesystem;
namespace Looper::Options {
std::optional<const char *> options_path_override;
bool options_enabled() {
return !(options_path_override.has_value() && options_path_override.value() == nullptr);
};
toml::table *options;
toml::table opts_value;
std::string get_options_path() {
if (options_path_override.has_value()) {
if (options_path_override.value() == nullptr) {
return "";
} else {
return options_path_override.value();
}
}
#ifdef __EMSCRIPTEN__
return "config_toml";
#else
path prefs_path(get_prefs_path());
prefs_path /= path("looper");
create_directories(prefs_path);
path config_file = prefs_path / path("config.toml");
return config_file.string();
#endif
}
void load_options() {
if (!options_enabled()) {
options = new toml::table();
return;
}
std::string config_path = get_options_path();
#ifdef __EMSCRIPTEN__
const char *value = nullptr;
read_storage(config_path.c_str(), &value, nullptr);
if (value != nullptr) {
auto res = toml::parse(value);
opts_value = res;
options = &opts_value;
if (options == nullptr) {
options = new toml::table();
}
} else {
options = new toml::table();
}
#else
if (exists(config_path)) {
auto res = toml::parse_file(config_path);
opts_value = res;
options = &opts_value;
if (options == nullptr) {
options = new toml::table();
}
} else {
options = new toml::table();
}
#endif
}
void save_options() {
if (!options_enabled()) {
return;
}
#ifdef __EMSCRIPTEN__
std::ostringstream output;
#else
std::ofstream output(get_options_path());
#endif
output << *options;
#ifdef __EMSCRIPTEN__
std::string str = output.str();
write_storage(get_options_path().c_str(), str.c_str(), str.length() + 1);
#else
output.close();
#endif
}
}