/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_check_map_line.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 17:28:34 by rbousset #+# #+# */ /* Updated: 2020/02/14 17:28:37 by rbousset ### ########lyon.fr */ /* */ /* ************************************************************************** */ #include #include #include #include static int8_t ft_first_checks(char *line, size_t i, t_cub *clist) { if (!ft_ischarset("012NSEW ", line[i])) { ft_strlcpy(clist->errmsg, FT_ERR_ILL_MAP, ft_strlen(FT_ERR_ILL_MAP) + 1); return (-1); } if (ft_ischarset("NSEW", line[i])) clist->mlist->isspawn += 1; if (clist->mlist->isspawn > 1) { ft_strlcpy(clist->errmsg, FT_ERR_MULT_SPAWN, ft_strlen(FT_ERR_MULT_SPAWN) + 1); return (-1); } return (0); } static int8_t ft_second_checks(char *line, size_t i, t_cub *clist) { if (!ft_ischarset("1 ", line[i])) { ft_strlcpy(clist->errmsg, FT_ERR_ILL_MAP, ft_strlen(FT_ERR_ILL_MAP) + 1); return (-1); } return (0); } size_t ft_get_line_len(char *line) { size_t i; size_t j; i = 0; j = 0; while (line[i]) { if (line[i] == ' ') j++; i++; } return (i - j); } static int8_t ft_check_side_walls(char *line, size_t i, t_cub *clist) { if (line[0] != '1' || line[i - 1] != '1') { ft_strlcpy(clist->errmsg, FT_ERR_ILL_ENTRY, ft_strlen(FT_ERR_ILL_ENTRY) + 1); return (-1); } return (0); } int8_t ft_check_map_line(char *line, uint8_t l, t_cub *clist) { size_t i; i = -1; while (line[++i]) { if (l != 1) { if (ft_first_checks(line, i, clist) < 0) return (-1); } else { if (ft_second_checks(line, i, clist) < 0) return (-1); } } if (ft_check_side_walls(line, i, clist) < 0) return (-1); if (ft_get_line_len(line) != clist->mlist->map_w) { ft_strlcpy(clist->errmsg, FT_ERR_MAP_LEN, ft_strlen(FT_ERR_MAP_LEN) + 1); return (-1); } return (0); }