-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils.c
108 lines (100 loc) · 2.66 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rjobert <rjobert@student.42barcelo> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/05 18:30:12 by rjobert #+# #+# */
/* Updated: 2023/06/13 21:52:29 by rjobert ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
void free_chain(t_list *list)
{
t_list *temp;
if (!list)
return ;
while (list)
{
temp = list->next;
free(list->buff);
free(list);
list = temp;
}
list = NULL;
}
int ft_search_nl(t_list *node)
{
int i;
i = 0;
if (!node)
return (0);
while (node->buff[i])
{
if (node->buff[i] == '\n')
return (1);
i++;
}
return (0);
}
//1 - we run through the entire linked list : as long as there are nodes,
//we read each node content char by char and count how many chars need
//to be read until we find \n (included)
//2 - we malloc this bytes quantity (1 char = 1 byte)
void measure_n_create(t_list **byte_buff, char **line, int i)
{
int j;
t_list *byte_list;
byte_list = *byte_buff;
if (!byte_list)
return ;
while (byte_list)
{
j = 0;
while (byte_list->buff[j])
{
if (byte_list->buff[j] == '\n')
{
i++;
break ;
}
i++;
j++;
}
byte_list = byte_list->next;
}
*line = malloc((i + 1) * sizeof(char));
if (*line == NULL)
return ;
(*line)[i] = '\0';
}
//1 - we measure the length of the node in char / bytes
//2 - we measure when is the \n if there is one
//3 - allocate corresponding memory for the buffer in
//handover node that will contain the remaining characer :
//the purpose is to pass the remaining char
//in static t_list for the next line
void ft_passover(t_list *byte_list, t_list **handover_node, int i, int j)
{
while (byte_list->buff[j])
j++;
while (byte_list->buff[i] && byte_list->buff[i] != '\n')
i++;
if (byte_list->buff[i] == '\n')
i++;
(*handover_node)->buff = malloc ((j - i) + 1);
if (!(*handover_node)->buff)
{
free(*handover_node);
return ;
}
j = 0;
while (byte_list->buff[i])
{
(*handover_node)->buff[j] = byte_list->buff[i];
i++;
j++;
}
(*handover_node)->buff[j] = '\0';
}