int get_next_line(int const fd, char **line) { char **buf; int len; int f; if (!(buf = &(ft_get_buffer(fd)->buffer)) || !BUFF_SIZE || !line) return (-1); ft_get_buffer(fd)->lline = 0; *line = NULL; while (f != -1) { if (!(*buf)[0] && (len = read(fd, *buf, BUFF_SIZE)) != BUFF_SIZE) { if (len == -1) return (-1); f = (ft_strchr(*buf, '\n') != NULL); len = ft_locate_eol(*buf); f = (ft_addbuffer(line, buf, len, fd) == -1 ? -1 : f); return ((f == -1 ? -1 : f || ft_strlen(*buf) || ft_strlen(*line))); } if ((len = ft_locate_eol(*buf)) != -2 && (*buf)[len] == '\0') f = ft_addbuffer(line, buf, len, fd); else if ((*buf)[len] == '\n') return (1 + (f = ft_addbuffer(line, buf, len, fd))); } return (-1); }
int ft_get_tetriminos(t_tetriminos *shapes, int fd) { char *buffer; int total; total = 0; buffer = ft_get_buffer(fd); while (buffer != NULL && total < 26) { if (ft_store_shapes(shapes, buffer, total) == 1) total++; else { write(1, "error\n", 6); return (-1); } free(buffer); buffer = NULL; buffer = ft_get_buffer(fd); } if (buffer != NULL || total == 0) { free(buffer); write(1, "error\n", 6); return (-1); } return (total); }
static int ft_addbuffer(char **line, char **buffer, int n, int fd) { int lline; char *tmp; int n_save; n_save = n; lline = ft_get_buffer(fd)->lline; tmp = *line; (*line) = (char*)ft_memalloc(sizeof(char) * (lline + n + 1)); ft_get_buffer(fd)->lline = lline + n; if (!*line) return (-1); lline = -1; while (++lline > -1 && tmp && tmp[lline]) (*line)[lline] = tmp[lline]; free(tmp); while (n--) (*line)[lline + n] = (*buffer)[n]; ft_resize_buff(buffer, n_save); return (0); }