-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
105 lines (95 loc) · 2.74 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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ftaffore <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/03/31 11:38:05 by ftaffore #+# #+# */
/* Updated: 2015/04/08 15:27:45 by ftaffore ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "libft.h"
#include "get_next_line.h"
static t_chain *create_chain(t_chain *all_chains, int fd)
{
t_chain *new_elem;
if ((new_elem = ft_memalloc(sizeof(*new_elem))) == NULL)
return (NULL);
new_elem->i = 0;
new_elem->n = 0;
new_elem->len = 0;
ft_bzero(new_elem->buff, BUFF_SIZE + 1);
new_elem->fd = fd;
new_elem->next = all_chains;
return (new_elem);
}
static t_chain *get_chain(int fd)
{
static t_chain *all_chains = NULL;
t_chain *ptr;
ptr = all_chains;
while (ptr != NULL && ptr->fd != fd)
ptr = ptr->next;
if (ptr == NULL)
{
all_chains = create_chain(all_chains, fd);
return (all_chains);
}
return (ptr);
}
static int read_next(t_chain *chain)
{
if (chain->i > 0 && chain->i < chain->len)
return (1);
chain->i = 0;
chain->n = 0;
ft_bzero(chain->buff, BUFF_SIZE + 1);
chain->len = read(chain->fd, chain->buff, BUFF_SIZE + 1);
if (chain->len < 0)
return (-1);
else if (chain->len == 0)
return (0);
chain->buff[chain->len] = '\0';
return (1);
}
static int my_strconcat(char **line, t_chain *chain)
{
char *res;
res = ft_memalloc(ft_strlen(*line) + chain->n - chain->i + 1);
if (res == NULL)
return (-1);
if (*line != NULL)
{
res = ft_strcat(res, *line);
free(*line);
*line = NULL;
}
res = ft_strncat(res, &(chain->buff[chain->i]), chain->n - chain->i);
*line = res;
chain->n += 1;
chain->i = chain->n;
return (0);
}
int get_next_line(int fd, char **line)
{
t_chain *c;
if (fd < 0 || line == NULL || (c = get_chain(fd)) == NULL)
return (-1);
*line = NULL;
while (read_next(c) > 0)
{
c->n = ft_strchr_index((char *)&(c->buff[c->i]), (int)'\n');
c->n += c->i;
if (my_strconcat(line, c) < 0)
return (-1);
if (c->n < c->len || \
(c->n == c->len && c->buff[c->n - 1] == '\n'))
return (1);
}
if (*line != NULL)
return (1);
return (c->len);
}