-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
114 lines (105 loc) · 2.43 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kokaimov <kokaimov@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/24 17:40:17 by kokaimov #+# #+# */
/* Updated: 2023/11/24 17:40:17 by kokaimov ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int line_len(t_list *list)
{
int i;
int len;
if (!list)
return (0);
len = 0;
while (list)
{
i = 0;
while (((char *)list->content)[i])
{
if (((char *)list->content)[i] == '\n')
{
len++;
return (len);
}
i++;
len++;
}
list = list->next;
}
return (len);
}
void free_list(t_list **list, t_list *clear_node, char *buf)
{
t_list *temp;
if (!*list)
return ;
while (*list)
{
temp = (*list)->next;
free((*list)->content);
free(*list);
*list = temp;
}
*list = NULL;
if (((char *)clear_node->content)[0])
*list = clear_node;
else
{
free(buf);
free(clear_node);
}
}
void clear_list(t_list **list)
{
t_list *last_node;
t_list *clear_node;
int i;
int j;
char *buf;
buf = malloc((BUFFER_SIZE + 1) * sizeof(char));
clear_node = malloc(sizeof(t_list));
if (!buf || !clear_node)
return ;
last_node = ft_lstlast(*list);
i = 0;
j = 0;
while (((char *)last_node->content)[i]
&& ((char *)last_node->content)[i] != '\n')
i++;
while (((char *)last_node->content)[i] && ((char *)last_node->content)[++i])
buf[j++] = ((char *)last_node->content)[i];
buf[j] = '\0';
clear_node->content = buf;
clear_node->next = NULL;
free_list(list, clear_node, buf);
}
void str_copy(t_list *list, char *str)
{
int i;
int j;
if (!list)
return ;
j = 0;
while (list)
{
i = 0;
while (((char *)list->content)[i])
{
if (((char *)list->content)[i] == '\n')
{
str[j++] = '\n';
str[j] = '\0';
return ;
}
str[j++] = ((char *)list->content)[i++];
}
list = list->next;
}
str[j] = '\0';
}