81 lines
1.7 KiB
C
81 lines
1.7 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* utils.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/08/09 20:27:06 by aortigos #+# #+# */
|
|
/* Updated: 2024/08/13 06:18:17 by aortigos ### ########.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 error_exit(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 neg;
|
|
int i;
|
|
|
|
nb = 0;
|
|
i = 0;
|
|
neg = 1;
|
|
if (str[i] == '+')
|
|
i++;
|
|
else if (str[i] == '-')
|
|
{
|
|
neg *= -1;
|
|
i++;
|
|
}
|
|
while (str[i] >= '0' && str[i] <= '9')
|
|
{
|
|
nb = (nb * 10) + (str[i] - '0');
|
|
i++;
|
|
}
|
|
return (nb * neg);
|
|
}
|
|
|
|
void ft_putstr(char *str)
|
|
{
|
|
int i;
|
|
|
|
i = 0;
|
|
while (str[i])
|
|
{
|
|
write(1, &str[i++], 1);
|
|
}
|
|
}
|
|
|
|
int abs(int nb)
|
|
{
|
|
if (nb < 0)
|
|
return (nb * -1);
|
|
return (nb);
|
|
}
|