-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_arg.c
102 lines (93 loc) · 2.55 KB
/
parse_arg.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_arg.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ybarbier <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/12/12 13:25:45 by ybarbier #+# #+# */
/* Updated: 2015/03/06 23:35:55 by ybarbier ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
int ft_getnbr(char *str)
{
int nbr;
int neg;
neg = 0;
if (str[0] == '-')
neg = 1, str++;
nbr = 0;
while ((*str >= '0') && (*str <= '9'))
nbr = (nbr * 10) + *str++ - '0';
return ((neg == 1) ? -nbr : nbr);
}
int ft_points(char *line, int nb_line, t_point ***array_points)
{
char **array_str;
t_point *a_point;
int i;
array_str = ft_strsplit(line, ' ');
i = 0;
while (array_str[i] != 0)
i++;
if (!((*array_points) = (t_point**)malloc(sizeof(t_point) * i)))
fdf_malloc_error();
i = 0;
while (array_str[i] != 0)
{
a_point = (t_point*)malloc(sizeof(t_point));
a_point->x = i * SIZE_W;
a_point->y = nb_line * SIZE_H;
a_point->z = ft_getnbr(array_str[i]);
a_point->s = 1;
a_point->z_color = a_point->z;
(*array_points)[i] = a_point;
i++;
}
return (i);
}
int ft_map_line(char *map)
{
int fd;
int nb_lines;
char buf;
fd = 0;
nb_lines = 0;
if ((fd = open(map, O_RDONLY)) < 0)
fdf_map_error();
while (read(fd, &buf, 1))
{
if (buf == '\n')
nb_lines++;
}
close(fd);
return (nb_lines);
}
t_map *ft_parse_map(char **av, int fd)
{
char *line;
int nb_line;
t_map *map;
t_line *line_map;
t_point **array_points;
nb_line = 0;
if (!(map = (t_map*)malloc(sizeof(t_map))) ||
!(map->lines = (t_line**)malloc(sizeof(t_line) * ft_map_line(av[1]))))
fdf_malloc_error();
map->len = 0;
if ((fd = open(av[1], O_RDONLY)) > 0)
{
while ((get_next_line(fd, &line)) > 0)
{
if (!(line_map = (t_line*)malloc(sizeof(t_line))))
fdf_malloc_error();
line_map->len = ft_points(line, nb_line, &array_points);
line_map->points = array_points;
map->lines[nb_line] = line_map;
nb_line++;
}
map->len = nb_line;
}
return (map);
}