-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_long.c
More file actions
111 lines (105 loc) · 3.04 KB
/
print_long.c
File metadata and controls
111 lines (105 loc) · 3.04 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_long.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tyassine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/04/07 10:16:25 by tyassine #+# #+# */
/* Updated: 2016/04/07 10:16:28 by tyassine ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void do_norm2(t_form *info, t_data *d, int ret, int *newret)
{
if (d->neg && !info->zero)
ft_putchar(d->string[0]);
if (d->string[0] != '-' && (info->plus || info->space))
{
ft_putchar(info->plus ? '+' : ' ');
*newret += 1;
}
while (info->prec - (ret - d->neg) > 0)
{
*newret += 1;
ft_putchar('0');
info->prec--;
}
if (d->string[d->neg] == '0' && d->bck == 0)
(info->width <= 0 ? *newret -= 1 : ft_putchar(' '));
else
ft_putstr(d->string + d->neg);
}
static void do_norm(t_form *info, t_data *d, int ret, int *newret)
{
if (info->left == 0)
{
if (d->string[0] != '-' && (info->plus || info->space) && info->zero)
{
ft_putchar(info->plus ? '+' : ' ');
*newret += 1;
info->width--;
info->space = 0;
info->plus = 0;
}
else if (d->string[0] != '-' && (info->plus || info->space))
info->width--;
if (d->neg && info->zero == 1)
ft_putchar(d->string[0]);
while (info->width - (info->prec - (ret - d->neg)) - ret > 0)
{
*newret += 1;
ft_putchar(d->ospace);
info->width--;
}
do_norm2(info, d, ret, newret);
}
}
static void do_left(t_form *info, t_data *d, int ret, int *newret)
{
if (d->string[0] == '-' && info->zero == 1)
ft_putchar(d->string[0]);
else if (d->string[0] != '-' && (info->plus || info->space))
{
ft_putchar(info->plus ? '+' : ' ');
*newret += 1;
info->width--;
}
while (info->prec - (ret) > 0)
{
ft_putchar('0');
if (info->width > 0)
info->width--;
if (info->prec > 0)
info->prec--;
*newret += 1;
}
if (d->string[d->neg] == '0' && d->bck == 0)
*newret -= 1;
else
ft_putstr(d->string + d->neg);
}
int print_long(t_form *info, t_data *d, int ret)
{
int newret;
d->bck = info->prec;
d->neg = d->string[0] == '-' && (info->zero == 1 || info->prec > 0) ? 1 : 0;
d->ospace = info->zero == 1 && info->prec < 0 ? '0' : ' ';
newret = 0;
if (info->prec == -1 || info->prec < ret - d->neg)
info->prec = ret - d->neg;
if (info->width == -1)
info->width = 0;
do_norm(info, d, ret, &newret);
if (info->left)
{
do_left(info, d, ret, &newret);
while (info->width - ret > 0)
{
ft_putchar(' ');
info->width--;
newret++;
}
}
return (ret + newret);
}