-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
116 lines (107 loc) · 2.98 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: vsivanat <vsivanat@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/05 12:22:51 by vsivanat #+# #+# */
/* Updated: 2023/12/12 20:13:19 by vsivanat ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *ft_trim_temp(char *temp, char **cache)
{
int newline;
int ind;
char *result;
result = NULL;
if (temp == NULL || ft_strlen(temp, '\0') == 0)
return (free(temp), NULL);
newline = ft_strlen(temp, '\n');
if (newline == -1)
return (temp);
result = malloc(sizeof(char) * (newline + 2));
if (!result)
return (free(*cache), *cache = NULL, free(temp), temp = NULL, NULL);
ind = -1;
while (++ind < newline + 1)
result[ind] = temp[ind];
result[ind] = 0;
free(temp);
return (result);
}
char *next(char *cache)
{
int i;
int j;
char *line;
i = 0;
if (cache == NULL)
return (NULL);
while (cache[i] && cache[i] != '\n')
i++;
if (!cache[i])
return (NULL);
line = malloc((ft_strlen(cache, '\0') - i + 1));
if (!line)
return (NULL);
j = 0;
while (cache[++i])
line[j++] = cache[i];
line[j++] = '\0';
return (line);
}
char *get_next_line(int fd)
{
int read_len;
char *temp;
static char *cache;
char buffer[BUFFER_SIZE + 1];
temp = NULL;
if (fd < 0 || read(fd, 0, 0) < 0 || BUFFER_SIZE <= 0)
return (free (cache), cache = NULL, free(temp), NULL);
read_len = 1;
temp = cache;
while (read_len > 0 && ft_strlen(temp, '\n') == -1)
{
read_len = read(fd, buffer, BUFFER_SIZE);
if (read_len == 0 && cache == NULL)
break ;
if (read_len < 0)
return (NULL);
buffer[read_len] = 0;
cache = ft_strjoin(cache, buffer);
if (cache == NULL)
return (NULL);
temp = cache;
}
cache = next(cache);
return (ft_trim_temp(temp, &cache));
}
// # include <unistd.h>
// # include <stdlib.h>
// # include <stdio.h>
// #include <fcntl.h>
// int main()
// {
// int fd;
// fd = open("test.txt", O_RDWR);
// if (fd == -1)
// {
// // perror("Error opening file");
// return 1;
// }
// // printf("%d", fd);
// printf("%s", get_next_line(fd));
// printf("%s", get_next_line(fd));
// printf("%s", get_next_line(fd));
// printf("%s", get_next_line(fd));
// printf("%s", get_next_line(fd));
// printf("%s", get_next_line(fd));
// printf("%s", get_next_line(fd));
// printf("%s", get_next_line(fd));
// printf("%s", get_next_line(fd));
// close(fd);
// return (0);
// }