#include #include #include #include static int ft_linecpy(char *line, char *mapl, size_t start) { size_t i; size_t j; i = 0; j = start; while (i < ft_strlen(line) && line[i]) { mapl[j] = line[i]; i += 2; j++; } mapl[j] = '\n'; return (0); } static int ft_check_err(char *line, size_t size) { size_t i; i = 1; if (ft_strlen(line) != size) return (-1); while (i < ft_strlen(line) && line[i]) { if (line[i] != ' ') return (-1); i += 2; } return (0); } static char *ft_get_first_line(char *line, t_cub *clist) { size_t len; char *mapl; len = ft_strlen(line); if (len < 2 || ft_check_err(line, len) < 0) return (NULL); if (!(mapl = (char*)malloc(((len / 2) + 2) * sizeof(char)))) return (NULL); ft_linecpy(line, mapl, 0); ft_memdel(line); clist->map_width = len; return (mapl); } int ft_get_map(int fd, t_cub *clist) { size_t i; size_t len; int ret; char *line; char *mapl; if (get_next_line(fd, &line) <= 0 || !(mapl = ft_get_first_line(line, clist))) { ft_memdel(line); return (-1); } i = 1; ret = 1; len = clist->map_width; while (ret > 0) { ret = get_next_line(fd, &line); ft_check_err(line, len); mapl = (char*)ft_nrealloc(mapl, (((len / 2) + 2) * i) * sizeof(char), ((((len / 2) + 2) * i) + (ft_strlen(line) / 2) + 2) * sizeof(char)); ft_linecpy(line, mapl, ((len / 2) + 2) * i); ft_memdel(line); i++; } mapl[(i * ((len / 2) + 2)) - 1] = '\0'; ft_free_words(clist->map, NULL); clist->map = ft_split(mapl, '\n'); ft_memdel(mapl); return (0); }