LunaHook-mirror/LunaHost/GUI/confighelper.cpp

47 lines
961 B
C++
Raw Normal View History

2024-07-21 19:28:47 +08:00
#include "confighelper.h"
#include "stringutils.h"
std::string readfile(const wchar_t *fname)
{
FILE *f;
2024-02-09 09:25:26 +08:00
_wfopen_s(&f, fname, L"rb");
2024-07-21 19:28:47 +08:00
if (f == 0)
return {};
2024-02-09 09:25:26 +08:00
fseek(f, 0, SEEK_END);
auto len = ftell(f);
fseek(f, 0, SEEK_SET);
std::string buff;
buff.resize(len);
fread(buff.data(), 1, len, f);
fclose(f);
return buff;
2024-07-21 19:28:47 +08:00
}
void writefile(const wchar_t *fname, const std::string &s)
{
FILE *f;
2024-02-09 09:25:26 +08:00
_wfopen_s(&f, fname, L"w");
2024-07-21 19:28:47 +08:00
fprintf(f, "%s", s.c_str());
2024-02-09 09:25:26 +08:00
fclose(f);
}
2024-07-21 19:28:47 +08:00
confighelper::confighelper()
{
2024-11-01 23:34:22 +08:00
configpath = std::filesystem::current_path() / "config.json";
2024-07-21 19:28:47 +08:00
try
{
configs = nlohmann::json::parse(readfile(configpath.c_str()));
2024-02-09 09:25:26 +08:00
}
2024-07-21 19:28:47 +08:00
catch (std::exception &)
{
configs = {};
2024-02-09 09:25:26 +08:00
}
2024-11-01 23:34:22 +08:00
if (configs.find(pluginkey) == configs.end())
2024-07-21 19:28:47 +08:00
{
2024-11-01 23:34:22 +08:00
configs[pluginkey] = {};
}
2024-02-09 09:25:26 +08:00
}
2024-07-21 19:28:47 +08:00
confighelper::~confighelper()
{
2024-02-09 09:25:26 +08:00
writefile(configpath.c_str(), configs.dump(4));
}