-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi_base.c
86 lines (78 loc) · 1.95 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yde-goes <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/28 02:11:33 by yde-goes #+# #+# */
/* Updated: 2022/08/29 20:24:24 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdio.h>
static int check_base(char *base);
static int convert_string(char *str, char *base);
int ft_atoi_base(char *str, char *base)
{
int signal;
signal = 1;
if (!check_base(base))
return (0);
while (*str == ' ' || (*str >= 9 && *str <= 13))
str++;
if (*str == '+' || *str == '-')
{
if (*str == '-')
signal *= -1;
str++;
}
if (*str == '0')
{
str++;
if (*str == 'x')
str++;
}
return (signal * convert_string(str, base));
}
static int check_base(char *base)
{
int i;
int j;
i = 0;
if (!base)
return (1);
if (ft_strlen(base) <= 1)
return (1);
while (base[i])
{
j = 0;
while (base[i + j])
{
if (base[i + j] == base[i])
return (1);
j++;
}
i++;
}
return (0);
}
static int convert_string(char *str, char *base)
{
int nbr_sys;
int nbr_conv;
int i;
int ch;
i = 0;
nbr_sys = ft_strlen(base);
nbr_conv = 0;
while (ft_strchr(base, ft_tolower(str[i])) && str[i])
{
ch = 0;
while (base[ch] != ft_tolower(str[i]) && base[ch])
ch++;
nbr_conv = (nbr_conv * nbr_sys) + ch;
i++;
}
return (nbr_conv);
}