-
Notifications
You must be signed in to change notification settings - Fork 0
/
unsigned.c
93 lines (85 loc) · 2.68 KB
/
unsigned.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* unsigned.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: moboustt <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/07 13:31:03 by moboustt #+# #+# */
/* Updated: 2019/12/20 10:44:24 by moboustt ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int zero_input_u(t_struct *list, unsigned int out_int)
{
if (out_int == 0 && list->dot == 1 && list->width > 0 &&
(list->precision == -1 || list->precision == 0))
{
while (list->width-- > 0)
list->n += write(1, " ", 1);
return (1);
}
else if (out_int == 0 && list->dot == 1 &&
(list->precision == 0 || list->precision == -1))
{
write(1, "", 1);
return (1);
}
return (0);
}
int is_unsigned(t_struct *list, va_list ap)
{
unsigned int out_unsigned;
unsigned int unsigned_len;
char *s;
out_unsigned = (unsigned int)va_arg(ap, unsigned int);
unsigned_len = ft_intlen(out_unsigned);
s = ft_itoa(out_unsigned);
if (zero_input(list, out_unsigned) == 1)
return (0);
if (list->width == -1 && list->precision == -1)
{
list->n += write(1, s, unsigned_len);
return (0);
}
else
{
widthprecisionhandle(list, unsigned_len);
if (list->minus == 1)
unsigned_rightparese(list, s, unsigned_len);
else
unsigned_leftparese(list, s, unsigned_len);
}
free(s);
return (0);
}
void widthprecisionhandle(t_struct *list, unsigned int unsigned_len)
{
if (list->dot && list->precision > (int)unsigned_len)
list->precision -= unsigned_len;
else
list->precision = 0;
list->width = list->width - (list->precision + unsigned_len);
}
void unsigned_rightparese(t_struct *list, char *s, unsigned int unsigned_len)
{
while (list->precision-- > 0)
list->n += write(1, "0", 1);
list->n += write(1, s, unsigned_len);
while (list->width-- > 0)
list->n += write(1, " ", 1);
}
void unsigned_leftparese(t_struct *list, char *s, unsigned int unsigned_len)
{
int rc;
rc = '\0';
if (list->zero && !list->dot)
rc = '0';
else
rc = ' ';
while (list->width-- > 0)
list->n += write(1, &rc, 1);
while (list->precision-- > 0)
list->n += write(1, "0", 1);
list->n += write(1, s, unsigned_len);
}