-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils_bonus.c
136 lines (125 loc) · 2.84 KB
/
get_next_line_utils_bonus.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: acaceres <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/10 05:24:19 by acaceres #+# #+# */
/* Updated: 2023/09/11 06:16:10 by acaceres ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
void ft_lstclear(t_list **lst, void (*del)(void*))
{
t_list *current;
t_list *link;
if (!lst || !del)
return ;
current = *lst;
link = 0;
while (current)
{
link = current->next;
del(current->content);
free(current);
current = link;
}
*lst = NULL;
}
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *tmp;
t_list *tail;
tail = NULL;
if (!*lst)
{
*lst = new;
return ;
}
tmp = *lst;
while (tmp->next)
tmp = tmp->next;
tail = tmp;
tail->next = new;
}
t_list *create_node(char *content)
{
t_list *node;
int i;
i = 0;
if (!content)
return (NULL);
node = malloc(sizeof(t_list));
if (!node)
return (NULL);
while (content[i])
i++;
node->content = malloc(sizeof(char) * (i + 1));
if (!node->content)
return (free(node), NULL);
i = -1;
node->nl = -1;
node->len = -1;
while (content[++i])
{
if (node->nl == -1 && content[i] == '\n')
node->nl = i;
node->content[i] = content[i];
}
node->content[i] = '\0';
return (node->len = i, node->next = NULL, node);
}
char *ft_strjoin_gnl(char const *s1, char const *s2)
{
char *new_str;
int i;
int s1_len;
int s2_len;
i = 0;
s1_len = 0;
s2_len = 0;
if (!s2)
return (0);
if (!s1)
s1 = "";
while (s1[s1_len])
s1_len++;
while (s2[s2_len])
s2_len++;
new_str = malloc(((s1_len + s2_len) + 1) * sizeof(char));
if (!new_str)
return (0);
while (*s1)
new_str[i++] = *s1++;
while (*s2)
new_str[i++] = *s2++;
return (new_str[i] = '\0', new_str);
}
char *buff_size_1(int fd, char *line, char *tmp)
{
char *buff;
int _r;
_r = 1;
buff = malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!buff)
return (NULL);
while (_r > 0)
{
_r = read(fd, buff, BUFFER_SIZE);
if (_r == -1)
return (free(buff), NULL);
buff[_r] = '\0';
tmp = line;
line = ft_strjoin_gnl(tmp, buff);
if (!line)
return (free(buff), NULL);
free(tmp);
if (buff[0] == '\n')
break ;
}
free(buff);
if (line[0] == '\0')
return (free(line), NULL);
return (line);
}