/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   pf_x_up.c                                          :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: bchanot <bchanot@students.42.fr>           +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2016/02/27 01:57:47 by bchanot           #+#    #+#             */
/*   Updated: 2017/04/15 00:43:55 by bchanot          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "libftprintf.h"

static int	pf_width_xl(long long unsigned int nb, t_inf *inf, int len)
{
	if (inf->prec > pf_hexulen(nb))
	{
		if (len < inf->width - (inf->prec + inf->di * 2))
			return (true);
	}
	else if (len < inf->width - (pf_hexulen(nb) + inf->di * 2))
		return (true);
	return (false);
}

static int	pf_width_x(unsigned int nb, t_inf inf, int len)
{
	if (inf.prec > pf_hexlen(nb))
	{
		if (len < inf.width - (inf.prec + inf.di * 2))
			return (true);
	}
	else if (len < inf.width - (pf_hexlen(nb) + inf.di * 2))
		return (true);
	return (false);
}

static int	pf_x_uplong(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->di == 1 && nb != 0 && inf->zero && !inf->prec)
	{
		inf->di = 0;
		len += pf_putstr("0X", inf->fd);
	}
	if (inf->width > 0 && inf->min == 0)
		while (pf_width_xl(nb, inf, len) && len++ >= 0)
			ft_putchar_fd((inf->zero && !inf->prec ? '0' : ' '), inf->fd);
	if (inf->di == 1 && nb != 0)
		len += pf_putstr("0X", inf->fd);
	cpt = -1;
	while (++cpt < inf->prec - pf_hexulen(nb) && len++ >= 0)
		ft_putchar_fd('0', inf->fd);
	return (pf_x_uplong2(len, inf, nb));
}

static int	pf_x_up2(unsigned int nb, t_inf inf, int len)
{
	len += pf_puthexm(nb, inf.fd);
	if (inf.width > 0 && inf.min == 1)
		while (len < inf.width && len++ >= 0)
			ft_putchar_fd(' ', inf.fd);
	return (len);
}

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

	if (inf.z || inf.l || inf.ll || inf.j)
		return (pf_x_uplong(ap, &inf));
	nb = inf.hh ? (unsigned char)va_arg(ap, unsigned int) :
		va_arg(ap, unsigned int);
	len = 0;
	if (inf.di == 1 && nb != 0 && inf.zero && !inf.prec)
	{
		inf.di = 0;
		len += pf_putstr("0X", inf.fd);
	}
	if (inf.width > 0 && inf.min == 0)
		while (pf_width_x(nb, inf, len) && len++ >= 0)
			ft_putchar_fd((inf.zero && !inf.prec ? '0' : ' '), inf.fd);
	if (inf.di == 1 && nb != 0)
		len += pf_putstr("0X", inf.fd);
	cpt = -1;
	while (++cpt < inf.prec - pf_hexlen(nb) && len++ >= 0)
		ft_putchar_fd('0', inf.fd);
	return (pf_x_up2(nb, inf, len));
}
