-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
138 lines (126 loc) · 3.45 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rnarciso <rnarciso@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/20 19:11:58 by rnarciso #+# #+# */
/* Updated: 2022/12/27 17:16:54 by rnarciso ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
// just to be cautious, Valgrind isn't that good.
char *memory_leak_destroyer_fu(char *s)
{
free(s);
s = NULL;
return (NULL);
}
// this will check if there is an existing '\n'
// during the reading, and return the whole read
// if it found the '\n' char
char *read_and_get_line(int fd, char *backup)
{
ssize_t bytes;
char *buffer;
bytes = 1;
buffer = (char *)malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!buffer)
return (NULL);
while (bytes > 0 && !ft_strchr(backup, '\n'))
{
bytes = read(fd, buffer, BUFFER_SIZE);
if (bytes < 0)
{
if (backup && ft_strchr(backup, '\0'))
free(backup);
free(buffer);
return (NULL);
}
if (bytes == 0)
break ;
buffer[bytes] = 0;
backup = ft_strjoin(backup, buffer);
}
memory_leak_destroyer_fu(buffer);
return (backup);
}
// this will cleanse the read and returned line from the previous function
// making it only the first line
char *correcting_the_line(char *backup)
{
char *updated_line;
int stop;
if (ft_strlen(backup) == 0)
return (NULL);
stop = 0;
while (backup[stop] != '\n' && backup[stop])
stop++;
updated_line = ft_substr(backup, 0, (stop + 1));
if (!updated_line)
return (memory_leak_destroyer_fu(updated_line));
return (updated_line);
}
// this will update the backup, by adding only the extra
// that was previously ignored on the correct line
char *backup_update(char *backup)
{
int stop;
char *updated_backup;
stop = 0;
while (backup[stop] != '\n' && backup[stop])
stop++;
if (!backup[stop] || backup[1] == '\0')
return (memory_leak_destroyer_fu(backup));
updated_backup = ft_substr(backup, (stop + 1),
(ft_strlen(backup) - stop));
if (!updated_backup)
return (memory_leak_destroyer_fu(updated_backup));
free(backup);
return (updated_backup);
}
// main function
char *get_next_line(int fd)
{
char *correct_line;
static char *backup[FD_MAX];
if (fd > FD_MAX || fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
if (ft_strchr(backup[fd], '\n') && backup[fd])
{
correct_line = correcting_the_line(backup[fd]);
backup[fd] = backup_update(backup[fd]);
return (correct_line);
}
backup[fd] = read_and_get_line(fd, backup[fd]);
if (!backup[fd])
{
memory_leak_destroyer_fu(backup[fd]);
return (NULL);
}
correct_line = correcting_the_line(backup[fd]);
backup[fd] = backup_update(backup[fd]);
return (correct_line);
}
/*
int main(void)
{
int fd;
int i;
char *string;
i = 2;
fd = open("test.txt", O_RDONLY);
if (fd <= -1)
return (0);
while (i != 0)
{
string = get_next_line(fd);
printf("%s", string);
free(string);
--i;
}
close(fd);
return (0);
}
*/