-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.c
1668 lines (1325 loc) · 45.6 KB
/
functions.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
/*
* functions.c
Student number: 18384439
*
* Created on: 30 Sep 2014
* Author: Elan van Biljon
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include "myData.h"
#include "functions.h"
#include "structures.h"
#include "StringHandling.h"
/*
* gets the date passed to it by reference(or as an array/ pointer) and splits it and stores the values into 6 integers that are also passed by reference
*/
void extractDate(char *date, int *day, int *month, int *year, int *hour, int *min, int *sec)
{
char sday[3] = {'\0'}, smonth[3] = {'\0'}, syear[5] = {'\0'}, shour[3] = {'\0'}, smin[3] = {'\0'}, ssec[3] = {'\0'};
/*
* first splits the date intp 6 seperate strings
*/
strncpy (sday, date, 2);
date = date + 3;
strncpy (smonth, date, 2);
date = date + 3;
strncpy (syear, date, 4);
date = date + 5;
strncpy (shour, date, 2);
date = date + 3;
strncpy (smin, date, 2);
date = date + 3;
strncpy (ssec, date, 2);
/*
* scans the 6 seperate strings into 6 seperate integers that were passed to the function by reference
*/
sscanf(sday, "%d", day);
sscanf(smonth, "%d", month);
sscanf(syear, "%d", year);
sscanf(shour, "%d", hour);
sscanf(smin, "%d", min);
sscanf(ssec, "%d", sec);
}
time_t convertTimeToEpoch(char* datetime)
{
time_t result;
struct tm humanTime;
int day, month, year, hour, min, sec;
extractDate(datetime, &day, &month, &year, &hour, &min, &sec);
/*
*take the values that the previous function has extracted and put them into the members of a time_t variable
*/
humanTime.tm_mday = day;
humanTime.tm_mon = (month - 1); //month offset (jan is 0th month
humanTime.tm_year = (year - 1900); //year offset, starts at 1900
humanTime.tm_hour = hour;
humanTime.tm_min = min;
humanTime.tm_sec = sec;
result = mktime(&humanTime); //give the member to the mktime function and it returns with epoch time
return result;
}
/*
* takes in a trace line type and converts it to a number trace type
*/
nr_trace_t convertTrace(str_trace_line_t trace_in)
{
nr_trace_t result;
char stime[20], slatitude[20], slongitude[20], sspeed[20];
double latitude = 0, longitude = 0, speed = 0;
/*
* parseCSV function splits the trace line into 4 strings
*/
parseCSV(trace_in.Line, stime, slatitude, slongitude, sspeed);
/*
* the strings are then scaned into other variables as doubles
*/
sscanf(slatitude, "%lf", &latitude);
sscanf(slongitude, "%lf", &longitude);
sscanf(sspeed, "%lf", &speed);
/*
* the new values are then stored in the members of a number trace type
*/
result.speed = speed * 1.6; //conversion to km/h from m/h
result.coord.latitude = latitude;
result.coord.longitude = longitude;
result.timestamp = (long) convertTimeToEpoch(stime); //converts the string time into epochtime and strores it in the timestamp member
if(DebugConvertTrace)
{
static int counter = 0;
printf("%d, %ld, %lf, %lf, %lf\n", counter, (long) convertTimeToEpoch(stime), latitude, longitude, speed);
printf("%d, %ld, %lf, %lf, %lf\n", counter++, (long) result.timestamp, result.coord.latitude, result.coord.longitude, result.speed);
}
return result;
}
/*
* a function that loops the convertTrace function for an array and converts all elements to number traces
* the function takes in an array of string trace lines and number traces, the string traces lines are then
* converted and stored in the array that was (technically) passed by reference (as all arrays are
*/
void convertTraceArrayToNumbers(str_trace_line_t *strTrace, nr_trace_t *numTrace, int numOfTrace)
{
int i;
for(i = 0; i < numOfTrace; i++)
{
numTrace[i] = convertTrace(strTrace[i]);
if(DebugconvertTraceArrayToNumbers)
{
printf("%d, %ld, %lf, %lf, %lf\n", i, (long) numTrace[i].timestamp, numTrace[i].coord.latitude, numTrace[i].coord.longitude, numTrace[i].speed);
}
}
if(DebugPostconvertTraceArrayToNumbers)
{
i--;
printf("%d, %ld, %lf, %lf, %lf\n", i, (long) numTrace[i].timestamp, numTrace[i].coord.latitude, numTrace[i].coord.longitude, numTrace[i].speed);
}
}
/*
* sorts the number traces in order from the earliest to the latest
*/
void sortNrTraces(nr_trace_t *traces, int numOfTraces)
{
if(DebugPreSort)
{
int i = numOfTraces - 1;
printf("%d, %ld, %lf, %lf, %lf\n", i, (long) traces[i].timestamp, traces[i].coord.latitude, traces[i].coord.longitude, traces[i].speed);
}
int x, y;
/*
* this isnt bubble sort, it is slightly more efficient and
*/
for(x = 0; x < numOfTraces - 1; x++)
{
if(DebugSort)
{
printf("%d, %ld, %lf, %lf, %lf\n", x, (long) traces[numOfTraces - 1].timestamp, traces[numOfTraces - 1].coord.latitude, traces[numOfTraces - 1].coord.longitude, traces[numOfTraces - 1].speed);
}
for(y = x + 1; y < numOfTraces; y++)
{
if(traces[x].timestamp > traces[y].timestamp)
{
nr_trace_t temp = traces[x];
traces[x] = traces[y];
traces[y] = temp;
}
}
}
if(DebugPostSort)
{
int i = numOfTraces - 1;
printf("%d, %ld, %lf, %lf, %lf\n", i, (long) traces[i].timestamp, traces[i].coord.latitude, traces[i].coord.longitude, traces[i].speed);
}
}
/*
* this function loops through the entire number trace array given to it and finds the min max and mean speed of the array
* and since integers are parsed by reference to this function thier values are changed with in the function
*/
void calcSpeedStats(nr_trace_t traces[], int numOfItems, double *meanSpeed, double *maxSpeed, double *minSpeed)
{
double totalSpeed = 0;
*meanSpeed = 0;
*maxSpeed = 0; //initialize max speed to something unrealisticly small
*minSpeed = 999999999; //initialize min speed to something unrealisticly big
int i;
for(i = 0; i < numOfItems; i++)
{
totalSpeed = totalSpeed + traces[i].speed; //sum up speeds
if(*maxSpeed < traces[i].speed) //check if current traces speed is higher than current max
{
*maxSpeed = traces[i].speed; //replaces current max with current traces soeed
}
if(*minSpeed > traces[i].speed)
{
*minSpeed = traces[i].speed; //checks if current traces speed is smaller than the current min and replaces if so
}
}
*meanSpeed = (totalSpeed/numOfItems); //gets average speed
}
/*
* calculates the duration of the trip by subtracting the last trace's epoch time from the first
*/
long calcDuration(nr_trace_t *numTraces, int numOfTraces)
{
long result;
result = numTraces[numOfTraces - 1].timestamp - numTraces[0].timestamp;
if(DebugCalcDuration)
{
printf("num Traces: %d, Last time: %ld, First time: %ld\n", numOfTraces, (long) numTraces[numOfTraces - 1].timestamp, (long) numTraces[0].timestamp);
}
return result;
}
/*
* converts seconds to hours
*/
double convertSecToHour(long seconds)
{
double time;
time = ((double) seconds/3600);
return time;
}
/*
* this function simply applies the haversine formula to two sets of coordinates to find the straight line distance between them
* the two coordinates are read in by reference
*/
double distanceHaversine(coord_t* coord1, coord_t* coord2)
{
double result, latitude1, latitude2, longitude1, longitude2, a, c;
static const int R = 6371; //radius of the earth (average)
latitude1 = coord1->latitude * (M_PI/180); //converts from degrees to radians
latitude2 = coord2->latitude * (M_PI/180);
longitude1 = coord1->longitude * (M_PI/180);
longitude2 = coord2->longitude * (M_PI/180);
a = pow(sin((latitude2 - latitude1)/2), 2) + cos(latitude1) * cos(latitude2) * pow(sin((longitude2 - longitude1)/2), 2);
c = 2 * (atan2(sqrt(a), sqrt(1 - a)));
result = R * c;
return result;
}
/*
* loops the haversine formula for all traces in the array and accumulates the distance and returns it
*/
double calcTotalHaversine(nr_trace_t *numTrace, int numOfTrace)
{
double result = 0;
int i;
for(i = 0; i < numOfTrace - 1; i++)
{
result = result + distanceHaversine(&numTrace[i].coord, &numTrace[i + 1].coord);
}
return result;
}
/*
* this takes the average speed between two points and the time change between two points and multiplies them together
* it then loops this for the whole number trace array to find the accumulated distance
*/
double calcRiemannDist(nr_trace_t *numTrace, int numOfTrace)
{
double result = 0;
int i;
for(i = 0; i < numOfTrace - 1; i++)
{
double averageSpeed = (numTrace[i].speed + numTrace[i + 1].speed)/2;
double timeChange = ((double) numTrace[i + 1].timestamp - (double) numTrace[i].timestamp)/3600;
result = result + (averageSpeed * timeChange);
}
return result;
}
/*
* this function asks the user to specify a filename, it checks if it exists, and then it counts the number if lines in the file
* the function gets two arguments by reference (an integer and a string) it alters these two values by reference
*/
nr_trace_t * readCSVFile(int * traceCounter, char * fileName)
{
setbuf(stdout, 0);
FILE *filePtr;
*traceCounter = -1; //start counter at -1 to account for the headings
printf("Enter the Text file's name to be read from to get the Traces(max 100 characters): \n");
scanf("%s", fileName);
filePtr = fopen(fileName, "r");
while(filePtr == NULL) //checks if file exists
{
printf("Invalid file name \n");
printf("Enter the Text file's name to be read from to get the Traces(max 100 characters): \n");
scanf("%s", fileName);
filePtr = fopen(fileName, "r");
}
do
{
char temp[100];
fgets(temp, 100, filePtr);//moves cursor on in file
(*traceCounter)++;//counts lines
}while (!feof(filePtr));
fclose(filePtr);
return (nr_trace_t*) malloc((*traceCounter) * sizeof(nr_trace_t)); //returns a malloc of the size required to store all the elements in the file in a number trace array
}
/*
* this function then reads all the elements from the file and converts them to number traces and stores them in the array given as the first argument
*/
void populateArrayWithFile(nr_trace_t *arrayOfTraces, const int numOfTraces, const char *fileName)
{
FILE *filePtr;
filePtr = fopen(fileName, "r");//do not need to check if file exists as this was already done
int i;
for(i = 0; i < numOfTraces; i++)
{
str_trace_line_t LineOfFile;
if(i == 0)
{
fgets(LineOfFile.Line, 100, filePtr); //gets headings and ignores them
}
fgets(LineOfFile.Line, 100, filePtr);
arrayOfTraces[i] = convertTrace(LineOfFile); //converts file line by line
}
fclose(filePtr);
}
/*
* this function takes in an array of traces and POIs, it then loops through them and determines when (and if) the taxi arrived in and departed from a town
*/
void calcArrDepPOI(poi_t * POI, nr_trace_t * tracesPtr, int numOfItems, int numPOI)
{
int inCity = 0;
int **poiArrivalDeparture = (int**) malloc(numPOI * sizeof(int)); //this will become a 2D array that will store a true or false value for the taxi arriving at and departing from a POI
int traceCounter;
int poiCounter;
for(poiCounter = 0; poiCounter < numPOI; poiCounter++)
{
poiArrivalDeparture[poiCounter] = (int*) malloc(2 * sizeof(int)); //gives the array its second dimension
}
for(poiCounter = 0; poiCounter < numPOI; poiCounter++) //initializes all values to false
{
poiArrivalDeparture[poiCounter][0] = 0;
poiArrivalDeparture[poiCounter][1] = 0;
}
for(poiCounter = 0; poiCounter < numPOI; poiCounter++) //loop through all pois
{
for(traceCounter = 0; traceCounter < numOfItems; traceCounter++) //then loop through all traces
{
/*
* if the straight line distance between two points is smaller than the raidus of the POI, and the taxi isnt already in a POI, and if the taxi hasnt already arrived in this specific POI
* this excludes the first point as the -1 factor for getting the timestamp would cause errors
*/
if(distanceHaversine(&tracesPtr[traceCounter].coord, &POI[poiCounter].coord) < POI[poiCounter].radius && !inCity && poiArrivalDeparture[poiCounter][0] == 0 && traceCounter != 0)
{
poiArrivalDeparture[poiCounter][0] = 1; //set that the taxi now has arrived in this specific POI
POI[poiCounter].arrival = tracesPtr[traceCounter - 1].timestamp; //the taxi is now already in the POI so set the time it arrived to be the previous traces timestamp
convertTimeFromEpoch(tracesPtr[traceCounter - 1].timestamp, POI[poiCounter].arrivalStr);
inCity = 1;
if(DebugCalcArrDepPOI)
{
printf("Arrival: ");
printPOI(POI[poiCounter]);
}
}
else
{
if(inCity && distanceHaversine(&tracesPtr[traceCounter].coord, &POI[poiCounter].coord) > POI[poiCounter].radius && poiArrivalDeparture[poiCounter][0] == 1)
{
poiArrivalDeparture[poiCounter][1] = 1; //set that the taxi now has deoparted from this specific POI
POI[poiCounter].departure = tracesPtr[traceCounter - 1].timestamp; //the taxi is now already outside the POI so set the time it departed to be the previous traces timestamp
convertTimeFromEpoch(tracesPtr[traceCounter - 1].timestamp, POI[poiCounter].departureStr);
inCity = 0;
if(DebugCalcArrDepPOI)
{
printf("Departure: ");
printPOI(POI[poiCounter]);
}
}
}
}
}
for(poiCounter = 0; poiCounter < numPOI; poiCounter++) //this then checks the first trace and if the taxi is in a POI it sets the arrival to false as the data set stared with the taxi at that location
{
if(distanceHaversine(&tracesPtr[0].coord, &POI[poiCounter].coord) < POI[poiCounter].radius)
{
poiArrivalDeparture[poiCounter][0] = 0;
}
}
for(poiCounter = 0; poiCounter < numPOI; poiCounter++) //checks which POIs where never arrived to
{
if(poiArrivalDeparture[poiCounter][0] == 0)
{
POI[poiCounter].arrival = 0;
strncpy(POI[poiCounter].arrivalStr, "N/A", 3); //set arrival time to zero and arrival string to N/A
POI[poiCounter].arrivalStr[3] = '\0';
}
if(poiArrivalDeparture[poiCounter][1] == 0)//checks which POIs where never departed from
{
POI[poiCounter].departure = 0;
strncpy(POI[poiCounter].departureStr, "N/A", 3);//set departure time to zero and departure string to N/A
POI[poiCounter].departureStr[3] = '\0';
}
}
for(poiCounter = 0; poiCounter < numPOI; poiCounter++) //frees the 2D array
{
free(poiArrivalDeparture[poiCounter]);
}
free(poiArrivalDeparture);
}
/*
*takes in an array of POIs and prints the details to a txt file
*/
void printPOIToText(poi_t * POI, int numberOfPOI)
{
FILE *filePtr = fopen("myPOI.txt", "w");
int timeSpent = 0;
int i;
for(i = 0; i < numberOfPOI; i++)
{
if(i == 0) //prints the heading to the file before it prints the data
{
fputs("\t\tPOI \t\t\t\t\tArrival \t\t\t\t\tDeparture \t\t\t\t\t\tSpent in\n--------------------------------------------------------------------------------------------------------------------\n", filePtr);
}
if(POI[i].departure > 0 && POI[i].arrival > 0) //calculates time spent in the POI
{
timeSpent = (POI[i].departure - POI[i].arrival)/60;
}
else
{
timeSpent = 0;
}
fprintf(filePtr, "%20s %30s %30s %10d min\n", POI[i].name, POI[i].arrivalStr, POI[i].departureStr, timeSpent); //prints the data intp the txt file
}
fclose(filePtr);
}
/*
* this funtion counts how many poi type variables are stored in the binary file and allocates enough memory to store all the POIs to an array
*/
poi_t * countAndAllocateMemoryPOI(char * fileName, int *numOfPOI)
{
setbuf(stdout, 0);
FILE *filePtr;
*numOfPOI = -1;
poiFileHandling(fileName);
filePtr = fopen(fileName, "rb");
while(filePtr == NULL)
{
printf("Invalid file name \n");
poiFileHandling(fileName);
filePtr = fopen(fileName, "rb");
}
do
{
char temp[sizeof(poi_t)];
fread(temp, sizeof(poi_t), 1, filePtr);//counts lines
(*numOfPOI)++;
}while (!feof(filePtr));
fclose(filePtr);
return (poi_t*) malloc((*numOfPOI) * sizeof(poi_t)); //returns a malloc of sufficient size to store data in an array
}
/*
* this function then reads the pois in from a binary file and stores them into the array that is given as the first argument
*/
void ReadInPOI(poi_t * poiArray, char *fileName, int numOfPOI)
{
FILE *filePtr = fopen(fileName, "r");
int i;
for(i = 0; i < numOfPOI; i++)
{
fread(&poiArray[i], sizeof(poi_t), 1, filePtr);
}
fclose(filePtr);
}
/*
* this function creates a new binary file and stores the poi array given to it in this file
*/
void WritePOIToBinary(poi_t * poiArray, int numOfPOI)
{
FILE *filePtr = fopen("UpdatedPOI.bin", "wb");
int i;
for(i = 0; i < numOfPOI; i++)
{
fwrite(&poiArray[i], sizeof(poi_t), 1, filePtr);
}
fclose(filePtr);
}
/*
* this function then reads in the pois from your binaryfile that the previous function created and stores them in an array
*/
void ReadInUpdatedPOI(poi_t * poiArray, int numOfPOI)
{
FILE *filePtr = fopen("UpdatedPOI.bin", "r");
int i;
for(i = 0; i < numOfPOI; i++)
{
fread(&poiArray[i], sizeof(poi_t), 1, filePtr);
}
fclose(filePtr);
}
/*
* displays all the poi data of the pois passed to it
*/
void displayAllPOI(poi_t * poiArray, int numOfPOI)
{
int i;
for(i = 0; i < numOfPOI; i++)
{
printPOI(poiArray[i]);
}
}
/*
* this function allows the user to enter a date and time and the function will find the trace at that time
* or if it doesnt exist it will find the next trace (as stated in the PDF project description)
*/
nr_trace_t findVehicleLocationlinearSearch(nr_trace_t * traces, int numOfTraces)
{
nr_trace_t result;
char userTime[80] = {'\0'}, date[40] = {'\0'}, time[40] = {'\0'};
time_t tempUserEpoch;
int counter = 0;
printf("Enter the date and time please: (DD/MM/YYYY HH:MM:SS)\n");
scanf("%s %s", date, time); //scans in the date and time seperated by a space into two variables (strings)
sprintf(userTime, "%s %s", date, time); //uses the sprintf function to combine these two strings and stores them in one string
while(!checkValidity(userTime, traces, numOfTraces)) //check if the date they entered is valid and if not ask them to enter a different one
{
printf("Invalid entry.\nEnter the date and time please: (DD/MM/YYYY HH:MM:SS)\n");
scanf("%s %s", date, time);
sprintf(userTime, "%s %s", date, time);
}
tempUserEpoch = convertTimeToEpoch(userTime); //converts the entered date into epoch time
while(tempUserEpoch > traces[counter].timestamp) //compares the epoch time to the timestamp of every trace, as soon as the epoch time is equal to or bigger than the trace timestam you have found the correct trace
{
counter++;
}
result = traces[counter];
return result;
}
/*
* this function takes in an epoch time and checks what element number it is in the array of number traces and returns that number
*/
int findTraceElementlinearSearch(time_t userTime, nr_trace_t * traces)
{
int counter = 0;
while(userTime > traces[counter].timestamp)
{
counter++;
}
return counter;
}
/*
* checks if the argument is a number and returns true or false
*/
int isNumber(char character)
{
int result = 0;
if(character >= '0' && character <= '9')
{
result++;
}
return result;
}
/*
* checks if the argument is a '/' or ' ' or ':' and returns true or false
*/
int isPunctuation(char character)
{
int result = 0;
if(character == '/' || character == ':' || character == ' ')
{
result++;
}
return result;
}
/*
* checks if the argument (which is the entered date) is in the correct format so that the it can be converted to epoch time and returns true or false
*/
int checkFormatValidity(char * enteredDate)
{
int result = 0;
if(isNumber(enteredDate[0]))
{
if(isNumber(enteredDate[1]))
{
if(isPunctuation(enteredDate[2]))
{
if(isNumber(enteredDate[3]))
{
if(isNumber(enteredDate[4]))
{
if(isPunctuation(enteredDate[5]))
{
if(isNumber(enteredDate[6]))
{
if(isNumber(enteredDate[7]))
{
if(isNumber(enteredDate[8]))
{
if(isNumber(enteredDate[9]))
{
if(isPunctuation(enteredDate[10]))
{
if(isNumber(enteredDate[11]))
{
if(isNumber(enteredDate[12]))
{
if(isPunctuation(enteredDate[13]))
{
if(isNumber(enteredDate[14]))
{
if(isNumber(enteredDate[15]))
{
if(isPunctuation(enteredDate[16]))
{
if(isNumber(enteredDate[17]))
{
if(isNumber(enteredDate[18]))
{
result++;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return result;
}
/*
* checks if the entered date falls in the range of the trip and returns true or false
*/
int checkDateValidity(char *enteredDate, nr_trace_t *traces, int numOfTraces)
{
int result = 0;
time_t epochTime = convertTimeToEpoch(enteredDate);
if(epochTime <= traces[numOfTraces - 1].timestamp && epochTime >= traces[0].timestamp)
{
result++;
}
return result;
}
/*
* uses the above two funtions to definitivley decide whether the entered date is valid and can be worked with and returns true or false
*/
int checkValidity(char *enteredDate, nr_trace_t *traces, int numOfTraces)
{
int result = 0;
if(checkFormatValidity(enteredDate) && checkDateValidity(enteredDate, traces, numOfTraces))
{
result++;
}
return result;
}
/*
* prints out the details of one number trace
*/
void printNrTrace(nr_trace_t trace)
{
char humanTime[40];
convertTimeFromEpoch(trace.timestamp, humanTime);
printf("Human Time: %s\nEpoch Time: %ld\nLatitude: %lf\nLongitude: %lf\nSpeed: %.2lf (km/h)\n", humanTime, (long) trace.timestamp, trace.coord.latitude, trace.coord.longitude, trace.speed);
}
/*
* prints out the details of one poi
*/
void printPOI(poi_t poi)
{
printf("%20s", poi.name);
printf("%20lf", poi.coord.latitude);
printf("%20lf", poi.coord.longitude);
printf("%20lf", poi.radius);
printf("%20ld", (long) poi.arrival);
printf("%30s", poi.arrivalStr);
printf("%20ld", (long) poi.departure);
printf("%30s\n", poi.departureStr);
}
/*
* sorts the poi array by the date and time the taxi arrived to the poi and then by the departure time
*/
void sortPOI(poi_t *poi, int numOfPOI)
{
int x, y;
for(x = 0; x < numOfPOI - 1; x++)
{
for(y = x + 1; y < numOfPOI; y++)
{
if(poi[x].arrival > poi[y].arrival || poi[x].departure > poi[y].departure)
{
poi_t temp = poi[x];
poi[x] = poi[y];
poi[y] = temp;
}
}
}
}
/*
* this function displays the arrival and departure dates and times as well as the time spent in for a certain poi
*/
void displayPOIinfo(poi_t poi)
{
int timeSpent;
if(poi.departure > 0 && poi.arrival > 0)
{
timeSpent = (poi.departure - poi.arrival)/60;
}
else
{
timeSpent = 0;
}
printf("%20s %30s %30s %10d min\n", poi.name, poi.arrivalStr, poi.departureStr, timeSpent);
}
/*
* uses a loop and the previous function to print all the important information for all the pois
*/
void displayAllPOIinfo(poi_t *poi, int numOfPOI)
{
int i;
printf("\t\tPOI \t\t\tArrival \t\tDeparture \t\tSpent in\n----------------------------------------------------------------------------------------------------------\n");
for(i = 0; i < numOfPOI; i++)
{
displayPOIinfo(poi[i]);
}
}
/*
*
*/
void poiFileHandling(char *fileName)
{
setbuf(stdout, 0);
int option;
if(sizeof(poi_t) == 120)
{
printf("\nYou should use the 32 bit POI Binary file.\nWould you like to: \n1. Use the default 32 bit POI Binary File \n2. Specify your own Binary file");
scanf("%d", &option);
do
{
switch(option)
{
case 1:
sprintf(fileName, "%s", "POIs_32bit.bin");
break;
case 2:
printf("Enter your own Binary file name: \n");
scanf("%s", fileName);
break;
default:
printf("Invalid Option.\n");
printf("\nYou should use the 32 bit POI Binary file.\nWould you like to: \n1. Use the default 32 bit POI Binary File \n2. Specify your own Binary file");
scanf("%d", &option);
break;
}
}while(option != 1 && option != 2);
}
else
{
if(sizeof(poi_t) == 128)
{
printf("\nYou should use the 64 bit POI Binary file.\nWould you like to: \n1. Use the default 64 bit POI Binary File \n2. Specify your own Binary file");
scanf("%d", &option);
do
{
switch(option)
{
case 1:
sprintf(fileName, "%s", "POIs_64bit.bin");
break;
case 2:
printf("Enter your own Binary file name: \n");
scanf("%s", fileName);
break;
default:
printf("Invalid Option.\n");
printf("\nYou should use the 64 bit POI Binary file.\nWould you like to: \n1. Use the default 64 bit POI Binary File \n2. Specify your own Binary file");
scanf("%d", &option);
break;
}
}while(option != 1 && option != 2);
}
else
{
printf("\nYou should create your own POI Binary file.\nWould you like the program to do it for you now? (y/n)");
scanf("%d", &option);
do
{
switch(option)
{
case 121:
case 89:
createOwnPOIFile();
sprintf(fileName, "%s", "myOwnPOIFile.bin");
break;
case 110:
case 78:
printf("Enter your own Binary file name: \n");
scanf("%s", fileName);
break;
default:
printf("Invalid Option.\n");
printf("\nYou should create your own POI Binary file.\nWould you like the program to do it for you now? (y/n)");
scanf("%d", &option);
break;
}
}while(option != 121 && option != 89 && option != 78 && option != 110);