-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.c
More file actions
215 lines (167 loc) · 4.79 KB
/
data.c
File metadata and controls
215 lines (167 loc) · 4.79 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "data.h"
#include "list.h"
// Skips the header line of the given CSV file and points to the next line
void SkipHeaderLine(FILE *f) {
while (fgetc(f) != '\n');
}
// Reads each line of the CSV file and stores the data in each field as a dynamic struct element
footpath_t *read_data(FILE *f) {
char line[MAX_LINE_LEN+1], temp[MAX_STRING_LEN+1];
char *string;
int str_len, column, x;
footpath_t *a = NULL;
// Define the type of each struct element
int footpath_id;
char address[MAX_STRING_LEN + 1];
char clue_sa[MAX_STRING_LEN + 1];
char asset_type[MAX_STRING_LEN + 1];
double deltaz;
double distance;
double grade1in;
int mcc_id;
int mccid_int;
double rlmax;
double rlmin;
char segside[MAX_STRING_LEN + 1];
int statusid;
int streetid;
int street_group;
double start_lat;
double start_lon;
double end_lat;
double end_lon;
// Scans through the input CSV file line by line
while (fscanf(f, "%[^\n]", line)!=EOF) {
fgetc(f);
// allocates memory for struct a
a = malloc(sizeof(*a));
assert(a);
// Converts the line into a single string and finds its length
string = strdup(line);
str_len = strlen(string);
// Variable to check for quotation marks
int found = 0;
// Counts the number of fields already stored into the struct
column=-1;
// Character array to process each individual character in the line
char curr_char[MAX_STRING_LEN+1];
memset(temp,0,MAX_STRING_LEN);
// Parses through each character in the line to add it to its appropriate struct element
for(x=0;x<str_len;x++) {
sscanf(&string[x],"%c",curr_char);
// If double quotation marks are found within the fields
if (strcmp(curr_char,"\"")==0) {
found++;
// If current character is a comma
} else if (strcmp(curr_char,",")==0) {
// If the comma is present before or after the quotation marks
if (found==0 || found==2) {
// Move to the next column
column++;
// Lines 86-179 convert the string to the appropriate type for that particular attribute by identifying the column number it is present in and stores it in the struct element
if (column==0) {
sscanf(temp,"%d",&footpath_id);
a->footpath_id = footpath_id;
}
if (column==1) {
a->address = strdup(temp);
assert(a->address);
}
if (column==2) {
a->clue_sa = strdup(temp);
assert(a->clue_sa);
}
if (column==3) {
a->asset_type = strdup(temp);
assert(a->asset_type);
}
if (column==4) {
sscanf(temp,"%lf",&deltaz);
a->deltaz = deltaz;
}
if (column==5) {
sscanf(temp,"%lf",&distance);
a->distance = distance;
}
if (column==6) {
sscanf(temp,"%lf",&grade1in);
a->grade1in = grade1in;
}
if (column==7) {
sscanf(temp,"%d",&mcc_id);
a->mcc_id = mcc_id;
}
if (column==8) {
sscanf(temp,"%d",&mccid_int);
a->mccid_int = mccid_int;
}
if (column==9) {
sscanf(temp,"%lf",&rlmax);
a->rlmax = rlmax;
}
if (column==10) {
sscanf(temp,"%lf",&rlmin);
a->rlmin = rlmin;
}
if (column==11) {
a->segside = strdup(temp);
assert(a->segside);
}
if (column==12) {
sscanf(temp,"%d",&statusid);
a->statusid = statusid;
}
if (column==13) {
sscanf(temp,"%d",&streetid);
a->streetid = streetid;
}
if (column==14) {
sscanf(temp,"%d",&street_group);
a->street_group = street_group;
}
if (column==15) {
sscanf(temp,"%lf",&start_lat);
a->start_lat = start_lat;
}
if (column==16) {
sscanf(temp,"%lf",&start_lon);
a->start_lon = start_lon;
}
if (column==17) {
sscanf(temp,"%lf",&end_lat);
a->end_lat = end_lat;
}
if (column==18) {
sscanf(temp,"%lf",&end_lon);
a->end_lon = end_lon;
}
memset(temp,0,MAX_STRING_LEN);
} else {
// If comma is present within quotation marks
// Adds the current character to the buffer array until another quote is found
strcat(temp,curr_char);
continue;
}
} else {
// Adds the current character to the buffer array until another quote is found
strcat(temp,curr_char);
}
// If the end of line is reached, convert the remaining string into appropriate type and store it in the last element of the struct
if (x==str_len-1) {
sscanf(temp,"%lf",&end_lon);
a->end_lon = end_lon;
}
if (found==2) {
//If value within quotation marks is stored
// Resets counter to 0
found=0;
}
}
}
// Returns the struct back to the node of the linked list
return a;
}