-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainprintf.c
More file actions
90 lines (81 loc) · 2.25 KB
/
Copy pathmainprintf.c
File metadata and controls
90 lines (81 loc) · 2.25 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mainprintf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zael-mou <zael-mou@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/19 09:38:24 by zael-mou #+# #+# */
/* Updated: 2024/11/21 14:34:03 by zael-mou ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include <stdio.h>
int printfadrees(unsigned long long uptr, char *str, int i, char *hexa)
{
int count;
count = 0;
while (uptr)
{
str[--i] = hexa[uptr % 16];
uptr /= 16;
}
i = 0;
while (str[i])
count += ft_putchar(str[i++]);
return (count);
}
int checkchar(va_list args, const char str)
{
int i;
i = 0;
if (str == 'c')
i += ft_putchar(va_arg(args, int));
else if (str == '%')
i += ft_putchar('%');
else if (str == 's')
i += ft_putstr(va_arg(args, char *));
else if (str == 'p')
i += ft_putaddress(va_arg(args, void *));
return (i);
}
int checkother(va_list args, const char str)
{
int i;
i = 0;
if (str == 'd' || str == 'i')
i += ft_putnbr(va_arg(args, int));
else if (str == 'u')
i += ft_putunbr(va_arg(args, unsigned int));
else if (str == 'x')
i += ft_puthexa(va_arg(args, unsigned long long), 0);
else if (str == 'X')
i += ft_puthexa(va_arg(args, unsigned long long), 1);
return (i);
}
int ft_printf(const char *str, ...)
{
va_list args;
int i;
if (!str)
return (-1);
i = 0;
va_start(args, str);
while (*str)
{
if (*str == '%')
{
str++;
if (*str == 'c' || *str == '%' || *str == 's' || *str == 'p')
i += checkchar(args, *str);
else if ((*str >= 'a' && *str <= 'z') || *str == 'X')
i += checkother(args, *str);
}
else
i += ft_putchar(*str);
if (*str)
str++;
}
va_end(args);
return (i);
}