-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
executable file
·121 lines (110 loc) · 2.91 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: arebelo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/01 18:42:37 by arebelo #+# #+# */
/* Updated: 2022/02/07 17:59:24 by arebelo ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
void clean_buffer(char buf[BUFFER_SIZE])
{
unsigned long i;
i = 0;
while (i < BUFFER_SIZE)
{
buf[i] = 0;
i++;
}
}
int read_aux(int fd, char buf[BUFFER_SIZE], char *result)
{
ssize_t ret;
clean_buffer(buf);
ret = read(fd, buf, BUFFER_SIZE);
if (ret == -1)
{
free(result);
result = 0;
return (0);
}
else if (ret == 0)
return (0);
else
return (1);
}
char *get_next_line(int fd)
{
static int i = 0;
char *result;
static char buf[BUFFER_SIZE];
if (BUFFER_SIZE <= 0 || fd < 0)
return (0);
if (i == 0)
{
clean_buffer(buf);
result = first_line(fd, buf, &i);
}
else
{
if (i >= BUFFER_SIZE)
result = first_line(fd, buf, &i);
else
{
if (buf[i] == 0)
return (0);
result = following_lines(fd, buf, &i);
}
}
return (result);
}
char *first_line(int fd, char buf[BUFFER_SIZE], int *i)
{
int ret;
char *result;
char *temp;
*i = 0;
clean_buffer(buf);
ret = read(fd, buf, BUFFER_SIZE);
if (ret == -1 || ret == 0)
return (0);
result = ft_strndup(buf, (counter(buf, *i) + 1));
while (counter(buf, *i) == (BUFFER_SIZE - 1) && buf[BUFFER_SIZE - 1] != '\n'
&& buf[BUFFER_SIZE - 1] != '\0')
{
if (!read_aux(fd, buf, result))
return (result);
temp = ft_strndup(buf, (counter(buf, *i) + 1));
result = ft_strjoin(result, temp);
}
*i = counter(buf, *i) + 1;
return (result);
}
char *following_lines(int fd, char buf[BUFFER_SIZE], int *i)
{
char *result;
char *temp;
result = ft_strndup(&buf[*i], counter(&buf[*i], *i) + 1);
*i += counter(&buf[*i], *i) + 1;
if (buf[*i - 1] != '\n' && buf[*i - 1] != '\0')
{
*i = 0;
if (!read_aux(fd, buf, result))
return (result);
temp = ft_strndup(buf, counter(buf, *i) + 1);
result = ft_strjoin(result, temp);
while (counter(buf, *i) == (BUFFER_SIZE - 1)
&& buf[BUFFER_SIZE - 1] != '\n' && buf[BUFFER_SIZE - 1] != '\0')
{
if (!read_aux(fd, buf, result))
return (result);
temp = ft_strndup(buf, counter(buf, *i) + 1);
result = ft_strjoin(result, temp);
}
*i = counter(buf, *i) + 1;
}
return (result);
}