-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_next_line.c
More file actions
executable file
·96 lines (88 loc) · 2.4 KB
/
Copy pathget_next_line.c
File metadata and controls
executable file
·96 lines (88 loc) · 2.4 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nrepak <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/12/22 19:26:30 by nrepak #+# #+# */
/* Updated: 2017/12/28 17:28:22 by nrepak ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
static int ft_put_in_line(char *tmp, char **line)
{
char *p;
int a;
if (!(p = ft_strchr(tmp, '\n')))
{
if (ft_strlen(tmp) > 0)
{
*line = ft_strdup(tmp);
free(tmp);
return (1);
}
free(tmp);
return (0);
}
a = p - tmp;
*line = ft_strsub(tmp, 0, a);
free(tmp);
return (1);
}
static void ft_check(t_list **list, char **tmp)
{
char *p;
if (!(p = ft_strchr((*list)->content, '\n')))
*tmp = ft_strnew(0);
else
{
*tmp = ft_strdup(p + 1);
*p = ' ';
}
}
static void ft_check_list(t_list **head, t_list **list, char **tmp, int fd)
{
if (!(*head))
{
*tmp = ft_strnew(BUFF_SIZE);
*head = ft_lstnew(*tmp, BUFF_SIZE + 1);
(*head)->content_size = fd;
free(*tmp);
}
*list = *head;
while ((int)(*list)->content_size != fd && (*list)->next)
*list = (*list)->next;
if ((int)(*list)->content_size != fd)
{
*tmp = ft_strnew(BUFF_SIZE);
(*list)->next = ft_lstnew(*tmp, BUFF_SIZE + 1);
*list = (*list)->next;
(*list)->content_size = fd;
free(*tmp);
}
}
int get_next_line(const int fd, char **line)
{
int res;
char *tmp;
char *clean;
t_list *list;
static t_list *head;
if (fd < 0 || !line)
return (-1);
*line = NULL;
ft_check_list(&head, &list, &tmp, fd);
ft_check(&list, &tmp);
while (!ft_strchr(tmp, '\n') &&
(res = read(list->content_size, list->content, BUFF_SIZE)) > 0)
{
((char *)(list->content))[res] = '\0';
clean = tmp;
tmp = ft_strjoin(tmp, list->content);
free(clean);
}
if (res == -1)
return (-1);
return (ft_put_in_line(tmp, line));
}