-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.c
732 lines (698 loc) · 20.5 KB
/
project.c
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//creates a global head pointer for the structures
struct AccountNode *Accounthead = NULL;
struct ContactNode *Contacthead = NULL;
// to call the functions
void login(char *name, char *password);
void freeAccountList();
void freeContactList();
struct AccountNode *createAccountNode(const char *name, const char *password);
struct ContactNode *createContactNode(const char *name, const char *number);
void appendAccountNode(struct AccountNode **head, const char *name, const char *password);
void printAccountList();
void loadAccountsCSV();
void unloadAccountCSV();
void freeContactList();
struct ContactNode *createContactNode(const char *name, const char *number);
void appendContactNode(struct ContactNode **head, const char *name, const char *number);
void printContactList();
void loadContactCSV(char *filename);
void unloadContactCSV(char *filename);
void newAccount();
void loggingIn();
void deleteAccount();
void forgetPassword();
void newcontact(char *name, char *password);
void deletecontact(char *name, char *password);
void editName(char *name, char *password);
void editNumber(char *name, char *password);
char *searchContact(char *name, char *password);
void login(char *name, char *password);
// creates a structure to store the accounts
struct AccountNode
{
char name[100];
char password[100];
struct AccountNode *prev;
struct AccountNode *next;
};
// deletes an account
void freeAccountList()
{
struct AccountNode *current = Accounthead;
while (current != NULL)
{
struct AccountNode *temp = current;
current = current->next;
free(temp);
}
Accounthead = NULL;
}
// creates new account nodes
struct AccountNode *createAccountNode(const char *name, const char *password)
{
struct AccountNode *newNode = (struct AccountNode *)malloc(sizeof(struct AccountNode));
if (newNode == NULL)
{
perror("\t\t\t Memory allocation failed");
exit(1);
}
strncpy(newNode->name, name, sizeof(newNode->name));
strncpy(newNode->password, password, sizeof(newNode->password));
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
// adds account nodes to account structure in the begining
void appendAccountNode(struct AccountNode **head, const char *name, const char *password)
{
struct AccountNode *newNode = createAccountNode(name, password);
if (*head == NULL)
{
*head = newNode;
}
else
{
struct AccountNode *current = *head;
struct AccountNode *temp = current->next;
current->next = newNode;
newNode->prev = current;
newNode->next = temp;
if (temp != NULL)
{
temp->prev = newNode;
}
}
}
// prints the list of accounts
void printAccountList()
{
struct AccountNode *current = Accounthead;
while (current != NULL)
{
printf("\t\t\t Name: %s, Password: %s\n", current->name, current->password);
current = current->next;
}
}
// reads information from accounts csv file
void loadAccountsCSV()
{
freeAccountList();
FILE *file = fopen("accounts.csv", "r");
if (file == NULL)
{
perror("\t\t\t Error opening the file");
return;
}
char line[256];
while (fgets(line, sizeof(line), file) != NULL)
{
char name[100];
char password[100];
if (sscanf(line, " %[^,],%s", name, password) == 2)
{
appendAccountNode(&Accounthead, name, password);
}
}
fclose(file);
}
// uploads the account information to 'accounts.csv'
void unloadAccountCSV()
{
FILE *fptr = fopen("accounts.csv", "w");
if (fptr == NULL)
{
perror("\t\t\t Error opening the file");
return;
}
struct AccountNode *current = Accounthead;
int numAccount = 0;
while (current != NULL)
{
numAccount++;
current = current->next;
}
struct AccountNode *accountArray = (struct AccountNode *)malloc(numAccount * sizeof(struct AccountNode));
current = Accounthead;
for (int i = 0; i < numAccount; i++)
{
strcpy(accountArray[i].name, current->name);
strcpy(accountArray[i].password, current->password);
current = current->next;
}
for (int i = 0; i < numAccount - 1; i++)
{
for (int j = i + 1; j < numAccount; j++)
{
if (strcmp(accountArray[i].name, accountArray[j].name) > 0)
{
struct AccountNode temp = accountArray[i];
accountArray[i] = accountArray[j];
accountArray[j] = temp;
}
}
}
for (int i = 0; i < numAccount; i++)
{
fprintf(fptr, "%s,%s\n", accountArray[i].name, accountArray[i].password);
}
fclose(fptr);
free(accountArray);
freeAccountList();
}
// creates a structure to store contact information
struct ContactNode
{
char name[100];
char number[100];
struct ContactNode *prev;
struct ContactNode *next;
};
// deletes a contact
void freeContactList()
{
struct ContactNode *head = Contacthead;
while (head != NULL)
{
struct ContactNode *temp = head;
head = head->next;
free(temp);
}
Contacthead = NULL;
}
// creates nodes for contact structure
struct ContactNode *createContactNode(const char *name, const char *number)
{
struct ContactNode *newNode = (struct ContactNode *)malloc(sizeof(struct ContactNode));
if (newNode == NULL)
{
perror("\t\t\t Memory allocation failed");
exit(1);
}
strncpy(newNode->name, name, sizeof(newNode->name));
strncpy(newNode->number, number, sizeof(newNode->number));
newNode->next = NULL;
return newNode;
}
// adds contact nodes to contact structure at the begining
void appendContactNode(struct ContactNode **head, const char *name, const char *number)
{
struct ContactNode *newNode = createContactNode(name, number);
if (*head == NULL)
{
*head = newNode;
}
else
{
struct ContactNode *current = *head;
struct ContactNode *temp = current->next;
current->next = newNode;
newNode->prev = current;
newNode->next = temp;
if (temp != NULL)
{
temp->prev = newNode;
}
}
}
// reads the information from respective account csv file
void loadContactCSV(char *filename)
{
freeContactList();
FILE *file = fopen(filename, "r");
if (file == NULL)
{
perror("\t\t\t Error opening the file");
return;
}
char line[256];
while (fgets(line, sizeof(line), file) != NULL)
{
char name[100];
char number[100];
if (sscanf(line, "%[^,],%s", name, number) == 2)
{
appendContactNode(&Contacthead, name, number);
}
}
fclose(file);
}
// uploads the contact information in a csv file named according to username
void unloadContactCSV(char *filename)
{
FILE *fptr = fopen(filename, "w");
if (fptr == NULL)
{
perror("\t\t\t Error opening the file");
return;
}
struct ContactNode *current = Contacthead;
int numContact = 0;
while (current != NULL)
{
numContact++;
current = current->next;
}
struct ContactNode *contactArray = (struct ContactNode *)malloc(numContact * sizeof(struct ContactNode));
current = Contacthead;
for (int i = 0; i < numContact; i++)
{
strcpy(contactArray[i].name, current->name);
strcpy(contactArray[i].number, current->number);
current = current->next;
}
for (int i = 0; i < numContact - 1; i++)
{
for (int j = i + 1; j < numContact; j++)
{
if (strcmp(contactArray[i].name, contactArray[j].name) > 0)
{
struct ContactNode temp = contactArray[i];
contactArray[i] = contactArray[j];
contactArray[j] = temp;
}
}
}
for (int i = 0; i < numContact; i++)
{
fprintf(fptr, "%s,%s\n", contactArray[i].name, contactArray[i].number);
}
fclose(fptr);
free(contactArray);
freeContactList();
}
// creates new account
void newAccount()
{
char name[15];
int flag = 0;
char password[15];
printf("\n\t\t\t Enter your name: ");
scanf(" %[^\n]", name);
printf("\n");
printf("\t\t\t Enter your password: ");
scanf(" %[^\n]", password);
printf("\n");
loadAccountsCSV();
struct AccountNode *curr = Accounthead;
while (curr != NULL)
{
if (strcmp(curr->name, name) == 0)
{
flag = 1;
}
curr = curr->next;
}
if (flag == 0)
{
appendAccountNode(&Accounthead, name, password);
unloadAccountCSV(Accounthead);
char filename[50];
snprintf(filename, sizeof(filename), "%s%s.csv", name, password);
FILE *file = fopen(filename, "w");
if (file == NULL)
{
perror("\n\t\t\t Failed to create file");
return;
}
fclose(file);
}
}
// logs into an existing account
void loggingIn()
{
char name[15];
char password[15];
printf("\n\t\t\t Enter your name: ");
scanf(" %[^\n]", name);
printf("\n");
printf("\t\t\t Enter your password: ");
scanf(" %[^\n]", password);
printf("\n");
loadAccountsCSV();
struct AccountNode *curr = Accounthead;
while (curr != NULL)
{
if (strcmp(curr->name, name) == 0 && strcmp(curr->password, password) == 0)
{
login(name, password);
return;
}
curr = curr->next;
}
printf("\n\t\t\t Login credentials not found!\n");
}
// deletes an account
void deleteAccount()
{
char name[15];
char password[15];
printf("\n\t\t\t Enter your name: ");
scanf(" %[^\n]", name);
printf("\033[0m");
printf("\n\t\t\t Enter your password: ");
scanf("%s", password);
printf("\n");
loadAccountsCSV();
if (Accounthead == NULL)
{
printf("\n\t\t\t No accounts found.\n");
return;
}
struct AccountNode *curr = Accounthead;
struct AccountNode *prev = NULL;
while (curr != NULL)
{
if (strcmp(curr->name, name) == 0 && strcmp(curr->password, password) == 0)
{
if (prev == NULL)
{
Accounthead = Accounthead->next;
}
else
{
prev->next = curr->next;
}
free(curr);
char filename[50];
snprintf(filename, sizeof(filename), "%s%s.csv", name, password);
if (remove(filename) == 0)
{
printf("\n\t\t\t Account '%s' has been deleted.\n", name);
}
else
{
printf("\n\t\t\t Failed to delete account '%s'.\n", name);
}
unloadAccountCSV(Accounthead);
return;
}
prev = curr;
curr = curr->next;
}
printf("\n\t\t\t Account with name '%s' and password '%s' not found.\n", name, password);
}
// changes the password incase the user forgets it
// ADMIN PASSWORD IS 123
void forgetPassword()
{
int adminPass;
printf("\n\t\t\t Enter admin password : ");
scanf("%d", &adminPass);
printf("\n");
if (adminPass != 123)
{
printf("\t\t\t Wrong admin password!!");
return;
}
char name[15], newpass[15];
printf("\t\t\t Enter your name : ");
scanf(" %[^\n]", name);
printf("\033[0m");
loadAccountsCSV();
struct AccountNode *current = Accounthead;
while (current != NULL)
{
if ((strcmp(current->name, name) == 0))
{
printf("\t\t\t Enter your new password : ");
scanf(" %[^\n]", newpass);
printf("\033[0m");
char oldName[35];
strcpy(oldName, name);
strcat(oldName, current->password);
strcat(oldName, ".csv");
char newName[35];
strcpy(newName, name);
strcat(newName, newpass);
strcat(newName, ".csv");
strcpy(current->password, newpass);
unloadAccountCSV(Accounthead);
// csvfile
if (rename(oldName, newName) == 0)
{
printf("\t\t\t File renamed successfully.\n");
}
else
{
perror("\t\t\t Error renaming file\n");
}
return;
}
current = current->next;
}
}
// creates a new contact
void newcontact(char *name, char *password)
{
char contactname[15];
char number[15];
int flag = 0;
printf("\t\t\t Enter your name : ");
scanf(" %[^\n]", contactname);
printf("\033[0m");
printf("\t\t\t Enter your number : ");
scanf(" %[^\n]", number);
printf("\033[0m");
char filename[50];
snprintf(filename, sizeof(filename), "%s%s.csv", name, password);
loadContactCSV(filename);
struct ContactNode *curr = Contacthead;
while (curr != NULL)
{
if (strcmp(curr->name, contactname) == 0 && strcmp(curr->number, number) == 0)
{
flag = 1;
}
curr = curr->next;
}
if (flag == 0)
{
appendContactNode(&Contacthead, contactname, number);
unloadContactCSV(filename);
}
}
// deletes a contact
void deletecontact(char *name, char *password)
{
char contactname[15];
char number[15];
printf("\t\t\t Enter your name : ");
scanf(" %[^\n]", contactname);
printf("\033[0m");
printf("\t\t\t Enter your number : ");
scanf(" %[^\n]", number);
printf("\033[0m");
char filename[50];
snprintf(filename, sizeof(filename), "%s%s.csv", name, password);
loadContactCSV(filename);
if (Contacthead == NULL)
{
printf("\t\t\t No contacts found.");
return;
}
struct ContactNode *curr = Contacthead;
struct ContactNode *prev = NULL;
while (curr != NULL)
{
if (strcmp(curr->name, contactname) == 0 && strcmp(curr->number, number) == 0)
{
if (prev == NULL)
{
Contacthead = Contacthead->next;
}
else
{
prev->next = curr->next;
}
free(curr);
unloadContactCSV(filename);
return;
}
prev = curr;
curr = curr->next;
}
printf("\t\t\t Account with name '%s' and number '%s' not found.\n", contactname, number);
}
// edits a name of an existing contact
void editName(char *name, char *password)
{
char filename[50];
snprintf(filename, sizeof(filename), "%s%s.csv", name, password);
loadContactCSV(filename);
printf("\t\t\t Enter the number whose name you want to edit : ");
char number[15];
scanf(" %[^\n]", number);
printf("\033[0m");
char Contactname[15];
printf("\t\t\t Enter the new name : ");
scanf(" %[^\n]", Contactname);
printf("\033[0m");
struct ContactNode *curr = Contacthead;
while (curr != NULL)
{
if (strcmp(curr->number, number) == 0)
{
strcpy(curr->name, Contactname);
break;
}
curr = curr->next;
}
unloadContactCSV(filename);
}
// edit the number of an existing contact
void editNumber(char *name, char *password)
{
char filename[50];
snprintf(filename, sizeof(filename), "%s%s.csv", name, password);
loadContactCSV(filename);
printf("\t\t\t Enter the name whose number you want to edit : ");
char Contactname[15];
scanf(" %[^\n]", Contactname);
printf("\033[0m");
char number[15];
printf("\t\t\t Enter the new number : ");
scanf(" %[^\n]", number);
printf("\033[0m");
struct ContactNode *curr = Contacthead;
while (curr != NULL)
{
if (strcmp(curr->name, Contactname) == 0)
{
strcpy(curr->number, number);
break;
}
curr = curr->next;
}
unloadContactCSV(filename);
}
// searches for a contact
char *searchContact(char *name, char *password)
{
char filename[50];
snprintf(filename, sizeof(filename), "%s%s.csv", name, password);
loadContactCSV(filename);
char contactName[15];
char contactNumber[15];
printf("\t\t\t Enter the name of the contact : ");
scanf(" %[^\n]", contactName);
printf("\033[0m");
struct ContactNode *curr = Contacthead;
while (curr != NULL)
{
if (strcmp(curr->name, contactName) == 0)
{
strcpy(contactNumber, curr->number);
freeContactList();
printf("\t\t\t This is the contact number : %s\n", contactNumber);
return contactNumber;
}
curr = curr->next;
}
freeContactList();
printf("\t\t\t No account found\n", contactNumber);
return "-1";
}
// prints all contacts in the account
void printContactList(char *name, char *password)
{
char filename[50];
snprintf(filename, sizeof(filename), "%s%s.csv", name, password);
loadContactCSV(filename);
struct ContactNode *current = Contacthead;
while (current != NULL)
{
printf("\t\t\t Name: %s \t Phone-number: %s\n", current->name, current->number);
current = current->next;
}
}
// shows the options after loging into an existing account
void login(char *name, char *password)
{
printf("\t\t\t ------------------------------------------\n");
printf("\t\t\t WELCOME %s!\n", name);
while (1)
{
int choice;
printf("\t\t\t ------------------------------------------\n");
printf("\t\t\t 1. CREATE NEW CONTACT\n");
printf("\t\t\t 2. DELETE A CONTACT\n");
printf("\t\t\t 3. EDIT NAME\n");
printf("\t\t\t 4. EDIT NUMBER\n");
printf("\t\t\t 5. SEARCH A CONTACT\n");
printf("\t\t\t 6. DISPLAY CONTACT\n");
printf("\t\t\t 7. EXIT\n");
printf("\t\t\t ENTER YOUR CHOICE: ");
scanf("%d", &choice);
printf("\n");
switch (choice)
{
case 1:
newcontact(name, password);
printf("\t\t\t New contact has been added\n");
break;
case 2:
deletecontact(name, password);
break;
case 3:
editName(name, password);
break;
case 4:
editNumber(name, password);
break;
case 5:
searchContact(name, password);
break;
case 6:
printContactList(name, password);
break;
case 7:
return;
default:
printf("\t\t\t WRONG CHOICE\n\n");
}
}
}
// shows all the available options in the program
int main()
{
printf("\t\t\t ------------------------------------------\n");
printf("\t\t\tWELCOME TO OUR PHONE DIRECTORY\n");
printf("\t\t\t------------------------------------------\n");
while (1)
{
int choice;
printf("\t\t\t ------------------------------------------\n");
printf("\t\t\t 1. NEW USER RESIGTRATION\n");
printf("\t\t\t 2. LOGIN/SIGNIN\n");
printf("\t\t\t 3. DELETION ACCOUNT\n");
printf("\t\t\t 4. FORGOT PASSWORD\n");
printf("\t\t\t 5. EXIT\n");
printf("\t\t\t ENTER YOUR CHOICE: ");
scanf("%d", &choice);
printf("\n");
switch (choice)
{
case 1:
newAccount();
printf("\t\t\t Your account has been successfully created!\n\n");
break;
case 2:
loggingIn();
printf("\t\t\t Your account is not found, please create a new account to login\n");
break;
case 3:
deleteAccount();
break;
case 4:
forgetPassword();
break;
case 5:
exit(0);
default:
printf("\t\t\t WRONG CHOICE\n\n");
}
}
}