-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.cpp
More file actions
197 lines (150 loc) · 6.81 KB
/
Copy pathuser.cpp
File metadata and controls
197 lines (150 loc) · 6.81 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
#include <string>
#include <unordered_map>
#include <fstream>
#include <iostream>
#include "user.h"
using namespace std;
void User::addUser(std::shared_ptr<User> user, const std::string& username) {
cout << "adding user to map: " << username << endl;
loadedUsers.emplace(username, std::move(user));
}
bool User::deleteUser(const string& username) {
if (const auto it = loadedUsers.find(username); it != loadedUsers.end()) {
loadedUsers.erase(it);
return true;
}
return false;
}
bool User::userExists(const string& username) {
if (const auto it = loadedUsers.find(username); it != loadedUsers.end()) {
return true;
}
return false;
}
shared_ptr<User> User::getUser(const string& username){
if (const auto it = loadedUsers.find(username); it != loadedUsers.end()) {
return it->second;
}
return nullptr;
}
bool User::correctPassword(const string& username, const string& password) {
if (const auto it = loadedUsers.find(username); it != loadedUsers.end()) {
return it->second->getPassword() == password;
}
return false;
}
unsigned int User::loadAllUsers() {
ifstream usersFile("files/users.dat", ios::binary);
if (!usersFile.is_open()) {
//create file if it doesn't exist
ofstream createFile("files/users.dat", ios::binary);
createFile.close();
usersFile.open("files/users.dat", ios::binary);
}
if (!usersFile.is_open()) {
cout << "File could not be opened, read-binary, users.data" << endl;
return 0;
}
//total number of users in file
// FILE STRUCTURE (For each user)
// Username String Length (int), Char[] of string username
// Password String Length (int), Char[] of string password
// DEVICES SECTION PER USER
// number of total devices (int)
//for each device
// Name Length (int), char[] of string name
// Brand Length (int), char[] of string brand
// Model Length (int), char[] of string model
// IMEI Length (int), char[] of string IMEI
// Seller Length (int), char[] of string seller
// Listed Status (bool)
unsigned int numUsers = 0;
usersFile.read(reinterpret_cast<char*>(&numUsers), sizeof(numUsers));
uint8_t fieldLength;// 0 - 12
for (int i = 0; i < numUsers; i++) {
//read username data
usersFile.read(reinterpret_cast<char*>(&fieldLength), sizeof(fieldLength));
string username(fieldLength, '\0');
usersFile.read(&username[0], fieldLength);
//read password data
usersFile.read(reinterpret_cast<char*>(&fieldLength), sizeof(fieldLength));
string password(fieldLength, '\0');
usersFile.read(&password[0], fieldLength);
//setup User pointer and add to map
auto user = std::make_shared<User>(username, password);
loadedUsers.emplace(username, user);
std::cout << "Loading user: " << username << std::endl;
//loading user devices
std::cout << "Loading devices for -> " << username <<std::endl;
//get total amount of devices per user
unsigned int devicesCount =0;
usersFile.read(reinterpret_cast<char*>(&devicesCount), sizeof(devicesCount));
//loop through their devices and get 5 string from each
for (int j = 0; j < devicesCount; j++) {
string prompts[5];
for (string& prompt : prompts) {
usersFile.read(reinterpret_cast<char*>(&fieldLength), sizeof(fieldLength));
prompt.resize(fieldLength, '\0');
usersFile.read(&prompt[0], fieldLength);
}
//read marketplace attributes
bool listed = false;
usersFile.read(reinterpret_cast<char*>(&listed), sizeof(bool));
string listCode;
usersFile.read(reinterpret_cast<char*>(&fieldLength), sizeof(fieldLength));
listCode.resize(fieldLength, '\0');
usersFile.read(&listCode[0], fieldLength);
listed ? user->addDevice(prompts, listed, listCode) : user->addDevice(prompts);
cout << "Marketplace Conditions: " << listed << listCode << endl;
}
}
cout << "Successfully read user data." << endl << endl;
return numUsers;
}
//write usernames and passwords and devices per user.
void User::saveUserData() {
ofstream usersFile("files/users.dat", ios::binary);
if (!usersFile.is_open()) {
cout << "File could not be opened to save." << endl;
return;
}
uint8_t fieldLength;
cout << "Saving all user data..." << endl;
//the new user was already added to loadedUsers Map when createAccount is called.
//save the size of loaded users map
unsigned int size = loadedUsers.size();
usersFile.write(reinterpret_cast<char*>(&size), sizeof(size));
//loop through all users saving username, password, devices
for (const auto&[name, user] : loadedUsers) {
//write username size and its characters
fieldLength = static_cast<uint8_t>(user->getUsername().size());
usersFile.write(reinterpret_cast<char*>(&fieldLength), sizeof(fieldLength));
usersFile.write(user->getUsername().c_str(), fieldLength);
//write password size and its characters
fieldLength = static_cast<uint8_t>(user->getPassword().size());
usersFile.write(reinterpret_cast<char*>(&fieldLength), sizeof(fieldLength));
usersFile.write(user->getPassword().c_str(), fieldLength);
//write devices
//devices count
unsigned int numDevices = user->devices.size();
cout << "Saving devices for " << user->getUsername() <<" -> amount, " << numDevices << endl;
usersFile.write(reinterpret_cast<char*>(&numDevices), sizeof(numDevices));
for (const auto&[name, device] : user->devices) {
string prompts[5] = {device->name, device->model, device->brand, device->imeiNumber, device->seller};
cout << "Saving device: " << name << endl;
for (const auto& prompt : prompts) {
//write length and the characters of string
fieldLength = static_cast<uint8_t>(prompt.size());
usersFile.write(reinterpret_cast<char*>(&fieldLength), sizeof(fieldLength));
usersFile.write(prompt.c_str(), fieldLength);
}
bool listed = device->listed;
usersFile.write(reinterpret_cast<char*>(&listed), sizeof(listed));
fieldLength = static_cast<uint8_t>(device->listCode.size());
usersFile.write(reinterpret_cast<char*>(&fieldLength), sizeof(fieldLength));
usersFile.write(device->listCode.c_str(), fieldLength);
cout << "Marketplace Conditions: " << listed << device->listCode << endl;
}
}
cout << "Successfully saved user data." << endl << endl;
}