-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_next_line.c
More file actions
99 lines (92 loc) · 2.4 KB
/
get_next_line.c
File metadata and controls
99 lines (92 loc) · 2.4 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ladloff <ladloff@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/02 09:55:21 by ladloff #+# #+# */
/* Updated: 2024/08/30 22:04:01 by ladloff ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
static char *extract_line(t_gnl **list, size_t line_size)
{
size_t i;
char *line;
char *p_line;
if (!list || !(*list))
return (NULL);
line = ft_calloc((line_size + 1), sizeof(char));
if (!line)
return (NULL);
p_line = line;
i = (*list)->buffer_index;
while (line_size--)
{
*line++ = (*list)->buffer[i++];
if ((size_t)(*list)->read_bytes == i)
{
*list = free_gnl_node(*list);
i = 0;
}
}
*line = '\0';
if (*list)
(*list)->buffer_index = i;
return (p_line);
}
static size_t get_line_size(t_gnl *list, int fd)
{
size_t i;
size_t line_size;
t_gnl *new_node;
if (!list || !list->buffer)
return (0);
line_size = 0;
i = list->buffer_index;
while (list->buffer[i] && list->buffer[i] != '\n')
{
line_size++;
if ((size_t)list->read_bytes == ++i)
{
new_node = create_gnl_node(fd);
if (!new_node)
return (0);
new_node->next = list->next;
list->next = new_node;
list = new_node;
i = 0;
}
}
if (list->buffer[i] == '\n')
line_size++;
return (line_size);
}
char *get_next_line(int fd)
{
static t_gnl *list;
char *line;
size_t line_size;
line = NULL;
if (fd < 0 || read(fd, NULL, 0) < 0)
{
while (list)
list = free_gnl_node(list);
return (line);
}
if (!list)
list = create_gnl_node(fd);
if (list && list->read_bytes)
{
line_size = get_line_size(list, fd);
if (line_size)
line = extract_line(&list, line_size);
if (!line || !list)
while (list)
list = free_gnl_node(list);
}
else
list = free_gnl_node(list);
return (line);
}