-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc_uinteger.c
executable file
·80 lines (73 loc) · 2.33 KB
/
proc_uinteger.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* proc_uinteger.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aseptimu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/02 20:59:08 by aseptimu #+# #+# */
/* Updated: 2021/11/05 17:12:42 by aseptimu ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include "libft/libft.h"
static void ft_putunsigned_fd(unsigned int n, int fd)
{
char c;
if (n < 10)
{
c = n + '0';
write(fd, &c, 1);
}
else
{
ft_putunsigned_fd(n / 10, fd);
ft_putunsigned_fd(n % 10, fd);
}
}
static int proc_int_minus(t_params params, unsigned int tmp, \
unsigned int i, unsigned int length)
{
tmp += proc_precision(params.precision, length);
ft_putunsigned_fd(i, 1);
tmp += proc_width(params.width, params.zero, tmp);
return (tmp);
}
static int proc_int_no_minus(t_params params, unsigned int tmp, \
unsigned int i, unsigned int length)
{
if ((long)params.precision >= (long)tmp)
tmp += proc_width(params.width, params.zero, \
params.space + params.plus + params.precision);
else
tmp += proc_width(params.width, params.zero, \
tmp + params.space + params.plus);
tmp += proc_precision(params.precision, length);
ft_putunsigned_fd(i, 1);
return (tmp);
}
int proc_uinteger(t_params params, unsigned int i)
{
int length;
unsigned int tmp;
length = 0;
tmp = i;
if (i == 0)
length++;
while (tmp)
{
tmp /= 10;
length++;
}
if (params.precision == 0 && i == 0)
{
tmp = proc_width(params.width, params.zero, 0);
return (tmp);
}
tmp = length;
if (params.minus == 1)
tmp = proc_int_minus(params, tmp, i, length);
if (params.minus == 0)
tmp = proc_int_no_minus(params, tmp, i, length);
return (tmp);
}