-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
136 lines (125 loc) · 3.2 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kyungsle <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/01 12:25:01 by kyungsle #+# #+# */
/* Updated: 2022/06/08 03:14:11 by kyungsle ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap_bonus.h"
int rtn_split(t_list *curr_ptr, char *nlptr, char **res, size_t dup_size)
{
char *temp;
*res = ft_strndup(curr_ptr->content, dup_size);
if (!*res)
return (-1);
if (nlptr)
temp = ft_strndup(nlptr + 1, ft_strlen(nlptr + 1));
else
temp = ft_strndup("", 0);
if (!temp)
{
free(*res);
*res = NULL;
return (-1);
}
free(curr_ptr->content);
curr_ptr->content = temp;
if (nlptr)
return (ft_strlen(curr_ptr->content) > 0);
return (0);
}
int set_result(t_list *curr_ptr, char *nlptr, char **res)
{
int rtn;
rtn = 0;
if (nlptr)
{
rtn = rtn_split(curr_ptr, nlptr, res, nlptr - (curr_ptr->content) + 1);
}
else
{
if (*(curr_ptr->content) == '\0')
*res = NULL;
else
rtn = rtn_split(curr_ptr, nlptr, res, ft_strlen(curr_ptr->content));
}
return (rtn);
}
int read_file(t_list *curr_ptr, char *buff, char **res)
{
char *nlptr;
char *temp;
ssize_t len;
int eof;
eof = 0;
while (1)
{
nlptr = ft_strchr(curr_ptr->content, '\n');
if (nlptr || eof)
break ;
len = read(curr_ptr->fd, buff, BUFFER_SIZE);
if (len == -1)
return (-1);
buff[len] = '\0';
temp = ft_strjoin(curr_ptr->content, buff);
if (!temp)
return (-1);
free(curr_ptr->content);
curr_ptr->content = temp;
if (len < BUFFER_SIZE)
eof = 1;
}
return (set_result(curr_ptr, nlptr, res));
}
t_list *set_curr_ptr(t_list **head_ptr, int fd)
{
t_list *curr_ptr;
curr_ptr = *head_ptr;
while (curr_ptr && (curr_ptr->fd != fd))
curr_ptr = curr_ptr->next;
if (curr_ptr)
return (curr_ptr);
curr_ptr = (t_list *)malloc(sizeof(t_list));
if (!curr_ptr)
exit (1);
curr_ptr->fd = fd;
curr_ptr->content = ft_strndup("", 0);
if (!(curr_ptr->content))
{
free(curr_ptr);
curr_ptr = NULL;
return (NULL);
}
curr_ptr->next = *head_ptr;
*head_ptr = curr_ptr;
return (curr_ptr);
}
char *get_next_line(int fd)
{
static t_list *head_ptr;
t_list *curr_ptr;
char *buff;
char *res;
int status;
res = NULL;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
curr_ptr = set_curr_ptr(&head_ptr, fd);
if (!curr_ptr)
return (NULL);
buff = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buff)
exit (1);
status = read_file(curr_ptr, buff, &res);
free(buff);
buff = NULL;
if (status == 0 || status == -1)
ft_lstdel(&head_ptr, curr_ptr);
if (status == -1)
return (NULL);
return (res);
}