-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
87 lines (79 loc) · 1.87 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: brfernan <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/03 17:35:20 by brfernan #+# #+# */
/* Updated: 2024/05/08 18:27:53 by brfernan ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
int buffer(char *buf)
{
int trig;
int i;
int j;
trig = 0;
i = 0;
j = 0;
while (buf[i])
{
if (trig == 1)
{
buf[j] = buf[i];
j++;
}
if (buf[i] == '\n')
trig = 1;
buf[i++] = 0;
}
return (trig);
}
char *clearbuf(char *buf)
{
int i;
i = 0;
while (buf[i])
buf[i++] = 0;
return (NULL);
}
char *get_next_line(int fd)
{
static char buf[BUFFER_SIZE + 1];
char *line;
int trig;
if (BUFFER_SIZE <= 0 || read(fd, 0, 0) == -1)
return (clearbuf(buf));
line = NULL;
trig = 0;
while (trig == 0 && (buf[0] || read(fd, buf, BUFFER_SIZE)))
{
line = ft_strjoin(line, buf);
if (!line)
return (NULL);
trig = buffer(buf);
}
return (line);
}
/*int main(void)
{
int fd;
char *ret;
fd = open("/home/(YOURUSER)/Documents/example.txt", O_RDONLY);
if (fd == -1)
{
perror("Error in opening file");
return (1);
}
ret = get_next_line(fd);
while (ret)
{
printf("%s", ret);
free(ret);
ret = get_next_line(fd);
}
close(fd);
return (0);
}*/