-
Notifications
You must be signed in to change notification settings - Fork 1
/
Persistence.cpp
499 lines (451 loc) · 13.3 KB
/
Persistence.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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/**
* @brief The persistence class which contains functions for session-to-session persistence fo data
* @author Gurkiran Tatla
* @author Jake Schindler
* @author Justine Kim
* @author Paul Salvatore
* @author Timal Peramune
*/
#include "Persistence.h"
std::string Persistence::encryptDecrypt(std::string toEncrypt) {
char key = 'K';
char key1 = 'L';
std::string output = toEncrypt;
for (int i = 0; i < toEncrypt.size(); i++){
output[i] = toEncrypt[i] ^ key;
char c =output[i];
if (isspace(c)){
output[i] = toEncrypt[i] ^ key1;
}
}
return output;
}
bool Persistence::storeUser(std::string fname, std::string lname, std::string email, std::string password){
std::string userFile = email + ".txt";
std::ifstream ifile(userFile);
password=encryptDecrypt(password);
if (ifile) { //this file already exists
std::cout << "This account already exists" << std::endl;
return false;
}
else{ //doesnt exist, make a new one
std::ofstream file(userFile);
file << fname + '\n';
file << lname + '\n';
file << email + '\n';
file << password + '\n';
userFile = email + "Bridges.txt";
std::ofstream file2(userFile);
return true;
}
}
std::string Persistence::getFirstName(std::string email){
std::string userFile = email + ".txt";
std::ifstream ifile(userFile);
if (ifile) { //this file already exists
char output[100];
if (ifile.is_open()) {
int counter = 0;
while (!ifile.eof()) {
counter++;
ifile >> output;
if (counter==1){ //first line is name
return output;
}
}
}
}
else { //doesnt exist, make a new one
return "";
}
return "";
}
std::string Persistence::getLastName(std::string email){
std::string userFile = email + ".txt";
std::ifstream ifile(userFile);
if (ifile) { //this file already exists
char output[100];
if (ifile.is_open()) {
int counter = 0;
while (!ifile.eof()) {
counter++;
ifile >> output;
if (counter==2){ //last name is 2nd line
return output;
}
}
}
}
else{ //doesnt exist, make a new one
return "";
}
return "";
}
std::string Persistence::getPassword(std::string email){
std::string userFile = email + ".txt";
std::ifstream ifile(userFile);
if (ifile) { //this file already exists
char output[100];
if (ifile.is_open()) {
int counter = 0;
while (!ifile.eof()) {
counter++;
ifile >> output;
if (counter==4){ //password is 4th line
std::string password = encryptDecrypt(output);
return password;
}
}
}
}
else{ //doesnt exist, make a new one
return "";
}
return "";
}
void Persistence::setFirstName(std::string email, std::string newFirstName){
std::string userFile = email + ".txt";
std::ifstream ifile(userFile);
std::string lname;
std::string password;
//collect text file info
char output[100];
if (ifile.is_open()) {
int counter = 0;
while (!ifile.eof()) {
counter++;
ifile >> output;
if (counter==2){ //lname is 2nd line
lname=output;
}
if (counter==4){ //password is 4th line
password=output;
}
}
}
std::ofstream file(userFile);
file << newFirstName + '\n';
file << lname + '\n';
file << email + '\n';
file << password + '\n';
}
void Persistence::setLastName(std::string email, std::string newLastName){
std::string userFile = email + ".txt";
std::ifstream ifile(userFile);
std::string fname;
std::string password;
//collect text file info
char output[100];
if (ifile.is_open()) {
int counter = 0;
while (!ifile.eof()) {
counter++;
ifile >> output;
if (counter==1){ //fname is 1st line
fname=output;
}
if (counter==4){ //password is 4th line
password=output;
}
}
}
std::ofstream file(userFile);
file << fname + '\n';
file << newLastName + '\n';
file << email + '\n';
file << password + '\n';
}
void Persistence::setEmail(std::string email, std::string newEmail){
std::string userFile = email + ".txt";
std::ifstream ifile(userFile);
std::string fname;
std::string lname;
std::string password;
//collect text file info
char output[100];
if (ifile.is_open()) {
int counter = 0;
while (!ifile.eof()) {
counter++;
ifile >> output;
if (counter==1){ //fname is 1st line
fname=output;
}
if (counter==2){ //fname is 1st line
lname=output;
}
if (counter==4){ //password is 4th line
password=output;
}
}
}
userFile = newEmail + ".txt";
std::ofstream file(userFile);
file << fname + '\n';
file << lname + '\n';
file << newEmail + '\n';
file << password + '\n';
userFile = newEmail + "Bridges.txt";
std::ofstream file2(userFile);
file2 << fname + '\n';
file2 << lname + '\n';
file2 << newEmail + '\n';
file2 << password + '\n';
}
void Persistence::setPassword(std::string email, std::string newPassword){
std::string userFile = email + ".txt";
std::ifstream ifile(userFile);
std::string fname;
std::string lname;
//collect text file info
char output[100];
if (ifile.is_open()) {
int counter = 0;
while (!ifile.eof()) {
counter++;
ifile >> output;
if (counter==1){ //fname is 1st line
fname=output;
}
if (counter==2){ //password is 4th line
lname=output;
}
}
}
std::ofstream file(userFile);
file << fname + '\n';
file << lname + '\n';
file << email + '\n';
file << encryptDecrypt(newPassword) + '\n';
//need to hash password still
}
bool Persistence::deleteUser(std::string email){
std::string filename = email + ".txt";
if(remove((filename).c_str())){
return true;
}else{
return false;
};
}
//delete all their groups as well
///*
//* incrementFilenum(int filenum)
//* Static method that increments the given integer by 1
//* @param: int filenum: integer to increment
//* @return: incremented integer
//*/
//int Persistence::incrementFilenum(int filenum) {
// filenum += 1;
// return filenum;
//}
///*
//* makeGroup(std::string userID, int groupID, std::string groupname)
//* Static method that creates a new light group for a given user
//* @param: std::string email: user's unique email
//* @param: int groupID: group number for the user
//* @param: std::string groupname: user can give the group a descriptor
//* @return: true if group is successfully made, false otherwise
//*/
//bool Persistence::makeGroup(std::string email, int groupID, std::string groupname) {
// std::string filename = email + "group" + std::to_string(groupID) + ".txt";
// std::ifstream ifile(filename);
// if (ifile) { //this file already exists
// std::cout << "This group already exists" << std::endl;
// return false;
//
// }
// else{ //doesnt exist, make a new one
// std::ofstream file(filename);
// file << groupname + '\n';
// return true;
// }
//}
//
///*
//* makeGroup(std::string userID, std::string groupname)
//* Static method that creates the first new light group for a given user
//* @param: std::string email: user's unique email
//* @param: std::string groupname: user can give the group a descriptor
//* @return: true if group is successfully made, false otherwise
//*/
////bool Persistence::makeGroup(std::string email, std::string groupname) {
//// int filenum=0;
//// std::string filename = email + "group" + std::to_string(filenum) + ".txt";
//// std::ifstream infile(filename.c_str());
//// std::ofstream groupfile;
//// if(infile) {
//// infile.close();
//// int newfilenum = incrementFilenum(filenum);
//// makeGroup(email, newfilenum, groupname);
//// }
//// else {
//// groupfile.open(filename.c_str());
//// groupfile << groupname;
//// groupfile.close();
//// }
//// return 0;
////}
//
///*
//* addLightToGroup(std::string userID, int groupID, int lightID)
//* Static method that adds a new lightID to a given group owned by a given user
//* @param: std::string email: user's unique email
//* @param: int groupID: group number for the user
//* @param: int lightID: ID number for the light
//* @return: true if light is successfully added, false otherwise
//*/
//bool Persistence::addLightToGroup(std::string email, int groupID, int lightID) {
// std::string filename = email + "group" + std::to_string(groupID) + ".txt";
// std::string newLight = std::to_string(lightID);
// std::ifstream infile(filename.c_str());
// std::ofstream groupfile;
// groupfile.open(filename.c_str(), std::ios_base::app);
// if(!infile) {
// std::cout << "That group doesn't exist" << std::endl;
// std::remove(filename.c_str());
// return true;
// }
// std::string line;
// while(std::getline(infile, line)) {
// if(line == std::to_string(lightID)) {
// std::cout << "That light already exists" << std::endl;
// return true;
// }
// }
// groupfile << newLight.c_str();
// groupfile << "\n";
// groupfile.close();
// return false;
//}
//
///*
//* deleteLightFromGroup(std::string userID, int groupID, int lightID)
//* Static method that deletes a lightID from a given group owned by a given user
//* @param: std::string email: user's unique email
//* @param: int groupID: group number for the user
//* @param: int lightID: ID number for the light
//* @return: true if light is successfully deleted, false otherwise
//*/
//bool Persistence::deleteLightFromGroup(std::string email, int groupID, int lightID) {
// std::string filename = email + "group" + std::to_string(groupID) + ".txt";
// std::string deletedLight = std::to_string(lightID);
// std::ifstream groupfile(filename.c_str());
// if(!groupfile) {
// std::cout << "That group doesn't exist" << std::endl;
// std::remove(filename.c_str());
// return 1;
// }
// std::string line;
// std::ofstream tempfile;
// tempfile.open("tempfile.txt");
// while(std::getline(groupfile, line)) {
// if(line != deletedLight && !line.empty()) {
// tempfile << "\n";
// tempfile << line;
// }
// }
// tempfile.close();
// groupfile.close();
// std::remove(filename.c_str());
// std::rename("tempfile.txt", filename.c_str());
// return 0;
//}
//
///*
//* deleteGroup(std::string userID, int groupID)
//* Static method that deletes a specified group owned by a given user
//* @param: std::string email: user's unique email
//* @param: int groupID: group number for the user
//* @return: true if group is successfully deleted, false otherwise
//*/
//bool Persistence::deleteGroup(std::string email, int groupID) {
// std::string filename = email + "group" + std::to_string(groupID) + ".txt";
// if(std::remove(filename.c_str()) != 0) {
// perror("That group doesn't exist");
// return 1;
// }
// else {
// puts("Group successfully deleted");
// }
// return 0;
//}
static int incrementLightNum(int lightNum) {
lightNum += 1;
return lightNum;
}
static bool makeLight(std::string bridgeID, int lightID) {
std::string filename = bridgeID + "light" + std::to_string(lightID) + ".txt";
std::ifstream infile(filename.c_str());
if(infile) {
infile.close();
int newLightID = incrementLightNum(lightID);
return makeLight(bridgeID, newLightID);
}
else {
std::ofstream lightFile;
lightFile.open(filename.c_str());
lightFile.close();
std::cout << "Light successfully created" << std::endl;
}
return 0;
}
static bool makeLight(std::string bridgeID) {
int lightID = 0;
std::string filename = bridgeID + "light" + std::to_string(lightID) + ".txt";
std::ifstream infile(filename.c_str());
if(infile) {
infile.close();
int newLightID = incrementLightNum(lightID);
return makeLight(bridgeID, newLightID);
}
else {
std::ofstream lightFile;
lightFile.open(filename.c_str());
lightFile.close();
std::cout << "Light successfully created" << std::endl;
}
return 0;
}
static bool deleteLight(std::string bridgeID, int lightID) {
std::string filename = bridgeID + "light" + std::to_string(lightID) + ".txt";
if(std::remove(filename.c_str()) != 0) {
perror("That light doesn't exist");
return 1;
}
else {
puts("Light successfully deleted");
}
return 0;
}
/*
* Day: 1-7 (Sunday-Saturday)
* Hour: 0-23 (12am-11pm)
* State: 0, 1 (off, on)
*/
static bool storeData(std::string bridgeID, int lightID, int day, int hour, int state) {
if(day < 1 || day > 7) {
std::cout << "Please enter a valid day" << std::endl;
return 1;
}
else if(hour < 0 || hour > 23) {
std::cout << "Please enter a valid time" << std::endl;
return 1;
}
else if(state < 0 || state > 1) {
std::cout << "Please enter a valid state" << std::endl;
return 1;
}
else {
std::string filename = bridgeID + "light" + std::to_string(lightID) + ".txt";
std::ifstream infile(filename.c_str());
if(!infile) {
std::cout << "That light doesn't exist" << std::endl;
std::remove(filename.c_str());
return 1;
}
std::ofstream groupfile;
groupfile.open(filename.c_str(), std::ios_base::app);
groupfile << std::to_string(day) << "\t" << std::to_string(hour) << "\t" << std::to_string(state) << std::endl;
return 0;
}
}