-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
134 lines (123 loc) · 3.31 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tbrowang <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/06 05:14:41 by tbrowang #+# #+# */
/* Updated: 2022/03/02 16:22:21 by tbrowang ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <stdlib.h>
#include <unistd.h>
static t_buffer *free_lst(t_buffer *buffer_lst)
{
t_buffer *tmp_buff;
tmp_buff = buffer_lst->next;
free(buffer_lst->buff);
free(buffer_lst);
buffer_lst = NULL;
return (tmp_buff);
}
static t_buffer *init_buffer(int fd)
{
t_buffer *new_buffer;
new_buffer = malloc(sizeof(t_buffer));
if (!new_buffer)
return (NULL);
new_buffer->buff = malloc(sizeof(char) * BUFFER_SIZE);
*(new_buffer->buff) = '\0';
if (!(new_buffer->buff))
return (free(new_buffer), NULL);
new_buffer->length = read(fd, new_buffer->buff, BUFFER_SIZE);
if (new_buffer->length < 0)
{
free(new_buffer->buff);
free(new_buffer);
return (NULL);
}
new_buffer->index = 0;
if (!new_buffer->length)
new_buffer->eof = TRUE;
else
new_buffer->eof = FALSE;
new_buffer->next = NULL;
return (new_buffer);
}
static char *build_string(register int pos_eol, register t_buffer **buffer_lst)
{
register int index;
register char *str;
char *ptr_str;
if (!((*buffer_lst)->length))
return (NULL);
str = malloc(sizeof(char) * (pos_eol + 1));
ptr_str = str;
index = (*buffer_lst)->index;
if (pos_eol == -1)
return (free(str), NULL);
while (pos_eol--)
{
*str++ = (*buffer_lst)->buff[index++];
if (index == (*buffer_lst)->length)
{
*buffer_lst = free_lst(*buffer_lst);
index = 0;
}
}
if (*buffer_lst)
(*buffer_lst)->index = index;
*str = '\0';
return (ptr_str);
}
static int search_eol(int fd, register t_buffer *buffer_lst)
{
register int i;
register int pos_endl;
t_buffer *tmpbuff;
i = buffer_lst->index;
pos_endl = 0;
while (buffer_lst->buff[i] && i <= buffer_lst->length)
{
++pos_endl;
if ((i == buffer_lst->length && buffer_lst->eof)
|| buffer_lst->buff[i] == '\n')
break ;
++i;
if (i == buffer_lst->length)
{
tmpbuff = init_buffer(fd);
if (!tmpbuff)
return (-1);
tmpbuff->next = buffer_lst->next;
buffer_lst->next = tmpbuff;
buffer_lst = tmpbuff;
i = 0;
}
}
return (pos_endl);
}
char *get_next_line(int fd)
{
static t_buffer *fd_open[MAX_OPEN];
int pos_endl;
char *str;
if (fd < 0)
return (NULL);
if (!fd_open[fd])
fd_open[fd] = init_buffer(fd);
if (!fd_open[fd])
return (NULL);
pos_endl = search_eol(fd, fd_open[fd]);
if (fd_open[fd] != NULL && fd_open[fd]->eof == TRUE)
{
free(fd_open[fd]->buff);
free(fd_open[fd]);
fd_open[fd] = NULL;
return (NULL);
}
str = build_string(pos_endl, &fd_open[fd]);
return (str);
}