/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/28 00:01:15 by ljiriste #+# #+# */
-/* Updated: 2023/12/09 15:52:02 by ljiriste ### ########.fr */
+/* Updated: 2024/02/17 00:05:19 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
return (node);
}
-// How to free?
-// static in get_next_line? So it could be passed to select AND delete
+static void delete_file_node(int fd, t_list **list)
+{
+ t_list *cur;
+ t_list *prev;
+
+ prev = NULL;
+ cur = *list;
+ while (fd >= 0 && ((t_ft *)(cur->content))->fd != fd)
+ {
+ prev = cur;
+ cur = cur->next;
+ }
+ if (prev == NULL)
+ *list = cur->next;
+ else
+ prev->next = cur->next;
+ free(((t_ft *)(cur->content))->buffer);
+ free(cur->content);
+ free(cur);
+ return ;
+}
+
static char *select_file_buffer(int fd, t_list **list)
{
t_list *local_head;
+ if (fd < 0)
+ {
+ while (*list != NULL)
+ delete_file_node(-1, list);
+ return (NULL);
+ }
if (*list == NULL)
{
*list = new_ft_node(fd);
return (((t_ft *)(local_head->next->content))->buffer);
}
-static void delete_file_node(int fd, t_list **list)
-{
- t_list *cur;
- t_list *prev;
-
- prev = NULL;
- cur = *list;
- while (((t_ft *)(cur->content))->fd != fd)
- {
- prev = cur;
- cur = cur->next;
- }
- if (prev == NULL)
- *list = cur->next;
- else
- prev->next = cur->next;
- free(((t_ft *)(cur->content))->buffer);
- free(cur->content);
- free(cur);
- return ;
-}
-
/* Concatenates res and buffer up to newline.
Shifts buffer by the transfered characters.
Returns 0 when buffer is empty (and needs filling with read).
return (buffer[0] || nl);
}
+// Passing negative fd causes every buffer to close
char *get_next_line(int fd)
{
static t_list *list;
res = NULL;
buffer = select_file_buffer(fd, &list);
- while (!transfer_string(&res, buffer))
+ while (buffer && !transfer_string(&res, buffer))
{
if (buffer[0] == '\0')
{