99 lines
2.2 KiB
C++
99 lines
2.2 KiB
C++
/*
|
|
SWR INI Tool - Switchroot INI Configuration Editor
|
|
Copyright (C) 2026 Switchroot
|
|
*/
|
|
|
|
#include "app_config.h"
|
|
#include "ini_handler.h"
|
|
|
|
#include <sys/stat.h>
|
|
#include <cstring>
|
|
|
|
// Create parent directories for a file path
|
|
static void ensureParentDir(const std::string& filePath)
|
|
{
|
|
std::string dir = filePath;
|
|
size_t pos = dir.find_last_of('/');
|
|
if (pos == std::string::npos)
|
|
return;
|
|
dir = dir.substr(0, pos);
|
|
|
|
// Simple recursive mkdir
|
|
for (size_t i = 1; i < dir.size(); i++)
|
|
{
|
|
if (dir[i] == '/')
|
|
{
|
|
std::string sub = dir.substr(0, i);
|
|
mkdir(sub.c_str(), 0755);
|
|
}
|
|
}
|
|
mkdir(dir.c_str(), 0755);
|
|
}
|
|
|
|
AppConfig& AppConfig::get()
|
|
{
|
|
static AppConfig instance;
|
|
return instance;
|
|
}
|
|
|
|
AppConfig::AppConfig()
|
|
{
|
|
// Defaults
|
|
paths[(int)OsTarget::ANDROID] = DEFAULT_ANDROID_INI;
|
|
paths[(int)OsTarget::LINUX] = DEFAULT_LINUX_INI;
|
|
paths[(int)OsTarget::LAKKA] = DEFAULT_LAKKA_INI;
|
|
}
|
|
|
|
void AppConfig::load()
|
|
{
|
|
IniHandler cfg;
|
|
if (!cfg.load(APP_CONFIG_PATH))
|
|
return; // use defaults
|
|
|
|
std::string val;
|
|
|
|
val = cfg.get("paths", "android");
|
|
if (!val.empty()) paths[(int)OsTarget::ANDROID] = val;
|
|
|
|
val = cfg.get("paths", "linux");
|
|
if (!val.empty()) paths[(int)OsTarget::LINUX] = val;
|
|
|
|
val = cfg.get("paths", "lakka");
|
|
if (!val.empty()) paths[(int)OsTarget::LAKKA] = val;
|
|
}
|
|
|
|
void AppConfig::save()
|
|
{
|
|
ensureParentDir(APP_CONFIG_PATH);
|
|
|
|
IniHandler cfg;
|
|
cfg.load(APP_CONFIG_PATH); // load existing or start fresh
|
|
|
|
cfg.set("paths", "android", paths[(int)OsTarget::ANDROID]);
|
|
cfg.set("paths", "linux", paths[(int)OsTarget::LINUX]);
|
|
cfg.set("paths", "lakka", paths[(int)OsTarget::LAKKA]);
|
|
|
|
cfg.save(APP_CONFIG_PATH);
|
|
}
|
|
|
|
std::string AppConfig::getPath(OsTarget target) const
|
|
{
|
|
return paths[(int)target];
|
|
}
|
|
|
|
void AppConfig::setPath(OsTarget target, const std::string& path)
|
|
{
|
|
paths[(int)target] = path;
|
|
}
|
|
|
|
const char* AppConfig::getLabel(OsTarget target)
|
|
{
|
|
switch (target)
|
|
{
|
|
case OsTarget::ANDROID: return "Android";
|
|
case OsTarget::LINUX: return "Linux";
|
|
case OsTarget::LAKKA: return "Lakka";
|
|
default: return "Unknown";
|
|
}
|
|
}
|