-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
76 lines (70 loc) · 2.09 KB
/
get_next_line.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kacoulib <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/27 23:07:51 by kacoulib #+# #+# */
/* Updated: 2016/12/29 04:54:38 by kacoulib ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include "libft/libft.h"
#include "stdio.h" // to remove
t_list *ft_get_or_create(t_list *lst, void const *content, size_t id)
{
while (lst->next && lst->content_size != id)
{
lst = lst->next;
}
//printf("(%zu)",id);
if (lst->content_size == id)
return (lst);
lst->next = ft_lstnew(content, id);
return (lst->next);
}
char *ft_get_first_char(char buff[])
{
char *tmp;
tmp = ft_strdup(buff);
while (*tmp && *tmp == '\n')
tmp++;
return (tmp);
}
int get_next_line(const int fd, char **line)
{
static int i;
int stop;
int end;
static t_list list;
char buff[BUFF_SIZE + 1];
char *r;
i = 0;
stop = 1;
end = 0;
list = (t_list){(void*)NULL, (size_t)fd, NULL};
//printf("%p\t %zu\n", &lst->content_size, lst->content_size);
r = ft_strnew(0);
while (stop && (i = read(fd, buff, BUFF_SIZE)) > 0)
{
if (ft_indexof(buff, '\0'))
end = 1;
buff[i] = '\0';
if (ft_indexof(buff, '\n') >= 0)
{
buff[ft_indexof(buff, '\n')] = '\0';
list.content = ft_strtrim(buff);
if (list.content)
r = ft_strjoin(r, list.content);
else
r = ft_strjoin(buff, r);
stop = 0;
}
else
r = ft_strjoin(r, buff);
}
line[0] = ft_strdup(r);
free(r);
return ((end) ? 1 : 0);
}