-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils.c
118 lines (107 loc) · 2.26 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fmorenil <fmorenil@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/22 12:42:41 by fmorenil #+# #+# */
/* Updated: 2024/05/15 16:08:37 by fmorenil ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
t_list *ft_lstlast(t_list *lst)
{
t_list *last;
last = lst;
while (last && last->next)
last = last->next;
return (last);
}
int ft_newline(t_list *list)
{
int i;
if (list == NULL)
return (0);
while (list)
{
i = 0;
while (list->str_buf[i] && i < BUFFER_SIZE)
{
if (list->str_buf[i] == '\n')
return (1);
i++;
}
list = list->next;
}
return (0);
}
void generate_line(t_list *list, char *next_line)
{
int i;
int len;
if (list == NULL)
return ;
len = 0;
while (list)
{
i = 0;
while (list->str_buf[i])
{
if (list->str_buf[i] == '\n')
{
next_line[len++] = '\n';
next_line[len] = '\0';
return ;
}
next_line[len++] = list->str_buf[i++];
}
list = list->next;
}
next_line[len] = '\0';
}
int ft_strlen_to_nl(t_list *list)
{
int i;
int len;
if (list == NULL)
return (0);
len = 0;
while (list)
{
i = 0;
while (list->str_buf[i] && i < BUFFER_SIZE)
{
if (list->str_buf[i] == '\n')
{
len++;
return (len);
}
i++;
len++;
}
list = list->next;
}
return (len);
}
void free_list(t_list **list, t_list *clean_node, char *buf)
{
t_list *next;
if (*list == NULL)
return ;
while (*list)
{
next = (*list)->next;
free((*list)->str_buf);
free(*list);
*list = next;
}
*list = NULL;
if (clean_node->str_buf[0])
*list = clean_node;
else
{
free(buf);
free(clean_node);
}
}