85 lines
2.1 KiB
C
85 lines
2.1 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_parser.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: pbonilla <eirodeis.lepnj@gmail.com> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2021/03/03 02:19:59 by pbonilla #+# #+# */
|
|
/* Updated: 2021/03/21 20:00:15 by pbonilla ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "../includes/ft_printf.h"
|
|
|
|
t_param ft_init_param(void)
|
|
{
|
|
t_param param;
|
|
|
|
param.minus = 0;
|
|
param.zero = 0;
|
|
param.width = 0;
|
|
param.prec = -1;
|
|
param.type = 0;
|
|
param.len = 0;
|
|
return (param);
|
|
}
|
|
|
|
int is_valid_type(const char c)
|
|
{
|
|
return (c == 'c' || c == 's' || c == 'p' || c == 'd' ||
|
|
c == 'i' || c == 'u' || c == 'x' || c == 'X' || c == '%');
|
|
}
|
|
|
|
int ft_parse_para2(const char *s, int i, t_param *par, va_list args)
|
|
{
|
|
if (s[i] == '.')
|
|
{
|
|
par->prec = 0;
|
|
if (s[++i] == '*')
|
|
{
|
|
par->prec = va_arg(args, int);
|
|
++i;
|
|
}
|
|
else if (s[i] >= '0' && s[i] <= '9')
|
|
{
|
|
par->prec = ft_atoi(&s[i]);
|
|
while (s[i] >= '0' && s[i] <= '9')
|
|
++i;
|
|
}
|
|
}
|
|
if (is_valid_type(s[i]))
|
|
par->type = (int)s[i];
|
|
else
|
|
return (-1);
|
|
return (i);
|
|
}
|
|
|
|
int ft_parse_param(const char *s, int i, t_param *par, va_list args)
|
|
{
|
|
while (s[i] == '0' || s[i] == '-')
|
|
{
|
|
if (s[i] == '0')
|
|
par->zero = 1;
|
|
else if (s[i] == '-')
|
|
par->minus = 1;
|
|
++i;
|
|
}
|
|
if (s[i] >= '0' && s[i] <= '9')
|
|
par->width = ft_atoi(&s[i]);
|
|
while (s[i] >= '0' && s[i] <= '9')
|
|
++i;
|
|
if (s[i] == '*')
|
|
{
|
|
par->width = va_arg(args, int);
|
|
++i;
|
|
}
|
|
if (par->width < 0)
|
|
{
|
|
par->width *= -1;
|
|
par->minus = 1;
|
|
}
|
|
i = ft_parse_para2(s, i, par, args);
|
|
return (i);
|
|
}
|