Files
ft_printf/libftprintf/ft_vprintf_decimal.c
2022-04-26 17:53:39 +02:00

68 lines
1.8 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_vprintf_decimal.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbaconni@student.42lausanne.ch +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/20 11:55:02 by gbaconni #+# #+# */
/* Updated: 2022/04/26 17:48:37 by gbaconni ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
int ft_vprintf_decimal(const char *format, va_list ap)
{
int ret;
unsigned int u;
int d;
char *s;
char c;
ret = 0;
c = *(format + ft_eoflags(format));
if (c == 'u')
{
u = va_arg(ap, unsigned int);
s = ft_ltoa_base((long) u, "0123456789");
}
else
{
d = va_arg(ap, int);
s = ft_itoa(d);
}
const char *f;
int flags;
int precision;
int len;
precision = 0;
flags = ft_vprintf_flags_bonus(format, ap);
f = format;
while (*f != '\0')
{
if (*f >= '0' && *f <= '9')
precision = precision * 10 + *f - '0';
if (*f == 'c' || *f == 's' || *f == 'p' || *f == 'd' || *f == 'i' || *f == 'u' || *f == 'x' || *f == 'X' || *f == '%')
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);
free(s);
return (ret);
}