-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_map.c
91 lines (83 loc) · 2.05 KB
/
read_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoumni <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/20 12:40:11 by mmoumni #+# #+# */
/* Updated: 2022/01/05 22:49:59 by mmoumni ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
void free_tab(char **tab)
{
int i;
i = 0;
while (tab[i] != NULL)
{
free(tab[i]);
i++;
}
free(tab);
}
int check_line_error(int width, int i, int *check_fd, char **split)
{
free_tab(split);
if (width > i)
{
*check_fd = -2;
return (1);
}
return (0);
}
int get_line_info(char *line, t_stack **stack, int *width, int *check_fd)
{
int i;
char **split;
char **sec_split;
split = ft_split(line, ' ');
i = 0;
while (split[i] != NULL)
{
sec_split = ft_split(split[i], ',');
if (fill_stack(sec_split, stack))
{
free_tab(sec_split);
free_tab(split);
*check_fd = -1;
return (1);
}
free_tab(sec_split);
i++;
}
if (check_line_error(*width, i, check_fd, split))
return (1);
return (0);
}
t_stack *read_map(int fd, int *height, int *width, int *check_fd)
{
char *tmp;
t_stack *stack;
int i;
int check;
i = 0;
tmp = get_next_line(fd);
if (tmp == NULL)
print_error(1);
stack = NULL;
*width = ft_words(tmp, ' ');
while (tmp != NULL)
{
i++;
check = get_line_info(tmp, &stack, width, check_fd);
if (check != 0)
return (NULL);
free(tmp);
tmp = get_next_line(fd);
}
*height = i;
free(tmp);
close(fd);
return (stack);
}