-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi_modified.c
44 lines (40 loc) · 1.53 KB
/
ft_atoi_modified.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vsosevic <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 15:02:30 by vsosevic #+# #+# */
/* Updated: 2016/12/01 15:20:44 by vsosevic ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void has_alpha(const char *str, int i)
{
while (str[i] != '\0')
if (!ft_isdigit(str[i++]))
ft_exit("\x1B[31mError\n\x1B[33mNot a number in the list\n");
}
int ft_atoi_modified(const char *str)
{
int i;
long long int n;
int flag;
i = 0;
n = 0;
flag = 1;
if (str[i] == '+' && (str[i + 1] >= '0' && str[i + 1] <= '9'))
i++;
if (str[i] == '-' && (str[i + 1] >= '0' && str[i + 1] <= '9'))
{
flag = -1;
i++;
}
has_alpha(str, i);
while (str[i] >= '0' && str[i] <= '9')
n = n * 10 + (str[i++] - '0');
if (n < -2147483648 || n > 217483647)
ft_exit("\x1B[31mError\n\x1B[33mOut of int range\n");
return (n * flag);
}