-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_args.c
105 lines (95 loc) · 2.79 KB
/
handle_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_args.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: meghribe <meghribe@student.42barcelona.co +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/11 22:07:42 by meghribe #+# #+# */
/* Updated: 2024/11/15 13:38:17 by meghribe ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
/* Handles the error message for handle_args function */
int handle_error(t_stack **stack_a)
{
ft_putstr_fd("Error\n", 2);
ft_free_stack_or_args(stack_a, NULL, FREE_STACK);
exit(EXIT_FAILURE);
}
/* Process the arguments and individual numbers */
int process_args(char **args, t_stack **stack_a)
{
int j;
int num;
check_if_repeated(args);
j = 0;
while (args[j])
{
if (!ps_atoi(args[j], &num))
return (ft_free_stack_or_args(stack_a, args, FREE_ARGS), 0);
if (!push_to_stack(stack_a, num))
return (ft_free_stack_or_args(stack_a, args, FREE_ARGS), 0);
j++;
}
return (1);
}
/*
* Checks the arguments and processes them
* Checks if the arguments are only spaces and checks
* Returns 1 if the arguments were processed successfully
* Returns 0 if the arguments were not processed successfully
*/
int handle_args(int argc, char *argv[], t_stack **stack_a)
{
int i;
char **args;
i = 1;
while (i < argc)
{
if (!is_only_spaces(argv[i]))
return (handle_error(stack_a));
args = ft_split(argv[i], ' ');
if (!process_args(args, stack_a))
return (0);
ft_free_stack_or_args(stack_a, args, FREE_ARGS);
i++;
}
return (1);
}
/*
* Checks if the string is only composed of spaces
* Returns 0 if the string is only composed of spaces
* Returns 1 if the string is not only composed of spaces
*/
int is_only_spaces(char *str)
{
int i;
if (!str || ft_strlen(str) == 0)
return (0);
i = 0;
while (str[i])
{
if (!ft_ischar(str[i], SPACE))
return (1);
i++;
}
return (0);
}
/*
* This function pushes a value to the stack
* Returns 1 if the value was pushed successfully
* Returns 0 if the value was not pushed successfully
*/
int push_to_stack(t_stack **stack, int value)
{
t_stack *new_node;
new_node = malloc(sizeof(t_stack));
if (!new_node)
return (0);
new_node->value = value;
new_node->index = 0;
new_node->next = NULL;
ps_lstadd(stack, new_node, BACK);
return (1);
}