-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_z_check_argv.c
108 lines (99 loc) · 2.42 KB
/
ft_z_check_argv.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_z_check_argv.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: flormich <flormich@student.42wolfsburg.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/07/22 20:37:46 by flormich #+# #+# */
/* Updated: 2021/08/02 11:39:17 by flormich ### ########.fr */
/* */
/* ************************************************************************** */
#include"push_swap.h"
// Fill the stack & Check if there are double
static int ft_z_fill_stack_a(t_stack *stack, int position, int nb)
{
int i;
i = 0;
while (i < position)
{
if (stack->elt[i] == nb)
{
free(stack);
return (0);
}
else
i++;
}
stack->elt[position] = nb;
stack->size = position + 1;
return (1);
}
// Extract number von argv and check if they are all digits
static int ft_z_extract_number(char *argv, int *nb)
{
int j;
int sign;
j = 0;
*nb = 0;
sign = 1;
if (argv[j] == '+' || argv[j] == '-')
{
if (argv[j] == '-')
sign = -1;
j++;
}
while (argv[j] != '\0')
{
if (ft_isdigit(argv[j]) == 1)
{
*nb = *nb * 10 + (argv[j] - 48);
j++;
}
else
return (0);
}
j++;
*nb = *nb * sign;
return (1);
}
// Pilote: screen argv, create first Chunk, fill stack_a
static t_stack *ft_z_check_argv(int argc, char **argv)
{
int i;
int nb;
t_stack *stack;
stack = ft_calloc(1, sizeof(t_stack));
stack->elt = ft_calloc(argc, sizeof(int));
if (!stack)
return (NULL);
i = 1;
while (i < argc)
{
if (ft_z_extract_number(argv[i], &nb) == 0 || nb < -32768 || nb > 32767)
{
free(stack);
return (NULL);
}
else
{
if (ft_z_fill_stack_a(stack, i - 1, nb) == 0)
return (NULL);
i++;
}
}
return (stack);
}
// If arguments are OK return stack_a
// Evtl. exit if error or already sorted
t_stack *ft_z_import_argv(int argc, char **argv)
{
t_stack *stack_a;
stack_a = ft_z_check_argv(argc, argv);
if (!stack_a || !stack_a->elt)
{
write(1, "Error\n", 6);
return (NULL);
}
return (stack_a);
}