-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_new_decimal.c
109 lines (99 loc) · 3.05 KB
/
ft_new_decimal.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
107
108
109
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_new_decimal.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mokhames <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/04 20:18:22 by mokhames #+# #+# */
/* Updated: 2019/12/14 22:28:03 by mokhames ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void ft_display_sign(int nb, t_struct *f)
{
if (nb < 0)
f->len += ft_putstr2("-");
}
static int ft_pad(t_struct *f, int size, int nb, int a)
{
int width;
if (f->width <= 0)
return (size);
width = 0;
if (f->width == 1 && f->precision == 0 && nb == 0 &&
!f->zero && !f->minus && f->precision_specified)
width = -1;
if (!f->minus && f->precision < f->width && f->precision > a && nb < 0
&& f->precision_specified && f->precision)
width++;
if (f->minus && f->precision > a && nb < 0 &&
f->precision_specified && f->precision)
width++;
while (width++ < f->width - size)
{
if ((f->zero && !f->minus))
f->len += ft_putstr2("0");
else
f->len += ft_putstr2(" ");
}
return (size + width - 1);
}
static int ft_display_d(t_struct *f, int size, int precision, int nb)
{
int width_size;
int width;
int a;
width = 0;
a = ft_num_of_digits(nb, 10);
width_size = 0;
if (!f->minus)
{
if (f->width && f->zero)
ft_display_sign(nb, f);
width_size = ft_pad(f, size, nb, a) - size;
}
if ((f->width && (f->minus || !f->zero)) || !f->width)
ft_display_sign(nb, f);
if (nb < 0)
width--;
while (width++ < precision)
f->len += ft_putstr2("0");
if (nb == 0 && f->precision == 0 && f->precision_specified)
ft_putstr2("");
else if (size > 0)
f->len += ft_itoa_base(nb, 10);
return (size + width_size);
}
static int type_d(int nb, t_struct *f)
{
int size;
int precision;
int a;
if (f->precision != 0)
f->zero = 0;
size = 0;
size = ft_num_of_digits(nb, 10);
a = ft_num_of_digits(nb, 10);
if (!nb && !f->precision && f->width > size &&
f->precision_specified && !f->zero && !f->minus)
f->width++;
precision = f->precision - size;
size = (f->precision > size) ? f->precision : size;
size = (f->precision == -1 && nb == 0) ? 0 : size;
size = ft_display_d(f, size, precision, nb);
size = (f->minus) ? ft_pad(f, size, nb, a) : size;
return (size);
}
void ft_print_decimal(t_struct *f, va_list ap)
{
long nbr;
if (f->nash == 1)
nbr = va_arg(ap, int);
if (f->op == 1)
nbr = va_arg(ap, int);
nbr = va_arg(ap, int);
if (f->minus && f->precision_specified && !f->precision && !nbr)
f->width++;
type_d(nbr, f);
}