-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_next_line_utils.c
More file actions
131 lines (120 loc) · 2.79 KB
/
get_next_line_utils.c
File metadata and controls
131 lines (120 loc) · 2.79 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edesaint <edesaint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/12 17:43:50 by edesaint #+# #+# */
/* Updated: 2023/01/17 15:27:29 by edesaint ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
t_list *ft_lstlast(t_list *lst)
{
t_list *current;
current = lst;
while (current && current->next)
current = current->next;
return (current);
}
char *generate_line(t_list *lst)
{
char *line;
int i;
int len;
i = 0;
len = 0;
if (!lst)
return (NULL);
while (lst)
{
while (lst->content[i] && lst->content[i] != '\n')
i++;
if (lst->content[i] && lst->content[i] == '\n')
i++;
len += i;
i = 0;
lst = lst->next;
}
line = (char *) malloc(sizeof(char) * (len + 1));
return (line);
}
char *extract_line(t_list *lst)
{
char *line;
int i;
int len;
line = generate_line(lst);
if (!line)
return (NULL);
i = 0;
len = 0;
while (lst)
{
while (lst->content[i] && lst->content[i] != '\n')
{
line[len + i] = lst->content[i];
i++;
}
if (lst->content[i] && lst->content[i] == '\n')
line[len + i++] = '\n';
len += i;
i = 0;
lst = lst->next;
}
line[len] = '\0';
return (line);
}
void free_stash(t_list **lst, int type_clean)
{
t_list *current;
t_list *next;
current = *lst;
if (type_clean == 1)
{
while (current != NULL && current->next != NULL)
{
free(current->content);
current->content = NULL;
next = current->next;
free(current);
current = NULL;
current = next;
}
*lst = current;
}
if (type_clean == 2)
{
free(current->content);
current->content = NULL;
free(current);
current = NULL;
}
*lst = current;
}
void clean_stash(t_list **lst)
{
char *buf;
int len_buf;
int i;
int l;
i = 0;
l = 0;
free_stash(lst, 1);
while ((*lst)->content[i] && (*lst)->content[i] != '\n')
i++;
if ((*lst)->content[i] && (*lst)->content[i] == '\n')
i++;
len_buf = ft_strlen((*lst)->content);
buf = (char *) malloc(sizeof(char) * (len_buf - i + 1));
len_buf -= i;
if (!buf)
return ;
while ((*lst)->content[i])
buf[l++] = (*lst)->content[i++];
buf[l] = '\0';
free_stash(lst, 2);
add_to_stash(lst, buf, len_buf);
free(buf);
}