-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
97 lines (88 loc) · 2.18 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tadiyamu <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/01 10:33:01 by tadiyamu #+# #+# */
/* Updated: 2023/01/03 18:10:08 by tadiyamu ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *ft_strdup(const char *src, char *nl)
{
size_t s;
size_t i;
char *res;
if (!src || !nl)
return (0);
res = 0;
i = 0;
s = nl - src;
if (nl - src <= 0)
return (0);
res = (char *) malloc (s + 1);
if (res == 0)
return (0);
while ((src + i) != nl)
{
res[i] = src[i];
i++;
}
res[i] = '\0';
return (res);
}
char *ft_prepare_line(char **res)
{
char *line;
char *nl;
char *dump;
nl = ft_strchr(*res, '\n');
line = ft_strdup(*res, nl + 1);
if (!line)
return (0);
dump = ft_strdup(nl + 1, ft_strchr(*res, '\0'));
free(*res);
*res = dump;
return (line);
}
int get_next_line_buff(int fd, char **res)
{
int rc;
char *buff;
char *line;
rc = 1;
buff = malloc(BUFFER_SIZE + 1);
while (fd >= 0 && !ft_strchr(*res, '\n') && rc > 0)
{
rc = read(fd, buff, BUFFER_SIZE);
if (rc > 0)
{
line = *res;
buff[rc] = '\0';
*res = ft_strjoin(*res, buff);
free(line);
}
}
free(buff);
return (rc);
}
char *get_next_line(int fd)
{
static char *res = 0;
char *line;
int rc;
line = 0;
rc = get_next_line_buff(fd, &res);
if (rc >= 0 && res && !ft_strchr(res, '\n'))
{
line = ft_strdup(res, ft_strchr(res, '\0'));
free(res);
res = 0;
return (line);
}
if (rc >= 0 && res && ft_strchr(res, '\n'))
line = ft_prepare_line(&res);
return (line);
}