-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflag1.c
74 lines (67 loc) · 2.25 KB
/
flag1.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* flag1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abello-r <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/21 02:00:41 by abello-r #+# #+# */
/* Updated: 2020/02/21 02:00:48 by abello-r ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
size_t ft_intlen(long nb)
{
size_t count;
count = 0;
if (nb == 0)
{
count++;
return (count);
}
if (nb < 0)
{
nb *= -1;
count++;
}
while (nb > 0)
{
nb = nb / 10;
count++;
}
return (count);
}
void ft_while(int space, int zero, t_printf *format, int nb)
{
int flag;
flag = (nb < 0 && (space > format->precision && format->zero_space == '0'))
? write(1, "-", 1) : 0;
while (space > 0 && format->tab != '-')
{
format->len_str += write(1, (format->zero_space != '0' ||
(format->dot == '.' && format->precision >= 0)) ? " " : "0", 1);
space--;
}
(nb < 0 && flag == 0) ? ft_putchar_fd('-', 1) : 0;
while (zero-- > 0)
format->len_str += write(1, "0", 1);
(format->dot == '.' && nb == 0 && format->precision == 0)
? 0 : ft_putnbr_fd(nb, 1);
while (space-- > 0)
format->len_str += write(1, " ", 1);
format->len_str -= (format->dot == '.' && nb == 0 &&
format->precision == 0) ? 1 : 0;
}
void ft_spacex(t_printf *format, int nb)
{
int len;
int space;
int zero;
format->len_str += len = ft_intlen(nb);
space = format->width - ((format->precision < len)
? len : format->precision);
space -= (nb < 0 && format->precision >= len) ? 1 : 0;
space += (format->dot == '.' && nb == 0 && format->precision == 0) ? 1 : 0;
zero = (nb < 0) ? (format->precision - len) + 1 : (format->precision - len);
ft_while(space, zero, format, nb);
}