-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs.c
89 lines (80 loc) · 1.92 KB
/
args.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* args.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: joalmeid <joalmeid@student.42.rio> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/28 14:42:24 by joalmeid #+# #+# */
/* Updated: 2023/03/10 08:25:00 by joalmeid ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void free_trimmed_argv(char **trimmed_argv)
{
int i;
i = 0;
while (trimmed_argv[i])
{
free(trimmed_argv[i]);
i ++;
}
free(trimmed_argv);
trimmed_argv = NULL;
}
char **remove_space_argv(char **argv)
{
int i;
char *tmp;
char *acc;
char **result;
i = 1;
acc = malloc(sizeof(char) * ft_strlen(argv[i]));
while (argv[i])
{
tmp = ft_strjoin(argv[i], " ");
acc = ft_strjoin_special(&acc, tmp);
free(tmp);
i ++;
}
result = ft_split(acc, ' ');
free(acc);
return (result);
}
int is_arg_number(char *str)
{
int i;
i = 0;
while (str[i])
{
if (i == 0 && (str[i] == '-' || str[i] == '+'))
i ++;
if (!ft_isdigit(str[i]))
return (0);
i ++;
}
return (1);
}
int validate_args(char **argv)
{
int i;
int j;
i = 0;
j = 0;
while (argv[i])
{
if (!is_arg_number(argv[i]) \
|| ((ft_atoi(argv[i]) > INT_MAX) \
|| (ft_atoi(argv[i]) < INT_MIN)))
return (0);
j = i - 1;
while (i >= 1 && j >= 0)
{
if (ft_atoi(argv[i]) == ft_atoi(argv[j]))
return (0);
j --;
}
i ++;
}
return (1);
}