/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_padding_bonus.c                                 :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: thrieg <thrieg@student.42mulhouse.fr>      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/10/27 16:54:14 by thrieg            #+#    #+#             */
/*   Updated: 2025/02/16 19:07:59 by thrieg           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

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

//len is the len of what's already printed
//returns the number of characters writen or -1 if an error occurs.
int	padding_if_needed_before(t_arglist *arglist,
char caller,
size_t len,
t_vector *vec)
{
	char	padding_char;
	int		i;
	int		padding_len;

	padding_len = arglist->width_mini - (int)len;
	if (padding_len <= 0)
		return (0);
	if (arglist->flag_justified_left)
		return (0);
	if ((arglist->flag_zero == 1) && caller != 'c' && caller != 's'
		&& caller != 'p' && (arglist->precision < 0))
		padding_char = '0';
	else
		padding_char = ' ';
	i = 0;
	while (i++ < padding_len)
		if (!ft_vector_pushback(vec, padding_char))
			return (-1);
	return (padding_len);
}

//len is the len of what's already printed
//returns the number of characters writen or -1 if an error occurs.
int	padding_if_needed_after(t_arglist *arglist,
char caller,
size_t len,
t_vector *vec)
{
	int		i;
	int		padding_len;

	(void)caller;
	padding_len = arglist->width_mini - (int)len;
	if (padding_len <= 0)
		return (0);
	if (!(arglist->flag_justified_left))
		return (0);
	i = 0;
	while (i++ < padding_len)
		if (!ft_vector_pushback(vec, ' '))
			return (-1);
	return (padding_len);
}
