-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_next_line_utils_bonus.c
More file actions
95 lines (86 loc) · 2.13 KB
/
get_next_line_utils_bonus.c
File metadata and controls
95 lines (86 loc) · 2.13 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vbronov <vbronov@student.42lausanne.ch> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/17 15:45:15 by vbronov #+# #+# */
/* Updated: 2024/11/17 16:19:02 by vbronov ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
t_lst_node *create_node(char **buf)
{
t_lst_node *node;
node = (t_lst_node *)malloc(sizeof(t_lst_node));
if (!node)
{
free(*buf);
*buf = NULL;
return (NULL);
}
node->buf = *buf;
node->next = NULL;
return (node);
}
ssize_t find_new_line(t_list *list)
{
int i;
if (!list || !list->tail)
return (-1);
i = 0;
while (list->tail->buf[i] != '\0')
{
if (list->tail->buf[i] == '\n')
return (i);
i++;
}
return (-1);
}
static void free_helper(t_list **list)
{
if ((*list)->head)
{
free((*list)->head->buf);
(*list)->head->buf = NULL;
free((*list)->head);
(*list)->head = NULL;
}
(*list)->tail = NULL;
(*list)->len = 0;
free(*list);
*list = NULL;
}
char *free_list(t_list **list, int free_all)
{
t_lst_node *temp;
if (!*list)
return (NULL);
while ((*list)->head != (*list)->tail)
{
temp = (*list)->head;
(*list)->head = (*list)->head->next;
if (temp->buf)
free(temp->buf);
temp->buf = NULL;
temp->next = NULL;
free(temp);
}
if (free_all)
free_helper(list);
return (NULL);
}
t_list *init_list(t_list **list)
{
if (!*list)
{
*list = (t_list *)malloc(sizeof(t_list));
if (!*list)
return (NULL);
(*list)->head = NULL;
(*list)->tail = NULL;
(*list)->len = 0;
}
return (*list);
}