/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   pf_u.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: bchanot <bchanot@students.42.fr>           +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2016/02/27 01:55:55 by bchanot           #+#    #+#             */
/*   Updated: 2018/10/12 02:46:37 by bchanot          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "libftprintf.h"

static int	pf_width_u(unsigned int nb, t_inf inf, int len)
{
	if (inf.prec > (int)ft_nbrulen(nb))
	{
		if (len < inf.width - inf.prec)
			return (true);
	}
	else if (len < inf.width - (int)ft_nbrulen(nb))
		return (true);
	return (false);
}

int			pf_u(va_list ap, t_inf inf)
{
	unsigned int	nb;
	size_t			cpt;
	int				len;

	if (inf.l || inf.ll || inf.z || inf.j)
		return (pf_u_up(ap, inf));
	if (inf.h || inf.hh)
		nb = inf.hh ? (unsigned char)va_arg(ap, unsigned int) :
			(unsigned short)va_arg(ap, unsigned int);
	else
		nb = va_arg(ap, unsigned int);
	len = 0;
	if (inf.width > 0 && inf.min == 0)
		while (pf_width_u(nb, inf, len) && ++len > 0)
			ft_putchar_fd((inf.zero == 1 && inf.prec == 0 ? '0' : ' '), inf.fd);
	cpt = -1;
	while (++cpt < inf.prec - ft_nbrulen(nb) && ++len > 0)
		ft_putchar_fd('0', inf.fd);
	len += pf_putunbr(nb, inf.fd);
	if (inf.width > 0 && inf.min == 1)
		while (len < inf.width && ++len > 0)
			ft_putchar_fd(' ', inf.fd);
	return (len);
}
