-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.c
116 lines (106 loc) · 2.24 KB
/
utilities.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
109
110
111
112
113
114
115
116
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utilities.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: iel-moha <iel-moha@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/09 12:34:42 by iel-moha #+# #+# */
/* Updated: 2022/07/19 01:05:31 by iel-moha ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int ft_atoi(const char *str)
{
unsigned long r;
int i;
int o;
i = 0;
r = 0;
o = 1;
while (str[i] == 32 || (str[i] >= 9 && str[i] <= 13))
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
o = -1;
i++;
}
while (str[i] && str[i] >= '0' && str[i] <= '9')
{
r = r * 10 + (str[i] - '0');
i++;
}
if (r >= 2147483649 && o == -1)
error_handling();
else if (r >= 2147483648 && o == 1)
error_handling();
return (o * r);
}
void error_handling(void)
{
ft_putstr("Error\n");
exit(2);
}
int ft_putstr(char *str)
{
int i;
i = 0;
if (!str)
{
return (ft_putstr("(null)"));
}
while (str[i] != '\0')
{
write(1, &str[i], 1);
i++;
}
return (i);
}
int check_dups(char **av)
{
int i;
int j;
i = 1;
while (av[i + 1])
{
j = i + 1;
while (av[j])
{
if (ft_atoi(av[i]) == ft_atoi(av[j]))
{
return (0);
}
j++;
}
i++;
}
return (1);
}
int is_digit(char **av)
{
int i[2];
i[0] = 0;
while (av[++i[0]])
{
if (av[i[0]][0] == '\0')
return (0);
i[1] = 0;
while (av[i[0]][i[1]] == ' ')
i[1]++;
while (av[i[0]][i[1]])
{
if (av[i[0]][i[1]] == '-' || av[i[0]][i[1]] == '+')
{
i[1]++;
if (av[i[0]][i[1]] == '-' || av[i[0]][i[1]] == '+')
return (0);
}
else if ((av[i[0]][i[1]] > 47 && av[i[0]][i[1]] < 58))
i[1]++;
else
return (0);
}
}
return (1);
}