-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_next_line_utils.c
More file actions
88 lines (78 loc) · 1.94 KB
/
get_next_line_utils.c
File metadata and controls
88 lines (78 loc) · 1.94 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lrocca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/18 16:02:29 by lrocca #+# #+# */
/* Updated: 2021/01/22 18:11:44 by lrocca ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t ft_strlen(const char *s)
{
size_t len;
if (!s)
return (0);
len = 0;
while (*s++)
len++;
return (len);
}
int ft_realloc(char **line, char *buf, size_t len)
{
char *new;
size_t i;
i = 0;
while (buf[i] && buf[i] != '\n')
i++;
if (!(new = malloc(len + i + 1)))
return (0);
i = 0;
while (i < len)
{
new[i] = (*line)[i];
++i;
}
new[i] = '\0';
free(*line);
*line = new;
return (1);
}
t_buf *ft_lstnew(int fd)
{
t_buf *new;
if (!(new = malloc(sizeof(t_buf))))
return (NULL);
new->fd = fd;
new->next = NULL;
new->buffer[0] = '\0';
return (new);
}
t_buf *ft_findbuf(int fd, t_buf *curr)
{
while (curr->fd != fd && curr->next)
curr = curr->next;
if (curr->fd == fd)
return (curr);
return (curr->next = ft_lstnew(fd));
}
void ft_cleanbuf(t_buf **buffer, int fd)
{
t_buf *curr;
t_buf *tmp;
curr = *buffer;
if (curr->fd == fd)
{
tmp = curr->next;
free(curr);
*buffer = tmp;
return ;
}
while (curr->next->fd != fd)
curr = curr->next;
tmp = ft_findbuf(fd, *buffer);
curr->next = tmp->next;
free(tmp);
}