-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_rooms.c
98 lines (87 loc) · 2.57 KB
/
get_rooms.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
92
93
94
95
96
97
98
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_rooms.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vcombey <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/19 19:51:23 by vcombey #+# #+# */
/* Updated: 2017/03/08 16:52:50 by vcombey ### ########.fr */
/* */
/* ************************************************************************** */
#include "lem_in.h"
#include "libft/libft.h"
void room_add(char *name, t_anthill *a)
{
t_room *new;
new = (t_room*)malloc(sizeof(t_room));
if (!(new->name = ft_strdup(name)))
ft_exit_err("malloc error", NULL);
new->n = a->nb_room;
new->next = a->room;
a->room = new;
a->nb_room++;
}
void get_room(char *line, t_anthill *a, t_file *f)
{
char **s;
int result;
s = ft_strsplit(line, ' ');
if ((ft_strstrlen(s) != 3) || (!(ft_atoi_safe(s[1], &result))) ||
(!(ft_atoi_safe(s[2], &result))))
ft_exit_err("bad definition of a room", f);
if (s[0][0] == 'L')
ft_exit_err("name can't start with a L", f);
if (is_in_lst(s[0], a->room))
ft_exit_err("same room is defined twice", f);
room_add(s[0], a);
ft_tab_free(s);
}
void get_start(t_file *f, t_anthill *a)
{
char *line;
f->line++;
if ((line = f->data[f->line - 1]) == NULL)
ft_exit_err("no start", f);
if (a->start != -1)
ft_exit_err("redefinition of start", f);
a->start = a->nb_room;
get_room(line, a, f);
}
void get_end(t_file *f, t_anthill *a)
{
char *line;
f->line++;
if ((line = f->data[f->line - 1]) == NULL)
ft_exit_err("no end", f);
if (a->end != -1)
ft_exit_err("redefinition of end", f);
a->end = a->nb_room;
get_room(line, a, f);
}
void get_rooms(t_anthill *a, t_file *f)
{
char *line;
a->start = -1;
a->end = -1;
while ((line = f->data[f->line - 1]) != NULL)
{
if (ft_strequ(line, "##start"))
get_start(f, a);
else if (ft_strequ(line, "##end"))
get_end(f, a);
else if (line[0] == '#')
{
;
}
else if (ft_strchr(line, '-') != NULL)
break ;
else
get_room(line, a, f);
f->line++;
}
if (a->start == -1)
ft_exit_err("no start room", f);
if (a->end == -1)
ft_exit_err("no end room", f);
}