/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_case_s_bonus.c                                  :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: thrieg <thrieg@student.42mulhouse.fr>      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/10/25 17:03:40 by thrieg            #+#    #+#             */
/*   Updated: 2025/02/16 19:07:48 by thrieg           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

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

int	padding_if_needed_before(
		t_arglist *arglist,
		char caller,
		size_t len,
		t_vector *vec);
int	padding_if_needed_after(
		t_arglist *arglist,
		char caller,
		size_t len,
		t_vector *vec);

static int	ft_s_sub_bonus(t_vector *vec, char *str_to_cat, t_arglist *arglist)
{
	size_t	size;

	if (!str_to_cat)
	{
		size = 6;
		if ((int)size > arglist->precision && (arglist->precision >= 0))
			size = 0;
		if (padding_if_needed_before(arglist, 's', size, vec) < 0)
			return (0);
		if (!ft_vector_concat(vec, "(null)", size))
			return (0);
	}
	else
	{
		size = ft_strlen(str_to_cat);
		if ((int)size > arglist->precision && (arglist->precision >= 0))
			size = arglist->precision;
		if (padding_if_needed_before(arglist, 's', size, vec) < 0)
			return (0);
		if (!ft_vector_concat(vec, str_to_cat, size))
			return (0);
	}
	if (padding_if_needed_after(arglist, 's', size, vec) < 0)
		return (0);
	return (1);
}

//flag 0 has undefined behavior with c so the minimum width has to be spaces
int	ft_case_s_bonus(t_vector *vec, va_list *args, t_arglist *arglist)
{
	char	*str_to_cat;

	str_to_cat = va_arg(*args, char *);
	return (ft_s_sub_bonus(vec, str_to_cat, arglist));
}
