-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_int.c
119 lines (111 loc) · 3.6 KB
/
ft_int.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_int.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vkinnune <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/02 13:31:03 by vkinnune #+# #+# */
/* Updated: 2022/02/08 12:27:47 by vkinnune ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void printnum_more(t_info *var, int *n, char *p)
{
if (var->sign)
var->size += write (1, &(var->sign), 1);
if (var->prefix)
var->size += write (1, var->prefix, ft_strlen(var->prefix));
if (var->padc == '0')
*n = var->length;
if (var->base == 8 && p[1] == '0' && var->alt)
var->prec = 0;
}
int printnum(t_info *var, char buf[MAXBUF], char *p, int len)
{
int n;
n = var->prec - len;
if (n > 0)
var->length -= n;
var->length -= len;
if (var->sign)
var->length--;
if (var->prefix)
var->length -= ft_strlen(var->prefix);
if (var->length > 0 && !(var->ladjust) && var->padc == ' ')
while (--(var->length) >= 0)
var->size += write(1, " ", 1);
printnum_more(var, &n, p);
while (--n >= 0)
var->size += write(1, "0", 1);
if (var->prec != 0 || p[1] != '0')
while (++p != &buf[MAXBUF])
var->size += write (1, p, 1);
else if (var->prec == 0 && p[1] == '0' && var->iswidth == TRUE)
var->size += write (1, " ", 1);
if (var->ladjust && var->length > 0)
while (--(var->length) >= 0)
var->size += write (1, " ", 1);
return (1);
}
int unsigned_int(t_info *var, char *ptr)
{
init_unsigned(var, ptr);
if (var->bytes == 64)
return (donumber(var, va_arg(*(var->args), unsigned long long)));
else if (var->bytes == 32)
return (donumber(var, va_arg(*(var->args), unsigned long)));
else if (var->bytes == 16)
return (donumber(var, (unsigned short)va_arg(*(var->args),
unsigned int)));
else if (var->bytes == 8)
return (donumber(var, (unsigned char)va_arg(*(var->args),
unsigned int)));
else if (sizeof(int) == sizeof(long) && var->bytes == 0)
return (donumber(var, va_arg(*(var->args), unsigned long)));
else if (sizeof(int) < sizeof(long) && var->bytes == 0)
return (donumber(var, va_arg(*(var->args), unsigned int)));
return (0);
}
void init_unsigned(t_info *var, char *ptr)
{
if (*ptr == 'o')
var->base = 8;
else if (*ptr == 'x' || *ptr == 'p')
{
var->base = 16;
}
else if (*ptr == 'X')
{
var->base = 16;
var->capitals = 16;
}
if (*ptr == 'p')
{
var->alt = TRUE;
var->bytes = 64;
}
if (var->sign == '+' || var->sign == ' ')
var->sign = 0;
}
int signed_int(t_info *var)
{
signed long long n;
n = 0;
if (var->bytes == 64)
n = va_arg(*(var->args), long long);
else if (var->bytes == 32)
n = va_arg(*(var->args), long);
else if (var->bytes == 16)
n = (signed short)va_arg(*(var->args), int);
else if (var->bytes == 8)
n = (signed char)va_arg(*(var->args), int);
else if (sizeof(int) == sizeof(long) && var->bytes == 0)
n = va_arg(*(var->args), long);
else if (sizeof(int) < sizeof(long) && var->bytes == 0)
n = va_arg(*(var->args), int);
if (n >= 0)
return (donumber(var, (unsigned long long)n));
var->sign = '-';
return (donumber(var, (unsigned long long)-n));
}