-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
116 lines (107 loc) · 2.57 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dlom <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/30 17:11:44 by dlom #+# #+# */
/* Updated: 2023/06/18 17:27:24 by dlom ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
/*
RETURN: the line that was read or NULL if an error occured or if there
is nothing else to be read.
Function that returns a line read from a file descriptor.
*/
/*
ft_get_line_from_store
*/
char *ft_get_line_from_store(char *store)
{
int i;
int j;
char *line;
i = 0;
j = 0;
if (store[i] == '\0')
return (NULL);
while (store[i] && store[i] != '\n')
i++;
line = (char *)malloc(i + 2);
if (line == NULL)
return (NULL);
i = 0;
while (store[j] && store[j] != '\n')
line[i++] = store[j++];
if (store[i] == '\n')
line[i++] = store[j++];
line[i] = '\0';
return (line);
}
/*
ft_get_rest_from_store
*/
char *ft_get_rest_from_store(char *store)
{
int i;
int j;
char *rest;
i = 0;
j = 0;
while (store[i] && store[i] != '\n')
i++;
if (store[i] == '\0')
{
free(store);
return (NULL);
}
rest = (char *)malloc(ft_strlen(store) - i + 1);
if (rest == NULL)
return (NULL);
i++;
while (store[i] != '\0')
rest[j++] = store[i++];
rest[j] = '\0';
free(store);
return (rest);
}
/*
ft_i_read_file
*/
char *ft_i_read_file(int fd, char *store)
{
int r;
char *buf;
r = 1;
buf = (char *)malloc(BUFFER_SIZE + 1);
if (buf == NULL)
return (0);
while (r > 0 && !ft_strchr(store, '\n'))
{
r = read(fd, buf, BUFFER_SIZE);
if (r == -1)
{
free(buf);
return (0);
}
buf[r] = '\0';
store = ft_strjoin(store, buf);
}
free(buf);
return (store);
}
char *get_next_line(int fd)
{
static char *store[2048];
char *line;
if (BUFFER_SIZE <= 0 || fd < 0)
return (NULL);
store[fd] = ft_i_read_file(fd, store[fd]);
if (store[fd] == NULL)
return (NULL);
line = ft_get_line_from_store(store[fd]);
store[fd] = ft_get_rest_from_store(store[fd]);
return (line);
}