-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
139 lines (126 loc) · 3.47 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
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
137
138
139
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kyork <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/09/26 12:34:56 by kyork #+# #+# */
/* Updated: 2017/01/25 14:48:46 by kyork ### ########.fr */
/* */
/* ************************************************************************** */
#include <libft.h>
#include "get_next_line_private.h"
#include <stdlib.h>
#include <unistd.h>
#define GRSZ_A(s) ((s)->chars.item_count + BUFF_SIZE)
#define GRSZ_B(s) ((s)->chars.item_cap * 2)
#define GROW_SIZE(s) MAX(GRSZ_A(s), GRSZ_B(s))
t_gnl_fd *gnl_setup(t_gnlstate *t, const int fd)
{
t_gnl_fd *s;
if (t->fds.item_count >= t->fds.item_cap || t->fds.item_cap == 1)
if (0 != ft_ary_grow(&(t->fds), MAX(4UL, t->fds.item_cap * 2)))
return (NULL);
s = ft_ary_get(&(t->fds), t->fds.item_count++);
s->fd = fd;
s->nl_off = -1;
s->chars = ft_ary_create(sizeof(char));
if (!s->chars.ptr)
return (NULL);
return (s);
}
static void gnl_destroy(t_gnlstate *t, const int fd)
{
size_t i;
t_gnl_fd *s;
i = -1ULL;
s = NULL;
while (++i < t->fds.item_count)
if ((s = (t_gnl_fd*)ft_ary_get(&(t->fds), i))->fd == fd)
break ;
if (s == NULL)
{
return ;
}
ft_ary_destroy(&(s->chars));
s->fd = 0;
s->nl_off = -1;
ft_ary_remove(&(t->fds), i);
}
ssize_t gnl_read(t_gnl_fd *s)
{
ssize_t read_ret;
if (s->chars.item_cap < s->chars.item_count + BUFF_SIZE)
{
if (0 != ft_ary_grow(&s->chars, GROW_SIZE(s)))
return (-1);
}
read_ret = read(s->fd, s->chars.item_count + (char*)s->chars.ptr,
BUFF_SIZE);
if (read_ret < 0)
return (-1);
if (read_ret == 0)
{
if (s->chars.item_count > 0)
{
s->nl_off = (ssize_t)s->chars.item_count;
((char*)s->chars.ptr)[s->chars.item_count++] = 0;
return (-2);
}
s->nl_off = -1;
return (0);
}
s->chars.item_count += (size_t)read_ret;
return (1);
}
static int gnl_next(t_gnl_fd *s)
{
char *c;
ssize_t read_ret;
if (0 != ft_ary_remove_mul(&s->chars, 0, (size_t)(s->nl_off + 1)))
return (-1);
while (1)
{
c = s->chars.ptr;
while (c < ((char*)s->chars.ptr + s->chars.item_count) && *c != '\n')
c++;
if (c < ((char*)s->chars.ptr + s->chars.item_count) && *c == '\n')
{
s->nl_off = (c - (char*)s->chars.ptr);
*c = 0;
return (1);
}
read_ret = gnl_read(s);
if (read_ret == -1 || read_ret == 0)
return ((int)read_ret);
if (read_ret == -2)
return (1);
}
}
t_gnlstate g_bufio_state = {{0, sizeof(t_gnl_fd), 0, 1}};
int get_next_line(const int fd, char **line)
{
t_gnl_fd *s;
int status;
size_t i;
if (fd < 0 || !line)
return (-1);
i = -1ULL;
s = NULL;
while (++i < g_bufio_state.fds.item_count)
if ((s = (t_gnl_fd*)ft_ary_get(&g_bufio_state.fds, i))->fd == fd)
break ;
if (s == NULL || s->fd != fd)
if (!(s = gnl_setup(&g_bufio_state, fd)))
return (-1);
status = gnl_next(s);
if (status == 1)
*line = ft_strdup(s->chars.ptr);
else
{
*line = 0;
gnl_destroy(&g_bufio_state, fd);
}
return (status);
}