2022-04-13 08:01:46 +02:00
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* */
|
|
|
|
|
/* ::: :::::::: */
|
|
|
|
|
/* ft_vprintf_decimal.c :+: :+: :+: */
|
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
|
/* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */
|
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
|
/* Created: 2022/01/20 11:55:02 by gbaconni #+# #+# */
|
2022-04-29 08:46:53 +02:00
|
|
|
/* Updated: 2022/04/29 08:42:35 by gbaconni ### lausanne.ch */
|
2022-04-13 08:01:46 +02:00
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
|
|
#include "libftprintf.h"
|
|
|
|
|
|
|
|
|
|
int ft_vprintf_decimal(const char *format, va_list ap)
|
|
|
|
|
{
|
2022-04-17 09:59:41 +02:00
|
|
|
int ret;
|
|
|
|
|
unsigned int u;
|
|
|
|
|
int d;
|
|
|
|
|
char *s;
|
2022-04-26 17:53:39 +02:00
|
|
|
char c;
|
2022-04-13 08:01:46 +02:00
|
|
|
|
2022-04-26 17:53:39 +02:00
|
|
|
ret = 0;
|
|
|
|
|
c = *(format + ft_eoflags(format));
|
|
|
|
|
if (c == 'u')
|
2022-04-17 09:59:41 +02:00
|
|
|
{
|
|
|
|
|
u = va_arg(ap, unsigned int);
|
|
|
|
|
s = ft_ltoa_base((long) u, "0123456789");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
d = va_arg(ap, int);
|
|
|
|
|
s = ft_itoa(d);
|
|
|
|
|
}
|
2022-04-26 17:53:39 +02:00
|
|
|
|
|
|
|
|
const char *f;
|
|
|
|
|
int flags;
|
|
|
|
|
int precision;
|
|
|
|
|
int len;
|
|
|
|
|
precision = 0;
|
|
|
|
|
flags = ft_vprintf_flags_bonus(format, ap);
|
|
|
|
|
f = format;
|
|
|
|
|
while (*f != '\0')
|
|
|
|
|
{
|
2022-04-29 08:46:53 +02:00
|
|
|
if (ft_isdigit(*f))
|
2022-04-26 17:53:39 +02:00
|
|
|
precision = precision * 10 + *f - '0';
|
2022-04-29 08:46:53 +02:00
|
|
|
if (ft_strchr("cspdiuxX%", *f) != NULL)
|
2022-04-26 17:53:39 +02:00
|
|
|
break ;
|
|
|
|
|
f++;
|
|
|
|
|
}
|
|
|
|
|
len = 0;
|
|
|
|
|
if (precision > 0)
|
|
|
|
|
{
|
|
|
|
|
len = precision - ft_strlen(s);
|
|
|
|
|
while (len --> 0)
|
|
|
|
|
{
|
|
|
|
|
if (flags & F_ZERO)
|
|
|
|
|
ret += ft_putchar('0');
|
|
|
|
|
else
|
|
|
|
|
ret += ft_putchar(' ');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret += ft_puts(s);
|
2022-04-13 08:01:46 +02:00
|
|
|
free(s);
|
|
|
|
|
return (ret);
|
|
|
|
|
}
|