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
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/18 10:16:08 by cacharle #+# #+# */
/* Updated: 2020/01/18 10:52:42 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "checker.h"
#define ACTION_ID_BUF_SIZE 4
t_status check(t_stack *a, t_stack *b)
{
t_status read_status;
while ((read_status = read_action(a, b)) != STATUS_EOF)
if (read_status == STATUS_ERROR)
return (STATUS_ERROR);
return (stack_sorted(a) && stack_empty(b) ?
STATUS_SUCCESS : STATUS_FAILURE);
}
t_status read_action(t_stack *a, t_stack *b)
{
int i;
int ret;
char c;
char buf[ACTION_ID_BUF_SIZE];
ft_bzero(buf, sizeof(char) * 4);
i = 0;
while ((ret = read(STDIN_FILENO, &c, 1)) > 0)
{
if (c == '\n')
return (exec_action(buf, a, b));
if (i >= ACTION_ID_BUF_SIZE - 1)
return (STATUS_ERROR);
buf[i++] = c;
buf[i] = '\0';
}
return (ret == 0 ? STATUS_EOF : STATUS_ERROR);
}
t_action g_actions[] = {
{"sa", FLAG_ARG_A, {.arg_1 = &swap_a}},
{"sb", FLAG_ARG_B, {.arg_1 = &swap_b}},
{"ss", FLAG_ARG_A_B, {.arg_2 = &swap_both}},
{"pa", FLAG_ARG_A_B, {.arg_2 = push_a}},
{"pb", FLAG_ARG_B_A, {.arg_2 = push_a}},
{"ra", FLAG_ARG_A, {.arg_1 = rotate_a}},
{"rb", FLAG_ARG_B, {.arg_1 = rotate_b}},
{"rr", FLAG_ARG_A_B, {.arg_2 = rotate_both}},
{"rra", FLAG_ARG_A, {.arg_1 = reverse_rotate_a}},
{"rrb", FLAG_ARG_B, {.arg_1 = reverse_rotate_b}},
{"rrr", FLAG_ARG_A_B, {.arg_2 = reverse_rotate_both}},
};
#define ACTIONS_SIZE (sizeof(g_actions) / sizeof(t_action))
t_status exec_action(char action_id[ACTION_ID_BUF_SIZE], t_stack *a, t_stack *b)
{
int i;
i = -1;
while (ft_strcmp(action_id, g_actions[i].id) != 0)
i++;
if (i == ACTIONS_SIZE)
return (STATUS_ERROR);
if (g_actions[i].args & FLAG_ARG_A)
g_actions[i].func.arg_1(a);
else if (g_actions[i].args & FLAG_ARG_B)
g_actions[i].func.arg_1(b);
else if (g_actions[i].args & FLAG_ARG_A_B)
g_actions[i].func.arg_2(a, b);
return (STATUS_SUCCESS);
}
t_bool stack_sorted(t_stack *stack)
{
int i;
if (stack_length(stack) < 2)
return (TRUE);
i = -1;
while (++i < stack->top - 1)
if (stack->elements[i] > stack->elements[i + 1])
return (FALSE);
return (TRUE);
}
|