-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilereader.c
More file actions
68 lines (53 loc) · 1.46 KB
/
filereader.c
File metadata and controls
68 lines (53 loc) · 1.46 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "filereader.h"
#include <ctype.h>
Point* getPoints(char* line) {
Point* point = malloc(sizeof(Point));
double x;
double y;
char* tokens = strtok(line, ",");
x = atof(tokens);
tokens = strtok(NULL, ",");
y = atof(tokens);
point->x = x;
point->y = y;
return point;
}
Artwork* filereader(char* path) {
char line[BUFFER];
FILE* artfile = fopen(path, "r");
Artwork* artwork = malloc(sizeof(Artwork));
artwork->segments = malloc(sizeof(Segment*));
artwork->numSegments = 0;
Segment* segment;
Point* point;
int pointCount = 0;
int segmentCount = 0;
while ((fgets(line, BUFFER, artfile)) != NULL) {
if (strncmp(line, "STA", 3) == 0) {
pointCount = 0;
segment = malloc(sizeof(Segment));
segment->points = malloc(sizeof(Point*));
segment->points[pointCount] = '\0';
segment->numPoints = 0;
} else if (strncmp(line, "STO", 3) == 0) {
artwork->segments = realloc(artwork->segments,
sizeof(Segment*) * (segmentCount + 2));
artwork->segments[segmentCount] = segment;
artwork->segments[segmentCount + 1] = '\0';
artwork->numSegments++;
segmentCount++;
} else if (isdigit(line[0])) {
segment->points = realloc(segment->points, sizeof(Point*) * (pointCount + 2));
point = getPoints(line);
segment->points[pointCount] = point;
segment->points[pointCount + 1] = '\0';
segment->numPoints++;
pointCount++;
}
}
fclose(artfile);
return artwork;
}