-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi_base.c
95 lines (87 loc) · 2.16 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
90
91
92
93
94
95
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ohachim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/31 23:55:26 by ohachim #+# #+# */
/* Updated: 2019/04/02 16:31:49 by ohachim ### ########.fr */
/* */
/* ************************************************************************** */
static int ft_getblen(char *base)
{
int blen;
int blen_dup;
blen = 0;
while (base[blen] != '\0')
{
if (base[blen] == '+' || base[blen] == '-')
return (0);
blen_dup = blen + 1;
while (base[blen_dup])
{
if (base[blen_dup] == base[blen])
return (0);
blen_dup++;
}
blen++;
}
if (blen == 1)
return (0);
return (blen);
}
static int ft_error(char *str, char *base)
{
int cn;
int cnb;
cn = 0;
while (*str == ' ' || *str == '\t')
str++;
if (str[cn] == '+' || str[cn] == '-')
cn++;
while (str[cn] != '\0')
{
if (str[cn] == '+' || str[cn] == '-')
return (0);
cnb = 0;
while (base[cnb] != str[cn] && base[cnb] != '\0')
cnb++;
if (base[cnb] == '\0')
return (0);
cn++;
}
if (cn < 1)
return (0);
return (1);
}
static int ft_getfbase(char c, char *base)
{
int cn;
cn = 0;
while (base[cn] != c)
cn++;
return (cn);
}
int ft_atoi_base(char *str, char *base)
{
int b_len;
int cn;
int sign;
int ret;
sign = 1;
cn = 0;
ret = 0;
if (!str || !base || !(b_len = ft_getblen(base)) || !ft_error(str, base))
return (0);
if (str[cn] == '-')
sign = -1;
while (*str == ' ' || *str == '\t' || *str == '+' || *str == '-')
str++;
while (str[cn] != '\0')
{
ret = (ret * b_len) + ft_getfbase(str[cn], base);
cn++;
}
return (ret * sign);
}