-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
116 lines (105 loc) · 2.52 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: youel-id <youel-id@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/18 20:19:37 by youel-id #+# #+# */
/* Updated: 2022/12/14 01:37:33 by youel-id ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *get_line_from_save(char *save)
{
int i;
char *ret;
i = 0;
if (!save[0])
return (NULL);
while (save[i] && save[i] != '\n')
i++;
if (save[i] && save[i] == '\n')
i++;
ret = (char *)malloc(sizeof(char) * i + 1);
if (!ret)
return (0);
i = 0;
while (save[i] && save[i] != '\n')
{
ret[i] = save[i];
i++;
}
if (save[i] == '\n')
ret[i++] = '\n';
ret[i] = '\0';
return (ret);
}
char *update_save(char *save)
{
int i;
int j;
char *new_save;
i = 0;
j = 0;
while (save[i] && save[i] != '\n')
i++;
if (!save[i])
{
free(save);
return (NULL);
}
new_save = (char *)malloc(sizeof(char) * (ft_strlen(save) - i) + 1);
i++;
while (save[i])
new_save[j++] = save[i++];
new_save[j] = '\0';
free(save);
return (new_save);
}
char *get_next(char *save, int fd, char *buffer)
{
int rd;
rd = 1;
while (rd > 0)
{
rd = read(fd, buffer, BUFFER_SIZE);
if (rd == -1)
{
free(save);
free(buffer);
return (NULL);
}
buffer[rd] = '\0';
save = ft_strjoin(save, buffer);
if (ft_strchr(save, '\n'))
break ;
}
//free(buffer);
return (save);
}
char *get_next_line(int fd)
{
char *buffer;
static char *save;
char *line;
if (fd < 0 || fd == 1 || fd == 2|| BUFFER_SIZE <= 0)
return (NULL);
buffer = (char *)malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!buffer)
return (NULL);
save = get_next(save, fd, buffer);
if (!save)
return (NULL);
line = get_line_from_save(save);
save = update_save(save);
return (line);
}
//int main ()
//{
// int fd = open ("test",O_RDONLY);
// printf("%s",get_next_line(fd));
// close(fd);
// open("test",O_RDONLY);
// printf("%s",get_next_line(fd));
//}