diff options
Diffstat (limited to 'src/ft_check_map_line.c')
-rw-r--r-- | src/ft_check_map_line.c | 66 |
1 files changed, 55 insertions, 11 deletions
diff --git a/src/ft_check_map_line.c b/src/ft_check_map_line.c index 9e4e484..1009aa3 100644 --- a/src/ft_check_map_line.c +++ b/src/ft_check_map_line.c @@ -15,6 +15,38 @@ #include <stddef.h> #include <stdint.h> +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) { @@ -32,32 +64,44 @@ size_t 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 = 0; - while (line[i]) + i = -1; + while (line[++i]) { if (l != 1) { - if (!ft_ischarset("012NSEW ", line[i])) - return (-1); - if (ft_ischarset("NSEW", line[i])) - clist->mlist->isspawn += 1; - if (clist->mlist->isspawn > 1) + if (ft_first_checks(line, i, clist) < 0) return (-1); } else { - if (!ft_ischarset("1 ", line[i])) + if (ft_second_checks(line, i, clist) < 0) return (-1); } - i++; } - if (line[0] != '1' || line[i - 1] != '1' - || ft_get_line_len(line) != clist->mlist->map_w) + 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); } |