-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserFile.cpp
102 lines (81 loc) · 2.5 KB
/
UserFile.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "UserFile.h"
User UserFile::getUserData()
{
User user;
xml.IntoElem(); //Make User element current parent position
xml.FindElem("userId");
user.setUserId(atoi(xml.GetData().c_str()));
xml.FindElem("name");
user.setName(xml.GetData());
xml.FindElem("surname");
user.setSurname(xml.GetData());
xml.FindElem("login");
user.setLogin(xml.GetData());
xml.FindElem("password");
user.setPassword(xml.GetData());
xml.OutOfElem(); //Make Users element current parent position again (go out from current user)
return user;
}
vector<User> UserFile::loadUsersFromFile()
{
if(xml.Load(getFileName()))
{
vector<User> users;
xml.FindElem(); //Sets position to "Users" element
xml.IntoElem(); //Make Users element current parent position
while(xml.FindElem("User"))
{
users.push_back(getUserData());
}
return users;
}
else
{
cout << "Nie udalo sie zaladowac pliku z uzytkownikami!";
return vector<User>();
}
}
void UserFile::appendUserToFile(User user)
{
bool fileExists = xml.Load(getFileName());
if(!fileExists)
{
xml.SetDoc("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
xml.AddElem("Users");
}
xml.FindElem(); //Sets position to "Users" element
xml.IntoElem(); //Make Users element current parent position
xml.AddElem("User");
xml.IntoElem();
xml.AddElem("userId", user.getUserId());
xml.AddElem("name", user.getName());
xml.AddElem("surname", user.getSurname());
xml.AddElem("login", user.getLogin());
xml.AddElem("password", user.getPassword());
xml.Save(getFileName());
}
void UserFile::updateUserPassword(User user)
{
if(xml.Load(getFileName()))
{
xml.FindElem(); //Sets position to "Users" element
xml.IntoElem(); //Make Users element current parent position
while(xml.FindElem("User"))
{
xml.FindChildElem("userId");
if(AuxiliaryMethods::stringToInt(xml.GetChildData()) == user.getUserId())
{
//User found
xml.FindChildElem("password");
xml.SetChildData(user.getPassword());
xml.Save(getFileName());
return;
}
}
AuxiliaryMethods::timedMessage("BLAD! W pliku nie ma takiego uzytkownika!");
}
else
{
AuxiliaryMethods::timedMessage("Nie udalo sie zaladowac pliku z uzytkownikami!");
}
}