/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_ptr_case.c                                      :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: pbonilla <eirodeis.lepnj@gmail.com>        +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2021/03/11 12:54:20 by pbonilla          #+#    #+#             */
/*   Updated: 2021/03/21 20:53:18 by pbonilla         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "../includes/ft_printf.h"

int				zero_case(t_param *param, unsigned long long i)
{
	if (param->prec == 0 && i == 0)
		return (0);
	return (1);
}

char			*fill_s_ptr(unsigned long long j, int len, char *s_x,
t_param *param)
{
	char		*s;
	int			i;

	if (!(s = malloc(sizeof(char) * (len + 1))))
		return (NULL);
	s[len] = 0;
	ft_printf_memset(s, len, param);
	if (param->minus)
		len = ft_strlen(s_x) + 1 + zero_case(param, j);
	i = ft_strlen(s_x);
	while (--len >= 0 && --i >= 0)
	{
		s[len] = s_x[i];
		if (j == 0 && param->prec == 0)
		{
			s[len] = ' ';
			++len;
		}
	}
	s[len] = 'x';
	s[--len] = '0';
	free(s_x);
	return (s);
}

char			*ft_ptr_case(t_param *param, unsigned long long i)
{
	char		*s_x;
	char		*s_ul;
	int			len;

	s_ul = ft_u_itoa(i);
	s_x = ft_u_convert(s_ul, "0123456789", "0123456789abcdef");
	free(s_ul);
	len = ft_strlen(s_x) + 1 + zero_case(param, i);
	if (param->width > len)
		len = param->width;
	return (fill_s_ptr(i, len, s_x, param));
}