-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
150 lines (140 loc) · 3.39 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
140
141
142
143
144
145
146
147
148
149
150
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abelov <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/20 21:56:33 by abelov #+# #+# */
/* Updated: 2023/11/20 21:56:35 by abelov ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
int makebuf(t_fp *fp)
{
void *p;
p = malloc(BUFFER_SIZE);
if (!p)
{
fp->_flags |= UN_BUF;
fp->ptr = fp->nbuf;
fp->sbf._base = fp->ptr;
fp->sbf._size = 1;
fp->_flags |= FOUND_ERR;
return (-1);
}
fp->_flags |= MBF;
fp->ptr = (unsigned char *)p;
fp->sbf._base = p;
fp->sbf._size = BUFFER_SIZE;
return (0);
}
int refill(t_fp *fp)
{
if (!fp->sbf._base && makebuf(fp))
return (EOF);
fp->ptr = fp->sbf._base;
fp->_r = read(fp->_file, (char *)fp->ptr, fp->sbf._size);
fp->_flags &= ~SMOD;
if (fp->_r <= 0)
{
if (fp->_r == 0)
fp->_flags |= FOUND_EOF;
else
{
fp->_r = 0;
if (fp->_flags & MBF)
{
free(fp->lbf._base);
fp->lbf._base = NULL;
fp->lbf._size = 0;
}
fp->_flags |= FOUND_ERR;
}
return (EOF);
}
return (0);
}
t_fp *sfp(int fd, t_fp *ptr)
{
t_fp *fp;
if (!ptr)
{
fp = (t_fp *) malloc(sizeof(t_fp) * 1);
if (!fp)
return (NULL);
fp->ptr = NULL;
fp->_file = fd;
fp->_flags = 1;
fp->_r = 0;
fp->sbf._base = NULL;
fp->sbf._size = 0;
ptr = fp;
}
ptr->lbf._base = NULL;
ptr->lbf._size = 0;
return (ptr);
}
/**
* We have to copy the current buffered data to the line buffer making sure
* there is room for more bytes. Copy data from string buffer to line buffer,
* refill file and look for newline.
* The loop stops only when we find a newline or EOF.
*
* OPTIMISTIC is length that we (optimistically) expect will
* accommodate the `rest' of the string, on each trip through the
* loop below.
*/
char *ft_fgetln(t_fp *fp)
{
size_t len;
size_t offset;
if (fp->_r <= 0 && refill(fp))
return (NULL);
offset = 0;
len = fp->_r;
if (get_str(fp, len, offset))
return ((char *)fp->lbf._base);
while (fp->_r > 0)
{
if ((lbchange(fp, len + OPTIMISTIC, !DO_SHRINK)
|| ft_memcpy(fp->lbf._base + offset, fp->ptr, len - offset))
&& (refill(fp) && (fp->_flags & FOUND_ERR)))
return (NULL);
offset = len;
if (fp->_flags & FOUND_EOF)
break ;
if (get_str(fp, len, offset))
return ((char *)fp->lbf._base);
len += fp->_r;
}
if (lbchange(fp, len + 1, DO_SHRINK))
return (NULL);
return ((char *)fp->lbf._base);
}
char *get_next_line(int fd)
{
char *buf;
static t_fp *fp = NULL;
if (fd < 0 || fd > SHRT_MAX)
return (NULL);
fp = sfp(fd, fp);
if (!fp)
return (NULL);
buf = ft_fgetln(fp);
if (!buf)
{
if (fp->_flags & MBF)
{
free(fp->sbf._base);
fp->sbf._base = NULL;
fp->sbf._size = 0;
}
free(fp->lbf._base);
fp->lbf._base = NULL;
fp->lbf._size = 0;
free(fp);
fp = NULL;
}
return (buf);
}