-
Notifications
You must be signed in to change notification settings - Fork 0
/
argv_check.c
127 lines (117 loc) · 2.57 KB
/
argv_check.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* argv_check.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: joandre- <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/25 00:04:29 by joandre- #+# #+# */
/* Updated: 2024/04/20 01:33:19 by joandre- ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
bool need_parse(char *s)
{
unsigned int i;
i = 0;
if (s == NULL)
return (false);
while (s[i])
{
if (s[i++] == ' ')
return (true);
}
return (false);
}
bool check_parse(char *s)
{
unsigned int i;
char **splits;
bool result;
result = true;
splits = ft_split(s, ' ');
if (!splits)
return (false);
i = 0;
while (result && splits[i])
{
result = int_check(ft_atol(splits[i]));
free(splits[i++]);
}
while (splits[i])
free(splits[i++]);
free(splits);
return (result);
}
void parse_2stack(t_stack **a, char *s)
{
t_stack *new;
unsigned int i;
char **splits;
i = 0;
splits = ft_split(s, ' ');
if (!splits)
return ;
if (*a == NULL)
{
*a = create_node(ft_atoi(splits[i]));
free(splits[i++]);
}
while (splits[i])
{
new = create_node(ft_atoi(splits[i]));
if (new)
{
new->prev = last_node(*a);
last_node(*a)->next = new;
}
free(splits[i++]);
}
free(splits);
}
static bool check_string(int argc, char **argv)
{
int c;
unsigned int i;
c = 1;
i = 0;
while (c < argc)
{
if (argv[c][0] == '\0')
return (false);
if (argv[c][i] == '-' || argv[c][i] == '+')
if (argv[c][++i] == '\0' || !ft_isdigit(argv[c][i]))
return (false);
while (ft_isdigit(argv[c][i]))
++i;
if (argv[c][i++] == ' ')
continue ;
if (argv[c++][--i])
return (false);
i = 0;
}
return (true);
}
bool argv_check(int argc, char **argv)
{
int i;
bool result;
i = 1;
result = true;
if (check_string(argc, argv))
{
while (argc > i && result)
{
if (need_parse(argv[i]))
{
if (!check_parse(argv[i++]))
return (false);
}
else
result = int_check(ft_atol(argv[i++]));
}
}
else
return (false);
return (result);
}