-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.cpp
More file actions
299 lines (257 loc) · 7.68 KB
/
database.cpp
File metadata and controls
299 lines (257 loc) · 7.68 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "database.hpp"
Database* Database::instance = nullptr;
Database::Database()
{
setPath();
loadData();
msg = msg->getInstance();
}
Database::Database(QListWidget *wdList)
{
this->wdList = wdList;
Database();
}
Database* Database::getInstance()
{
if(instance == nullptr)
{
instance = new Database();
}
return instance;
}
UserData Database::getData(const int index)
{
return list.at(index);
}
void Database::updateView()
{
if(wdList != nullptr)
{
wdList->clear();
for(std::size_t i = 0; i < list.size(); i++)
{
UserData data = list.at(i);
std::string text = data.getText().toStdString();
if(text.size() > MAX_ROW_SIZE)
{
text = text.substr(0, MAX_ROW_SIZE);
text += " . . .";
}
text = std::regex_replace(text, std::regex("\n"), " ");
QString row = QString(std::to_string(i + 1).c_str()) + ": " + text.c_str();
wdList->addItem(row);
}
}
}
void Database::updateDatabase()
{
list.clear();
loadData();
if(wdList)
updateView();
}
void Database::setView(QListWidget *wdList)
{
this->wdList = wdList;
updateView();
}
void Database::loadData()
{
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(file_location.c_str());
if(result)
{
pugi::xml_node records = doc.child("records");
for(pugi::xml_node record = records.child("record"); record; record = record.next_sibling("record"))
{
UserData usd(record.attribute("date").value(), record.child_value("text"));
list.insert(list.end(), usd);
}
}
else
{
msg->displayMessage("data.xml parsed with error!");
std::cerr << "data.xml parsed with error!" << std::endl;
}
}
void Database::setPath()
{
#ifdef WIN
std::string appdata = getenv("APPDATA");
location = appdata + "\\torar\\";
file_location = location + "data.xml";
#else
std::string appdata = getenv("HOME");
location = appdata + "/.torar/";
file_location = location + "data.xml";
#endif
if(!boost::filesystem::exists(location))
{
if(!boost::filesystem::create_directory(location))
{
msg->displayMessage("Unable to create directory");
std::cerr << "Unable to create directory" << std::endl;
}
else
{
std::cout << "Directory created" << std::endl;
}
}
if(!boost::filesystem::exists(file_location))
{
try
{
pugi::xml_document doc;
auto decNode = doc.append_child(pugi::node_declaration);
decNode.append_attribute("version") = "1.0";
decNode.append_attribute("encoding") = "utf-8";
doc.append_child("records").append_child(pugi::node_pcdata);
auto success = doc.save_file(file_location.c_str());
if(!success)
{
std::string err("Unable to create data.xml at: ");
err += location;
msg->displayMessage(QString(err.c_str()));
std::cerr << "Unable to create " << "data.xml" << " at: " << location << std::endl;
}
}
catch(std::ios::failure &e)
{
std::string err(e.what());
std::cerr << err << std::endl;
msg->displayMessage(QString(err.c_str()));
}
}
std::cout << "Location: " << location << std::endl;
std::cout << "Data location: " << file_location << std::endl;
}
size_t Database::indexOf(const std::string date)
{
for(size_t i = 0; i < list.size(); i++)
{
if(list[i].getDate() == date)
return i;
}
throw std::logic_error("Unable to find index!");
}
void Database::addData(UserData data)
{
if(list.size() < MAX_LIST_SIZE)
{
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(file_location.c_str());
if(result)
{
pugi::xml_node records = doc.child("records");
pugi::xml_node record = records.append_child("record");
record.append_attribute("date").set_value(data.getDate().c_str());
pugi::xml_node text = record.append_child("text");
text.append_child(pugi::node_pcdata).set_value(data.getText().toUtf8());
if(doc.save_file(file_location.c_str()))
{
list.insert(list.end(), data);
if(wdList)
updateView();
}
}
else
{
msg->displayMessage("data.xml parsed with error!");
std::cerr << "data.xml parsed with error!" << std::endl;
}
}
else
{
msg->displayMessage("You can store only 7500 items!");
std::cerr << "You can store only 7500 items!" << std::endl;
}
}
void Database::editData(UserData Data)
{
try
{
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(file_location.c_str());
if(result)
{
size_t index = indexOf(Data.getDate());
list[index].setText(Data.getText());
pugi::xml_node records = doc.child("records");
for(pugi::xml_node record = records.child("record"); record; record = record.next_sibling("record"))
{
if(record.attribute("date").value() == Data.getDate())
{
record.child("text").text().set(Data.getText().toUtf8().toStdString().c_str());
if(!doc.save_file(file_location.c_str()))
{
msg->displayMessage("Unable to save file!");
std::cerr << "Unable to save file!" << std::endl;
}
updateView();
break;
}
}
}
else
{
msg->displayMessage("data.xml parsed with error!");
std::cerr << "data.xml parsed with error!" << std::endl;
}
}
catch(std::logic_error &e)
{
std::string err(e.what());
msg->displayMessage(QString(err.c_str()));
std::cerr << err << std::endl;
}
}
void Database::removeData(UserData data)
{
try
{
size_t index = indexOf(data.getDate());
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(file_location.c_str());
if(result)
{
pugi::xml_node records = doc.child("records");
for(pugi::xml_node record = records.child("record"); record; record = record.next_sibling("record"))
{
if(record.attribute("date").value() == data.getDate())
{
records.remove_child(record);
list.erase(list.begin() + index);
if(wdList)
updateView();
if(!doc.save_file(file_location.c_str()))
std::cerr << "Unable to save file!" << std::endl;
break;
}
}
}
else
{
msg->displayMessage("data.xml parsed with error!");
std::cerr << "data.xml parsed with error!" << std::endl;
}
}
catch(std::logic_error &e)
{
std::string err(e.what());
msg->displayMessage(QString(err.c_str()));
std::cerr << err << std::endl;
}
}
void Database::removeData(const int index)
{
try
{
removeData(list.at(index));
}
catch(std::out_of_range &e)
{
std::string err(e.what());
msg->displayMessage(QString(err.c_str()));
std::cerr << err << std::endl;
}
}