-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_bonus.c
85 lines (78 loc) · 2.14 KB
/
get_next_line_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hbaddrul <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/06/08 01:09:54 by hbaddrul #+# #+# */
/* Updated: 2021/11/24 00:56:02 by hbaddrul ### ########.fr */
/* */
/* ************************************************************************** */
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#include "get_next_line_bonus.h"
static void gnl_read(int fd, char *buf, char **str)
{
int i;
char *tmp;
if (!str[fd] || !ft_strchr(str[fd], '\n'))
{
i = read(fd, buf, BUFFER_SIZE);
while (i > 0)
{
buf[i] = 0;
if (!str[fd])
str[fd] = ft_substr(buf, 0, i);
else
{
tmp = str[fd];
str[fd] = ft_strjoin(str[fd], buf);
free(tmp);
}
if (ft_strchr(buf, '\n'))
break ;
i = read(fd, buf, BUFFER_SIZE);
}
}
free(buf);
}
static char *gnl_process(int fd, char **str)
{
int i;
int j;
char *ret;
char *tmp;
if (!str[fd])
return (0);
if (!ft_strchr(str[fd], '\n'))
{
ret = ft_substr(str[fd], 0, ft_strlen(str[fd]));
free(str[fd]);
str[fd] = 0;
return (ret);
}
i = ft_strlen(str[fd]);
j = ft_strlen(ft_strchr(str[fd], '\n'));
ret = ft_substr(str[fd], 0, i - j + 1);
tmp = str[fd];
str[fd] = ft_substr(ft_strchr(str[fd], '\n'), 1, j - 1);
free(tmp);
return (ret);
}
char *get_next_line(int fd)
{
char *buf;
static char *str[OPEN_MAX];
buf = malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!buf)
return (0);
if (BUFFER_SIZE < 1 || fd == -1 || read(fd, buf, 0) == -1)
{
free(buf);
return (0);
}
gnl_read(fd, buf, str);
return (gnl_process(fd, str));
}