-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
58 lines (53 loc) · 1.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ashongwe <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/06/21 10:35:30 by ashongwe #+# #+# */
/* Updated: 2019/06/24 10:21:13 by ashongwe ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <stdio.h>
void *ft_substring(int fd, char *file[], char **line, int el)
{
unsigned int line_len;
size_t file_len;
int len;
char *substr;
line_len = (unsigned int)ft_strlen(*line);
file_len = (size_t)ft_strlen(file[fd]);
len = (int)ft_strlen(*line);
substr = ft_strsub(file[fd], line_len + el, file_len - len + el);
return (substr);
}
int get_next_line(const int fd, char **line)
{
static char *file[MAX_FD];
char buf[BUFF_SIZE + 1];
char *temp;
int ret;
int el;
if (fd < 0 || (!file[fd] && !(file[fd] = ft_strnew(1))) || !line)
return (-1);
while (!ft_strchr(file[fd], '\n') && (ret = read(fd, buf, BUFF_SIZE)) > 0)
{
buf[ret] = '\0';
temp = file[fd];
file[fd] = ft_strjoin(file[fd], buf);
ft_strdel(&temp);
}
if (ret == -1 || !*(temp = file[fd]))
{
return (ret == -1 ? -1 : 0);
}
if ((el = (ft_strchr(file[fd], '\n') > 0)))
*line = ft_strsub(file[fd], 0, ft_strchr(file[fd], '\n') - file[fd]);
else
*line = ft_strdup(file[fd]);
file[fd] = (char*)ft_substring(fd, file, line, el);
ft_strdel(&temp);
return (!(!file[fd] && !ft_strlen(*line)));
}