-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_bonus.c
110 lines (100 loc) · 2.35 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rabustam <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/14 21:10:01 by rabustam #+# #+# */
/* Updated: 2022/06/20 15:41:02 by rabustam ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
static char *find_line(int fd, char *keep);
static char *get_line(char *keep);
static char *save_line(char *keep);
char *get_next_line(int fd)
{
char *line;
static char *keep[1024];
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
keep[fd] = find_line(fd, keep[fd]);
if (!keep[fd])
return (NULL);
line = get_line(keep[fd]);
keep[fd] = save_line(keep[fd]);
return (line);
}
static char *find_line(int fd, char *keep)
{
char *buf;
int bts;
buf = malloc(BUFFER_SIZE + 1);
if (!buf)
return (NULL);
bts = 1;
while (bts && !ft_strchr(keep, '\n'))
{
bts = read(fd, buf, BUFFER_SIZE);
if (bts == -1)
{
free(buf);
return (NULL);
}
buf[bts] = '\0';
keep = ft_strjoin(keep, buf);
}
free(buf);
return (keep);
}
static char *get_line(char *keep)
{
char *ret;
int nli;
nli = 0;
if (!keep[nli])
return (NULL);
while (keep[nli] && keep[nli] != '\n')
nli++;
ret = malloc(nli + 2);
if (!ret)
return (NULL);
nli = 0;
while (keep[nli] && keep[nli] != '\n')
{
ret[nli] = keep[nli];
nli++;
}
if (keep[nli] == '\n')
{
ret[nli] = keep[nli];
nli++;
}
ret[nli] = '\0';
return (ret);
}
static char *save_line(char *keep)
{
char *ret;
int i;
int j;
i = 0;
while (keep[i] && keep[i] != '\n')
i++;
if (!keep[i])
{
free(keep);
return (NULL);
}
ret = malloc(ft_strlen(keep) - i + 1);
if (!ret)
return (NULL);
i++;
j = 0;
while (keep[i])
ret[j++] = keep[i++];
ret[j] = '\0';
free(keep);
return (ret);
}