-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_next_line_utils.c
91 lines (81 loc) · 2.02 KB
/
get_next_line_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yde-goes <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/30 22:12:17 by yde-goes #+# #+# */
/* Updated: 2022/07/10 00:41:34 by yde-goes ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
t_line *ft_lstnew(char *content)
{
t_line *new_node;
new_node = malloc(sizeof(*new_node));
if (!new_node)
return (NULL);
new_node->content = content;
new_node->length = 0;
new_node->next = NULL;
return (new_node);
}
t_line *ft_lstlast(t_line *lst)
{
if (!lst)
return (NULL);
while (lst->next != NULL)
{
lst = lst->next;
}
return (lst);
}
void ft_lstadd_back(t_line **lst, t_line *new)
{
t_line *temp;
if (!new)
return ;
if (!*lst)
{
*lst = new;
return ;
}
temp = ft_lstlast(*lst);
temp->next = new;
}
void ft_lstclear(t_line **lst, void (*del)(void *))
{
t_line *temp_lst;
if (!lst || !del)
return ;
while (*lst != NULL)
{
temp_lst = *lst;
*lst = (*lst)->next;
free(temp_lst->content);
free(temp_lst);
}
*lst = NULL;
}
void *ft_calloc(size_t nmemb, size_t size)
{
void *arr;
size_t alloc_size;
size_t i;
unsigned char *cast_s;
alloc_size = nmemb * size;
if (!alloc_size || alloc_size / nmemb != size)
return (NULL);
arr = malloc(alloc_size);
if (arr == NULL)
return (NULL);
i = 0;
cast_s = arr;
while (i < alloc_size)
{
cast_s[i] = '\0';
i++;
}
return (cast_s);
}