-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_pfputnbr.c
More file actions
86 lines (78 loc) · 2.06 KB
/
ft_pfputnbr.c
File metadata and controls
86 lines (78 loc) · 2.06 KB
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_pfputnbr_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jzubizar <jzubizar@student.42urduliz.co +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/26 09:58:32 by novadecordi #+# #+# */
/* Updated: 2023/07/10 13:00:17 by jzubizar ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_numer_digits(int n)
{
int count;
count = 1;
if (n == -2147483648)
return (10);
else if (n < 0)
n *= -1;
while (n > 9)
{
n = n / 10;
count++;
}
return (count);
}
static void ft_itoa_min(int *n, char *nbr, int *i)
{
if (*n == -2147483648)
{
nbr[9] = '8';
*n = *n / 10;
*i -= 1;
}
}
int ft_precision_nul(t_flag *flags)
{
int i;
i = 0;
if (flags->plus && flags->justify)
i += write(1, "+", 1);
if (flags->space && flags->justify)
i += write(1, " ", 1);
i += ft_print_width(flags->width, flags->plus + flags->space, 0);
if (flags->plus && !flags->justify)
i += write(1, "+", 1);
if (flags->space && !flags->justify)
i += write(1, " ", 1);
return (i);
}
int ft_pfputnbr_fd(int n, int fd, t_flag flags)
{
char nbr[12];
int i;
int dig;
int nb;
dig = ft_numer_digits(n);
i = dig - 1;
nb = n;
nbr[i + 1] = '\0';
ft_itoa_min(&n, nbr, &i);
if (n < 0 && fd)
n *= -1;
while (n > 9 && fd)
{
nbr[i] = "0123456789"[n % 10];
n = n / 10;
i--;
}
nbr[i] = "0123456789"[n % 10];
if (flags.precision && flags.precwidth == 0 && n == 0)
{
i = ft_precision_nul(&flags);
return (i);
}
return (ft_print_nbr(nbr, flags, nb));
}