-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
105 lines (94 loc) · 2.23 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fatkeski <fatkeski@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/23 19:23:57 by fatkeski #+# #+# */
/* Updated: 2023/12/25 19:54:11 by fatkeski ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
static int find_next_line(char *arr)
{
int i;
i = 0;
while (arr[i] != '\0')
{
if (arr[i] == '\n')
{
i++;
break ;
}
i++;
}
return (i);
}
static char *read_lines(int fd, char *arr)
{
int bytes_read;
char buff[BUFFER_SIZE + 1];
bytes_read = read(fd, buff, BUFFER_SIZE);
while (bytes_read > 0)
{
buff[bytes_read] = '\0';
arr = gnl_strjoin(arr, buff);
if (buff[find_next_line(buff) - 1] == '\n')
break ;
bytes_read = read(fd, buff, BUFFER_SIZE);
}
if (bytes_read == -1)
{
free(arr);
return (0);
}
return (arr);
}
static char *get_a_line(char *arr)
{
char *line;
int i;
int len;
if (arr == 0)
return (0);
len = find_next_line(arr);
i = 0;
line = (char *)malloc(sizeof(char) * (len + 1));
if (line == 0)
return (0);
while (i < len)
{
line[i] = arr[i];
i++;
}
line[len] = '\0';
return (line);
}
static char *control_line(char *line)
{
char *arr;
size_t after_nl_index;
if (line == 0)
return (0);
after_nl_index = find_next_line(line);
if (line[after_nl_index] == '\0')
{
free(line);
return (0);
}
arr = gnl_strdup(&line[after_nl_index]);
free(line);
return (arr);
}
char *get_next_line(int fd)
{
char *line;
static char *arr;
if (fd < 0 || BUFFER_SIZE <= 0)
return (0);
arr = read_lines(fd, arr);
line = get_a_line(arr);
arr = control_line(arr);
return (line);
}