-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_input.c
More file actions
executable file
·69 lines (64 loc) · 2.19 KB
/
parse_input.c
File metadata and controls
executable file
·69 lines (64 loc) · 2.19 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_input.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mbartole <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/07/05 18:58:51 by ehugh-be #+# #+# */
/* Updated: 2019/09/29 13:09:33 by mbartole ### ########.fr */
/* */
/* ************************************************************************** */
#include "lem_in.h"
void get_instruction(t_mngr *mngr, char *line)
{
if (ft_strcmp(line + 2, "start") == 0)
mngr->instr = START;
else if (ft_strcmp(line + 2, "end") == 0)
mngr->instr = FINISH;
else
mngr->instr = INSTR_NONE;
}
static t_elt parse_by_type(t_mngr *mngr, char *line, t_elt type)
{
if (type == LINK)
type = get_link(mngr, line);
else if (type == ROOM)
type = get_room(mngr, line);
else if (type == INSTRUCTION)
get_instruction(mngr, line);
else if (type == ANT_N)
mngr->ant_num = ft_atoi(line);
if (!(type == ERROR))
{
mngr->input = ft_vecpush(mngr->input, line, ft_strlen(line));
if (!(mngr->input = ft_vecpush(mngr->input, "\n", 1)))
ultimate_exit(mngr, MALLOC_ERROR);
}
return (type);
}
void parse_input(t_mngr *mngr)
{
char *line;
char *tmp;
t_elt type;
while (get_next_line(STDIN_FILENO, &line) > 0)
{
tmp = line;
line = ft_strtrim(line);
free(tmp);
type = check_line_type(line);
if (type < mngr->max_lt)
type = ERROR;
else if (type > mngr->max_lt && type < COMMENT)
mngr->max_lt = type;
type = parse_by_type(mngr, line, type);
free(line);
if (type == ERROR)
break ;
}
if (!mngr->start || !mngr->end || mngr->ant_num < 0)
ultimate_exit(mngr, NOT_ENOUGH_DATA);
if (!(mngr->input = ft_vecpush(mngr->input, "\n", 1)))
ultimate_exit(mngr, MALLOC_ERROR);
}