/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   pf_u_up.c                                          :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: bchanot <bchanot@students.42.fr>           +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2016/02/27 03:07:31 by bchanot           #+#    #+#             */
/*   Updated: 2018/10/12 02:47:20 by bchanot          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "libftprintf.h"

static int	pf_width_u_up(long long 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_up(va_list ap, t_inf inf)
{
	long long unsigned int	nb;
	int						len;
	int						cpt;

	nb = va_arg(ap, long long unsigned int);
	len = 0;
	if (inf.width > 0 && inf.min == 0)
		while (pf_width_u_up(nb, inf, len) && len++ >= 0)
			ft_putchar_fd((inf.zero == 1 && inf.prec == 0 ? '0' : ' '), inf.fd);
	cpt = -1;
	while (++cpt < inf.prec - (int)ft_nbrulen(nb) && len++ >= 0)
		ft_putchar_fd('0', inf.fd);
	len += pf_putunbrlong(nb, inf.fd);
	if (inf.width > 0 && inf.min == 1)
		while (len < inf.width && len++ >= 0)
			ft_putchar_fd(' ', inf.fd);
	return (len);
}
