-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf_unsigned.c
More file actions
98 lines (88 loc) · 2.5 KB
/
ft_printf_unsigned.c
File metadata and controls
98 lines (88 loc) · 2.5 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_unsigned.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dporhomo <dporhomo@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/03 22:08:40 by dporhomo #+# #+# */
/* Updated: 2025/12/09 10:37:50 by dporhomo ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static char *ft_uitoa(unsigned int n);
static int ft_format_unsigned(char *str, t_layout *tab, t_flags f);
static int ft_write_unsigned(char *str, int len, int zeros);
int ft_print_unsigned(unsigned int n, t_flags flags)
{
char *str;
t_layout tab;
int ret;
if (n == 0 && flags.precision == 0)
return (ft_print_padding(flags.width, 0, 0));
str = ft_uitoa(n);
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 = 0;
ret = ft_format_unsigned(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_unsigned(char *str, t_layout *tab, t_flags f)
{
int count;
int total_len;
count = 0;
total_len = tab->len + tab->zeros;
if (f.left == 0)
{
if (f.zero && f.precision == -1)
count += ft_print_padding(f.width, total_len, 1);
else
count += ft_print_padding(f.width, total_len, 0);
}
count += ft_write_unsigned(str, tab->len, tab->zeros);
if (f.left == 1)
count += ft_print_padding(f.width, total_len, 0);
return (count);
}
static int ft_write_unsigned(char *str, int len, int zeros)
{
int count;
count = 0;
while (zeros-- > 0)
count += write(1, "0", 1);
count += write(1, str, len);
return (count);
}