pushswap/utils.c
2023-12-12 17:44:13 +01:00

82 lines
1.7 KiB
C
Executable File

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/31 18:58:53 by fgras-ca #+# #+# */
/* Updated: 2023/07/31 19:12:30 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void free_stack(t_stack **stack)
{
t_stack *tmp;
if (!stack || !(*stack))
return ;
while (*stack)
{
tmp = (*stack)->next;
free(*stack);
*stack = tmp;
}
*stack = NULL;
}
void exit_error(t_stack **stack_a, t_stack **stack_b)
{
if (stack_a == NULL || *stack_a != NULL)
free_stack(stack_a);
if (stack_b == NULL || *stack_b != NULL)
free_stack(stack_b);
write(2, "Error\n", 6);
exit (1);
}
long int ft_atoi(const char *str)
{
long int nb;
int sign;
int i;
nb = 0;
sign = 1;
i = 0;
if (str[i] == '+')
i++;
else if (str[i] == '-')
{
sign *= -1;
i++;
}
while (is_digit(str[i]))
{
nb = (nb * 10) + (str[i] - '0');
i++;
}
return (nb * sign);
}
void ft_putstr(char *str)
{
int i;
i = 0;
while (str[i])
{
write(1, &str[i], 1);
i++;
}
}
int nb_abs(int nb)
{
if (nb < 0)
return (nb * -1);
return (nb);
}