-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
95 lines (80 loc) · 2.22 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abertran <abertran@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 15:37:38 by abertran #+# #+# */
/* Updated: 2023/02/23 15:38:40 by abertran ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
/* Frees each element in a given stack and sets the stack pointer to NULL.
???? porque lo pasamos como doble puntero*/
void free_stack(t_stack **stack)
{
t_stack *tmp;
if (!stack || !(*stack))
return ;
while (*stack)
{
tmp = (*stack)->next;
free(*stack);
*stack = tmp;
}
*stack = NULL;
}
/* Writes "Error\n" to the standard output after freeing stack a and b.
* Exits with standard error code 1. */
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);
}
/* Converts an alphanumeric string of characters into a long integer. */
long int ft_atoi(const char *str)
{
long int nb;
int isneg;
int i;
nb = 0;
isneg = 1;
i = 0;
if (str[i] == '+')
i++;
else if (str[i] == '-')
{
isneg *= -1;
i++;
}
while (str[i] >= '0' && str[i] <= '9')
{
nb = (nb * 10) + (str[i] - '0');
i++;
}
return (nb * isneg);
}
/* Prints a given string of characters to the standard output. */
void ft_putstr(char *str)
{
int i;
i = 0;
while (str[i])
{
write(1, &str[i], 1);
i++;
}
}
/* Returns the absolute value of a number,ç
which is the value without any sign consideration.*/
int abs(int nb)
{
if (nb < 0)
return (nb * -1);
return (nb);
}