-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi_base.c
89 lines (80 loc) · 2.03 KB
/
ft_atoi_base.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: knzeng-e <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/10/30 05:28:52 by knzeng-e #+# #+# */
/* Updated: 2016/03/26 11:55:24 by knzeng-e ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_get_size(char *base)
{
if (*base == '\0')
return (0);
return (1 + ft_get_size(++base));
}
static int ft_get_sign(char *nbr)
{
return (-1 + 2 * (nbr[0] != '-'));
}
static int ft_get_pos(char *base, char digit)
{
int pos;
pos = 0;
while (*base)
{
if (*base++ == digit)
return (pos);
pos++;
}
if (digit == '+' || digit == '-')
return (-2);
return (-1);
}
static int ft_check_base(char *base)
{
int current;
if (ft_get_size(base) <= 1)
return (0);
while (*base)
{
current = 1;
while (base[current] != '\0')
{
if (*base == base[current] || (base[current] == '-' \
|| base[current] == '+'))
return (0);
current++;
}
base++;
}
return (1);
}
int ft_atoi_base(char *str, char *base)
{
int output;
int sign;
int pos;
int last_pos;
sign = ft_get_sign(str);
if (!ft_check_base(base))
return (0);
if (*str == '-' || *str == '+')
str++;
output = 0;
last_pos = 0;
while (*str)
{
pos = ft_get_pos(base, *str++);
if (pos == -2)
return (last_pos);
if (pos == -1)
return (0);
last_pos = pos;
output = output * (ft_get_size(base)) + (pos);
}
return (sign * output);
}