-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
51 lines (44 loc) · 1.04 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
#include "push_swap.h"
//Hata kontrolü
void error_handle(t_stack *stacks)
{
write(2, "Error\n", 6);
all_free(stacks);
exit(1);
}
//Integer için sınır değer kontrolü
void result_checker(long result, t_stack *stacks)
{
if (result > 2147483647 || result < -2147483648)
error_handle(stacks);
}
//Atoi, double ile return ediliyor, sınır değerleri için ve int'i
//aldıktan sonra sayı dışı bir şey varsa diye kontrol sağlıyor
//Hata olursa stack'e yazımda sorun olacağı için stack hafızasını freeliyor.
long ft_atol_st(const char *str, t_stack *stacks)
{
int i;
int n;
long result;
i = 0;
n = 1;
while ((str[i] <= 13 && str[i] >= 9) || str[i] == 32)
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
n *= -1;
i++;
}
if (!str[i])
error_handle(stacks);
result = 0;
while (str[i] <= '9' && str[i] >= '0')
{
result = result * 10 + str[i++] - 48;
result_checker(result, stacks);
}
if (!(str[i] <= '9' && str[i] >= '0') && str[i])
error_handle(stacks);
return (result * n);
}