-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathola.c
1664 lines (1496 loc) · 51.7 KB
/
ola.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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "ola.h"
#include "settings.h"
//Function: open_log
//Description: opens the log file to write the success or failure status
//Input param: NULL
//Return Type: NULL
void open_log()
{
// Open the file
flog = fopen("log_txt","a+");
// If unable to open the file, alert the user
if(flog == NULL)
printf("LOG File not found. No log status will be captured.");
else
fprintf(flog,"%s","_________________START OF PROCESS_________________");
fprintf(flog,"%s","\n\n");
}
//Function: close_log
//Description: closes the log file if opened
//Input param: NULL
//Return Type: NULL
void close_log()
{
if(flog!=NULL){
// Add a message to indicate end of a iteration
fprintf(flog,"%s","_________________END OF PROCESS_________________");
fprintf(flog,"%s","\n\n");
// Close the file pointer
fclose(flog);
}
}
//Function: write_log
//Description: logs the status with success or failure message
//Input param: function name, status and message to display
//Return Type: NULL
void write_log(char *function_name,char *status,char *message)
{
// Write the appropriate log message with associated run time
mytime = time(NULL);
fprintf(flog,"%s %s : %s --->%s\n\n",ctime(&mytime),function_name,status,message);
}
//Function: file_empty_check
//Description: Check whether file has content or not
//Input param: name of the file
//Return Type: integer
// success state is returned if file has content
// FILE_EMPTY_ERROR otherwise
int file_empty_check(char *filename)
{
// Declaring "stat" a system call to know about content of file.
struct stat filestat;
stat(filename,&filestat);
// Variables to hold messages
char message_failure[40];
char message_success[40];
strcpy(message_failure, filename);
strcat(message_failure, "is empty. No data available");
strcpy(message_success,filename);
strcat(message_success," has content.");
//check size of file and return appropriate status.
if(filestat.st_size == 0){
write_log("file_empty_check","FILE_EMPTY_ERROR",message_failure);
return FILE_EMPTY_ERROR;
}
else{
write_log("file_empty_check","SUCCESS",message_success);
return SUCCESS;
}
}
//Function: users_memory_allocation
//Description: dynamically allocates the memory to the for the users list
//Input param: NULL
//Return Type: USER_PROFILE *
// success status on successful memory allocation
// MEMORY_ALLOCATION_ERROR on failure(by indicating through null node)
USER_PROFILE* users_memory_allocation(){
//Local node created
USER_PROFILE *newnode;
//Allocate memory of the structure to the node and initialise next to Null and return the node
newnode = (USER_PROFILE*)malloc(sizeof(USER_PROFILE));
if(newnode==NULL)
printf("Memory Allocation Failed.");
else{
newnode->next=NULL;
write_log("users_memory_allocation", "SUCCESS", "Memory allocation Successful");
}
return newnode;
}
//Function: users_insert
//Description: insert the nodes at the end of USERS list
//Input param: temp of type USER_PROFILE*
//Return Type: void
// success status on successful insertion to the log file
void user_insert(USER_PROFILE* temp){
//If the list is empty, assign to the head node
if(USER==NULL){
USER=temp;
}
else{
//Traverse till the last node using curr pointer and insert the node at the end
USER_PROFILE* curr=USER;
while(curr->next!=NULL){
curr=curr->next;
}
curr->next=temp;
}
write_log("user_insert", "SUCCESS", "Insertion of node at the end of USERS List Successful");
}
//Function: user_load
//Description: Transferring the data from secondary memory to primary memory using singly-linked list of users list
//Input param: NULL
//Return Type: void
// success status on successful creation of list
// MEMORY_ALLOCATION_ERROR on failure(by indicating through null node)
void user_load(){
FILE *fp;
USER_PROFILE *temp;
fp = fopen("users.txt","r");
while(!feof(fp)){
//calling function for memory allocation
temp=users_memory_allocation();
if(temp==NULL){
write_log("user_load", "MEMORY_ALLOCATION_ERROR", "Memory allocation failed");
return;
}
//Data is transferred from secondary memory to primary memory via linked lists
fscanf(fp,"%d %s %s %s %s ",&temp->user_id,temp->mobile_number,temp->name,temp->email,temp->password);
//Insert the node
user_insert(temp);
}
fclose(fp);
write_log("user_load", "SUCCESS", "User List successfully created");
}
//Function: drivers_memory_allocation
//Description: dynamically allocates the memory to the for the drivers list
//Input param: NULL
//Return Type: DRIVERS_PROFILE *
// success status on successful memory allocation
// MEMORY_ALLOCATION_ERROR on failure(by indicating through null node)
DRIVERS_PROFILE* drivers_memory_allocation(){
//Local node created
DRIVERS_PROFILE *newnode;
//Allocate memory of the structure to the node and initialise next to Null and return the node
newnode = (DRIVERS_PROFILE*)malloc(sizeof(DRIVERS_PROFILE));
if(newnode==NULL)
printf("Memory Allocation Failed.");
else{
newnode->next=NULL;
write_log("drivers_memory_allocation", "SUCCESS", "Memory allocation Successful");
}
return newnode;
}
//Function: drivers_insert
//Description: insert the nodes at the end of DRIVERS list
//Input param: temp of type DRIVERS_PROFILE*
//Return Type: void
// success status on successful insertion to the log file
void drivers_insert(DRIVERS_PROFILE* temp){
//If the list is empty, assign to the head node
if(DRIVERS==NULL){
DRIVERS=temp;
}
else{
//Traverse till the last node using curr pointer and insert the node at the end
DRIVERS_PROFILE* curr=DRIVERS;
while(curr->next!=NULL){
curr=curr->next;
}
curr->next=temp;
}
write_log("drivers_insert", "SUCCESS", "Insertion of node at the end of DRIVERS List Successful");
}
//Function: drivers_load
//Description: Transferring the data from secondary memory to primary memory using singly-linked list of drivers list
//Input param: NULL
//Return Type: void
// success status on successful creation of list
// MEMORY_ALLOCATION_ERROR on failure(by indicating through null node)
void drivers_load(){
FILE *fp;
DRIVERS_PROFILE *temp;
fp = fopen("drivers.txt","r");
while(!feof(fp)){
//calling function for memory allocation
temp=drivers_memory_allocation();
if(temp==NULL){
write_log("drivers_load", "MEMORY_ALLOCATION_ERROR", "Memory allocation failed");
return;
}
//Data is transferred from secondary memory to primary memory via linked lists
fscanf(fp,"%s %s %s %s %s %d ",temp->name,temp->location,temp->vehicle_type,temp->vehicle_name,temp->phone_number,&temp->status);
//Insert the node
drivers_insert(temp);
}
fclose(fp);
write_log("drivers_load", "SUCCESS", "Drivers List successfully created");
}
//Function: print_users
//Description: Display the contents of users list(admin priveleges)
//Input param: NULL
//Return Type: void
// success status on successful display of list
// EMPTY Response if list is empty(by indicating through null node)
void print_users(){
if(USER==NULL){
printf("USERS list is empty\n\n");
write_log("print_users", "USERS_LIST_EMPTY", "Users List is empty");
}
//Traverse the list using curr pointer and display the details till curr reaches NULL
USER_PROFILE* curr=USER;
while(curr!=NULL){
printf("%d\t%s\t%s\t%s\t%s\n",curr->user_id,curr->mobile_number,curr->name,curr->email,curr->password);
curr=curr->next;
}
printf("\n");
write_log("print_users", "SUCCESS", "Users List Displayed successfully.");
}
//Function: print_drivers
//Description: Display the contents of drivers list(admin priveleges)
//Input param: NULL
//Return Type: void
// success status on successful display of list
// EMPTY Response if list is empty(by indicating through null node)
void print_drivers(){
if(DRIVERS==NULL){
printf("DRIVERS list is empty\n\n");
write_log("print_drivers", "DRIVERS_LIST_EMPTY", "Drivers List is empty");
}
DRIVERS_PROFILE* curr=DRIVERS;
//Traverse the list using curr pointer and display the details till curr reaches NULL
while(curr!=NULL){
printf("%s\t%s\t%s\t%s\t%s\t%d\n",curr->name,curr->location,curr->vehicle_type,curr->vehicle_name,curr->phone_number,curr->status);
curr=curr->next;
}
printf("\n");
write_log("print_drivers", "SUCCESS", "Drivers List Displayed successfully.");
}
void print_coupons_code(){
COUPON_CODE *curr=CODES;
while(curr!=NULL){
printf("%s\t%d\n",curr->code,curr->discount);
curr=curr->next;
}
}
//Function: locations_load
//Description: Transferring the data from secondary memory to primary memory using dynamic array of type char* for locations list
//Input param: NULL
//Return Type: void
// success status on successful allocation of array
// MEMORY_ALLOCATION_ERROR on failure
void locations_load(){
FILE *fp;
int i=0;
//Column Dynamic allocation of locations using char* array
locations = (char**)malloc(locations_count*sizeof(char*));
if(locations == NULL){
write_log("locations_load", "MEMORY_ALLOCATION_ERROR", "Memory allocation failed");
return ;
}
fp = fopen("locations.txt","r");
while(!feof(fp)){
//Each row dynamic allocation of array
locations[i] = (char*)malloc(locations_length*sizeof(char*));
if(locations[i] == NULL){
write_log("locations_load", "MEMORY_ALLOCATION_ERROR", "Memory allocation failed");
return;
}
//Transfer of string from secondary memory to primary memory
fscanf(fp,"%s ",locations[i]);
i++;
}
fclose(fp);
write_log("locations_load", "SUCCESS", "Locations succssfully loaded to array.");
}
//Function: coupon_memory_allocation
//Description: Transferring the data from secondary memory to primary memory using linked lists for coupons list
//Input param: COUPON_CODE*
//Return Type: void
// success status on successful allocation of array
// MEMORY_ALLOCATION_ERROR on failure(via NULL node)
COUPON_CODE* coupon_memory_allocation(){
//local node of type COUPON_CODE*
COUPON_CODE* newnode;
//Dynamic allocation of memory to node-
newnode = (COUPON_CODE*)malloc(sizeof(COUPON_CODE));
if(newnode==NULL)
printf("Memory Allocation Failed\n");
else{
newnode->next = NULL;
write_log("Coupon_memory_allocation", "SUCCESS", "Coupon NODE Succefully created.");
}
return newnode;
}
//Function: coupon_insert
//Description: insert the nodes at the end of coupons list
//Input param: temp of type COUPON_CODE*
//Return Type: void
// success status on successful insertion to the log file
void coupon_insert(COUPON_CODE *temp){
//If list is empty, assign to head node
if(CODES==NULL){
CODES=temp;
}
else{
//Traverse till the last node using curr pointer and insert the node at the end
COUPON_CODE *curr=CODES;
while(curr->next!=NULL)
curr = curr->next;
curr->next = temp;
}
write_log("coupon_insert", "SUCCESS", "Coupon NODE Succefully inserted at the end of coupons list.");
}
//Function: coupon_codes_load
//Description: Transferring the data from secondary memory to primary memory using singly-linked list of coupons list
//Input param: NULL
//Return Type: void
// success status on successful creation of list
// MEMORY_ALLOCATION_ERROR on failure(by indicating through null node)
void coupon_codes_load()
{
FILE *fp;
COUPON_CODE *temp;
fp = fopen("coupon_codes.txt","r");
while(!feof(fp)){
//calling function for memory allocation
temp = coupon_memory_allocation();
if(temp == NULL){
write_log("drivers_load", "MEMORY_ALLOCATION_ERROR", "Memory allocation failed");
return;
}
//Data is transferred from secondary memory to primary memory via linked lists
fscanf(fp,"%s %d ",temp->code,&temp->discount);
//Insert the node
coupon_insert(temp);
}
fclose(fp);
write_log("coupon_codes_load", "SUCCESS", "Coupons Code List successfully created");
}
//Function: cabs_memory_allocation
//Description: Transferring the data from secondary memory to primary memory using linked lists for cabs list
//Input param: CABS*
//Return Type: void
// success status on successful allocation of array
// MEMORY_ALLOCATION_ERROR on failure(via NULL node)
CABS* cabs_memory_allocation(){
//local node of type COUPON_CODE*
CABS* newnode;
//Dynamic allocation of memory to node-
newnode = (CABS*)malloc(sizeof(CABS));
if(newnode==NULL)
printf("Memory Allocation Failed\n");
else{
newnode->next = NULL;
write_log("Cabs_memory_allocation", "SUCCESS", "Cabs NODE Succefully created.");
}
return newnode;
}
//Function: cabs_insert
//Description: insert the nodes at the end of cabs list
//Input param: temp of type CABS*
//Return Type: void
// success status on successful insertion to the log file
void cabs_insert(CABS *temp){
//If list is empty, assign to head node
if(CAB==NULL){
CAB=temp;
}
else{
//Traverse till the last node using curr pointer and insert the node at the end
CABS *curr=CAB;
while(curr->next!=NULL)
curr = curr->next;
curr->next = temp;
}
write_log("cabs_insert", "SUCCESS", "Cabs NODE Succefully inserted at the end of Cabs list.");
}
//Function: cabs_load
//Description: Transferring the data from secondary memory to primary memory using singly-linked list of cabs list
//Input param: NULL
//Return Type: void
// success status on successful creation of list
// MEMORY_ALLOCATION_ERROR on failure(by indicating through null node)
void cabs_load(){
FILE *fp;
CABS *temp;
fp = fopen("cabs.txt","r");
while(!feof(fp)){
//calling function for memory allocation
temp = cabs_memory_allocation();
if(temp == NULL){
write_log("cabs_load()", "MEMORY_ALLOCATION_ERROR", "Memory allocation failed");
return;
}
//Data is transferred from secondary memory to primary memory via linked lists
fscanf(fp,"%s %d ",temp->vehicle_type,&temp->fare);
//Insert the node
cabs_insert(temp);
}
fclose(fp);
write_log("cabs_load", "SUCCESS", " List successfully created");
}
//Function: print_cabs
//Description: Display the contents of cabs list
//Input param: NULL
//Return Type: void
// success status on successful display of list
// EMPTY Response if list is empty(by indicating through null node)
void print_cabs(){
if(CAB==NULL){
printf("CABS list is empty\n\n");
write_log("print_cabs", "Cabs_LIST_EMPTY", "Cabs List is empty");
}
CABS* curr=CAB;
//Traverse the list using curr pointer and display the details till curr reaches NULL
while(curr!=NULL){
printf("%s\t%d\n",curr->vehicle_type,curr->fare);
curr=curr->next;
}
printf("\n");
write_log("print_cabs()", "SUCCESS", "Cabs List Displayed successfully.");
}
//Function: load
//Description: loads the count and is a gateway for loading all the respective file
// loads the data from secondary memory to primary
//Input param: NULL
//Return Type: integer
// success status on successful operation
// respective error status code otherwise
int load()
{
// Start the log file
open_log();
// Use a file pointer to open various files to load the values
FILE *fp;
//Local variables
int status = 0;
int file_status = 0;
//check whether users_count.txt file is empty or not.
file_status = file_empty_check("users_count.txt");
if (file_status == 1006)
return FAILURE;
//check whether users.txt file is empty or not.
file_status = file_empty_check("users.txt");
if (file_status == 1006)
return FAILURE;
//check whether drivers_count.txt file is empty or not.
file_status = file_empty_check("drivers_count.txt");
if (file_status == 1006)
return FAILURE;
//check whether drivers.txt file is empty or not.
file_status = file_empty_check("drivers.txt");
if (file_status == 1006)
return FAILURE;
//check whether locations_count.txt file is empty or not.
file_status = file_empty_check("locations_count.txt");
if (file_status == 1006)
return FAILURE;
//check whether locations.txt file is empty or not.
file_status = file_empty_check("locations.txt");
if (file_status == 1006)
return FAILURE;
//check whether coupon_codes_count.txt file is empty or not.
file_status = file_empty_check("coupon_codes_count.txt");
if (file_status == 1006)
return FAILURE;
//check whether coupon_codes.txt file is empty or not.
file_status = file_empty_check("coupon_codes.txt");
if (file_status == 1006)
return FAILURE;
//check whether time_data.txt file is empty or not.
file_status = file_empty_check("time_data.txt");
if (file_status == 1006)
return FAILURE;
//check whether distance_data.txt file is empty or not.
file_status = file_empty_check("distance_data.txt");
if (file_status == 1006)
return FAILURE;
//check whether cabs_count.txt file is empty or not.
file_status = file_empty_check("cabs_count.txt");
if (file_status == 1006)
return FAILURE;
//check whether cabs.txt file is empty or not.
file_status = file_empty_check("cabs.txt");
if (file_status == 1006)
return FAILURE;
//Loading of USERS List
user_load();
//Loading of drivers list
drivers_load();
//Loading of coupon_codes list
coupon_codes_load();
//Open the locations_count file to read the number of locations
fp = fopen("locations_count.txt","r");
if(fp == NULL) {
write_log("load", "FILE_OPEN_ERROR", "Unable to open the locations_count file");
return FAILURE;
}
fscanf(fp,"%d ",&locations_count);
fclose(fp);
//Loading of locations list
locations_load();
cabs_load();
//Open the users_count file to read the number of users
fp = fopen("users_count.txt","r");
if(fp == NULL) {
write_log("load", "FILE_OPEN_ERROR", "Unable to open the users_count file");
return FAILURE;
}
fscanf(fp,"%d ",&users_count);
fclose(fp);
//Open the drivers_count file to read the number of drivers
fp = fopen("drivers_count.txt","r");
if(fp == NULL) {
write_log("load", "FILE_OPEN_ERROR", "Unable to open the drivers_count file");
return FAILURE;
}
fscanf(fp,"%d ",&drivers_count);
fclose(fp);
//Open the cabs_count file to read the number of cabs
fp = fopen("cabs_count.txt","r");
if(fp == NULL) {
write_log("load", "FILE_OPEN_ERROR", "Unable to open the cabs_count file");
return FAILURE;
}
fscanf(fp,"%d ",&cabs_count);
fclose(fp);
//Open the coupon_codes_count file to read the number of coupon codes
fp = fopen("coupon_codes_count.txt","r");
if(fp == NULL) {
write_log("load", "FILE_OPEN_ERROR", "Unable to open the coupon_codes_count file");
return FAILURE;
}
fscanf(fp,"%d ",&coupon_codes_count);
fclose(fp);
write_log("load", "SUCCESS", "Load Successful");
return SUCCESS;
}
//Function: signup_validation
//Description: Checks if the given credentials i.e., Mobile number is valid
// and checks if the user has an account already
//Input param: mobile_no of type char*
//Return Type: integer
// success status on successful operation
// otherwise FAILURE with appropriate message
int signup_validation(char *mobile_no){
//Check if the mobile number is 10 digits long(input has to be in digits)
if(strlen(mobile_no)==10){
//Traverse the list to check for any dupliicates exist
USER_PROFILE *curr=USER;
while(curr!=NULL){
if(!strcmp(mobile_no,curr->mobile_number)){
write_log("signup_validation", "FAILURE", "Mobile NUMBER IS PRE-EXISTING");
return FAILURE;
}
curr=curr->next;
}
write_log("signup_validation", "SUCCESS", "Successfully signed up");
return SUCCESS;
}
else{
write_log("signup_validation", "FAILURE", "Mobile NUMBER IS Invalid");
return FAILURE;
}
}
//Function: generateotp
//Description: A mock applicationf of A One-Time-Password(otp)
//Input param: NULL
//Return Type: CHAR*
// success status on successful operation
// Failure if memory is not allocated
char* generateotp(){
int n = strlen(str);
//Dynamic allocation of variable
char* otp = malloc(7*sizeof(char));
if(otp == NULL){
printf("Memory Allocation Failed\n");
write_log("generateotp", "MEMORY_ALLOCATION_ERROR", "Memory allocation failed");
}
//Using rand() function for a random otp to be generated modulus the string length
for(int i=0;i<6;i++){
otp[i] = str[rand()%n];
}
write_log("generateotp", "SUCCESS", "OTP is successfully created");
return otp;
}
//Function: passsword_validation
//Description: Checks if the given password is according to the given specifications
//Input param: password of type char*
//Return Type: integer
// success status on successful operation
// otherwise FAILURE with appropriate message
int password_validation(char* password){
if(strlen(password)>=6){
for(int i=0;i<strlen(password);i++){
//Check for a digit in the password,alphabets and special character
if((isdigit(password[i])!=0)){
digit=1;
}
else if((password[i] >= 97 && password[i] <= 122) || (password[i] >= 65 && password[i] <= 90)){
alphabet=1;
}
else{
spe_char=1;
}
if(alphabet==1 && digit==1 && spe_char==1){
write_log("password_validation", "SUCCESS", "Password is affirmed.");
return SUCCESS;
}
}
write_log("password_validation", "FAILURE", "Password is not affirmed.");
return FAILURE;
}
else{
write_log("password_validation", "FAILURE", "Password is of appropriate length.");
return FAILURE;
}
}
//Function: user_profile_details
//Description: Checks if the given password is according to the given specifications
//Input param: password of type char*
//Return Type: integer
// success status on successful operation
// otherwise FAILURE with appropriate message
int user_profile_details(char* mobile){
//creation of node to store the details
USER_PROFILE *temp;
temp = users_memory_allocation();
if(temp==NULL){
write_log("user_profile_details", "MEMORY_ALLOCATION_ERROR", "Memory allocation failed");
return MEMORY_ALLOCATION_ERROR;
}
//transfer of data to the temp node
strcpy(temp->mobile_number,mobile);
temp->user_id=users_count+1;
char buf[40];
//Creation of ride_history file
sprintf(buf,"ride_history_user%d.txt",temp->user_id);
FILE *fp;
fp = fopen(buf,"a+");
fclose(fp);
printf("Enter your profile details\n\n");
printf("Name:");
scanf(" %s",temp->name);
printf("Email ID:");
scanf("%s",temp->email);
re_enter:
printf("Password\n");
printf("Should have a length of 6 (max 40),consisting at least one number,one alphabet and one special character\n");
scanf("%s",passwd1);
//Calling of password_validation function, goto label to complete the process properly
status=password_validation(passwd1);
if(status!=1){
printf("\n\nIncorrect format of password!!\n\n");
goto re_enter;
}
printf("Re-enter your password\n");
scanf("%s",passwd2);
if(!strcmp(passwd1,passwd2)){
strcpy(temp->password,passwd1);
//Insertion of node to the end of the users file
user_insert(temp);
users_count++;
printf("\n\nPROFILE DETAILS COMPLETED!!");
write_log("user_profile_details", "SUCCESS", "User details updated successfully.");
Sleep(1000);
return SUCCESS;
}
printf("\n\nPasswords do not match\n\n");
goto re_enter;
}
//Function: login_validation1
//Description: Checks if the given mobile_no is an existing user of ola
//Input param: mobile number of type char*
//Return Type: integer
// success status on successful operation
// otherwise FAILURE with appropriate message
int login_validation1(char *mobile_no){
//Check for 10 digit mobile number
if(strlen(mobile_no)==10){
USER_PROFILE *curr=USER;
while(curr!=NULL){
//check is user is existing in users list
if(!strcmp(mobile_no,curr->mobile_number)){
write_log("login_validation1", "SUCCESS", "Login Sucessful.");
return SUCCESS;
}
curr=curr->next;
}
write_log("login_validation1", "FAILURE", "Login UnSucessful/not signed up.");
return FAILURE;
}
else{
write_log("login_validation1", "FAILURE", "Login UnSucessful/Incorrect MOBILE number.");
return FAILURE;
}
}
//Function: login_validation2
//Description: Checks if the given email_id and password is of an existing user of ola
//Input param: email and password of type char*
//Return Type: integer
// success status on successful operation
// otherwise FAILURE with appropriate message
int login_validation2(char* email,char *password){
USER_PROFILE *curr=USER;
while(curr!=NULL){
//Traverse to the list and check for compatibility
if(!strcmp(email,curr->email) && !strcmp(password,curr->password)){
write_log("login_validation1", "SUCCESS", "Login Sucessful.");
return SUCCESS;
}
curr=curr->next;
}
write_log("login_validation1", "FAILURE", "Login UnSucessful/not signed up/Incorrect details.");
return FAILURE;
}
//Function: rides_memory_allocation
//Description: Allocation of memory for Rides history list
//Input param: NULL
//Return Type: RIDE_HISTORY*
// success status on successful creation of list
// MEMORY_ALLOCATION_ERROR on failure(by indicating through null node)
RIDE_HISTORY* rides_memory_allocation(){
//Local node created
RIDE_HISTORY *newnode;
//Allocate memory of the structure to the node and initialise next to Null and return the node
newnode = (RIDE_HISTORY*)malloc(sizeof(RIDE_HISTORY));
if(newnode==NULL)
printf("Memory Allocation Failed.");
else{
newnode->next=NULL;
write_log("rides_memory_allocation", "SUCCESS", "Memory allocation Successful");
}
return newnode;
}
//Function: ride_insert
//Description: Insertion of node at the end of the rides linked list
//Input param: temp of type RIDE_HISTORY *
//Return Type: void
// success status on successful insertion of node at the end of the list
void ride_insert(RIDE_HISTORY *temp){
//If list is empty assign node to head
if(RIDES==NULL){
RIDES=temp;
}
else{
//Traverse the last node and attach the node to it
RIDE_HISTORY *curr=RIDES;
while(curr->next!=NULL)
curr=curr->next;
curr->next=temp;
}
write_log("ride_insert", "SUCCESS", "NOde Successfully inserted at th end of rides list.");
}
//Function: load_ride_history
//Description: Load the ride history of a user from secondary memory to primary memory
//Input param: id of type int
//Return Type: int
// success status on successful load of the file
int load_ride_history(int id){
int file_status = 0;
//Use a buffer to store the user id and access the appropriate user ride history file
char buf[40];
sprintf(buf,"ride_history_user%d.txt",id);
FILE *fp;
fp = fopen(buf,"r");
//check if the file is empty
file_status = file_empty_check(buf);
if (file_status == 1006){
write_log("load_ride_history", "FAILURE", "User Ride history is empty.");
return FAILURE;
}
RIDE_HISTORY *temp;
//Transfer of data from secondary memory to primary memory
while(!feof(fp)){
temp = rides_memory_allocation();
if(temp==NULL)
return MEMORY_ALLOCATION_ERROR;
fscanf(fp,"%s %s %s %s %s %0.2f ",temp->pickup,temp->destination,temp->driver_name,temp->vehicle_type,temp->vehicle_name,&temp->fare);
ride_insert(temp);
}
fclose(fp);
write_log("load_ride_history", "SUCCESS", "Ride history of user succefully loaded");
return SUCCESS;
}
//Function: print_ride_history
//Description: Display the ride history of the user
//Input param: NULL
//Return Type: int
// success status on successful DISPLAY of the file
int print_ride_history(){
if(RIDES==NULL){
write_log("print_ride_history", "FAILURE", "Ride history of user is empty");
return FAILURE;
}
RIDE_HISTORY* curr=RIDES;
//Traverse through the list and display the data
while(curr!=NULL){
printf("%s\t%s\t%s\t%s\t%s\t%0.2f\n",curr->pickup,curr->destination,curr->driver_name,curr->vehicle_type,curr->vehicle_name,curr->fare);
curr=curr->next;
}
Sleep(10000);
write_log("print_ride_history", "SUCCESS", "Displayed the data succesfully.");
return SUCCESS;
}
//Function: load_user1
//Description: After login, to hold the current user details in a separate structure
//Input param: mobile number of type char*
//Return Type: int
// success status on successful LOAD of the data
// FAILURE if data is not found
int load_user1(char *mobile){
USER_PROFILE* curr=USER;
//Traverse through the list and search for mobile number of registered user
while(curr!=NULL){
if(!strcmp(mobile,curr->mobile_number)){
break;
}
curr=curr->next;
}
//If user is not found
if(curr==NULL){
printf("Error in user file\n\n");
write_log("load_user1", "FAILURE", "User not found check users file.");
return FAILURE;
}
//Transfer of data to a new structure (temporary)
CURR_USER.user_id=curr->user_id;
strcpy(CURR_USER.name,curr->name);
strcpy(CURR_USER.mobile_number,curr->mobile_number);
strcpy(CURR_USER.email,curr->email);
strcpy(CURR_USER.password,curr->password);
load_ride_history(curr->user_id);
write_log("load_user1", "SUCCESS", "Successfully loaded the data into new structure.");
return SUCCESS;
}
//Function: load_user1
//Description: After login, to hold the current user details in a separate structure
//Input param: email and password of type char*
//Return Type: int
// success status on successful LOAD of the data
// FAILURE if data is not found
int load_user2(char *email,char* password){
USER_PROFILE* curr=USER;
//Traverse through the list and search for email and password of registered user
while(curr!=NULL){
if(!strcmp(email,curr->email) && !strcmp(password,curr->password)){
break;
}
curr=curr->next;
}
//If user is not found
if(curr==NULL){
write_log("load_user2", "FAILURE", "User not found check users file.");
printf("Error in user file\n\n");
return FAILURE;
}
//Transfer of data to a new structure (temporary)
CURR_USER.user_id=curr->user_id;
strcpy(CURR_USER.name,curr->name);
strcpy(CURR_USER.mobile_number,curr->mobile_number);
strcpy(CURR_USER.email,curr->email);
strcpy(CURR_USER.password,curr->password);
load_ride_history(curr->user_id);
write_log("load_user2", "SUCCESS", "Successfully loaded the data into new structure.");
return SUCCESS;
}
//Function: print_locations
//Description: Display the locations that can be used for pickup and destination
//Input param: email and password of type char*
//Return Type: int
// success status on successful LOAD of the data
// FAILURE if data is not found
void print_locations(){
//Display locations
printf("The Locations are\n");
for(int i=0;i<locations_count;i++){
printf("%s\n",locations[i]);
}
printf("\n");
write_log("print_locations", "SUCCESS", "Successfully displayed the locations.");
}