|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* 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"
|
|
|
|
|
|
|
|
#include "./libft/libft.h"
|
|
|
|
|
|
|
|
int handle_options(va_list args, const char *str, t_opts *opts)
|
|
|
|
{
|
|
|
|
while (*str && !ft_strchr("cspdiuxX%", *str)) {
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
if (opts->dot) opts->precision = ft_atoi(str);
|
|
|
|
else opts->width = ft_atoi(str);
|
|
|
|
while (ft_isdigit(*str)) {
|
|
|
|
str++;
|
|
|
|
opts->len++;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else if (*str == '*')
|
|
|
|
{
|
|
|
|
if (opts->dot) opts->precision = va_arg(args, int);
|
|
|
|
else opts->width = va_arg(args, int);
|
|
|
|
} else return (0);
|
|
|
|
str++;
|
|
|
|
opts->len++;
|
|
|
|
}
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int handle_functions(va_list args, const char *str, t_opts *opts)
|
|
|
|
{
|
|
|
|
size_t n = 0;
|
|
|
|
size_t ret = 0;
|
|
|
|
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};
|
|
|
|
while (g_types[n] && g_types[n] != *str)
|
|
|
|
n++;
|
|
|
|
if (g_types[n]) {
|
|
|
|
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;
|
|
|
|
ret += (g_printf_fcts[n])(args, str, opts);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
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};
|
|
|
|
if (handle_options(args, str, &opts))
|
|
|
|
{
|
|
|
|
str += opts.len;
|
|
|
|
i += handle_functions(args, str, &opts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (++i)
|
|
|
|
ft_putchar_fd(*str, 1);
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
return (i);
|
|
|
|
}
|