-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_get_the_map.c
91 lines (82 loc) · 2.13 KB
/
ft_get_the_map.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_get_the_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mbourdel <mbourdel@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/11/25 21:34:20 by mbourdel #+# #+# */
/* Updated: 2015/01/04 17:47:45 by mbourdel ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
static t_map ft_put_on_link(int *tab, t_map map, int size)
{
t_map new_link;
int i;
i = 0;
new_link = (t_map)malloc(sizeof(t_line));
new_link->intline = malloc(sizeof(int) * size);
new_link->size = size;
while (i < size)
{
new_link->intline[i] = tab[i];
i++;
}
new_link->nxt = map;
return (new_link);
}
static t_map ft_rev_map(t_map map)
{
t_map new_map;
new_map = NULL;
while (map != NULL)
{
new_map = ft_put_on_link(map->intline, new_map, map->size);
map = map->nxt;
}
return (new_map);
}
static int *ft_get_int_line(int fd, int *line, int *size)
{
char *str;
char **split;
int i;
*size = 0;
if (get_next_line(fd, &str) <= 0)
return (0);
split = ft_strsplit(str, ' ');
while (split[*size])
*size = *size + 1;
line = malloc(sizeof(int) * *size);
i = 0;
while (i < *size)
{
if (!ft_isnbr(split[i]))
{
*size = -1;
return (0);
}
line[i] = ft_atoi(split[i]);
free(split[i]);
i++;
}
free(split);
return (line);
}
t_map ft_get_the_map(int fd, t_map map)
{
int *line;
int size;
size = 1;
while (size > 0)
{
line = ft_get_int_line(fd, line, &size);
if (size > 0)
map = ft_put_on_link(line, map, size);
free(line);
}
if (size < 0)
return (NULL);
return (ft_rev_map(map));
}