-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker_main.c
79 lines (72 loc) · 2.35 KB
/
checker_main.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* checker_main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marias-e <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/09 10:52:56 by marias-e #+# #+# */
/* Updated: 2023/02/13 12:02:58 by marias-e ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void ft_execute_order(char *buffer, t_list **stack_a, t_list **stack_b,
int *error)
{
if (!ft_strncmp(buffer, "sa\n", 3))
ft_swap(stack_a);
else if (!ft_strncmp(buffer, "sb\n", 4))
ft_swap(stack_b);
else if (!ft_strncmp(buffer, "ss\n", 4))
ft_swap_both(stack_a, stack_b);
else if (!ft_strncmp(buffer, "pa\n", 4))
ft_push(stack_b, stack_a);
else if (!ft_strncmp(buffer, "pb\n", 4))
ft_push(stack_a, stack_b);
else if (!ft_strncmp(buffer, "ra\n", 4))
ft_rotate(stack_a);
else if (!ft_strncmp(buffer, "rb\n", 4))
ft_rotate(stack_b);
else if (!ft_strncmp(buffer, "rr\n", 4))
ft_rotate_both(stack_a, stack_b);
else if (!ft_strncmp(buffer, "rra\n", 5))
ft_rev_rotate(stack_a);
else if (!ft_strncmp(buffer, "rrb\n", 5))
ft_rev_rotate(stack_b);
else if (!ft_strncmp(buffer, "rrr\n", 5))
ft_rev_rotate_both(stack_a, stack_b);
else
*error = 1;
}
void ft_receive_orders(t_list **stack_a)
{
t_list *stack_b;
char *buffer;
int error;
error = 0;
stack_b = 0;
buffer = get_next_line(0);
while (buffer)
{
ft_execute_order(buffer, stack_a, &stack_b, &error);
buffer = get_next_line(0);
}
if (!ft_lstsize(stack_b) && !ft_check_order(*stack_a) && !error)
ft_printf("OK\n");
else if (error)
ft_exit(1);
else
ft_printf("KO\n");
ft_exit(0);
}
int main(int argc, char **argv)
{
t_list *stack_a;
if (argc == 1)
ft_exit(0);
stack_a = 0;
ft_arg_manager(argv, argc, &stack_a);
ft_receive_orders(&stack_a);
ft_exit(0);
}
//PROTEGER FUNCIONES