-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
48 lines (44 loc) · 1.46 KB
/
ft_atoi.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: okhiar <okhiar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/05 17:06:11 by okhiar #+# #+# */
/* Updated: 2022/12/13 23:16:45 by okhiar ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int ft_isspace(char c)
{
if ((c >= 9 && c <= 13) || c == 32)
return (1);
return (0);
}
int ft_atoi(const char *str)
{
int i;
int sign;
int result;
long tmp;
i = 0;
sign = 1;
tmp = 0;
while (ft_isspace(str[i]))
i++;
if (str[i] == '-' || str[i] == '+')
sign = 1 - 2 * (str[i++] == '-');
while (str[i] && str[i] >= 48 && str[i] <= 57)
{
tmp *= 10;
tmp += str[i] - 48;
if (tmp > __INT_MAX && sign > 0)
_ft_putstr_fd("ERORR\n", 2);
else if (tmp > __INT_MIN)
_ft_putstr_fd("ERORR\n", 2);
result = tmp;
i++;
}
return (result * sign);
}