-
Notifications
You must be signed in to change notification settings - Fork 0
/
integers.c
132 lines (121 loc) · 3.19 KB
/
integers.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
125
126
127
128
129
130
131
132
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* integers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: moboustt <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/26 14:37:34 by moboustt #+# #+# */
/* Updated: 2019/12/20 16:25:23 by moboustt ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void is_int(t_struct *list, va_list ap)
{
int out_int;
int int_len;
int refer;
char *s;
refer = 0;
out_int = (int)va_arg(ap, int);
int_len = ft_intlen(out_int);
s = ft_itoa(out_int);
if (zero_input(list, out_int) == 1)
return ;
if (list->precision == -1 && list->width == -1)
{
out_int < 0 ? int_len += 1 : int_len;
list->n += write(1, s, int_len);
}
else
{
if (out_int < 0)
refer = -1;
out_int < 0 ? out_int = -(out_int) : out_int;
is_int_handle(list, out_int, refer, int_len);
}
free(s);
}
int zero_input(t_struct *list, int out_int)
{
int c;
c = '\0';
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, &c, 0);
return (1);
}
return (0);
}
void is_int_handle(t_struct *list, long out_int, int refer, int int_len)
{
char *s;
out_int < 0 ? out_int = -out_int : out_int;
s = ft_itoa(out_int);
if (list->dot && list->precision > int_len)
list->precision -= int_len;
else
list->precision = 0;
if (refer == 0)
list->width = list->width - (list->precision + int_len);
else
list->width = list->width - (list->precision + int_len + 1);
if (list->minus == 1)
int_righthand(list, out_int, refer, s);
else
int_lefthand(list, out_int, refer, s);
free(s);
}
void int_righthand(t_struct *list, int out_int, int refer, char *s)
{
int ret;
int int_len;
ret = 0;
int_len = ft_intlen(out_int);
if (refer != 0)
list->n += write(1, "-", 1);
while (list->precision > 0)
{
list->n += write(1, "0", 1);
list->precision--;
}
list->n += write(1, s, int_len);
while (list->width > 0)
{
list->n += write(1, " ", 1);
list->width--;
}
}
void int_lefthand(t_struct *list, int out_int, int refer, char *s)
{
int ret;
int rc;
int int_len;
ret = 0;
rc = '\0';
int_len = ft_intlen(out_int);
if (list->zero && !list->dot)
rc = '0';
else
rc = ' ';
if (rc == '0' && refer != 0)
{
list->n += write(1, "-", 1);
refer = 0;
}
while (list->width-- > 0)
list->n += write(1, &rc, 1);
if (refer != 0)
list->n += write(1, "-", 1);
while (list->precision-- > 0)
list->n += write(1, "0", 1);
list->n += write(1, s, int_len);
}