-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_bonus.c
118 lines (109 loc) · 3.21 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/28 15:06:25 by ldulling #+# #+# */
/* Updated: 2023/11/04 18:55:39 by ldulling ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
char *get_next_line(int fd)
{
static t_list *head[FD_MAX];
char *result;
if (!initial_check(fd, &head[fd]))
return (NULL);
if (head[fd]->bytes_unsaved)
{
if (check_for_full_leftover_line(&head[fd], &result))
return (result);
if (!add_new_node(head[fd]))
return (free_list(&head[fd]), NULL);
}
if (!read_until_endofline(&head[fd], fd))
return (NULL);
result = copy_into_result_and_move_head_to_tail(&head[fd]);
if (!result)
return (NULL);
head[fd]->bytes_unsaved -= head[fd]->line_end + 1;
if (head[fd]->bytes_unsaved == 0)
free_list(&head[fd]);
return (result);
}
int check_for_full_leftover_line(t_list **head, char **result)
{
ssize_t i;
ssize_t new_line_end;
ssize_t result_size;
new_line_end = find_endofline(*head);
if (new_line_end != NO_NL)
{
result_size = new_line_end - (*head)->line_end;
*result = (char *) malloc(result_size + 1);
if (!*result)
return (free_list(head), 1);
i = 0;
(*head)->line_end++;
while (i < result_size)
(*result)[i++] = (*head)->buf[(*head)->line_end++];
(*result)[i] = '\0';
(*head)->bytes_unsaved -= result_size;
if ((*head)->bytes_unsaved)
(*head)->line_end = new_line_end;
else
free_list(head);
return (1);
}
return (0);
}
int read_until_endofline(t_list **head, int fd)
{
t_list *cur;
if ((*head)->next)
cur = (*head)->next;
else
cur = *head;
while (cur)
{
cur->bytes_unsaved = read(fd, cur->buf, BUFFER_SIZE);
if (cur->bytes_unsaved == -1 || (*head)->bytes_unsaved == 0)
return (free_list(head), 0);
if (BUFFER_SIZE <= SSIZE_MAX && cur->bytes_unsaved != BUFFER_SIZE)
cur->endoffile = 1;
cur->buf[cur->bytes_unsaved] = '\0';
cur->line_end = find_endofline(cur);
if (cur->line_end == NO_NL && !cur->endoffile)
if (!add_new_node(cur))
return (free_list(head), 0);
cur = cur->next;
}
return (1);
}
char *copy_into_result_and_move_head_to_tail(t_list **head)
{
t_list *cur;
char *result;
size_t i;
ssize_t j;
result = (char *) malloc(count_result_size(*head) + 1);
if (!result)
return (free_list(head), NULL);
i = 0;
while ((*head)->next)
{
cur = *head;
(*head) = (*head)->next;
j = cur->line_end + 1;
while (cur->buf[j])
result[i++] = cur->buf[j++];
free(cur);
}
j = 0;
while (j <= (*head)->line_end)
result[i++] = (*head)->buf[j++];
result[i] = '\0';
return (result);
}