-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
84 lines (75 loc) · 2.13 KB
/
parser.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mntumba <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/12/14 21:52:24 by mntumba #+# #+# */
/* Updated: 2018/01/09 15:37:16 by mntumba ### ########.fr */
/* */
/* ************************************************************************** */
#include "wolf3d.h"
static char *ft_strdup(char *str)
{
char *new_str;
int i;
i = 0;
while (str[i])
i++;
if (!(new_str = (char*)malloc((i + 2) * sizeof(char))))
return (NULL);
i = -1;
while (str[++i] != '\0')
new_str[i] = str[i];
new_str[i + 1] = '\0';
return (new_str);
}
static char *read_file(const int fd)
{
int i;
int len;
char buf[BUF_SIZE];
char *str;
i = 0;
if (!(str = (char*)malloc(sizeof(char))))
return (NULL);
str[0] = '\0';
while ((len = read(fd, buf, BUF_SIZE)) > 0 && buf[0] != '\n')
{
str = ft_strdup(str);
str[i++] = buf[0];
}
return ((len == 0 && i == 0) ? NULL : str);
}
static void fill_map(t_wolf *wolf, char *str, int j)
{
int i;
int len;
i = -1;
len = 0;
while (str[len])
len++;
if (!(wolf->map[j] = (char*)malloc(len * sizeof(char))))
exit(EXIT_FAILURE);
len = 0;
while (str[++i])
if (str[i] != ' ')
wolf->map[j][len++] = str[i];
}
void ft_parser(t_wolf *wolf)
{
int fd;
int i;
char *line;
i = -1;
if ((fd = open("map", O_RDONLY)) == 1 &&
write(1, "Fail to open the map\n", 21))
exit(EXIT_FAILURE);
if (!(wolf->map = (char**)malloc(24 * sizeof(char*))))
exit(EXIT_FAILURE);
while ((line = read_file(fd)) != NULL)
fill_map(wolf, line, i++);
free(line);
close(fd);
}