-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsing.c
95 lines (86 loc) · 2.43 KB
/
parsing.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: brulutaj <brulutaj@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 17:05:42 by brulutaj #+# #+# */
/* Updated: 2024/04/30 19:59:10 by brulutaj ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_dll *get_list_head(char **mtx)
{
t_dll *list;
t_dll *head;
int i;
long nl;
head = NULL;
i = 0;
while (mtx && mtx[i])
{
nl = ft_atol(mtx[i]);
if (nl > INT_MAX || nl < INT_MIN)
return (dll_clear(&head), NULL);
list = dll_new(nl);
if (!list)
return (dll_clear(&head), NULL);
list->index = i;
dll_add_back(&head, list);
i++;
}
return (free_mtx(mtx), head);
}
t_dll *parse_input_string(int ac, char *av)
{
t_dll *head;
char **mtx;
(void)ac;
head = (t_dll *){0};
mtx = ft_split(av, ' ');
if (!mtx || !(*mtx) || !mtx_check(mtx))
return (free_mtx(mtx), ft_printf("Error\n"), NULL);
head = get_list_head(mtx);
if (!head)
return (free_mtx(mtx), dll_clear(&head), ft_printf("Error\n"), NULL);
return (head);
}
t_dll *parse_input_args(int ac, char **av)
{
t_dll *head;
char **mtx;
int i;
(void)ac;
head = (t_dll *){0};
mtx = NULL;
i = 0;
if (!mtx_check(av))
return (ft_printf("Error\n"), NULL);
mtx = ft_calloc(ac, sizeof(char *));
if (!mtx)
return (free_mtx(mtx), NULL);
while (av[i])
{
mtx[i] = ft_strdup(av[i]);
i++;
}
head = get_list_head(mtx);
if (!head)
return (dll_clear(&head), free_mtx(mtx), ft_printf("Error\n"), NULL);
return (head);
}
t_dll *parse_input(int ac, char **av)
{
t_dll *stack_a;
stack_a = NULL;
if (ac == 1 || !av[1] || !av[1][0])
return (NULL);
if (ac == 2)
stack_a = parse_input_string(ac, av[1]);
else if (ac > 2)
stack_a = parse_input_args(ac, &av[1]);
if ((stack_a && is_sorted(stack_a)) || !stack_a)
return (dll_clear(&stack_a), NULL);
return (stack_a);
}