-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf_utils.c
106 lines (95 loc) · 2.38 KB
/
ft_printf_utils.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
96
97
98
99
100
101
102
103
104
105
106
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ebassi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/18 15:33:31 by ebassi #+# #+# */
/* Updated: 2022/02/01 18:07:20 by ebassi ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_return_res_x(unsigned int nbr, char *base, int index, int *len);
int non_valid_arg(char *base);
void ft_putnbr2(long int nb, t_params *my_struct)
{
if (nb < 0)
nb *= -1;
if (nb < 10)
{
ft_putchar(nb + 48);
return ;
}
else
ft_putnbr2(nb / 10, my_struct);
ft_putnbr2(nb % 10, my_struct);
return ;
}
char *ft_convert_base(unsigned long int nbr, char *base)
{
size_t index;
char *res;
unsigned long int nb;
int i;
index = ft_strlen(base);
nb = nbr;
res = (char *) malloc (index + 1);
i = 0;
while (nb >= (unsigned long int)index)
{
res[i] = base[nb % index];
nb /= index;
i++;
}
res[i] = base[nb % index];
i++;
res[i] = '\0';
return (res);
}
int ft_putnbr_base_x(unsigned int nbr, char *base)
{
int index;
int len[1];
len[0] = 0;
index = 0;
while (base[index] != '\0')
index++;
ft_return_res_x(nbr, base, index, len);
return (len[0]);
}
void ft_return_res_x(unsigned int nbr, char *base, int index, int *len)
{
unsigned int nb;
char str;
nb = 0;
if (nbr < 0)
{
write(1, "-", 1);
nb = nbr * (-1);
len[0] += 1;
}
else
nb = nbr;
if (nb >= (unsigned int)index)
ft_return_res_x(nb / index, base, index, len);
str = base[nb % index];
write(1, &str, 1);
len[0]++;
}
int non_valid_arg(char *base)
{
int index;
index = 0;
while (base[index] != '\0')
{
if (base[index] == '+' || base[index] == '-')
return (0);
else if (base[index] == base[index + 1])
return (0);
index++;
}
if (index <= 1)
return (0);
return (1);
}