-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_conversion_integer.c
116 lines (105 loc) · 2.62 KB
/
ft_conversion_integer.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_conversion_integer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: liafigli <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/05 13:39:11 by liafigli #+# #+# */
/* Updated: 2021/02/19 10:21:54 by liafigli ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
t_int ft_init_int(void)
{
t_int i;
i.neg = 0;
i.flag = 0;
i.count = 0;
i.len = 0;
i.cn = 0;
return (i);
}
static int ft_first(t_flags flags, t_int *i, char *str)
{
int c;
c = 0;
if (flags.minus == 1)
{
if (i->neg < 0 && i->flag == 0)
{
c += ft_putchar('-');
i->flag = 1;
}
i->count += ft_width(flags.precision, i->len, 1);
c += ft_putstr(str);
}
return (c);
}
static int ft_second(t_flags flags, t_int *i)
{
int c;
c = 0;
if (flags.precision > i->len)
{
if (i->neg < 0 && i->flag == 0 && flags.width <= flags.precision)
{
c += ft_putchar('-');
i->flag = 1;
}
i->count += ft_width(flags.width + i->neg, flags.precision,
flags.width <= flags.precision);
}
else if (flags.precision <= i->len)
{
if (flags.zero)
if (i->neg < 0 && i->flag == 0)
{
c += ft_putchar('-');
i->flag = 1;
}
i->count += ft_width(flags.width + i->neg, i->len + i->count,
flags.zero);
}
return (c);
}
static int ft_third(t_flags flags, t_int *i, char *str)
{
int c;
c = 0;
if (flags.minus == 0)
{
if (i->neg < 0 && i->flag == 0)
c += ft_putchar('-');
i->count += ft_width(flags.precision, i->len, 1);
c += ft_putstr(str);
}
return (c);
}
int ft_conversion_integer(int c, t_flags flags)
{
t_int i;
char *str;
i = ft_init_int();
if (c == -2147483648)
i.flag = 1;
if (c < 0)
{
c *= -1;
i.neg = -1;
}
str = ft_itoa(c);
i.len = ft_strlen(str);
if (flags.precision == 0 && c == 0)
{
str = ft_strdup("");
return (ft_width(flags.width, 0, flags.zero));
}
i.cn += ft_first(flags, &i, str);
if (flags.zero > 0)
if (flags.precision <= i.len && flags.precision > 0)
flags.zero = 0;
i.cn += ft_second(flags, &i);
i.cn += ft_third(flags, &i, str);
return (i.count + i.cn);
}