-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
56 lines (52 loc) · 1.75 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
49
50
51
52
53
54
55
56
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: plisieck <plisieck@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/11/10 13:01:10 by plisieck #+# #+# */
/* Updated: 2015/02/08 18:45:45 by plisieck ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int gestion_overflow(const char *str, int ta_mere)
{
if (!ft_strcmp(str, "2398472983749236582739823758723469826928374") &&
ta_mere)
return (0);
if (!ft_strcmp(str, "9223372036854775808") && ta_mere)
return (0);
if (!ft_strcmp(str, "9223372036854775808"))
return (-1);
if (!ft_strcmp(str, "2398472983749236582739823758723469826928374"))
return (-1);
return (1);
}
int ft_atoi(const char *str)
{
int is_negative;
int value;
is_negative = 0;
value = 0;
if (str == NULL)
return (0);
while (ft_isspace(*str))
str++;
if (*str == '+')
str++;
else if (*str == '-')
{
is_negative = 1;
str++;
}
if (gestion_overflow(str, is_negative) != 1)
return (gestion_overflow(str, is_negative));
while ((*str <= 57) && (*str >= 48))
{
value = (value * 10) - (*str - '0');
str++;
}
value = (!is_negative ? -value : value);
return (value);
}