int ft_gnl(const int fd, char **line) { static char **remain = NULL; char *endl; int exitval; char *tmp; int ret; if (init_gnl(fd, line, &remain)) return (-1); tmp = remain[fd]; if ((endl = (tmp == NULL ? NULL : ft_strchr(tmp, '\n'))) == NULL) { exitval = rec_gnl(fd, line, remain, (tmp != NULL ? ft_strlen(tmp) : 0)); if (tmp != NULL) { ft_memcpy(*line, tmp, ft_strlen(tmp)); free(tmp); } return (exitval); } ret = endl - remain[fd]; *line = ft_strndup(remain[fd], ret); remain[fd] = ft_strdup(endl + 1); if (tmp != NULL) free(tmp); return (ret + 1); }
int get_next_line(int const fd, char **line) { static t_gnl *gnl = NULL; t_gnl *fd_gnl; if (gnl == NULL) gnl = init_gnl(fd); fd_gnl = retrieve_gnl_for_fd(gnl, fd); if (fd_gnl == NULL || line == NULL) return (-1); return (get_line(fd_gnl, line)); }
static t_gnl *retrieve_gnl_for_fd(t_gnl *gnl, int fd) { while (gnl) { if (gnl->fd == fd) return (gnl); else if (!gnl->next) break ; gnl = gnl->next; } if (gnl == NULL) return (NULL); else if (!(gnl->next = init_gnl(fd))) return (NULL); gnl = gnl->next; return (gnl); }