-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_param_octal.c
97 lines (89 loc) · 2.92 KB
/
ft_param_octal.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_param_octal.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jraymond <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/02/07 14:49:09 by jraymond #+# #+# */
/* Updated: 2018/02/14 18:29:04 by jraymond ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void nb_c_addoct_pw(t_nbcaddpw *nbca, t_printf *elem, int intl)
{
int i;
ft_bzero(nbca, sizeof(t_nbcaddpw));
if (elem->flags & PRECI)
nbca->preci = (intl >= elem->precision) ? 0 : elem->precision - intl;
i = nbca->preci;
if (elem->width)
{
if (elem->flags & SHARP)
i++;
if (elem->width <= (i + intl))
nbca->width = 0;
else
nbca->width = elem->width - (i + intl);
}
}
void ft_padding_numb(t_printf *elem, t_nbcaddpw *nbca, char *numb)
{
char sp;
char zero;
sp = ' ';
zero = '0';
if (elem->flags & ZERO || !(elem->flags & MINUS))
{
if (!(elem->flags & ZERO))
ft_handle_overflow(elem, &sp, nbca->width, 1);
else
ft_handle_overflow(elem, &zero, nbca->width, 1);
if (elem->flags & SHARP)
ft_handle_overflow(elem, &zero, 1, 1);
ft_handle_overflow(elem, &zero, nbca->preci, 1);
ft_handle_overflow(elem, numb, ft_strlen(numb), 2);
}
else
{
if (elem->flags & SHARP)
ft_handle_overflow(elem, &zero, 1, 1);
ft_handle_overflow(elem, &zero, nbca->preci, 1);
ft_handle_overflow(elem, numb, ft_strlen(numb), 2);
ft_handle_overflow(elem, &sp, nbca->width, 1);
}
}
void ft_handle_sharp(t_nbcaddpw *nbca, t_printf *elem, unsigned long long nb)
{
if (elem->flags & SHARP && elem->flags & MINUS && nb != 0)
{
if (nbca->preci > 0 && !(nbca->width))
nbca->preci -= 1;
else
nbca->width = (nbca->width - 1) < 0 ? 0 : (nbca->width - 1);
}
else if (elem->flags & SHARP && nb != 0)
{
if (nbca->preci > 0 && !(nbca->width))
nbca->preci -= 1;
else
nbca->width = (nbca->width - 1) < 0 ? 0 : (nbca->width - 1);
}
}
int ft_param_octal(t_printf *elem, va_list ap)
{
unsigned long long arg;
char *numb;
t_nbcaddpw nbca;
arg = ft_handle_uoctsize(elem, ap);
numb = ft_ulltoa_base(arg, 8);
if (elem->flags & ZERO && (elem->flags & MINUS || elem->flags & PRECI))
elem->flags ^= ZERO;
nb_c_addoct_pw(&nbca, elem, ft_strlen(numb));
ft_handle_sharp(&nbca, elem, arg);
if (arg == 0)
return (ft_paddoct_null(elem, &nbca));
ft_padding_numb(elem, &nbca, numb);
ft_memdel((void**)&numb);
return (0);
}