From 0e55e163397f2b109321ad284d298e60ee3b97a7 Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Fri, 24 Jan 2020 18:52:32 +0100
Subject: cleaner frees

---
 Makefile                  |  1 +
 inc/cub3d.h               |  5 ++++-
 src/ft_check_empty_line.c | 16 ++++++++++++++++
 src/ft_free_words.c       |  3 ++-
 src/ft_get_res.c          |  9 +++++++--
 src/ft_get_sprite_tex.c   | 29 +++++++++--------------------
 src/ft_get_tex.c          | 44 ++++++++++++++++++++++++++++++++------------
 src/ft_parse_map.c        | 17 ++++++++++++-----
 src/main.c                |  1 -
 9 files changed, 83 insertions(+), 42 deletions(-)
 create mode 100644 src/ft_check_empty_line.c

diff --git a/Makefile b/Makefile
index cdffb1a..02e07cd 100644
--- a/Makefile
+++ b/Makefile
@@ -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);
 }
diff --git a/src/main.c b/src/main.c
index 7c9251a..4d7b341 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,7 +1,6 @@
 #include <libft.h>
 #include <mlx.h>
 #include <cub3d.h>
-#include <stdlib.h>
 
 int
 	main(void)
-- 
cgit v1.2.3