/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_printf.c                                        :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: aortigos <aortigos@student.42malaga.com>   #+#  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024-04-09 16:52:09 by aortigos          #+#    #+#             */
/*   Updated: 2024-04-09 16:52:09 by aortigos         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "ft_printf.h"

static int	ft_select_format(va_list args, const char str);

int	ft_printf(char const *str, ...)
{
	int		i;
	int		size;
	va_list	args;

	size = 0;
	i = 0;
	va_start(args, str);
	if (write(1, "", 0) == -1)
		return (-1);
	while (str[i])
	{
		if (str[i] == '%')
		{
			size += ft_select_format(args, str[i + 1]);
			i++;
		}
		else
		{
			size += ft_print_char(str[i]);
		}
		i++;
	}
	va_end(args);
	return (size);
}

static int	ft_select_format(va_list args, const char str)
{
	int	size;

	size = 0;
	if (str == 'c')
		size += ft_print_char(va_arg(args, int));
	else if (str == 's')
		size += ft_print_str(va_arg(args, char *));
	else if (str == 'd' || str == 'i')
		size += ft_print_digit(va_arg(args, int));
	else if (str == 'u')
		size += ft_print_unsigned(va_arg(args, int));
	else if (str == 'p')
		size += ft_print_pointer(va_arg(args, unsigned long long));
	else if (str == 'x' || str == 'X')
		size += ft_print_hexadecimal(va_arg(args, unsigned int), str);
	else
		size += ft_print_char(str);
	return (size);
}
