79 lines
2.4 KiB
C
79 lines
2.4 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_printf.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: pbonilla <eirodeis.lepnj@gmail.com> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2021/03/02 21:18:26 by pbonilla #+# #+# */
|
|
/* Updated: 2021/03/21 20:10:56 by pbonilla ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "../includes/ft_printf.h"
|
|
|
|
int ft_print_param(t_param *param, va_list args, char **buff)
|
|
{
|
|
if ((char)param->type == 'c')
|
|
*buff = ft_char_case(param, va_arg(args, int));
|
|
else if ((char)param->type == 's')
|
|
*buff = ft_str_case(param, va_arg(args, char *));
|
|
else if ((char)param->type == 'p')
|
|
*buff = ft_ptr_case(param, va_arg(args, unsigned long long));
|
|
else if ((char)param->type == 'd' || (char)param->type == 'i')
|
|
*buff = ft_int_case(param, va_arg(args, int));
|
|
else if ((char)param->type == 'u')
|
|
*buff = ft_ui_case(param, va_arg(args, unsigned int));
|
|
else if ((char)param->type == 'x' || (char)param->type == 'X')
|
|
*buff = ft_x_case(param, va_arg(args, unsigned long));
|
|
else if ((char)param->type == '%')
|
|
*buff = ft_percent_case(param);
|
|
if (param->type == 'c')
|
|
{
|
|
write(1, *buff, param->len);
|
|
return (param->len);
|
|
}
|
|
else
|
|
ft_putstr_fd(*buff, 1);
|
|
return (ft_strlen(*buff));
|
|
}
|
|
|
|
int ft_read_str(const char *s, va_list args)
|
|
{
|
|
t_param param;
|
|
int i;
|
|
int nb_chars;
|
|
char *buff;
|
|
|
|
i = -1;
|
|
nb_chars = 0;
|
|
while (s[++i])
|
|
{
|
|
buff = NULL;
|
|
if (s[i] != '%')
|
|
nb_chars += write(1, &s[i], 1);
|
|
else if (s[i] == '%' && s[i + 1])
|
|
{
|
|
param = ft_init_param();
|
|
i = ft_parse_param(s, ++i, ¶m, args);
|
|
if (i == -1)
|
|
return (-1);
|
|
nb_chars += ft_print_param(¶m, args, &buff);
|
|
if (buff)
|
|
free(buff);
|
|
}
|
|
}
|
|
return (nb_chars);
|
|
}
|
|
|
|
int ft_printf(const char *s, ...)
|
|
{
|
|
va_list args;
|
|
int nb_chars;
|
|
|
|
va_start(args, s);
|
|
nb_chars = ft_read_str(s, args);
|
|
va_end(args);
|
|
return (nb_chars);
|
|
}
|