-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_d.c
138 lines (130 loc) · 4.19 KB
/
type_d.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* type_d.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nahmed-m <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/01/25 11:42:26 by nahmed-m #+# #+# */
/* Updated: 2016/02/01 15:57:05 by nahmed-m ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void ft_putstr_left(t_var *e, long value)
{
if (value < 0)
e->ret++;
else if (e->f_positive == 1)
ft_putchar_ret('+', e);
else if (e->f_positive == 0 && e->f_space == 1)
ft_putchar_ret(' ', e);
if ((e->f_positive == 1 || e->f_space == 1) && e->f_precis != 1)
e->f_precis++;
if (value < 0 && e->f_precis != 1)
{
value *= -1;
ft_putchar('-');
e->f_precis++;
}
if (e->f_precis != 1 && e->f_width < e->f_precis)
ft_put_zero(e->f_precis - e->t_size, e);
else if (e->f_precis != 1 && e->f_width > e->f_precis)
ft_put_zero(e->f_precis - e->t_size, e);
ft_putnbr(value);
if (e->f_left == 1 && e->f_width != 0 && e->f_width > e->t_size && \
e->f_precis == 1)
ft_put_space(e->f_width - e->t_size, e);
else if (e->f_left == 1 && e->f_width != 0 && e->f_width > e->t_size\
&& e->f_precis != 1)
ft_put_space(e->f_width - e->f_precis, e);
}
static void ft_putstr_right_next(t_var *e, long value)
{
if ((e->f_zero == 1 || e->f_precis != 1) && value < 0)
{
ft_putchar_ret('-', e);
if (e->f_precis != 1 && e->f_precis >= e->t_size && value < 0)
{
ft_put_zero(e->f_precis - e->t_size + 1, e);
e->u_exep = 1;
}
value *= -1;
}
if (e->f_zero == 1 && e->f_precis == 1 && e->f_space == 1)
ft_put_zero(e->f_width - e->t_size - 1, e);
else if (e->f_zero == 1 && e->f_precis == 1)
ft_put_zero(e->f_width - e->t_size, e);
else if (e->f_precis != 1 && e->f_precis > e->t_size && value >= 0 && \
e->u_exep != 1)
ft_put_zero(e->f_precis - e->t_size, e);
if (e->f_positive == 0 && e->f_space == 1 && e->f_precis == 0)
ft_putchar_ret(' ', e);
if (value < 0)
e->ret++;
ft_putnbr(value);
}
static void ft_putstr_right(t_var *e, long value)
{
if (e->f_zero == 1 && e->f_space == 1 && e->f_positive == 0 && value == 0)
ft_put_space(1, e);
else if ((e->f_zero == 0 && e->f_precis == 1) || (e->f_precis != 1 &&
e->f_width > e->f_precis && e->f_precis < e->t_size))
ft_put_space(e->f_width - e->t_size, e);
else if (e->f_precis != 1 && e->f_width > e->f_precis && e->f_precis >\
e->t_size)
ft_put_space(e->f_width - e->f_precis - (value < 0 ? 1 : 0), e);
else if (e->f_precis != 1 && e->f_width > e->f_precis && e->f_precis ==\
e->t_size)
ft_put_space(e->f_width - e->f_precis - 1, e);
if (e->f_positive == 1)
ft_putchar_ret('+', e);
if (e->f_positive == 1 && e->f_precis != 1)
e->f_precis++;
ft_putstr_right_next(e, value);
}
static int len_d(long value, t_var *e)
{
int i;
i = 0;
if (value < 0 && e->f_positive == 1)
e->f_positive = 0;
if (value == 0)
{
e->ret++;
return (1);
}
while (value != 0)
{
i++;
value /= 10;
}
e->ret += i;
return (i);
}
void type_d(t_var *e)
{
long value;
if (e->f_hh == 1 && e->u_exep == 0)
value = (char)va_arg(e->ap, int);
else if (e->f_h == 0 && e->f_hh == 0 && e->f_ll == 0 && e->f_l == 0 \
&& e->f_j == 0 && e->f_z == 0 && e->u_exep == 0)
value = va_arg(e->ap, int);
else
value = va_arg(e->ap, long);
if (e->f_precis == 0 && value == 0)
{
ft_put_space(e->f_width, e);
return ;
}
value = ft_verif_exep(value, e);
e->t_size = len_d(value, e);
if (e->error == 1)
return ;
if (value < 0 || e->f_positive == 1 || (e->f_space == 1 && \
e->f_positive == 1))
e->t_size++;
if (e->f_left == 0 && e->f_width != 0 && e->f_width > e->t_size)
ft_putstr_right(e, value);
else
ft_putstr_left(e, value);
}