-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.cpp
80 lines (65 loc) · 1.56 KB
/
config.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "config.h"
void c_config::run(std::string name)
{
if (PWSTR appdata_path; SUCCEEDED(SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, nullptr, &appdata_path)))
{
path = appdata_path;
path /= name;
CoTaskMemFree(appdata_path);
}
if (std::filesystem::is_directory(path))
{
std::transform
(
std::filesystem::directory_iterator{ path },
std::filesystem::directory_iterator{ },
std::back_inserter(configs),
[](const auto& entry) { return entry.path().filename().string(); }
);
}
}
void c_config::load(size_t id)
{
if (!std::filesystem::is_directory(path))
{
std::filesystem::remove(path);
std::filesystem::create_directory(path);
}
std::ifstream in{ path / configs[id] };
if (!in.good())
return;
archivex<std::ifstream>{ in } >> feature;
in.close();
}
void c_config::save(size_t id) const
{
if (!std::filesystem::is_directory(path))
{
std::filesystem::remove(path);
std::filesystem::create_directory(path);
}
std::ofstream out{ path / configs[id] };
if (!out.good())
return;
archivex<std::ofstream>{ out } << feature;
out.close();
}
void c_config::add(std::string name)
{
if (!(name.empty()) && std::find(std::cbegin(configs), std::cend(configs), name) == std::cend(configs))
configs.emplace_back(name);
}
void c_config::remove(size_t id)
{
std::filesystem::remove(path / configs[id]);
configs.erase(configs.cbegin() + id);
}
void c_config::rename(size_t item, std::string new_name)
{
std::filesystem::rename(path / configs[item], path / new_name);
configs[item] = new_name;
}
void c_config::reset()
{
feature = { };
}