-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_tab.c
More file actions
100 lines (91 loc) · 2.1 KB
/
Copy pathinit_tab.c
File metadata and controls
100 lines (91 loc) · 2.1 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init_tab.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kamanfo <kamanfo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/21 14:30:19 by kamanfo #+# #+# */
/* Updated: 2022/01/01 19:18:09 by kamanfo ### ########.fr */
/* */
/* ************************************************************************** */
#include "init_tab.h"
void free_map(char **map)
{
int i;
i = 0;
while (map[i])
{
free(map[i]);
map[i] = NULL;
i++;
}
free(map);
}
char **initialize_tmap(t_map *lmap)
{
char **tab;
int i;
int len;
if (!lmap)
print_error(NULL);
len = lst_len(lmap);
tab = (char **)malloc((len + 1) * sizeof(char *));
if (!tab)
return (NULL);
i = 0;
while (i < len)
{
tab[i] = ft_strdup(lmap->line);
if (!tab[i])
{
return (free_map(tab), NULL);
}
i++;
lmap = lmap->next;
}
tab[i] = NULL;
if (!verif_map(tab))
return (free_map(tab), NULL);
return (tab);
}
int verif_list(t_map *lmap)
{
t_map *tmp;
tmp = lmap;
if (lst_len(lmap) < 3)
return (0);
while (tmp->next)
{
if (tmp->len != tmp->next->len || tmp->len < 4)
return (0);
tmp = tmp->next;
}
return (1);
}
t_map *parse_map(int fd)
{
char *line;
t_map *lmap;
size_t len;
int i;
if (fd == -1)
print_error(NULL);
i = 0;
len = 0;
line = get_next_line(fd);
if (!line)
return (NULL);
len = ft_strlen(line);
lmap = creat_lmap_elem(line, len);
while (line)
{
line = get_next_line(fd);
if (!line)
break ;
push_last_elem(&lmap, line, ft_strlen(line));
}
if (!verif_list(lmap))
return (ft_lstclear(&lmap), NULL);
return (lmap);
}