-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
125 lines (111 loc) · 2.99 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
122
123
124
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: szerisen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/19 03:09:06 by szerisen #+# #+# */
/* Updated: 2023/02/07 18:48:28 by szerisen ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *joinandfree(char *resultant, char *appender)
{
char *temp;
temp = ft_strjoin(resultant, appender);
free(resultant);
return (temp);
}
char *find_remaining(char *resultant)
{
int i;
int j;
char *remstatic;
i = 0;
while (resultant[i] && resultant[i] != '\n')
i++;
if (!resultant[i])
{
free(resultant);
return (NULL);
}
remstatic = ft_calloc((ft_strlen(resultant) - i + 1), sizeof(char));
i++;
j = 0;
while (resultant[i])
remstatic[j++] = resultant[i++];
free(resultant);
return (remstatic);
}
char *find_line(char *resultant)
{
char *one_line;
int i;
i = 0;
if (!resultant[i])
return (NULL);
while (resultant[i] && resultant[i] != '\n')
i++;
one_line = ft_calloc(i + 2, sizeof(char));
i = 0;
while (resultant[i] && resultant[i] != '\n')
{
one_line[i] = resultant[i];
i++;
}
if (resultant[i] && resultant[i] == '\n')
one_line[i++] = '\n';
return (one_line);
}
char *read_file(int fd, char *resultant)
{
char *appender;
int noofbytes;
if (!resultant)
resultant = ft_calloc(1, 1);
appender = ft_calloc((size_of)BUFFER_SIZE + 1, sizeof(char));
noofbytes = 1;
while (noofbytes > 0)
{
noofbytes = read(fd, appender, BUFFER_SIZE);
if (noofbytes == -1)
{
free(appender);
return (NULL);
}
appender[noofbytes] = 0;
resultant = joinandfree(resultant, appender);
if (ft_strchr(resultant, '\n'))
break ;
}
free(appender);
return (resultant);
}
char *get_next_line(int fd)
{
static char *container_static;
char *one_line;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
if (BUFFER_SIZE > 2147483647)
return (NULL);
container_static = read_file(fd, container_static);
if (!container_static)
return (NULL);
one_line = find_line(container_static);
container_static = find_remaining(container_static);
return (one_line);
}
// #include <fcntl.h>
// #include <unistd.h>
// #include <stdio.h>
// int main(){
// int fd;
// char *s = "/Users/szerisen/Desktop/simon.txt";
// fd = open(s, O_RDONLY);
// printf("%s", get_next_line(fd));
// // printf("%s", get_next_line(fd));
// // printf("%s", get_next_line(fd));
// printf("%d", fd);
// }