/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_printf.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/12 16:27:59 by erey-bet #+# #+# */ /* Updated: 2022/10/15 23:19:38 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ #include "ft_printf.h" #include 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_bis(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, int)); else if (format == 'X') count = ft_putnbrhex_upper(va_arg(args, int)); else if (format == '%') ft_putchar('%'); else if (format == 'p') count = ft_putvd(va_arg(args, void *)); return (count); } static int ft_post_character(char format, va_list args) { int count; count = ft_post_character_bis(format, args); if (count == -1) { ft_putstr("(null)"); count = 6; } else if (count == -2) { ft_putstr("(nil)"); count = 5; } return (count); } int ft_printf(const char *str, ...) { va_list args; int i; int y; int count; va_start(args, str); i = -1; y = 0; count = 0; while (str[++i]) { if (str[i] == '%' && ft_is_format(str[i + 1])) { count += ft_post_character(str[i + 1], args); i++; y++; } else { ft_putchar(str[i]); count++; } } va_end(args); return (count); }