Update Config.hpp

This commit is contained in:
0TheSpy
2021-07-10 18:21:25 +03:00
committed by GitHub
parent d854ab1f50
commit d98c7c5d9f

View File

@ -8,6 +8,20 @@
#include <fstream>
#include <sstream>
#include <filesystem>
namespace fs = std::filesystem;
static std::time_t to_time_t(FILETIME ft)
{
std::time_t ret = 0;
ULARGE_INTEGER ull;
ull.LowPart = ft.dwLowDateTime;
ull.HighPart = ft.dwHighDateTime;
ret = (ull.QuadPart / 10000000ULL - 11644473600ULL);
return ret;
}
std::vector<char> HexToBytes(const std::string& hex) {
std::vector<char> res;
@ -44,11 +58,46 @@ std::vector<std::string> Split(const std::string& str, const char* delim) {
return res;
}
using namespace std;
class Config : public Singleton<Config> {
public:
void Save() {
std::ofstream fout("seaside210407.cfg", std::ios::binary);
std::vector<std::string> configs = { };
std::vector<std::string> times = { };
bool file_exists(std::string filename) {
std::ifstream ifile(filename);
return (bool)ifile;
}
void Create(char* name) {
string newname = string(name) + ".ss";
if (!file_exists(newname)) {
ofstream newcfg;
newcfg.open(newname);
newcfg.close();
}
Refresh();
}
void Delete(int selected) {
string name = configs[selected];
if (remove(name.c_str()) != 0)
{
#ifdef DEBUG
printf("Error deleting file %s\n", name.c_str());
#endif
}
Refresh();
}
void Save(int selected, char* new_name) {
string name = configs[selected];
string newname = string(new_name) + ".ss";
std::ofstream fout(name.c_str(), std::ios::binary);
const auto sz = sizeof(Options);
const auto var_sz = sizeof(Var<bool>);
const auto cnt = sz / var_sz;
@ -60,19 +109,19 @@ public:
fout << name << "\t" << BytesToString((unsigned char*)*(int*)&val, sizeof_val) << std::endl;
}
fout.close();
rename(name.c_str(), newname.c_str());
Refresh();
}
bool file_exists(std::string filename) {
std::ifstream ifile(filename);
return (bool)ifile;
}
void Load() {
void Load(int selected) {
opt.loading = 1;
string name = configs[selected];
if (file_exists("seaside210407.cfg") ) {
std::ifstream fin("seaside210407.cfg", std::ios::binary);
if (file_exists(name.c_str()) ) {
std::ifstream fin(name.c_str(), std::ios::binary);
std::stringstream ss;
ss << fin.rdbuf();
@ -99,6 +148,36 @@ public:
else PlaySoundA((char*)"null", opt.hModuleGlobal, SND_ASYNC);
}
void OpenFolder() {
ShellExecute(NULL, NULL, "", NULL, NULL, SW_SHOWNORMAL);
}
void Refresh() {
configs.clear(); times.clear();
std::string search_path = "*.ss";
WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile(search_path.c_str(), &fd);
if (hFind != INVALID_HANDLE_VALUE) {
do {
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
configs.push_back(fd.cFileName);
time_t epoch = to_time_t(fd.ftLastWriteTime);
std::string vremya = asctime(gmtime(&epoch));
times.push_back(vremya);
#ifdef DEBUG
printf("fn %s %s", fd.cFileName, vremya.c_str());
#endif
}
} while (::FindNextFile(hFind, &fd));
::FindClose(hFind);
}
}
};