45 lines
1,016 B
C
45 lines
1,016 B
C
#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);
|
|
}
|