-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_int_conv.c
71 lines (65 loc) · 2.26 KB
/
type_int_conv.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* type_int_conv.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: achane-l <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/07/14 12:06:59 by achane-l #+# #+# */
/* Updated: 2021/07/14 12:07:05 by achane-l ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
char *ft_itoa_with_prec(int param, t_flags *flags)
{
char *asc_conv;
long long nbr;
asc_conv = ft_strdup("");
if (asc_conv == NULL)
return (NULL);
nbr = (long long)param;
if (nbr < 0)
nbr = -nbr;
while (nbr > 9)
{
append_char(&asc_conv, (nbr % 10) + '0');
nbr /= 10;
}
if (flags->precision != 0 || nbr != 0)
append_char(&asc_conv, (nbr % 10) + '0');
while (flags->precision > ft_strlen(asc_conv))
append_char(&asc_conv, '0');
treat_flags_sign_int(&asc_conv, flags, param);
return (asc_conv);
}
void treat_flags_sign_int(char **asc_conv, t_flags *flags, int param)
{
while (flags->width > ft_strlen(*asc_conv) + 1 && flags->zero > 0)
append_char(asc_conv, '0');
if (flags->space > 0 && flags->number_sign == 0 && param >= 0)
append_char(asc_conv, ' ');
else if (flags->number_sign > 0 && param >= 0)
append_char(asc_conv, '+');
else if (param < 0)
append_char(asc_conv, '-');
}
char *ft_uitoa_with_prec(unsigned long param, t_flags *flags)
{
char *asc_conv;
unsigned int nbr;
asc_conv = ft_strdup("");
if (asc_conv == NULL)
return (NULL);
nbr = (unsigned int)param;
if (nbr == 0 && flags->precision == 0)
return (asc_conv);
while (nbr > 9)
{
append_char(&asc_conv, (nbr % 10) + '0');
nbr /= 10;
}
append_char(&asc_conv, (nbr % 10) + '0');
while (flags->precision > ft_strlen(asc_conv))
append_char(&asc_conv, '0');
return (asc_conv);
}