-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileWithUsers.cpp
More file actions
86 lines (79 loc) · 2.58 KB
/
FileWithUsers.cpp
File metadata and controls
86 lines (79 loc) · 2.58 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
#include "FileWithUsers.h"
#include "Markup.h"
#include "SupportMethods.h"
using namespace std;
void FileWithUsers :: addUserToFile(User user) {
CMarkup xml;
string fileNameWithUsers = getFileName();
bool fileExists = xml.Load(fileNameWithUsers);
if (!fileExists) {
xml.SetDoc("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
xml.AddElem("Users");
}
xml.FindElem();
xml.IntoElem();
xml.AddElem("User");
xml.IntoElem();
xml.AddElem("UserID", user.getUserId());
xml.AddElem("Login", user.getLogin());
xml.AddElem("Password", user.getPassword());
xml.AddElem("Name", user.getUserName());
xml.AddElem("Surname", user.getUserSurname());
xml.Save(fileNameWithUsers);
}
vector <User> FileWithUsers :: loadUserFromFile() {
User user;
vector <User> users;
CMarkup xml;
string fileNameWithUsers = getFileName();
bool fileExists = xml.Load(fileNameWithUsers);
if (fileExists == true) {
xml.FindElem();
xml.IntoElem();
while ( xml.FindElem("User") == true) {
xml.IntoElem();
xml.FindElem( "UserID");
int userId = atoi(xml.GetData().c_str());
user.setupUserId(userId);
xml.FindElem( "Login");
string login = xml.GetData();
user.setupLogin(login);
xml.FindElem( "Password");
string password = xml.GetData();
user.setupPassword(password);
xml.FindElem( "Name");
string userName = xml.GetData();
user.setupUserName(userName);
xml.FindElem( "Surname");
string userSurname = xml.GetData();
user.setupUserSurname(userSurname);
users.push_back(user);
xml.OutOfElem();
}
}
return users;
}
bool FileWithUsers::changePasswordLoggedUser(vector <User>::iterator itr) {
CMarkup xml;
string fileNameWithUsers = getFileName();
if (xml.Load(fileNameWithUsers)) {
xml.FindElem();
xml.IntoElem();
while(xml.FindElem("User")) {
xml.IntoElem();
xml.FindElem("UserID");
int userId = SupportMethods::convertStringToInt(xml.GetData());
if (userId == itr -> getUserId()) {
xml.FindElem("Password");
xml.SetData(itr -> getPassword());
xml.Save(fileNameWithUsers);
return true;
}
xml.OutOfElem();
}
} else {
cout << "Cannot open file with name :" << getFileName() << endl;
return false;
}
return false;
}