-
Notifications
You must be signed in to change notification settings - Fork 0
/
Users.h
444 lines (442 loc) · 16.4 KB
/
Users.h
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#pragma once
#include "files/pugixml.hpp"
#include <algorithm>
#include <unistd.h>
#include <iostream>
#include <errno.h>
#include <cstring>
#include <cstdlib>
#include <cstddef>
#include <vector>
#include <cstdio>
#include <ctime>
#include <set>
#include <map>
#include "Books.h"
class Users {
private:
std::string username;
std::vector<std::string> cautari;
static pugi::xml_document doc;
static std::map<int, pugi::xml_node> userMapIdToNodes;
int threadId = -1;
unsigned userInList;
static int xmlCheck;
public:
void setUsername(char param[])
{
this->username = std::string(param);
}
static void checkXmlFile(const char *source)
{
pugi::xml_parse_result result = doc.load_file(source);
if(result)
{
std::cout << "XML [" << source << "] parsed successfully!\nThe file is ready for use.\n\n";
xmlCheck = 1;
}
else
{
std::cout << result.description();
std::cout << "XML [" << source << "] parsed with errors, attr value: [" << doc.child("node").attribute("attr").value() << "]\n";
std::cout << "Error description: " << result.description() << "\n";
std::cout << "Error offset: " << result.offset << " (error at [..." << (source + result.offset) << "]\n\n";
}
auto users = doc.child("XmlUsers").child("UsersDb").child("Users").children();
}
static void traverseDocument()
{
int i = 0;
auto user = doc.child("XmlUsers").child("UsersDb").child("Users").children();
for(auto &obj : user)
userMapIdToNodes[i++] = obj;
i = 0;
}
static bool Check(char *username)
{
std::string nume(username);
auto user = doc.child("XmlUsers").child("UsersDb").child("Users").children();
for(auto &obj : user)
{
if(obj.attribute("Username").as_string() == nume)
return true;
}
return false;
}
static int insertUser(char *username)
{
auto node = doc.child("XmlUsers").child("UsersDb").child("Users");
pugi::xml_node param = node.append_child("User");
param.append_attribute("Username") = username;
doc.save_file("files/users.xml");
return getNumberOfUsers() - 1;
}
void insertDownload(const char *titlu, const char *autor, const char *gen, const char *subgen, const char *an)
{
auto nodes = doc.child("XmlUsers").child("UsersDb").child("Users").children();
for(auto user : nodes)
{
pugi::xml_attribute attr = user.attribute("Username");
if(attr.value() == this->username)
{
bool ok = true;
auto downloads = user.children();
for(auto nod : downloads)
{
if(nod.name() == std::string("Download") && nod.attribute("Titlu").value() == std::string(titlu) && nod.attribute("Autor").value() == std::string(autor))
{
ok = false;
break;
}
}
if(ok)
{
pugi::xml_node param = user.append_child("Download");
param.append_attribute("Titlu") = titlu;
param.append_attribute("Autor") = autor;
param.append_attribute("Gen") = gen;
param.append_attribute("Subgen") = subgen;
param.append_attribute("An") = an;
doc.save_file("files/users.xml");
}
break;
}
}
}
void insertEval(const char *titlu, const char *autor, char nota[], const char *gen, const char *subgen, const char *an)
{
auto nodes = doc.child("XmlUsers").child("UsersDb").child("Users").children();
for(auto user : nodes)
{
pugi::xml_attribute attr = user.attribute("Username");
if(attr.value() == this->username)
{
bool ok = true;
auto eval = user.children();
for(auto nod : eval)
{
if(nod.name() == std::string("Book") && nod.attribute("Titlu").value() == std::string(titlu) && nod.attribute("Autor").value() == std::string(autor))
{
nod.attribute("nota").set_value(nota);
doc.save_file("files/users.xml");
ok = false;
break;
}
}
if(ok)
{
pugi::xml_node param = user.append_child("Book");
param.append_attribute("Titlu") = titlu;
param.append_attribute("Autor") = autor;
param.append_attribute("Gen") = gen;
param.append_attribute("Subgen") = subgen;
param.append_attribute("An") = an;
param.append_attribute("nota") = nota;
doc.save_file("files/users.xml");
}
break;
}
}
}
void insertCautare(const char *criteriu, char *parametru)
{
auto nodes = doc.child("XmlUsers").child("UsersDb").child("Users").children();
for(auto user : nodes)
{
pugi::xml_attribute attr = user.attribute("Username");
if(attr.value() == this->username)
{
bool ok = true;
auto caut = user.children();
for(auto nod : caut)
{
if(nod.name() == std::string("Cautare") && nod.attribute(criteriu).as_string() == std::string(parametru))
{
ok = false;
break;
}
}
if(ok)
{
pugi::xml_node param = user.append_child("Cautare");
param.append_attribute(criteriu) = parametru;
doc.save_file("files/users.xml");
}
break;
}
}
}
static int getNumberOfUsers()
{
auto user = doc.child("XmlUsers").child("UsersDb").child("Users").children();
int numberOfUsers = 0;
for(auto &obj : user)
{
numberOfUsers++;
}
return numberOfUsers;
}
void setThreadId(int id)
{
threadId = id;
}
void setUserInfos(int index)
{
auto user = userMapIdToNodes[index];
this->username = user.attribute("Username").as_string();
}
int setUserIdInList(unsigned id)
{
if(id <= getNumberOfUsers())
{
this->userInList = id;
return id;
}
return -1;
}
std::string getUsername()
{
return this->username;
}
void setBooksInList(int id)
{
auto user = userMapIdToNodes[id].children();
}
void initializeVariables(int id)
{
setUserIdInList(id);
setBooksInList(id);
setUserInfos(id);
}
std::string getFavoriteSubgenre()
{
std::map<std::string, int> subgenuri;
std::string pref;
std::vector<std::string> subgenuriCautate;
auto nodes = doc.child("XmlUsers").child("UsersDb").child("Users").children();
for(auto user : nodes)
{
pugi::xml_attribute attr = user.attribute("Username");
if(attr.value() == this->username)
{
auto caut = user.children();
for(auto nod : caut)
{
if(nod.name() == std::string("Cautare"))
{
{
for(auto subgen : Books::fiction)
{
std::string s1 = nod.attribute("subgenre").as_string();
std::size_t found = s1.find(subgen);
if(found != std::string::npos)
{
if(subgenuri[subgen] == 0)
subgenuriCautate.push_back(subgen);
subgenuri[subgen]++;
}
}
}
{
for(auto subgen : Books::nonfiction)
{
std::string s1 = nod.attribute("subgenre").as_string();
std::size_t found = s1.find(subgen);
if(found != std::string::npos)
{
if(subgenuri[subgen] == 0)
subgenuriCautate.push_back(subgen);
subgenuri[subgen]++;
}
}
}
{
for(auto subgen : Books::poetry)
{
std::string s1 = nod.attribute("subgenre").as_string();
std::size_t found = s1.find(subgen);
if(found != std::string::npos)
{
if(subgenuri[subgen] == 0)
subgenuriCautate.push_back(subgen);
subgenuri[subgen]++;
}
}
}
{
for(auto subgen : Books::drama)
{
std::string s1 = nod.attribute("subgenre").as_string();
std::size_t found = s1.find(subgen);
if(found != std::string::npos)
{
if(subgenuri[subgen] == 0)
subgenuriCautate.push_back(subgen);
subgenuri[subgen]++;
}
}
}
}
}
break;
}
}
int max = 0, nrApMax = 0;
for(auto sub : subgenuriCautate)
if(max < subgenuri[sub])
{
max = subgenuri[sub];
pref = sub;
nrApMax = 1;
}
else
if(max == subgenuri[sub])
nrApMax++;
if(max == 0 || nrApMax > 1)
return "niciunul";
return pref;
}
std::string getFavoriteAuthor()
{
std::map<std::string, int> autor;
std::string pref;
std::vector<std::string> autoriCautati;
auto nodes = doc.child("XmlUsers").child("UsersDb").child("Users").children();
for(auto user : nodes)
{
pugi::xml_attribute attr = user.attribute("Username");
if(attr.value() == this->username)
{
auto caut = user.children();
for(auto nod : caut)
{
if(nod.name() == std::string("Cautare"))
{
if(autor[nod.attribute("author").as_string()] == 0)
autoriCautati.push_back(nod.attribute("author").as_string());
autor[nod.attribute("author").as_string()]++;
}
}
break;
}
}
int max = 0, nrApMax = 0;
for(auto aut : autoriCautati)
if(max < autor[aut])
{
max = autor[aut];
pref = aut;
nrApMax = 1;
}
else
if(max == autor[aut])
nrApMax++;
if(max == 0 || nrApMax > 1)
return "niciunul";
return pref;
}
void Determine(std::string nume_user, std::string &genul, std::string &subgenul, std::string &autorul, std::string &anul)
{
std::map<std::string, int> top_autor;
std::map<std::string, int> top_gen;
std::map<std::string, int> top_subgen;
std::map<std::string, int> top_an;
auto nodes = doc.child("XmlUsers").child("UsersDb").child("Users").children();
for(auto user : nodes)
{
pugi::xml_attribute attr = user.attribute("Username");
if(attr.value() == nume_user)
{
auto caut = user.children();
for(auto nod : caut)
{
if(nod.name() == std::string("Cautare"))
{
top_autor[nod.attribute("author").as_string()]++;
top_gen[nod.attribute("genre").as_string()]++;
top_subgen[nod.attribute("subgenre").as_string()]++;
top_an[nod.attribute("year").as_string()]++;
}
else
if(nod.name() == std::string("Book") || nod.name() == std::string("Download"))
{
top_autor[nod.attribute("Autor").as_string()]++;
top_gen[nod.attribute("Gen").as_string()]++;
top_an[nod.attribute("An").as_string()]++;
char c[500];
std::string aux = nod.attribute("Subgen").as_string();
strcpy(c, aux.c_str());
char *p = strtok(c, ",");
while(p != NULL)
{
top_subgen[std::string(p)]++;
p = strtok(NULL, ",");
}
}
}
break;
}
}
for(auto i : top_autor)
{
if(i.second > top_autor[autorul])
autorul = i.first;
}
for(auto i : top_gen)
{
if(i.second > top_gen[genul])
genul = i.first;
}
for(auto i : top_subgen)
{
if(i.second > top_subgen[subgenul])
subgenul = i.first;
}
for(auto i : top_an)
{
if(i.second > top_an[anul])
anul = i.first;
}
}
std::vector<Books> getBooksOfAnUserWithSimilarTaste(Books b[])
{
std::vector<Books> vector_final;
std::map<std::string, bool> mm;
std::string genul, subgenul, autorul, anul;
Determine(this->username, genul, subgenul, autorul, anul);
bool gasitAlt = false, gasitUser = false;
for(int i = 0; i < getNumberOfUsers(); i++)
{
if(userMapIdToNodes[i].attribute("Username").as_string() != this->username)
{
std::string g, sb, aut, an;
Determine(userMapIdToNodes[i].attribute("Username").as_string(), g, sb, aut, an);
if(autorul == aut && genul == g && subgenul == sb || subgenul == sb && anul == an)
{
auto downloads = userMapIdToNodes[i].children();
for(auto x : downloads)
{
if(x.name() == std::string("Download") || (x.name() == std::string("Book") && x.attribute("nota").as_float() >= 7.5))
{
std::string title = x.attribute("Titlu").as_string();
std::string autor = x.attribute("Autor").as_string();
for(int k = 0; k < Books::getNumberOfBooks(); k++)
{
if(b[k].getAuthorOfBook() == autor && b[k].getTitleOfBook() == title && mm[b[k].getISBNOfBook()] != true)
{
mm[b[k].getISBNOfBook()] = true;
vector_final.push_back(b[k]);
}
}
}
}
break;
}
}
}
return vector_final;
}
};
std::map<int, pugi::xml_node> Users::userMapIdToNodes;
pugi::xml_document Users::doc;
int Users::xmlCheck = 1;