-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecker.c
130 lines (120 loc) · 3.42 KB
/
checker.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* checker.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: krios-fu <krios-fu@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/06/02 19:03:33 by krios-fu #+# #+# */
/* Updated: 2021/06/03 15:53:56 by krios-fu ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void exec_arg(t_list **stack_a, t_list **stack_b, char *cmd)
{
if (ft_lstsize((*stack_a)) > 1 && (!ft_strncmp("sa", cmd, 2)
|| !ft_strncmp(cmd, "ss", 2)))
swap_stack(*stack_a, 'Q');
if (ft_lstsize((*stack_b)) > 1 && (!ft_strncmp("sb", cmd, 2)
|| !ft_strncmp(cmd, "ss", 2)))
swap_stack(*stack_b, 'Q');
if (!ft_strncmp("pb", cmd, 2) && *stack_a)
push(stack_a, stack_b, 'Q');
if (!ft_strncmp("pa", cmd, 2) && *stack_b)
push(stack_b, stack_a, 'Q');
if (ft_lstsize((*stack_a)) > 1 && (!ft_strncmp("rra", cmd, 3)
|| !ft_strncmp("rrr", cmd, 3)))
reverse_rotate(stack_a, 'Q');
if (ft_lstsize((*stack_b)) > 1 && (!ft_strncmp("rrb", cmd, 3)
|| !ft_strncmp(cmd, "rrr", 3)))
reverse_rotate(stack_b, 'Q');
if (ft_lstsize((*stack_a)) > 1 && (!ft_strncmp("ra", cmd, 2)
|| !ft_strncmp(cmd, "rr", 2)) && ft_strlen(cmd) < 3)
rotate(stack_a, 'Q');
if (ft_lstsize((*stack_b)) > 1 && (!ft_strncmp("rb", cmd, 2)
|| !ft_strncmp(cmd, "rr", 2)) && ft_strlen(cmd) < 3 )
rotate(stack_b, 'Q');
}
static char **fill_array_cmd(void)
{
char **list;
list = malloc(sizeof(char *) * 12);
list[0] = ft_strdup("sa");
list[1] = ft_strdup("sb");
list[2] = ft_strdup("ss");
list[3] = ft_strdup("ra");
list[4] = ft_strdup("rb");
list[5] = ft_strdup("rr");
list[6] = ft_strdup("rra");
list[7] = ft_strdup("rrb");
list[8] = ft_strdup("rrr");
list[9] = ft_strdup("pa");
list[10] = ft_strdup("pb");
list[11] = NULL;
return (list);
}
static void check_comand(char **array_comand, char *cmd)
{
int i;
int flag;
i = 0;
flag = 0;
while (array_comand[i])
{
if (!ft_strncmp(array_comand[i], cmd, ft_strlen(cmd)))
flag = 1;
i++;
}
if (flag)
return ;
else
print_error();
}
static void set_arg(t_list **stack_a, t_list **stack_b)
{
char *line;
int end;
char **comand;
int j;
comand = fill_array_cmd();
line = 0;
end = 1;
j = 0;
while (end)
{
end = get_next_line (0, &line);
check_comand(comand, line);
exec_arg(stack_a, stack_b, line);
free (line);
}
if (check_a(*stack_a, ft_lstsize(*stack_a)) && !*stack_b)
printf("\nOK");
else
printf("\nKO");
while (comand[j])
free(comand[j++]);
free(comand);
}
int main (int argc, char *argv[])
{
t_list *stack_a;
t_list *stack_b;
char **split_argument;
int i;
int j;
i = 1;
while (i < argc)
{
j = 0;
split_argument = check_number_argv(argv[i]);
if (fill_stack(&stack_a, split_argument))
print_error();
while (split_argument[j])
free(split_argument[j++]);
i++;
}
free(split_argument);
if (argc > 1)
set_arg(&stack_a, &stack_b);
return (0);
}