-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
78 lines (70 loc) · 2.05 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jinsyang <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/13 16:48:07 by jinsyang #+# #+# */
/* Updated: 2023/04/25 19:09:04 by jinsyang ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
void free_and_null(char **str, int *flag)
{
*flag = 0;
free(*str);
*str = NULL;
}
char *fill_buf(char *buf, char *stay, int *fd_index)
{
int index;
index = 0;
while (stay[index] != '\0')
{
buf[index] = stay[index];
index++;
}
free(stay);
*fd_index = index;
return (NULL);
}
char *get_next_line2(char *result, int fd, int fd_index, int result_len)
{
char buf[BUFFER_SIZE];
static char *stay;
int index;
int flag;
flag = 1;
while (flag)
{
if (stay)
stay = fill_buf(buf, stay, &fd_index);
else
fd_index = read(fd, buf, BUFFER_SIZE);
if (fd_index < 0)
free_and_null(&result, &flag);
if (fd_index <= 0)
return (result);
index = where_n(buf, fd_index, &flag);
if (index != fd_index && BUFFER_SIZE != 1)
stay = gnl_strdup(buf + index, fd_index - index);
result = result_is(result, &result_len, buf, index);
if (!result)
free_and_null(&stay, &flag);
}
return (result);
}
char *get_next_line(int fd)
{
char *result;
int result_len;
int fd_index;
if (BUFFER_SIZE <= 0)
return (NULL);
result = NULL;
result_len = 0;
fd_index = 0;
result = get_next_line2(result, fd, fd_index, result_len);
return (result);
}