-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
95 lines (86 loc) · 2.33 KB
/
parse.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmomeni <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/26 20:04:31 by mmomeni #+# #+# */
/* Updated: 2023/07/10 16:50:26 by mmomeni ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
static size_t count_lines(int fd)
{
char *line;
size_t i;
i = 0;
while (1)
{
line = get_next_line(fd);
if (!line)
break ;
i++;
free(line);
line = NULL;
}
return (i);
}
static void write_row(t_point **points, char **v, size_t i, long int jz_max[2])
{
long int j;
long int z;
t_point p;
points[i] = malloc((ft_split_len(v) + 1) * sizeof(t_point));
if (!points[i])
terminate("map (one of lines) too large", -1);
j = 0;
while (v[j])
{
z = ft_atoi(v[j]);
if (z > jz_max[1])
jz_max[1] = z;
p = (t_point){.x = j, .y = i, .x0 = j, .y0 = i, .z = z, .z0 = z,
.eol = 0};
points[i][j] = p;
j++;
}
points[i][j] = (t_point){.x = 0, .y = 0, .z = 0, .z0 = 0, .eol = 1};
if (j > jz_max[0])
jz_max[0] = j;
}
static void file_into_map(int fd, t_map *map)
{
char *line;
char **vertices;
size_t i;
long int jz_max[2];
ft_bzero(jz_max, sizeof(jz_max));
lseek(fd, 0, SEEK_SET);
i = 0;
while (1)
{
line = get_next_line(fd);
if (!line)
break ;
vertices = ft_split(line, ' ');
free(line);
write_row(map->points, vertices, i++, jz_max);
ft_free_split(vertices);
vertices = NULL;
}
map->points[i] = NULL;
map->height = i;
map->width = jz_max[0];
map->depth = jz_max[1];
}
t_map *map_from_file(int fd)
{
t_map *map;
map = malloc(sizeof(t_map));
map->points = malloc((count_lines(fd) + 1) * sizeof(t_point *));
if (!map->points)
terminate("map (rows) too large", -1);
file_into_map(fd, map);
return (map);
}