-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathft_printf_fg.c
102 lines (95 loc) · 2.55 KB
/
ft_printf_fg.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_f_g.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apuchill <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/05/19 15:03:07 by apuchill #+# #+# */
/* Updated: 2020/05/30 22:12:06 by apuchill ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void print_spec_f_e_g(int *len, t_flags fl, double n)
{
union u_dbl dbl;
dbl.f = n;
fl.sign = (dbl.bits.sign > 0) ? '-' : '+';
fl.print_n0 = 1;
if (fl.point == 0)
fl.precision = 6;
if (fl.spe_c == 'f')
fl = print_spec_f(fl, n);
if (fl.spe_c == 'e')
fl = print_spec_e(fl, n);
if (fl.spe_c == 'g')
{
if (fl.point == 1 && fl.precision == 0)
fl = print_spec_g(fl, n, 1);
else
fl = print_spec_g(fl, n, fl.precision);
}
print_flags(len, fl);
if (fl.spe_c == 'e')
free(fl.d);
free(fl.a);
}
t_flags print_spec_f(t_flags fl, double n)
{
fl.f = (n >= 0) ? n : -n;
fl.ulli = fl.f;
fl.a = ft_ftoa_rnd(fl.f, fl.precision, 5);
if (fl.hash == 1 && fl.point == 1 && fl.precision == 0)
{
fl.tmp = ft_strjoin(fl.a, ".");
free(fl.a);
fl.a = fl.tmp;
}
return (fl);
}
static int rm_trailing_0s(int precision, char *str)
{
int i;
i = ft_strlen(str);
while (--i >= 0)
{
if (!(str[i] == '0' || str[i] == '.'))
break ;
if (str[i] == '.')
{
str[i] = '\0';
break ;
}
if (str[i] == '0')
{
precision--;
str[i] = '\0';
}
}
return (precision);
}
t_flags print_spec_g(t_flags fl, double n, int p)
{
fl = print_spec_e(fl, n);
free(fl.a);
free(fl.d);
if (p > fl.e_nbr && fl.e_nbr >= -4)
{
fl.spe_c = 'f';
fl.precision = p - (fl.e_nbr + 1);
fl = print_spec_f(fl, n);
if (fl.hash == 0 && ft_strchr_01(fl.a, '.') == 1)
fl.precision = rm_trailing_0s(fl.precision, fl.a);
}
else
{
fl.spe_c = 'e';
fl.precision = p - 1;
fl = print_spec_e(fl, n);
if (fl.hash == 0 && ft_strchr_01(fl.d, '.') == 1)
fl.precision = rm_trailing_0s(fl.precision, fl.d);
free(fl.a);
fl.a = ft_strjoin(fl.d, fl.e);
}
return (fl);
}