diff options
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | inc/cub3d.h | 5 | ||||
-rw-r--r-- | src/ft_check_empty_line.c | 16 | ||||
-rw-r--r-- | src/ft_free_words.c | 3 | ||||
-rw-r--r-- | src/ft_get_res.c | 9 | ||||
-rw-r--r-- | src/ft_get_sprite_tex.c | 29 | ||||
-rw-r--r-- | src/ft_get_tex.c | 44 | ||||
-rw-r--r-- | src/ft_parse_map.c | 17 | ||||
-rw-r--r-- | src/main.c | 1 |
9 files changed, 83 insertions, 42 deletions
@@ -25,6 +25,7 @@ SRCS_NAME += ft_parse_map.c SRCS_NAME += ft_get_res.c SRCS_NAME += ft_get_tex.c SRCS_NAME += ft_get_sprite_tex.c +SRCS_NAME += ft_check_empty_line.c SRCS_NAME += ft_free_words.c SRCS_NAME += ft_map_error.c SRCS_NAME += ft_init_winlx.c diff --git a/inc/cub3d.h b/inc/cub3d.h index e837a34..49f77ec 100644 --- a/inc/cub3d.h +++ b/inc/cub3d.h @@ -48,7 +48,10 @@ void ft_drawsquare(t_win *wlist, int a, int b); int ft_get_res(int fd, t_win *wlist); int ft_get_tex(int fd, t_win *wlist); int ft_get_sprite_tex(int fd, t_win *wlist); -void ft_free_words(char **words); +void ft_check_empty_line(int fd, + unsigned int linum, + t_win *wlist); +void ft_free_words(char **words, char *line); int ft_map_error(unsigned int linum, t_win *wlist); int ft_init_winlx(t_win *wlist); diff --git a/src/ft_check_empty_line.c b/src/ft_check_empty_line.c new file mode 100644 index 0000000..7f58205 --- /dev/null +++ b/src/ft_check_empty_line.c @@ -0,0 +1,16 @@ +#include <libft.h> +#include <cub3d.h> + +void +ft_check_empty_line(int fd, unsigned int linum, t_win *wlist) +{ + char *line; + + get_next_line(fd, &line); + if (*line) + { + ft_memdel(line); + ft_map_error(linum, wlist); + } + ft_memdel(line); +} diff --git a/src/ft_free_words.c b/src/ft_free_words.c index 3231791..b09b891 100644 --- a/src/ft_free_words.c +++ b/src/ft_free_words.c @@ -1,10 +1,11 @@ #include <libft.h> void -ft_free_words(char **words) +ft_free_words(char **words, char *line) { size_t i; + ft_memdel(line); i = 0; while (words[i]) { diff --git a/src/ft_get_res.c b/src/ft_get_res.c index 79df8a3..8cf1809 100644 --- a/src/ft_get_res.c +++ b/src/ft_get_res.c @@ -21,15 +21,20 @@ ft_get_res(int fd, t_win *wlist) get_next_line(fd, &line); if (!(words = ft_split(line, ' '))) + { + ft_memdel(line); return (ft_exit(5, wlist)); + } if (!(*words) || ft_strcmp(*words, "R") || !(*(words + 1)) || !(*(words + 2)) || (*(words + 3))) + { + ft_free_words(words, line); return (ft_map_error(1, wlist)); + } ft_checkdigit(words[1], wlist); ft_checkdigit(words[2], wlist); wlist->x_size = ft_atoi(words[1]); wlist->y_size = ft_atoi(words[2]); - ft_free_words(words); - ft_memdel(line); + ft_free_words(words, line); return (0); } diff --git a/src/ft_get_sprite_tex.c b/src/ft_get_sprite_tex.c index 5dbe4b4..4cec41a 100644 --- a/src/ft_get_sprite_tex.c +++ b/src/ft_get_sprite_tex.c @@ -2,20 +2,6 @@ #include <cub3d.h> #include <stdlib.h> -static void -ft_check_empty_line(int fd, unsigned int linum, t_win *wlist) -{ - char *line; - - get_next_line(fd, &line); - if (*line) - { - ft_memdel(line); - ft_map_error(linum, wlist); - } - ft_memdel(line); -} - int ft_get_sprite_tex(int fd, t_win *wlist) { @@ -23,22 +9,25 @@ ft_get_sprite_tex(int fd, t_win *wlist) char **words; size_t len; - ft_check_empty_line(fd, 6, wlist); get_next_line(fd, &line); if (!(words = ft_split(line, ' '))) + { + ft_memdel(line); return (ft_exit(5, wlist)); - if (!(*words) || ft_strcmp(*words, "S") - || !(*(words + 1)) || (*(words + 2))) + } + if (!(*words) || ft_strcmp(*words, "S") || !words[1] || words[2]) { - ft_free_words(words); + ft_free_words(words, line); return (ft_map_error(7, wlist)); } ft_memdel(wlist->clist->sprite_path); len = ft_strlen(*(words + 1)); if (!(wlist->clist->sprite_path = (char*)malloc((len + 1) * sizeof(char)))) + { + ft_free_words(words, line); return (-1); + } ft_strlcpy(wlist->clist->sprite_path, *(words + 1), len + 1); - ft_free_words(words); - ft_memdel(line); + ft_free_words(words, line); return (0); } diff --git a/src/ft_get_tex.c b/src/ft_get_tex.c index 652f4af..0e394dc 100644 --- a/src/ft_get_tex.c +++ b/src/ft_get_tex.c @@ -11,20 +11,25 @@ ft_get_tex_no(int fd, t_win *wlist) get_next_line(fd, &line); if (!(words = ft_split(line, ' '))) + { + ft_memdel(line); return (ft_exit(5, wlist)); + } if (!(*words) || ft_strcmp(*words, "NO") || !(*(words + 1)) || (*(words + 2))) { - ft_free_words(words); + ft_free_words(words, line); return (ft_map_error(2, wlist)); } ft_memdel(wlist->clist->no_tex_path); len = ft_strlen(*(words + 1)); if (!(wlist->clist->no_tex_path = (char*)malloc((len + 1) * sizeof(char)))) + { + ft_free_words(words, line); return (-1); + } ft_strlcpy(wlist->clist->no_tex_path, *(words + 1), len + 1); - ft_free_words(words); - ft_memdel(line); + ft_free_words(words, line); return (0); } @@ -37,20 +42,25 @@ ft_get_tex_so(int fd, t_win *wlist) get_next_line(fd, &line); if (!(words = ft_split(line, ' '))) + { + ft_memdel(line); return (ft_exit(5, wlist)); + } if (!(*words) || ft_strcmp(*words, "SO") || !(*(words + 1)) || (*(words + 2))) { - ft_free_words(words); + ft_free_words(words, line); return (ft_map_error(3, wlist)); } ft_memdel(wlist->clist->so_tex_path); len = ft_strlen(*(words + 1)); if (!(wlist->clist->so_tex_path = (char*)malloc((len + 1) * sizeof(char)))) + { + ft_free_words(words, line); return (-1); + } ft_strlcpy(wlist->clist->so_tex_path, *(words + 1), len + 1); - ft_free_words(words); - ft_memdel(line); + ft_free_words(words, line); return (0); } @@ -63,20 +73,25 @@ ft_get_tex_we(int fd, t_win *wlist) get_next_line(fd, &line); if (!(words = ft_split(line, ' '))) + { + ft_memdel(line); return (ft_exit(5, wlist)); + } if (!(*words) || ft_strcmp(*words, "WE") || !(*(words + 1)) || (*(words + 2))) { - ft_free_words(words); + ft_free_words(words, line); return (ft_map_error(4, wlist)); } ft_memdel(wlist->clist->we_tex_path); len = ft_strlen(*(words + 1)); if (!(wlist->clist->we_tex_path = (char*)malloc((len + 1) * sizeof(char)))) + { + ft_free_words(words, line); return (-1); + } ft_strlcpy(wlist->clist->we_tex_path, *(words + 1), len + 1); - ft_free_words(words); - ft_memdel(line); + ft_free_words(words, line); return (0); } @@ -89,20 +104,25 @@ ft_get_tex_ea(int fd, t_win *wlist) get_next_line(fd, &line); if (!(words = ft_split(line, ' '))) + { + ft_memdel(line); return (ft_exit(5, wlist)); + } if (!(*words) || ft_strcmp(*words, "EA") || !(*(words + 1)) || (*(words + 2))) { - ft_free_words(words); + ft_free_words(words, line); return (ft_map_error(5, wlist)); } ft_memdel(wlist->clist->ea_tex_path); len = ft_strlen(*(words + 1)); if (!(wlist->clist->ea_tex_path = (char*)malloc((len + 1) * sizeof(char)))) + { + ft_free_words(words, line); return (-1); + } ft_strlcpy(wlist->clist->ea_tex_path, *(words + 1), len + 1); - ft_free_words(words); - ft_memdel(line); + ft_free_words(words, line); return (0); } diff --git a/src/ft_parse_map.c b/src/ft_parse_map.c index 011dfcf..d078337 100644 --- a/src/ft_parse_map.c +++ b/src/ft_parse_map.c @@ -14,7 +14,7 @@ ft_check_cub(const char *map_path, t_win *wlist) { ft_dprintf(STDERR_FILENO, "Error\n"); ft_dprintf(STDERR_FILENO, "\033[31;1mMap is not a .cub\033[0m\n"); - ft_free_words(words); + ft_free_words(words, NULL); ft_exit(2, wlist); } i = 0; @@ -24,10 +24,10 @@ ft_check_cub(const char *map_path, t_win *wlist) { ft_dprintf(STDERR_FILENO, "Error\n"); ft_dprintf(STDERR_FILENO, "\033[31;1mMap is not a .cub\033[0m\n"); - ft_free_words(words); + ft_free_words(words, NULL); ft_exit(2, wlist); } - ft_free_words(words); + ft_free_words(words, NULL); } /* @@ -48,7 +48,14 @@ ft_parse_map(const char *map_path, t_win *wlist) ft_exit(2, wlist); } ft_get_res(fd, wlist); - if (ft_get_tex(fd, wlist) < 0 || - ft_get_sprite_tex(fd, wlist) < 0) + if (ft_get_tex(fd, wlist) < 0) return ; + ft_check_empty_line(fd, 6, wlist); + if (ft_get_sprite_tex(fd, wlist) < 0) + return ; + ft_printf("[%s]\n", wlist->clist->no_tex_path); + ft_printf("[%s]\n", wlist->clist->so_tex_path); + ft_printf("[%s]\n", wlist->clist->we_tex_path); + ft_printf("[%s]\n", wlist->clist->ea_tex_path); + ft_printf("[%s]\n", wlist->clist->sprite_path); } @@ -1,7 +1,6 @@ #include <libft.h> #include <mlx.h> #include <cub3d.h> -#include <stdlib.h> int main(void) |