-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.c
1430 lines (1228 loc) · 33.9 KB
/
helper.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
/*
* Contains all the helper functions that will help extract JSON data from weblink
* and create multple CSV files. Some functions also perform analysis so that a best fit
* can be found to buy content on a limited budget.
* Other functions help validate the command line input etc.
* All the operations are tracked by logging events and errors.
* There are various sections and each section has similar set of functions.
* 1. String Utilities.
* 2. Date related functions.
* 3. Input Validators.
* 4. File Download and directory exploration fucntions.
* 5. JSON data parsing and CSV file operations.
* 6. Operations involving calculation of best solutions.
* 7. Purging meta data files.
* @author: Srinivasan Rajappa
* Date: 2 July, 2015
* Edited:
* - 11 July, 2015
*/
#include "bistro.h"
#include "time.h"
#include "unistd.h"
#include "dirent.h"
#include "sys/stat.h"
#include "error.h"
const char url[] = "http://codapi.zappos.biz/menus";
int numOfVal=0;
int numOfFiles = 0;
char *firstEntry;
float bd;
int dot,prec=0;
char str_budget[10];
int norteDam = 0;
char filteredItem[MG];
int EXCLUSION_MODE = 0;
int WRONG_DATE = 0;
//===============================================================================================================================//
//=============================== String Utility functions here, below ==========================================================//
//===============================================================================================================================//
//===============================================================================================================================//
/*
* Function: Toupper
* Description:
* Converts a string in Any case to Upper case.
* @author: Srinivasan Rajappa
* Date: 2 July, 2015
*/
char *Toupper(char*strVal){
int i=0;
static char tmpStr[MG];
strcpy(tmpStr,strVal);
while(tmpStr[i]){
tmpStr[i] = toupper(tmpStr[i]);
i++;
}
return(tmpStr);
}
/*
* Function: stringToFloat
* Description:
* Character values in the string is mapped and checked and corresponding number is used
* for constructing a float value. The precision level of the float string is also calculated.
* @author: Srinivasan Rajappa
* Date: 2 July, 2015
*/
float stringToFloat(char *budStr){
int i,locDot,pv=0,x,v=0;
float rad;
float result=0.0;
locDot =0;
rad =1;
if(strcmp(budStr,"Free")==0){
return FREE;
}
for(i=0; i<strlen(budStr);i++){
if(budStr[i]=='.'){
dot = 1;
locDot = i+1;
}
}
if(locDot==0){
result = atoi(budStr);
return result;
}
locDot-=1;
for(i=locDot-1; i>=0; i--){
v = mapToInt(budStr[i]);
result += (rad * v);
rad*=10;
}
rad = 0.1;
x = min(strlen(budStr),locDot+3);
for(i=locDot+1; i<x;i++){
v = mapToInt(budStr[i]);
if(v==0)
pv = max(pv,0);
else
pv +=1;
result += (rad*v);
rad*=0.1;
}
prec = max(prec,pv); //Precision value is estimated and stored in the gloabl variable
return result;
}
/*
* Function: mapToInt
* Description:
* finds the corresponding value of character if it is a number.
* @author: Srinivasan Rajappa
* Date: 2 July, 2015
*/
int mapToInt(char c){
switch(c){
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
default: printf("mapToInt: Input Error\n");
logEntry("mapToInt: ","Input to switch incorrect",Err);
Exit(EXIT_ABNORMAL);
}
}
/*
* Function: strMatchCase
* Description:
* Checks whether a string is a substring of another irrespective of case.
* @author: Srinivasan Rajappa
* Date: 2 July, 2015
*/
int strMatchCase(char *ch, char *comp){
int i,j, k,l;
i=j=k=l=0;
int check=0,ind=0;
char s1[strlen(ch)];
char s2[strlen(comp)];
memset(s1,'\0',strlen(ch));
memset(s2,'\0',strlen(comp));
strcpy(s1,ch);
strcpy(s2,comp);
strcpy(s1,Toupper(s1)); //change case of both strings to upper.
strcpy(s2 ,Toupper(s2));
if(strlen(s1)<strlen(s2))
return NOTFOUND;
for(i=0; i<strlen(s1)-strlen(s2)+1; i++){
k=i;
for(j=0;j<strlen(s2);j++){
if(s1[k]==s2[j]){
k++;
check++;
}
else{
break;
}
}
k=0;
if(check==strlen(s2)){
l = 1;
break;
}
check=0;
}
return l==0 ? NOTFOUND : FOUND;
}
/*
* Function: strMatch
* Description:
* returns FOUND (1) if the second string is present in the first.
* @author: Srinivasan Rajappa
* Date: 2 July, 2015
*/
int strMatch(char *ch, char *comp){
int i,j, k,l;
i=j=k=l=0;
int check=0,ind=0;
if(strlen(ch)<strlen(comp))
return NOTFOUND;
for(i=0; i<strlen(ch)-strlen(comp)+1; i++){
k=i;
for(j=0;j<strlen(comp);j++){
if(ch[k]==comp[j]){
k++;
check++;
}
else{
break;
}
}
k=0;
if(check==strlen(comp)){
l = 1;
break;
}
check=0;
}
return l==0 ? NOTFOUND : FOUND;
}
/*
* Function: sortFileNames
* Description:
* Sorts a list of strings.
* @author: Srinivasan Rajappa
* Date: 2 July, 2015
*/
void sortFileNames(char **dayFiles, int num){
int i,j;
char temp[NG];
memset(temp,'\0',NG);
for(i=0; i< num; i++){
for (j=0; j < num-1; j++){
if(strcmp(dayFiles[j],dayFiles[j+1])>0){
strcpy(temp,dayFiles[j]);
strcpy(dayFiles[j],dayFiles[j+1]);
strcpy(dayFiles[j+1],temp);
memset(temp,'\0',NG);
}
}
}
}
//===============================================================================================================================//
//=============================== Date Related functions here, below ============================================================//
//===============================================================================================================================//
//===============================================================================================================================//
/*
* Function: systemDate
* Description:
* Returns system date as a string.
* @author: Srinivasan Rajappa
* Date: 2 July, 2015
*/
char *systemDate(){
time_t now = time(NULL);
int m,d,y;
struct tm *Time = localtime(&now);
m = Time->tm_mon + 1;
d = Time->tm_mday;
y = Time->tm_year+1900;
char *mm,*dd,*yy;
mm = (char*)malloc(sizeof(char)*NG);
dd = (char*)malloc(sizeof(char)*NG);
yy = (char*)malloc(sizeof(char)*NG);
memset(mm,'\0',NG);
memset(yy,'\0',NG);
memset(dd,'\0',NG);
if(m<10){
snprintf(mm,sizeof(mm),"%d",m);
mm[1] = mm[0];
mm[0] = '0';
}else{
snprintf(mm,sizeof(mm),"%d",m);
}
if(d<10){
snprintf(dd,sizeof(dd),"%d",d);
dd[1] = dd[0];
dd[0] = '0';
}else{
snprintf(dd,sizeof(dd),"%d",d);
}
snprintf(yy,sizeof(yy),"%d",y);
strcat(yy,"-");
strcat(mm,"-");
strcat(mm,dd);
strcat(yy,mm);
logEntry("systemDate: ",yy,Info);
return yy;
}
/*
* Function: formatDate
* Description:
* bespoke function to trim the date to a more friendly format.
* @author: Srinivasan Rajappa
* Date: 2 July, 2015
*/
void formatDate(char* dateVal){
int i;
int flag = 0;
for (i=0; i<strlen(dateVal);i++){
if(flag==1){
dateVal[i] = '\0';
}
if(dateVal[i]=='T'){
dateVal[i] = '\0';
flag =1;
}
}
}
/*
* Function: properFormat
* Description:
* Changes the format of date from YYYY-MM-DD to DD Month, YYYY. Looks pretty good.
* @author: Srinivasan Rajappa
* Date: 2 July, 2015
*/
char *properFormat(char *name){
int year, month, date,i,j=0;
for(i=0; i< 11; i++){
if(i==4 || i==7)
continue;
else {
if(name[i]>57 && name[i] <48){
printf("Date Error- expected format YYYY-MM-DD\n");
logEntry("properFormat (Incorrect date): ",name,Err);
Exit(EXIT_APP);
}
}
}
year = atoi(csvVal(name,'-',1));
month = atoi(csvVal(name,'-',2));
name[10]='-';
name[11]='\0';
date = atoi(csvVal(name,'-',3));
memset(name,'\0',MG);
switch(month){
case 1: snprintf(name,MG,"%d January,%d",date, year);
break;
case 2: snprintf(name,MG,"%d February,%d",date, year);
break;
case 3: snprintf(name,MG,"%d March,%d",date, year);
break;
case 4: snprintf(name,MG,"%d April,%d",date, year);
break;
case 5: snprintf(name,MG,"%d May,%d",date, year);
break;
case 6: snprintf(name,MG,"%d June,%d",date, year);
break;
case 7: snprintf(name,MG,"%d July,%d",date, year);
break;
case 8: snprintf(name,MG,"%d August,%d",date, year);
break;
case 9: snprintf(name,MG,"%d September,%d",date, year);
break;
case 10:snprintf(name,MG,"%d October,%d",date, year);
break;
case 11:snprintf(name,MG,"%d November,%d",date, year);
break;
case 12:snprintf(name,MG,"%d December,%d",date, year);
break;
default: logEntry("properFormat: ","Month entry incorrect",Err);
Exit(EXIT_ABNORMAL);
}
return name;
}
//===============================================================================================================================//
//=============================== Input Validating functions here, below ========================================================//
//===============================================================================================================================//
//===============================================================================================================================//
/*
* Function: validator
* Description:
* Analyzes the arguments and checks if the input are valid,
* else throws out errors and terminates the application.
* If the arguments are correct then budget value is extracted and is returned as float.
* @author: Srinivasan Rajappa
* Date: 2 July, 2015
*/
float validator(int argc, char *argv[]){
float ret;
char inDate[NG];
memset(inDate,'\0',NG);
int i;
if(argc==1){
printf("Invalid number of Arguments \nType\n\t./executable -HELP for help\n");
exit(0);
}else{
if(strcmp(Toupper(argv[1]),"-HELP")==0){
logEntry("Help: ", "happy to do ;)", Info);
printf("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
printf("Application prints the items that can be purchased\nfor consumption on a given budget per day.\n");
printf("Running modes are as follows\n");
printf("\t- ./executable BUDGET \n\t\t[BUDGET amount can have decimal values]\n");
printf("\t- ./executable BUDGET ITEM \n\t\t[ITEM refers to the specific food dish you want to exclude from list]\n");
printf("=====================================\n");
printf("Application can also show the Menu for particular day.\n");
printf("\t- ./executable MENU \n\t\tPrints the Menu available for the upcoming dates.\n");
printf("\t- ./executable MENU YYYY-MM-DD\n\t\tPrints the Menu available for given date.\n");
Exit(EXIT_APP);
}else if(strcmp(Toupper(argv[1]),"MENU")==0){
//If a date is entered
if(argc==3){
strtok(argv[2],"\n");
strcpy(inDate,argv[2]);
if(inDate[4]=='-' && inDate[7]=='-' && strlen(inDate)==10){
logEntry("Menu: Additional Param ",inDate,Info);
char *rawFile = readURL(url);
parseFile(rawFile);
properFormat(inDate);
printf("\n\t****Menu for Date: %s****\n",inDate);
printf("-----------------------------------------------------\n");
memset(inDate,'\0',NG);
strcpy(inDate,argv[2]);
strcat(inDate,".csv");
printContentFile(inDate);
printf("_____________________________________________________\n");
purgeFiles(directoryExam());
deleteFile(rawFile);
//free(rawFile);
}
Exit(EXIT_APP);
}
displayMenu();
}else{
ret = validBudgetInput(argv[1]);
if(argc == 3){
memset(filteredItem,'\0',MG);
strcpy(filteredItem,strtok(argv[2],"\n"));
logEntry("Filter: ",filteredItem,Info);
printf("\n****Results after filtering: %s****\n",filteredItem );
EXCLUSION_MODE = 1;
}
return ret;
}
}
}
/*
* Function: validBudgetInput
* Description:
* It analyzes if the budget entry is indeed a valid number.
* @author: Srinivasan Rajappa
* Date: 2 July, 2015
*/
float validBudgetInput(char *budStr){
int i,j,hasDoll;
float value;
if(strcmp("FREE",Toupper(budStr))==0)
return 0;
for(i=0; budStr[i]!='\0'; i++){
if(budStr[i] >= 48 && budStr[i] <= 57 || budStr[i]== 46){
continue;
}else{
printf("Invalid Argument (BUDGET)\n\t Expected Format - number \nType\n\t./executable -HELP for help\n");
WRONG_COMMAND;
logEntry("validBudgetInput: ","Budget Value not a number",Err);
Exit(EXIT_IO);
}
}
memset(str_budget,'\0',10);
strcpy(str_budget,budStr);
value = stringToFloat(budStr);
if(value<0){
printf("Budget value cannot be a negative, Try again\nType\n\t./executable -HELP for help\n");
WRONG_COMMAND;
logEntry("validBudgetInput: ","Budget Value is negative",Err);
Exit(EXIT_IO);
}
bd = value; //Store the Budget value in a Global Variable.
return value;
}
//===============================================================================================================================//
//=============================== File Download and directory exploration fucntions, below ======================================//
//===============================================================================================================================//
//===============================================================================================================================//
/*
* Function: readURL
* Description:
* wget command is used to save the JSON data from the given
* URL link. It saves the content in a txt file whose name is hard coded.
* @author: Srinivasan Rajappa
* Date: 2 July, 2015
*/
char *readURL(char* urlLink){
FILE *filePtr;
char wgetMagic[STR_MAX];
memset(wgetMagic,'\0',STR_MAX);
strcpy(wgetMagic, "wget --quiet ");
strcat(wgetMagic,urlLink);
strcat(wgetMagic," -O urlContent.txt");
logEntry("wget on URL: ",urlLink,Info);
filePtr = popen(wgetMagic,"w");
if(filePtr == NULL){
printf("Unable to connect to BISTRO Website\n\t Unable to check because computer isn't connected to Internet \n");
logEntry("readURL: ","Internet Access Limited",Err);
NO_CONNECTION_TO_INTERNET;
Exit(0);
}
pclose(filePtr);
return("urlContent.txt");
}
/*
* Function: getFileNames
* Description:
* Returns the file list in form of ; appending string values.
* @author: Srinivasan Rajappa
* Date: 2 July, 2015
* References: http://stackoverflow.com/questions/8149569/scan-a-directory-to-find-files-in-c
*/
char * getFileNames(char *dir){
int i=0,j=0,count=0;
char *namesOfFiles;
DIR *dp,*ds;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL) {
fprintf(stderr,"cannot open directory: %s\n", dir);
logEntry("getFileNames: ","Directory Missing",Err);
Exit(EXIT_TRANS);
}
chdir(dir);
namesOfFiles = (char*)malloc(sizeof(char)*MAXLINE);
memset(namesOfFiles,'\0',MAXLINE);
while((entry = readdir(dp)) != NULL) {
lstat(entry->d_name,&statbuf);
if(strMatch(entry->d_name,".csv")==FOUND && entry->d_name[4]== '-' && entry->d_name[7]=='-'){
strcat(namesOfFiles,entry->d_name);
strcat(namesOfFiles,";");
i++;
}
}
numOfFiles = i;
return namesOfFiles;
}
/*
* Function: directoryExam
* Description:
* It calls the above function; it has stop gate measures to avoid
* falling into a situation when the current working directory is
* not to be found.
* @author: Srinivasan Rajappa
* Date: 2 July, 2015
* References: http://stackoverflow.com/questions/298510/how-to-get-the-current-directory-in-a-c-program
*/
char* directoryExam(){
char cwd[MG];
if (getcwd(cwd, sizeof(cwd)) == NULL){
fprintf(stderr,"Cannot find current working directory\n");
logEntry("directoryExam: ","Current Working Directory missing",Err);
Exit(EXIT_TRANS);
}
else
return getFileNames(cwd);
}
//===============================================================================================================================//
//=============================== JSON data extraction and parsing functions here, below ========================================//
//===============================================================================================================================//
//===============================================================================================================================//
/*
* Function: parseFile
* Description:
* Using cues from the paranthesis rule, the date, names and price of respective items are found.
* Each menu for the date is assumed to be in a group and it will be easy to save the content from that
* entire file only for that subdate.
* @author: Srinivasan Rajappa
* Date: 2 July, 2015
*/
void parseFile(char *rawFile){
FILE *filePtr,*noInt;
char temp,c;
int i = 0,j, parendCalc =0,noI=0;
char *str = (char*) malloc(sizeof(char)*MAXLINE);
filePtr = fopen(rawFile,"r");
noInt = fopen(rawFile,"r");
if(filePtr==NULL){
printf("parseFile: FILE %s couldn't be opened\n", rawFile);
exit(0);
}
while(c=(char)fgetc(noInt)!=EOF){
noI++;
}
fclose(noInt);
if(noI <10){
printf("Unable to connect to BISTRO Website.\nUnable to check because computer isn't connected to Internet. \n");
logEntry("readURL: ","Internet Access Limited",Err);
NO_CONNECTION_TO_INTERNET;
deleteFile(rawFile);
Exit(0);
}
while(i!=10){ //Position the cursor to the head
temp = (char) fgetc(filePtr);
i++;
}
i=0,j=0;
memset(str,'\0',MAXLINE);
while(1){
temp = (char) fgetc(filePtr);
if(temp=='{'||temp=='['){
parendCalc++;
}
else if(temp == '}' || temp == ']'){
parendCalc--;
}else if(temp == EOF){
break;
}
else if(temp==','){
continue;
}else{
str[j] = temp;
j++;
}
if(parendCalc == 0){
j=0;
analyzeTrail(str);
memset(str,'\0',MAXLINE);
}
}
close(filePtr);
}
/*
* Function: extract_val
* Description:
* Finds the value of particular name, date or price value by positioning
* the index to the first " symbol.
* @author: Srinivasan Rajappa
* Date: 2 July, 2015
*/
char * extract_val(int index,char *ptr){
int i,k=0;
int countQuote =0;
char *newString = (char*)malloc(sizeof(char)*MG);
memset(newString,'\0',MG);
for(i =index; i<strlen(ptr); i++){
if(ptr[i]=='"'){
countQuote++;
}else{
newString[k] = ptr[i];
k++;
}
if(countQuote==2){
break;
}
}
return newString;
}
/*
* Function: match
* Description:
* Returns the index in a string (specifically for JSON) where
* open clause is found.
* @author: Srinivasan Rajappa
* Date: 2 July, 2015
*/
int match(char *ch, char *comp, int index){
int i,j, k;
i=j=k=0;
int check=0,ind=0;
for(i=index; i<strlen(ch)-strlen(comp)+1; i++){
k=i;
for(j=0;j<strlen(comp);j++){
if(ch[k]==comp[j]){
k++;
check++;
}
else{
break;
}
}
k=0;
if(check==strlen(comp)){
ind = i;
break;
}
check=0;
}
return ind==0 ? NOTFOUND : (ind + strlen(comp)+2);
}
/*
* Function: extractValue
* Description:
* returns the string of values for names,etc.
* @author: Srinivasan Rajappa
* Date: 2 July, 2015
*/
char ** extractValue(char *ptr, char *comp, int MODE){
if(MODE == SINGLE){
char **v = malloc(sizeof(char));
v[numOfVal] = malloc(sizeof(char)*MG);
memset(v[numOfVal],'\0',MG);
int index = match(ptr,comp,0);
strcpy(v[numOfVal],extract_val(index,ptr));
numOfVal++;
return v;
}else{
char **p = malloc(sizeof(char)*10);
int index = 1;
while(index != NOTFOUND)
//numOfVal!=39||index!=strlen(comp)+2)
{
p[numOfVal] = malloc(sizeof(char)*MG);
memset(p[numOfVal],'\0',MG);
index = (index == 1 ? match(ptr,comp,index-1): match(ptr,comp,index)) ;
strcpy(p[numOfVal],extract_val(index,ptr));
if(numOfVal==0)
strcpy(firstEntry,p[numOfVal]);
numOfVal++;
}
return p;
}
}
/*
* Function: analyzeTrail
* Description:
* Writes the content of the menu i.e. name, price etc. into a csv file.
* @author: Srinivasan Rajappa
* Date: 2 July, 2015
*/
void analyzeTrail(char *str){
int i;
FILE *writePtr;
char *fileName = (char*)malloc(sizeof(char)*MG);
firstEntry = (char*)malloc(sizeof(char)*MG);
char *t1 = (char*)malloc(sizeof(char)*MG);
char *t2 = (char*)malloc(sizeof(char)*MG);
memset(t1,'\0',MG);
memset(t2,'\0',MG);
memset(fileName,'\0',MG);
memset(firstEntry,'\0',MG);
char **v = extractValue(str,"date",SINGLE);
strcpy(fileName,v[0]);
//printf("analyzeTrail: date: %s\n",v[0]);
numOfVal =0;
formatDate(fileName);
strcat(fileName,".csv");
writePtr =fopen(fileName,"w");
//Find the names
char **n = extractValue(str,"name",MULTI);
numOfVal = 0;
strcpy(t1,firstEntry);
memset(firstEntry,'\0',MG);
//Find the price
char **p = extractValue(str,"price",MULTI);
strcpy(t2,firstEntry);
memset(firstEntry,'\0',MG);
for(i=0; i<numOfVal; i++){
if(i==0){
int numChar = fprintf(writePtr, "%s,%s,\n",t1,t2);
memset(t1,'\0',MG);
memset(t2,'\0',MG);
}
else{
int numChar = fprintf(writePtr, "%s,%s,\n",n[i],p[i]);
}
}
fflush(writePtr);
//free(p);
//free(n);
//free(v);
close(writePtr);
numOfVal = 0;
}
/*
* Function: csvVal
* Description:
* Extracts a string from a seperated value file, the inputs are the special symbols viz.
* , - or ; and the line input. Num identifies the column.
* @author: Srinivasan Rajappa
* Date: 2 July, 2015
* References: http://stackoverflow.com/questions/12911299/read-csv-file-in-c
*/
char *csvVal(char*line,char symbol, int num){
int i;
int fCom=0,lCom;
if(line[0]==symbol){
if(num==1){
return "NULL";
}else{
return strtok(line,",-");
}
}else{
for(i=0;i<strlen(line);i++){
if(line[i]==symbol){
lCom = i;
num--;
if(num==0){
break;
}else{
fCom = lCom+1;
}
}
}
char *result = (char*)malloc(sizeof(char)*MG);
memset(result,'\0',MG);
int k =0;
for(i=fCom; i<lCom; i++){
result[k] = line[i];
k++;
}
return result;
}
return NULL;
}
//===============================================================================================================================//
//=============================== Calculation of optimal solution done here, below ==============================================//
//===============================================================================================================================//
//===============================================================================================================================//
void printContentFile(char *fileName){
FILE *fptr,*recPtr;
fptr = fopen(fileName,"r");
recPtr = fopen(fileName,"r");
//char tmp[STR_MAX];
logEntry("printContentFile:(dated) ",fileName,Info);
char *tmp = (char*)malloc(sizeof(char)*STR_MAX);
char c;
int k=0;
//printf("FILE: %s\n",fileName);
if(fptr==NULL){
printf("No items available on %s in the database\n",properFormat(csvVal(fileName,'.',1)));
printf("_____________________________________________________\n");
purgeFiles(directoryExam());
deleteFile("urlContent.txt");
logEntry("printContentFile: ","Nothing available today",Info);
Exit(EXIT_APP);
}else{
char line[128];
memset(line,'\0',128);
while((c=(char)fgetc(recPtr))!=EOF){
k++;
}
fclose(recPtr);
//printf("All ok\n");
while(fgets(line,k,fptr)){
memset(tmp,'\0',STR_MAX);
strcpy(tmp,strdup(line));
if(strMatchCase(csvVal(tmp,',',1),"Closed")==FOUND || strMatchCase(csvVal(tmp,',',1),"Holiday")==FOUND)
printf("Closed for the Holiday\n");
else{
if(strMatchCase(csvVal(tmp,',',1),"NULL")==FOUND) //Skip null entries;
continue;
/* if(strMatchCase(csvVal(tmp,',',2),"free")==FOUND ||
strcmp(csvVal(tmp,',',2),"0.00")==0 ||
strcmp(csvVal(tmp,',',2),"0")==0 ||
strcmp(csvVal(tmp,',',2),"0.0")==0)
printf("%-40s Free\n",csvVal(tmp,'-',1));*/
else{
printf("%-40s $%.2f\n",csvVal(tmp,',',1),stringToFloat(csvVal(tmp,',',2)));
}
//printf("%s\n",tmp);
}
}
}
fclose(fptr);
}
void displayMenu(){
int i,j=0,k=0,q=0;
char c;
char name[NG];
memset(name,'\0',NG);
char *rawFile = readURL(url);
parseFile(rawFile);
char filesList[MAXLINE];
memset(filesList,'\0',MAXLINE);
strcpy(filesList,directoryExam());
char *sysDate = (char*)malloc(sizeof(char)*MG);
memset(sysDate,'\0',MG);
sysDate = systemDate();
//strcpy(sysDate,"2015-05-11");
char **dayFiles = malloc(sizeof(char*)*STR_MAX);
strcat(sysDate,".csv");
for(i=0; i<strlen(filesList);i++){
if(filesList[i]==';'){
name[j]='\0';
j=0;
if(strcmp(sysDate,name)<=0){
dayFiles[q] = (char*)malloc(sizeof(char)*NG);
memset(dayFiles[q],'\0',NG);
strcpy(dayFiles[q++],name);
}