/
put_u.c
124 lines (114 loc) · 3.61 KB
/
put_u.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* put_u.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: osalmine <osalmine@student.hive.fi> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/20 16:44:42 by osalmine #+# #+# */
/* Updated: 2020/01/24 11:28:21 by osalmine ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static size_t get_number(t_printf *pf)
{
size_t i;
if (pf->length[0] == TRUE)
i = (unsigned short)va_arg(pf->lst, int);
else if (pf->length[1] == TRUE)
i = (unsigned char)va_arg(pf->lst, int);
else if (pf->length[2] == TRUE)
i = (unsigned long)va_arg(pf->lst, unsigned long int);
else if (pf->length[3] == TRUE)
i = (unsigned long long)va_arg(pf->lst, unsigned long long int);
else if (pf->length[5] == TRUE)
i = (size_t)va_arg(pf->lst, size_t);
else
i = (unsigned int)va_arg(pf->lst, int);
i = (size_t)i;
return (i);
}
static void ft_width_nb(t_printf *pf, long long i)
{
if (i >= 0)
pf->width -= ft_nb_len(i, 10);
else
pf->width -= ft_nb_len(i, 10) + 1;
if (pf->flag[2] || pf->flag[4])
pf->width -= 1;
if ((pf->precision - ft_nb_len(i, 10)) > 0)
pf->width -= (pf->precision - ft_nb_len(i, 10));
if (i < 0 && pf->flag[0] == FALSE && (pf->flag[2] == TRUE || pf->flag[1]))
pf->width += 1;
if (i < 0 && pf->flag[0] == TRUE && pf->flag[1] == TRUE)
pf->width += 1;
if (i < 0 && pf->flag[2] == TRUE && pf->flag[0] == TRUE)
pf->width += 1;
}
static void front_padding_nb(t_printf *pf, long long i, char *str)
{
int len;
len = (int)ft_strlen(str);
if (i < 0)
len -= 1;
if (pf->width > 0 && pf->flag[0] == FALSE)
{
if (pf->flag[3] == TRUE && (pf->precision - len) < 0 \
&& pf->precision != -3 \
&& (pf->precision > pf->width || pf->precision <= -1))
{
put_spacing(pf, i);
while (pf->width--)
pf->len += ft_len_putchar('0', pf->fd);
}
else
{
while (pf->width--)
pf->len += ft_len_putchar(' ', pf->fd);
put_spacing(pf, i);
}
}
}
static int nb_start(t_printf *pf, long long i, char *str)
{
int ignore;
ignore = 0;
if (pf->width <= 0 || pf->flag[0] == TRUE)
put_spacing(pf, i);
if (i < 0 && ((pf->width <= 0 && pf->precision > 0) || pf->flag[3] == TRUE))
{
pf->len += ft_len_putchar('-', pf->fd);
ignore = 1;
}
front_padding_nb(pf, i, str);
if (i < 0 && pf->precision > 0 && !ignore)
{
pf->len += ft_len_putchar('-', pf->fd);
ignore = 1;
}
return (ignore);
}
void put_u(t_printf *pf)
{
size_t i;
char *str;
int ignore;
i = get_number(pf);
if ((pf->precision <= -2 || pf->precision == 0) && i == 0 &&
(str = ft_strnew(0)) && pf->width != 0)
pf->len += ft_len_putchar(' ', pf->fd);
else if ((pf->precision <= -2 || pf->precision == 0) && i == 0)
str = ft_strnew(0);
else
str = ft_itoa_base(i, 10, 'a');
ft_width_nb(pf, i);
ignore = nb_start(pf, i, str);
if ((pf->precision -= (i < 0 ? ft_nbs(-i) : ft_nbs(i))) >= 0)
while (pf->precision--)
pf->len += ft_len_putchar('0', pf->fd);
pf->len += ft_len_putstr(str, ignore, pf->fd);
if (pf->width > 0 && pf->flag[0] == TRUE)
while (pf->width--)
pf->len += ft_len_putchar(' ', pf->fd);
free(str);
}