Push Swap
This commit is contained in:
80
utils.c
Normal file
80
utils.c
Normal file
@@ -0,0 +1,80 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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);
|
||||
}
|
||||
Reference in New Issue
Block a user