/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_cases_mandatory_one.c                           :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: thrieg <thrieg@student.42mulhouse.fr>      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/10/22 18:52:37 by thrieg            #+#    #+#             */
/*   Updated: 2025/02/16 19:07:52 by thrieg           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "../libft.h"
#include "ft_printf.h"

int	ft_case_c(t_vector *vec, va_list *args)
{
	if (!ft_vector_pushback(vec, (char)va_arg(*args, int)))
		return (0);
	return (1);
}

int	ft_case_percent(t_vector *vec)
{
	if (!ft_vector_pushback(vec, '%'))
		return (0);
	return (1);
}

int	ft_case_s(t_vector *vec, va_list *args)
{
	char	*str_to_cat;

	str_to_cat = va_arg(*args, char *);
	if (!str_to_cat)
	{
		if (!ft_vector_concat(vec, "(null)", 6))
			return (free(str_to_cat), 0);
	}
	else
	{
		str_to_cat = ft_strdup(str_to_cat);
		if (!ft_vector_concat(vec, str_to_cat, ft_strlen(str_to_cat)))
			return (free(str_to_cat), 0);
		free(str_to_cat);
	}
	return (1);
}

int	ft_case_p(t_vector *vec, va_list *args)
{
	char	*str_to_cat;
	void	*arg;
	size_t	index;

	arg = va_arg(*args, void *);
	if (!arg)
	{
		if (!ft_vector_concat(vec, "(nil)", 5))
			return (0);
		return (1);
	}
	if (!ft_vector_concat(vec, "0x", 2))
		return (0);
	str_to_cat = ft_addr_to_strhex(arg);
	if (!str_to_cat)
		return (0);
	index = ft_strnonchr(str_to_cat, '0');
	if (!ft_vector_concat(vec, str_to_cat + index,
			ft_strlen(str_to_cat) - index))
		return (free(str_to_cat), 0);
	free(str_to_cat);
	return (1);
}

int	ft_case_u(t_vector *vec, va_list *args)
{
	char	*str_to_cat;

	str_to_cat = ft_utoa(va_arg(*args, unsigned int));
	if (!ft_vector_concat(vec, str_to_cat, ft_strlen(str_to_cat)))
		return (free(str_to_cat), 0);
	free(str_to_cat);
	return (1);
}
