From 38a81ff5a1dcb2cd07e10330fcbedf665ff7bc05 Mon Sep 17 00:00:00 2001 From: Rudy Bousset Date: Wed, 29 Jan 2020 19:14:24 +0100 Subject: C'est bav --- Makefile | 1 + inc/cub3d.h | 5 ++++- src/ft_check_map_line.c | 49 ++++++++++++++++++++++++++++++++++++++++++++ src/ft_get_map_redo.c | 54 +++++++++++++++++++++++++++++++------------------ 4 files changed, 88 insertions(+), 21 deletions(-) create mode 100644 src/ft_check_map_line.c diff --git a/Makefile b/Makefile index 817049f..273b034 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,7 @@ SRCS_NAME += ft_get_colors.c # SRCS_NAME += ft_get_map.c SRCS_NAME += ft_get_map_redo.c SRCS_NAME += ft_check_missing.c +SRCS_NAME += ft_check_map_line.c # SRCS_NAME += ft_check_empty_line.c SRCS_NAME += ft_free_words.c SRCS_NAME += ft_map_error.c diff --git a/inc/cub3d.h b/inc/cub3d.h index 40475eb..c8bf278 100644 --- a/inc/cub3d.h +++ b/inc/cub3d.h @@ -1,8 +1,8 @@ #ifndef CUB3D_H #define CUB3D_H -#include #include +#include #ifndef FT_W_KEY #define FT_W_KEY 13 @@ -38,6 +38,7 @@ typedef struct s_cub char *sprite_path; int f_color; int c_color; + char *mapl; char **map; size_t map_w; size_t line_chk; @@ -63,6 +64,8 @@ int ft_get_c_color(char **words, t_cub *clist); int ft_get_map_first_line(char *line, t_cub *clist); int ft_get_map_core(int fd, t_cub *clist); int ft_check_missing(t_cub *clist); +int8_t ft_check_map_line(char *line, uint8_t l, t_cub *clist); +size_t ft_get_line_len(char *line); int ft_missing_error(const char *err, t_cub *clist); void ft_free_words(char **words); int ft_map_error(t_cub *clist); diff --git a/src/ft_check_map_line.c b/src/ft_check_map_line.c new file mode 100644 index 0000000..1c8c6c6 --- /dev/null +++ b/src/ft_check_map_line.c @@ -0,0 +1,49 @@ +#include +#include +#include + +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); +} + +int8_t +ft_check_map_line(char *line, uint8_t l, t_cub *clist) +{ + size_t i; + + i = 0; + while (line[i]) + { + if (l != 1) + { + if (!ft_ischarset("012NSEW ", line[i])) + return (-1); + if (ft_ischarset("NSEW", line[i])) + clist->nsew += 1; + if (clist->nsew > 1) + return (-1); + } + else + { + if (!ft_ischarset("1 ", line[i])) + return (-1); + } + i++; + } + if (line[0] != '1' || line[i - 1] != '1') + return (-1); + return (0); +} diff --git a/src/ft_get_map_redo.c b/src/ft_get_map_redo.c index 869f432..e337849 100644 --- a/src/ft_get_map_redo.c +++ b/src/ft_get_map_redo.c @@ -1,34 +1,38 @@ #include #include #include -#include +#include -static int8_t -ft_check_line(char *line, uint8_t l, t_cub *clist) +static void +ft_linecpy(char *line, char *mapl, size_t start) { size_t i; + size_t j; + size_t slen; i = 0; - while (line[i]) + j = start; + slen = ft_strlen(line); + while (i < slen && line[i]) { - if (l != 1) - { - if (!ft_ischarset("012NSEW ", line[i])) - return (-1); - if (ft_ischarset("NSEW", line[i])) - clist->nsew += 1; - if (clist->nsew > 1) - return (-1); - } - else - { - if (!ft_ischarset("1 ", line[i])) - return (-1); - } + while (line[i] == ' ') + i++; + mapl[j] = line[i]; i++; + j++; } - if (line[0] != '1' || line[i - 1] != '1') + mapl[j] = '\0'; +} + +static int8_t +ft_cat_mapl(char *line, size_t i, t_cub *clist) +{ + if (!(clist->mapl = (char *)ft_nrealloc(clist->mapl, + ((clist->map_w + 1) * i) * sizeof(char), + ((clist->map_w + 1) * (i + 1)) * sizeof(char)))) return (-1); + ft_linecpy(line, clist->mapl, (clist->map_w + 1) * i); + ft_printf("mapl >>>> [%s]\n", clist->mapl); return (0); } @@ -40,11 +44,19 @@ ft_get_map_first_line(char *line, t_cub *clist) ft_memdel(line); return (-1); } - if (ft_check_line(line, 1, clist) < 0) + if (ft_check_map_line(line, 1, clist) < 0) { ft_memdel(line); return (-1); } + clist->map_w = ft_get_line_len(line); + clist->mapl = NULL; + if ((clist->mapl = ft_cat_mapl(line, 0, clist)) < 0) + { + ft_memdel(line); + return (-1); + } + ft_memdel(line); return (1); } @@ -52,7 +64,9 @@ int ft_get_map_core(int fd, t_cub *clist) { int ret; + size_t i; + i = 1; (void)fd; (void)ret; (void)clist; -- cgit v1.2.3