-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf_utils.c
83 lines (73 loc) · 1.83 KB
/
ft_printf_utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vgroux <vgroux@student.42lausanne.ch> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/20 15:48:16 by vgroux #+# #+# */
/* Updated: 2022/10/25 15:50:53 by vgroux ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_strlen(const char *s)
{
unsigned int i;
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
int ft_printf_char(char c)
{
return (write(1, &c, 1));
}
int ft_printf_str(char *str)
{
int i;
int len;
i = 0;
len = 0;
if (str != NULL)
{
while (str[i] != '\0')
{
len += ft_printf_char(str[i]);
i++;
}
}
else
len = ft_printf_str("(null)");
return (len);
}
int ft_printf_n_base(long long num, char *base)
{
int baselen;
int len;
baselen = ft_strlen(base);
len = 0;
if (num < 0)
{
len += ft_printf_char('-');
len += ft_printf_n_base(num * -1, base);
}
else if (num >= baselen)
{
len += ft_printf_n_base(num / baselen, base);
len += ft_printf_n_base(num % baselen, base);
}
else
len += ft_printf_char(base[num]);
return (len);
}
int ft_printf_ui(unsigned int n)
{
char *str;
int len;
str = ft_ulltoa_base(n, "0123456789");
if (!str)
return (0);
len = ft_printf_str(str);
free(str);
return (len);
}