72 lines
2.1 KiB
C
72 lines
2.1 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_printf.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2022/10/12 16:27:59 by erey-bet #+# #+# */
|
|
/* Updated: 2022/11/25 15:34:18 by erey-bet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "ft_printf.h"
|
|
|
|
static int ft_is_format(const char c)
|
|
{
|
|
return (c == 'c' || c == 's' || c == 'p' || c == 'i' || c == 'd'
|
|
|| c == 'u' || c == 'x' || c == 'X' || c == '%');
|
|
}
|
|
|
|
static int ft_post_character(char format, va_list args)
|
|
{
|
|
int count;
|
|
|
|
count = 1;
|
|
if (format == 'c')
|
|
ft_putchar(va_arg(args, int));
|
|
else if (format == 's')
|
|
count = ft_putstr(va_arg(args, char *));
|
|
else if (format == 'd' || format == 'i')
|
|
count = ft_putnbr(va_arg(args, int));
|
|
else if (format == 'u')
|
|
count = ft_putunbr(va_arg(args, unsigned int));
|
|
else if (format == 'x')
|
|
count = ft_putnbrhex(va_arg(args, unsigned int));
|
|
else if (format == 'X')
|
|
count = ft_putnbrhex_upper(va_arg(args, unsigned int));
|
|
else if (format == '%')
|
|
ft_putchar('%');
|
|
else if (format == 'p')
|
|
count = ft_putvd(va_arg(args, void *));
|
|
return (count);
|
|
}
|
|
|
|
int ft_printf(const char *str, ...)
|
|
{
|
|
va_list args;
|
|
int i;
|
|
int count;
|
|
|
|
if (str == NULL)
|
|
return (-1);
|
|
va_start(args, str);
|
|
i = -1;
|
|
count = 0;
|
|
while (str[++i])
|
|
{
|
|
if (str[i] == '%' && ft_is_format(str[i + 1]))
|
|
{
|
|
count += ft_post_character(str[i + 1], args);
|
|
i++;
|
|
}
|
|
else
|
|
{
|
|
ft_putchar(str[i]);
|
|
count++;
|
|
}
|
|
}
|
|
va_end(args);
|
|
return (count);
|
|
}
|