-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathft_print_util.c
76 lines (71 loc) · 2.16 KB
/
ft_print_util.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_util.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: smonroe <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/07/25 06:09:55 by smonroe #+# #+# */
/* Updated: 2018/08/27 17:26:36 by smonroe ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int positive(t_var v)
{
if ((v.flag == 'd' || v.flag == 'i') && v.uni.i > -1)
return (1);
if (v.flag == 'D' && v.uni.l > -1)
return (1);
return (0);
}
static char *prefix(t_var v)
{
if (v.flag == 'x')
return ("0x");
else if (v.flag == 'X')
return ("0X");
else if ((v.flag == 'O' || v.flag == 'o') && v.uni.l)
return ("0");
else if (v.flag == 'b')
return ("0b");
else
return ("");
}
static char *printer_prec(t_var v)
{
if (!positive(v) && v.base == 10 && v.prec >= (int)ft_strlen(v.str))
v.str[0] = '0';
while ((int)ft_strlen(v.str) < v.prec)
v.str = ft_strappfr("0", v.str);
if (!positive(v) && v.base == 10 && v.str[0] != '-')
v.str = ft_strappfr("-", v.str);
return (v.str);
}
char *printer(t_var v)
{
if (v.flag == 's')
return (string(v));
if (v.prec)
v.str = printer_prec(v);
if (v.plus && positive(v))
{
if (v.point == '0')
v.str[0] = '+';
else
v.str = ft_strappfr("+", v.str);
}
if (v.hash)
v.str = ft_strappfr(prefix(v), v.str);
if (v.spc && !v.plus && v.str[0] != '-')
v.str = ft_strappfr(" ", v.str);
if (v.pad)
{
if (v.neg)
while ((int)ft_strlen(v.str) < v.pad)
v.str = ft_strjoinfr(v.str, " ");
if (!v.neg)
while ((int)ft_strlen(v.str) < v.pad)
v.str = ft_strappfr(" ", v.str);
}
return (v.str);
}