-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
126 lines (115 loc) · 2.49 KB
/
parser.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rblondia <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/01 16:22:04 by rblondia #+# #+# */
/* Updated: 2021/12/13 10:13:29 by rblondia ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/fdf.h"
static t_map *create_map(void)
{
t_map *map;
map = malloc(sizeof(t_map));
if (!map)
return (NULL);
map->z_max = 0;
map->z_min = 0;
return (map);
}
static t_v3f **parse_line(char *line, int y)
{
t_v3f **vectors;
char **s;
int i;
t_v3f *tmp;
if (!line)
return (NULL);
s = ft_split(line, ' ');
i = 0;
vectors = malloc(sizeof(t_v3f *) * (double_length(s) + 1));
if (!vectors)
return (NULL);
while (s[i])
{
tmp = allocate_v3f(i, y, s[i]);
v3f_validate(tmp);
vectors[i] = tmp;
free(s[i]);
i++;
}
free(s);
free(line);
vectors[i] = 0;
return (vectors);
}
static t_map *parse_file(int fd)
{
t_map *map;
t_v3f **vectors;
t_v3f **c;
char *tmp;
tmp = get_next_line(fd);
map = create_map();
map->height = 0;
vectors = parse_line(tmp, map->height);
map->height++;
while (tmp)
{
tmp = get_next_line(fd);
if (!tmp)
break ;
c = parse_line(tmp, map->height);
vectors = join_v3f(vectors, c, map);
map->height++;
}
close(fd);
map->vectors = vectors;
return (map);
}
t_map *parse_map(char *filename)
{
int fd;
t_map *map;
if (!filename)
{
perror(strerror(2));
exit(EXIT_FAILURE);
}
fd = open(filename, O_RDONLY);
if (fd < 0)
{
perror(strerror(9));
exit(EXIT_FAILURE);
}
map = parse_file(fd);
if (!map)
{
perror(ERR_MAP);
exit(EXIT_FAILURE);
}
convert_vectors(map);
return (map);
}
void free_map(t_map *map)
{
int index;
index = 0;
while (map->vectors[index])
{
free(map->vectors[index]);
index++;
}
index = 0;
while (map->cords[index])
{
free(map->cords[index]);
index++;
}
free(map->cords);
free(map->vectors);
free(map);
}