-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities_1.c
82 lines (73 loc) · 1.95 KB
/
utilities_1.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utilities.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: natamazy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/13 12:17:34 by natamazy #+# #+# */
/* Updated: 2024/02/21 19:02:04 by natamazy ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_list *ft_lstnew(long long nbr, t_list *prev)
{
t_list *new_node;
new_node = (t_list *) malloc(sizeof(t_list));
if (!new_node)
return (NULL);
new_node->nbr = nbr;
new_node->next = NULL;
new_node->prev = prev;
return (new_node);
}
void ft_putstr(char *s, int fd)
{
if (!s)
{
write(fd, "(null)", 6);
return ;
}
write(fd, s, ft_strlen(s));
}
size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
int is_digit_space_sign(int c)
{
if ((c >= 48 && c <= 57) || (c >= 9 && c <= 13)
|| c == 32 || c == '-' || c == '+')
return (1);
return (0);
}
long long ft_atoi(const char *str, int i, int j, int flag)
{
long long sign;
long long res;
sign = 1;
res = 0;
if (str[i] == '-')
{
sign = -1;
i++;
}
else if (str[i] == '+')
flag = ++i;
while (str[i] != '\0' && str[i] == '0')
i++;
while (str[i] != '\0' && ft_isdigit(str[i]))
{
res *= 10;
res += str[i] - '0';
i++;
j++;
}
if (str[i] != '\0' || j > 10 || (res == 0 && (sign == -1 || flag > 0)))
return (LONG_MAX);
return (res * sign);
}