-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush_swap.c
109 lines (101 loc) · 2.63 KB
/
push_swap.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttavares <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/19 15:18:35 by ttavares #+# #+# */
/* Updated: 2023/03/23 16:25:44 by ttavares ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void arg_size_filter(t_stack **head_a, t_stack **head_b)
{
if (stacklen(*head_a) == 2)
sort_two(head_a);
else if (stacklen(*head_a) == 3)
sort_three(head_a, stacklen(*head_a), 'a');
else if (stacklen(*head_a) == 4 || stacklen(*head_a) == 5)
sort_small(head_a, head_b, stacklen(*head_a));
else if (stacklen(*head_a) > 5 && stacklen(*head_a) <= 200)
sort_large(head_a, head_b, stacklen(*head_a), 5);
else
sort_large(head_a, head_b, stacklen(*head_a), 15);
}
int one_argument_check(char **str, t_stack **head_a, t_stack **head_b)
{
char **argv;
argv = ft_split(str[1], ' ');
if (!check_range(argv))
{
argv_free(argv);
return (0);
}
if (!check_char(argv))
{
argv_free(argv);
return (0);
}
if (!check_doubles(argv))
{
argv_free(argv);
return (0);
}
add_stack(argv, head_a);
arg_size_filter(head_a, head_b);
argv_free(argv);
return (1);
}
int argument_check(t_stack **head_a, t_stack **head_b, char **argv)
{
argv++;
if (!check_range(argv))
return (0);
if (!check_char(argv))
return (0);
if (!check_doubles(argv))
return (0);
add_stack(argv, head_a);
arg_size_filter(head_a, head_b);
return (1);
}
int arg_sorter(int argc, char **argv, t_stack **head_a, t_stack **head_b)
{
if (argc == 2)
{
if (!(one_argument_check(argv, head_a, head_b)))
{
clear(head_a, head_b);
return (0);
}
}
else
{
if (!(argument_check(head_a, head_b, argv)))
{
clear(head_a, head_b);
return (0);
}
}
clear(head_a, head_b);
return (1);
}
int main(int argc, char **argv)
{
t_stack *head_a;
t_stack *head_b;
head_a = NULL;
head_b = NULL;
if (argc == 1)
{
clear(&head_a, &head_b);
return (0);
}
else if (!(arg_sorter(argc, argv, &head_a, &head_b)))
{
clear(&head_a, &head_b);
return (0);
}
return (0);
}