-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf_int.c
More file actions
121 lines (110 loc) · 2.91 KB
/
ft_printf_int.c
File metadata and controls
121 lines (110 loc) · 2.91 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_int.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dporhomo <dporhomo@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/03 00:46:32 by dporhomo #+# #+# */
/* Updated: 2025/12/09 10:29:02 by dporhomo ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static char *ft_uitoa(unsigned int n);
static int ft_format_out(char *str, t_layout *tab, t_flags f);
static int ft_get_sign(int n, t_flags flags);
static int ft_write_num(char *str, int len, int zeros, int sign);
int ft_print_int(int n, t_flags flags)
{
char *str;
long nb;
t_layout tab;
int ret;
nb = n;
if (nb < 0)
nb = -nb;
if (n == 0 && flags.precision == 0)
return (ft_print_padding(flags.width, 0, 0));
str = ft_uitoa((unsigned int)nb);
if (!str)
return (0);
tab.len = ft_strlen(str);
tab.zeros = 0;
if (flags.precision > tab.len)
tab.zeros = flags.precision - tab.len;
tab.sign = ft_get_sign(n, flags);
ret = ft_format_out(str, &tab, flags);
free(str);
return (ret);
}
static char *ft_uitoa(unsigned int n)
{
char *str;
int len;
unsigned int num;
num = n;
len = 0;
if (n == 0)
len = 1;
while (n > 0 || len == 0)
{
n /= 10;
len++;
}
str = (char *)malloc(sizeof(char) * (len + 1));
if (!str)
return (NULL);
str[len] = '\0';
if (num == 0)
str[0] = '0';
while (num > 0)
{
str[--len] = (num % 10) + '0';
num /= 10;
}
return (str);
}
static int ft_format_out(char *str, t_layout *tab, t_flags f)
{
int total_len;
int count;
count = 0;
total_len = tab->len + tab->zeros + (tab->sign != 0);
if (f.left == 0)
{
if (f.zero && f.precision == -1)
{
if (tab->sign)
count += write(1, &tab->sign, 1);
tab->sign = 0;
count += ft_print_padding(f.width, total_len, 1);
}
else
count += ft_print_padding(f.width, total_len, 0);
}
count += ft_write_num(str, tab->len, tab->zeros, tab->sign);
if (f.left == 1)
count += ft_print_padding(f.width, total_len, 0);
return (count);
}
static int ft_get_sign(int n, t_flags flags)
{
if (n < 0)
return ('-');
if (flags.plus)
return ('+');
if (flags.space)
return (' ');
return (0);
}
static int ft_write_num(char *str, int len, int zeros, int sign)
{
int count;
count = 0;
if (sign)
count += write(1, &sign, 1);
while (zeros-- > 0)
count += write(1, "0", 1);
count += write(1, str, len);
return (count);
}