-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
159 lines (150 loc) · 3.28 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "get_next_line.h"
/**
* @brief creates a new node and saves read data from fd
* - allocates node
* - reads from fd into node->frag
* - sets node->current (position tracker) and node->nl (newline index or EOF)
* @param fd file descriptor
* @return new node or NULL if error
*/
t_list *gnl_new_node(int fd)
{
t_list *node;
ssize_t bytes_read;
node = gnl_calloc(1, sizeof(t_list));
if (!node)
return (NULL);
bytes_read = read(fd, node->frag, BUFFER_SIZE);
if (bytes_read < 0)
return (gnl_free_list(&node));
if (bytes_read == 0)
node->nl = -2;
if (bytes_read > 0)
{
node->current = node->frag;
node->nl = gnl_find_nl_idx(node->frag);
}
return (node);
}
/**
* @brief updates node list
* - frees nodes w/o newline
* - updates node if newline found (current ptr if more data, free node if not)
* @param head list head
*/
void gnl_update_list(t_list **head)
{
t_list *temp;
while (*head && (*head)->nl == -1)
{
temp = *head;
*head = (*head)->next;
free(temp);
}
if (*head && (*head)->nl > -1)
{
if ((*head)->current[(*head)->nl + 1])
{
(*head)->current = &(*head)->current[(*head)->nl + 1];
(*head)->nl = gnl_find_nl_idx((*head)->current);
}
else
{
temp = *head;
*head = (*head)->next;
free(temp);
}
}
}
/**
* @brief creates a line from list nodes
* - calculates size of line and allocates memory
* - fills line with list nodes (gnl_fill_line)
* - updates list (gnl_update_list)
* @param head list head
* @return line or NULL if error
*/
char *gnl_make_line(t_list **head)
{
size_t size;
char *line;
t_list *temp;
size = 1;
temp = *head;
while (temp->nl < 0 && temp->next != NULL)
{
size += BUFFER_SIZE;
temp = temp->next;
}
if (temp->nl > -1)
{
size += temp->nl + 1;
}
if (temp->nl < 0)
size += gnl_strlen(temp->current);
line = gnl_calloc(1, size);
if (!line || size == 0)
return (gnl_free_list(head));
gnl_fill_line(line, *head, size);
if (line)
gnl_update_list(head);
return (line);
}
/**
* @brief checks list head
* - valid `fd` and `BUFFER_SIZE` values
* - creates new node if list is empty
* - frees list if `nl` is -2 (EOF) and returns NULL
* @param head list head
* @param fd file descriptor
* @return list head
*/
t_list *gnl_check_head(t_list **head, int fd)
{
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
if (!*head)
{
*head = gnl_new_node(fd);
if (!*head)
return (NULL);
}
if ((*head)->nl == -2)
return (gnl_free_list(head));
return (*head);
}
/**
* @brief reads a line from a file descriptor
* - checks for proper head (gnl_check_head)
* - checks for existing line in list
* - creates nodes until a line is found or EOF (while)
* @param fd file descriptor
* @return line read file descriptor or NULL if error, EOF or empty line
*/
char *get_next_line(int fd)
{
static t_list *head;
t_list *current;
char *line;
if (!gnl_check_head(&head, fd))
return (NULL);
if (head->nl > -1)
return (gnl_make_line(&head));
current = head;
while (current && current->nl == -1)
{
current->next = gnl_new_node(fd);
if (!current->next)
return (gnl_free_list(&head));
current = current->next;
if (current->nl == -2)
{
line = gnl_make_line(&head);
if (!line)
return (gnl_free_list(&head));
gnl_free_list(&head);
return (line);
}
}
return (gnl_make_line(&head));
}