-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.c
129 lines (118 loc) · 2.52 KB
/
input.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* input.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dvan-der <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/15 08:15:16 by dvan-der #+# #+# */
/* Updated: 2022/01/28 15:11:49 by dvan-der ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int input_correct(char *str)
{
int i;
int j;
static int length;
length = ft_strlen(str);
i = 0;
j = 0;
while (str[i] == ' ' || (str[i] > 8 && str[i] < 14))
i++;
if (str[i] == '-')
i++;
while (ft_isdigit(str[i]))
{
i++;
j++;
}
if (i != length || !ft_atoi_help(str) || j == 0)
return (0);
return (1);
}
static int checker_checker(t_stack **a, int nbrs)
{
t_stack *tmp;
tmp = *a;
while (tmp->next)
{
if (tmp->d_nbr < tmp->next->d_nbr)
tmp = tmp->next;
else
break ;
}
if (!tmp->next)
{
free_stack(a);
return (1);
}
if (!letsgo(a, nbrs))
{
free_stack(a);
return (0);
}
return (1);
}
static int input_single(char *str, t_stack *a, t_stack *new)
{
char **split_str;
int counter;
int i;
split_str = ft_split(str, ' ');
counter = 0;
while (split_str[counter])
counter++;
i = 0;
while (i < counter)
{
if (!create_stack(split_str[i], &a, &new))
{
ft_free_split(split_str);
free_stack(&a);
return (0);
}
i++;
}
ft_free_split(split_str);
if (!checker_checker(&a, counter))
return (0);
return (1);
}
static int input_multiple(int argc, char *argv[], t_stack *a, t_stack *new)
{
int i;
i = 1;
while (i < argc)
{
if (!create_stack(argv[i], &a, &new))
{
free_stack(&a);
return (0);
}
i++;
}
if (!checker_checker(&a, argc - 1))
return (0);
return (1);
}
int input_handler(int argc, char *argv[])
{
t_stack *a;
t_stack *new;
a = NULL;
new = NULL;
if (argc == 1)
return (1);
else if (argc == 2)
{
if (!input_single(argv[1], a, new))
return (0);
}
else if (argc > 2)
{
if (!input_multiple(argc, argv, a, new))
return (0);
}
return (1);
}