-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_bonus.c
156 lines (146 loc) · 3.57 KB
/
get_next_line_bonus.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
151
152
153
154
155
156
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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_bonus.h"
int makebuf(t_fp *fp)
{
void *p;
p = malloc(BUFFER_SIZE);
if (!p)
{
fp->_flags |= UN_BUF;
fp->ptr = fp->nbuf[fp->_file];
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->sbf._base);
fp->sbf._base = NULL;
fp->sbf._size = 0;
}
fp->_flags |= FOUND_ERR;
}
return (EOF);
}
return (0);
}
t_fp *find_fp(int fd, t_fp *all[])
{
t_fp *fp;
if (fd > MY_OPEN_MAX)
return (NULL);
fp = all[fd];
if (!fp)
{
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;
fp->lbf._base = NULL;
fp->lbf._size = 0;
all[fd] = fp;
}
fp->lbf._base = NULL;
fp->lbf._size = 0;
return (fp);
}
/**
* 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)
{
t_fp *fp;
char *buf;
static t_fp *all[MY_OPEN_MAX] = {NULL};
if (fd < 0 || fd > SHRT_MAX || fd > MY_OPEN_MAX)
return (NULL);
fp = find_fp(fd, all);
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);
all[fd] = NULL;
}
return (buf);
}