Projet de l'école 42 : Printf
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

103 lines
2.6 KiB

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/27 08:09:49 by narnaud #+# #+# */
/* Updated: 2021/11/17 09:40:16 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void get_digit(t_opts *opts, int n)
{
if (opts->dot)
opts->precision = n;
else
opts->width = n;
}
const char *handle_options(va_list args, const char *str, t_opts *opts)
{
if (*str == '-')
opts->minus = 1;
else if (*str == '0')
opts->zero = 1;
else if (*str == '#')
opts->hash = 1;
else if (*str == ' ')
opts->space = 1;
else if (*str == '+')
opts->plus = 1;
else if (*str == '.')
opts->dot = 1;
else if (ft_isdigit(*str))
{
get_digit(opts, ft_atoi(str));
while (ft_isdigit(*str) && str++)
opts->len++;
return (str);
}
else if (*str == '*')
get_digit(opts, va_arg(args, int));
else
return (str);
opts->len++;
return (++str);
}
int handle_functions(va_list args, const char *str, t_opts *opts)
{
size_t n;
const char *g_types = {"cspdiuxX%"};
static int (*g_printf_fcts[9])(va_list arg, const char *str,
t_opts *opts) = {va_print_char, va_print_str, va_print_ptr,
va_print_nbr, va_print_nbr, va_print_unsign, va_print_x,
va_print_x_cap, va_print_perc};
n = 0;
while (g_types[n] && g_types[n] != *str && ++n)
;
if (!g_types[n])
return (0);
if (opts->minus)
opts->zero = 0;
if (opts->precision > 0)
opts->zero = 0;
if (opts->width < 0)
{
opts->minus = 1;
opts->width *= -1;
}
if (opts->precision < 0)
opts->dot = 0;
return ((g_printf_fcts[n])(args, str, opts));
}
int ft_printf(const char *str, ...)
{
va_list args;
size_t i;
t_opts opts;
i = 0;
va_start(args, str);
while (*str)
{
if (*str == '%' && str++)
{
opts = (t_opts){0};
while (*str && !ft_strchr("cspdiuxX%", *str))
str = handle_options(args, str, &opts);
i += handle_functions(args, str, &opts);
}
else if (++i)
ft_putchar_fd(*str, 1);
str++;
}
va_end(args);
return (i);
}