-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf_unbr.c
74 lines (67 loc) · 1.87 KB
/
ft_printf_unbr.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_unbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mokhan <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/05 20:32:05 by mokhan #+# #+# */
/* Updated: 2023/10/06 13:38:51 by mokhan ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int print_nbr_right(char *nbr, int *flags)
{
int i;
int j;
i = 0;
j = 0;
while (i < flags[7])
i += write(1, " ", 1);
while (j < flags[6])
j += write(1, "0", 1);
if (flags[5] < 2)
ft_putstr_fd(nbr, 1);
free(nbr);
return (i + j);
}
static int print_nbr_left(char *nbr, int *flags)
{
int i;
int j;
i = 0;
j = 0;
while (j < flags[6])
j += write(1, "0", 1);
if (flags[5] < 2)
ft_putstr_fd(nbr, 1);
free(nbr);
while (i < flags[7])
i += write(1, " ", 1);
return (i + j);
}
int print_unsigned(unsigned int n, int *flags)
{
int len;
char *nbr;
nbr = ft_itoap(n);
len = ft_numlen(n, 10);
if (flags[5] && !flags[6] && !n)
flags[5] += len--;
if (flags[4] && !flags[5])
{
flags[6] = flags[7];
flags[7] = 0;
}
if (flags[6] > len)
flags[7] -= flags[6];
else
flags[7] -= len ;
if (flags[5])
flags[6] -= len;
else
flags[6] -= len ;
if (flags[3])
return (len + print_nbr_left(nbr, flags));
return (len + print_nbr_right(nbr, flags));
}