feat: added ft_printf
This commit is contained in:
parent
5276d36f0c
commit
6910e9e96f
1 changed files with 45 additions and 0 deletions
45
srcs/ft_printf.c
Normal file
45
srcs/ft_printf.c
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
#include "libft.h"
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
static int ft_print_format(const char *fmt, va_list args)
|
||||||
|
{
|
||||||
|
if (*fmt == 'c')
|
||||||
|
return (ft_putchar(va_arg(args, int)));
|
||||||
|
else if (*fmt == 's')
|
||||||
|
return (ft_putstr(va_arg(args, char *)));
|
||||||
|
else if (*fmt == 'p')
|
||||||
|
return (ft_putaddr((uintptr_t)va_arg(args, void *)));
|
||||||
|
else if (*fmt == 'd' || *fmt == 'i')
|
||||||
|
return (ft_putnbr(va_arg(args, int)));
|
||||||
|
else if (*fmt == 'u')
|
||||||
|
return (ft_putunbr(va_arg(args, unsigned int)));
|
||||||
|
else if (*fmt == 'x')
|
||||||
|
return (ft_puthex(va_arg(args, unsigned int), 0));
|
||||||
|
else if (*fmt == 'X')
|
||||||
|
return (ft_puthex(va_arg(args, unsigned int), 1));
|
||||||
|
else if (*fmt == '%')
|
||||||
|
return (ft_putchar('%'));
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_printf(const char *format, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
int i = 0;
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
va_start(args, format);
|
||||||
|
while (format[i])
|
||||||
|
{
|
||||||
|
if (format[i] == '%' && format[i + 1])
|
||||||
|
{
|
||||||
|
count += ft_print_format(&format[++i], args);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
count += ft_putchar(format[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
va_end(args);
|
||||||
|
return (count);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue