-
Notifications
You must be signed in to change notification settings - Fork 0
/
readmap.c
71 lines (65 loc) · 1.88 KB
/
readmap.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* readmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: eteyssed <eteyssed@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/11/28 15:09:24 by eteyssed #+# #+# */
/* Updated: 2014/11/28 19:38:19 by eteyssed ### ########.fr */
/* */
/* ************************************************************************** */
#include "head.h"
t_3dpos **ft_fill_tab(t_list *lst, t_3dpos pt)
{
t_3dpos **tab;
int i;
i = 0;
tab = (t_3dpos**)malloc(sizeof(t_3dpos*) * pt.y);
while (i < pt.y)
tab[i++] = (t_3dpos*)malloc(sizeof(t_3dpos) * pt.x);
while (lst)
{
pt = *((t_3dpos*)lst->content);
pt.z = -pt.z;
tab[pt.y][pt.x] = pt;
lst = lst->next;
}
return (tab);
}
t_all ft_read_map(int fd, char c)
{
char *tmp;
char **tabx;
int erno;
t_list *lst;
t_all all;
lst = NULL;
all.max.y = 0;
while ((erno = get_next_line(fd, &tmp)))
{
ft_dir_err(erno);
all.max.x = 0;
tabx = ft_strsplit(tmp, c);
free(tmp);
while (*tabx)
{
all.max.z = ft_atoi(*tabx);
ft_lstadd(&lst, ft_lstnew(&all.max, sizeof(t_3dpos)));
all.max.x++;
free(*tabx++);
}
all.max.y++;
}
all.tab = ft_fill_tab(lst, all.max);
return (all);
}
void ft_dir_err(int erno)
{
if (erno == -1)
{
ft_putstr_fd("fdf: The file you tried to open is a directory. ", 2);
ft_putendl_fd("Put a valid map as argument.", 2);
exit (0);
}
}