-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush_swap_utils.c
85 lines (77 loc) · 1.94 KB
/
push_swap_utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tkang <tkang@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/30 15:13:44 by tkang #+# #+# */
/* Updated: 2022/06/30 15:13:45 by tkang ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int my_atoi(const char *str)
{
size_t i;
long result;
long op;
i = 0;
result = 0;
op = 1;
while ((9 <= str[i] && str[i] <= 13) || str[i] == ' ')
i++;
if (str[i] == '-')
op = -1;
if (str[i] == '-' || str[i] == '+')
i++;
while ('0' <= str[i] && str[i] <= '9')
{
result = (result * 10) + (str[i] - '0');
i++;
}
if (str[i])
exit_trap(1);
if ((result > 2147483647 && op == 1) || (result > 2147483648 && op == -1))
exit_trap(1);
return (result * op);
}
void two_d_free(char **str)
{
size_t i;
i = 0;
while (str[i])
{
free(str[i]);
i++;
}
free(str);
}
void init_doubly(t_DoublyList **dList, char *argv[], int argc)
{
int i;
int j;
char **temp;
*dList = create_doubly_list();
i = 0;
while (++i < argc)
{
temp = ft_split(argv[i], ' ');
j = 0;
if (!temp[j])
exit_trap(1);
while (temp[j])
{
if (!add_right_dl_element(*dList, my_atoi(temp[j])))
exit_trap(0);
j++;
}
two_d_free(temp);
}
(*dList)->size = (*dList)->cnt;
}
void exit_trap(int flag)
{
if (flag)
write(2, "Error\n", 6);
exit(1);
}