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

#include "ft_printf.h"

int	ft_print_char(int character)
{
	write(1, &character, 1);
	return (1);
}

int	ft_print_str(char *str)
{
	int	size;

	size = 0;
	if (str == 0)
		return (ft_print_str("(null)"));
	while (str[size])
	{
		size += ft_print_char(str[size]);
	}
	return (size);
}

int	ft_print_digit(int number)
{
	int	size;

	size = 0;
	if (number == 0)
		size += ft_print_char('0');
	if (number == -2147483648)
	{
		size += ft_print_str("-2147483648");
		return (size);
	}
	if (number < 0)
	{
		size += ft_print_char('-');
		number = -number;
	}
	if (number > 0)
		size += ft_print_unsigned((unsigned int)number);
	return (size);
}

int	ft_print_unsigned(unsigned int number)
{
	int	size;

	size = 0;
	if (number == 0)
		size += ft_print_char('0');
	else
	{
		if (number / 10 != 0)
			ft_print_unsigned(number / 10);
		ft_print_char((number % 10) + '0');
		while (number > 0)
		{
			number /= 10;
			size++;
		}
	}
	return (size);
}
