-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_bonus.c
103 lines (91 loc) · 2.56 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ybachar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/02 22:57:01 by ybachar #+# #+# */
/* Updated: 2022/12/06 14:35:07 by ybachar ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
char *ft_strchr(char *s, int c)
{
int i;
i = 0;
if (!s)
return (NULL);
while (s[i])
{
if (s[i] == c)
return (&s[i]);
i++;
}
return (0);
}
char *reader(int fd, char *str)
{
char *buffer;
int len;
buffer = malloc(sizeof(char) * BUFFER_SIZE + 1);
if (!buffer)
return (NULL);
len = 1;
while (!(ft_strchr(str, '\n')) && len != 0)
{
len = read(fd, buffer, BUFFER_SIZE);
if (len == 0)
break ;
if (len == -1)
{
free(buffer);
free(str);
return (NULL);
}
buffer[len] = '\0';
str = ft_strjoin(str, buffer);
}
free(buffer);
return (str);
}
char *get_next_line(int fd)
{
static char *str[OPEN_MAX];
char *res;
str[fd] = reader(fd, str[fd]);
if (!str[fd])
return (NULL);
res = get_ligne(str[fd]);
str[fd] = get_extra(str[fd]);
return (res);
}
// int main()
// {
// char *str;
// int fd = open("foo.txt", O_RDONLY);
// int fd1 = open("foo1.txt", O_RDONLY);
// int fd2 = open("foo2.txt", O_RDONLY);
// // str = get_next_line(fd);
// // printf("%s", str);
// // while (str)
// // {
// // str = get_next_line(fd);
// // printf("%s", str);
// // }
// printf("%s", get_next_line(fd));
// printf("\n%s\n","file 1");
// printf("%s", get_next_line(fd1));
// printf("\n%s\n","file 2");
// printf("%s", get_next_line(fd));
// printf("\n%s\n","file 1");
// printf("%s", get_next_line(fd2));
// printf("%s\n","file 3");
// printf("%s", get_next_line(fd1));
// printf("%s", get_next_line(fd1));
// printf("\n%s\n","file 2");
// //printf("%s\n", get_next_line(fd));
// printf("%s", get_next_line(fd2));
// printf("%s", get_next_line(fd2));
// printf("\n%s\n","file 3");
// }