-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstorage.cpp
More file actions
114 lines (90 loc) · 2.47 KB
/
storage.cpp
File metadata and controls
114 lines (90 loc) · 2.47 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING
#include <fstream>
#include <iostream>
#include <experimental/filesystem>
#include <cstdlib>
#include <cstring>
#include "storage.h"
using namespace std;
namespace fs = std::experimental::filesystem;
void Storage::loadAppdata() {
char* appdata_path;
size_t len;
errno_t err = _dupenv_s(&appdata_path, &len, "APPDATA");
if (err != 0) {
cerr << "Error: Unable to get APPDATA environment variable." << endl;
return;
}
appdata = string(appdata_path);
free(appdata_path);
}
string Storage::getKey(int id) {
ifstream file(appdata + "/" + folder + "/" + keys + "/key_" + to_string(id) + ".txt");
if (file) {
string key;
char ch;
while (file.get(ch)) {
key += ch;
}
file.close();
return key;
}
else {
return "";
}
}
void Storage::setKey(int id, string data) {
ofstream file(appdata + "/" + folder + "/" + keys + "/key_" + to_string(id) + ".txt");
if (file) {
file << data;
file.close();
}
else {
cerr << "Error: Unable to create key file." << endl;
}
}
string Storage::getName() {
ifstream file(appdata + "/" + folder + "/" + name);
if (file) {
string name;
getline(file , name);
file.close();
return name;
}
else {
return "";
}
}
void Storage::setName(string data) {
ofstream file(appdata + "/" + folder + "/" + name);
if (file) {
file << data;
file.close();
}
else {
cerr << "Error: Unable to create name file." << endl;
}
}
void Storage::initialize() {
loadAppdata();
string folder_path = appdata + "/" + folder;
if (!fs::exists(folder_path)) {
fs::create_directory(folder_path);
}
string keys_path = folder_path + "/" + keys;
if (!fs::exists(keys_path)) {
fs::create_directory(keys_path);
}
else {
for (const auto& entry : fs::directory_iterator(keys_path)) {
fs::remove(entry.path());
}
}
string name_path = folder_path + "/" + name;
if (!fs::exists(name_path)) {
ofstream nameFile(name_path);
if (!nameFile) {
cerr << "Error: Unable to create name file." << endl;
}
}
}