-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.c
80 lines (67 loc) · 2.65 KB
/
load.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
#include "load.h"
#define BUFFER_SIZE 64
bool LoadConfiguration(const char *fileName, TStop **stops_head, TLine **lines_head) {
// Open config file
FILE *configFile = fopen(fileName, "r");
if (configFile == NULL) {
perror("[E_0x2]: Provided configuration file cannot be opened!");
return false;
}
// Read config file
char character;
char *data = NULL;
int dataP, dataMaxLength, latestTravelTime;
bool isNewTLine = true;
TLine *latestLineCreated = NULL;
ProcessStage currentStage;
while ((character = fgetc(configFile)) != EOF) {
if (isNewTLine)
currentStage = ProcessSign;
if (data == NULL) {
dataP = 0;
dataMaxLength = BUFFER_SIZE;
data = (char *) malloc(sizeof(char) * dataMaxLength);
} else if (dataP == dataMaxLength) {
dataMaxLength += BUFFER_SIZE;
data = (char *) realloc(data, sizeof(char) * dataMaxLength);
}
if (character == ';' || character == '\n') {
data[dataP] = '\0';
// Process data ("string" between ';' and '\n')
int id;
TStop *currentStop = NULL;
switch (currentStage) {
case ProcessSign:
latestLineCreated = TLine_init(data);
*lines_head = TLine_push(*lines_head, latestLineCreated);
latestTravelTime = -1;
currentStage = ProcessStop;
break;
case ProcessStop:
// Create new or get existing stop by name
currentStop = TStop_IsNameExists(*stops_head, data);
if (currentStop == NULL) {
currentStop = TStop_init(data);
*stops_head = TStop_push(*stops_head, currentStop);
} else
free(data); // The stop is already existing. Free up the unused text
// Add the latest line to the stop's transfers
TStop_addTransfer(currentStop, latestLineCreated);
// Add the stop to the line
TLine_addStop(latestLineCreated, currentStop, latestTravelTime);
currentStage = ProcessTime;
break;
case ProcessTime:
latestTravelTime = atoi(data);
free(data);
currentStage = ProcessStop;
break;
}
data = NULL;
} else
data[dataP++] = character;
isNewTLine = character == '\n';
}
fclose(configFile);
return true;
}