/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_get_map.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 17:28:47 by rbousset #+# #+# */ /* Updated: 2020/02/14 17:28:47 by rbousset ### ########lyon.fr */ /* */ /* ************************************************************************** */ #include #include #include #include #include static void ft_linecpy(char *line, char *mapl, size_t start, t_cub *cl) { size_t i; size_t j; size_t slen; i = 0; j = start; slen = ft_strlen(line); while (i < slen && line[i]) { mapl[j] = line[i]; if (mapl[j] - 48 > 2 && mapl[j] - 48 < 10 && mapl[j] - 48 > cl->mlist.topsp) ft_error(FT_RET_MAP_ERR, FT_ERR_UNDEF_SPRITE, cl); i++; j++; } mapl[j] = '\n'; } static int8_t ft_cat_mapl(char *line, t_cub *clist) { if (!line[0] || !(clist->mlist.mapl = (char *)ft_nrealloc(clist->mlist.mapl, clist->mlist.mapl_len * sizeof(char), (clist->mlist.mapl_len + ft_strlen(line) + 1) * sizeof(char)))) { ft_sprintf(clist->errmsg, FT_ERR_ALLOCATE); return (-1); } ft_linecpy(line, clist->mlist.mapl, clist->mlist.mapl_len, clist); clist->mlist.mapl_len += ft_strlen(line) + 1; return (0); } int ft_get_map_first_line(char *line, t_cub *clist) { clist->mlist.map_start = clist->mlist.line_chk; if (!line[0]) { ft_memdel((void*)&line); ft_sprintf(clist->errmsg, FT_ERR_READ); return (-1); } if (ft_check_map_line(line, 1, clist) < 0) { ft_memdel((void*)&line); return (-1); } ft_memdel((void*)&clist->mlist.mapl); if (ft_cat_mapl(line, clist) < 0) { ft_memdel((void*)&line); return (-1); } ft_memdel((void*)&line); return (1); } int ft_get_map_core(int fd, t_cub *clist) { int ret; char *line; ret = 1; while ((ret = get_next_line(fd, &line)) > 0) { clist->mlist.line_chk += 1; if (!line[0] || ft_check_map_line(line, 0, clist) < 0 || ft_cat_mapl(line, clist) < 0) { if (!line[0]) ft_sprintf(clist->errmsg, FT_ERR_MAP_EMPL); ft_memdel((void*)&line); return (-1); } ft_memdel((void*)&line); } ft_memdel((void*)&line); clist->mlist.mapl[clist->mlist.mapl_len - 1] = '\0'; ft_free_words(clist->mlist.map); clist->mlist.map = ft_split(clist->mlist.mapl, '\n'); clist->mlist.map_h = ft_get_map_h(clist->mlist.map); clist->mlist.map_w = ft_get_map_w(clist->mlist.map); ft_memdel((void*)&clist->mlist.mapl); return (0); }