From 030fe8c3316d1e1f5f3dea81c3fffd8e00c16afa Mon Sep 17 00:00:00 2001
From: Rudy Bousset <rbousset@z2r4p3.le-101.fr>
Date: Sat, 15 Feb 2020 20:29:21 +0100
Subject: Work in progress, res now

---
 src/ft_check_map_line.c  | 40 ++++++++++++++++++++++++++++++++++------
 src/ft_exit.c            |  3 ---
 src/ft_get_map.c         |  6 ++++++
 src/ft_get_res.c         | 25 ++++++++++++++++---------
 src/ft_get_screen_size.c |  4 ++++
 src/ft_init_lists.c      |  4 ++--
 src/ft_map_error.c       |  2 +-
 src/ft_parse_map.c       |  4 ++--
 8 files changed, 65 insertions(+), 23 deletions(-)

(limited to 'src')

diff --git a/src/ft_check_map_line.c b/src/ft_check_map_line.c
index 9e4e484..b41d052 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)
 {
@@ -42,16 +74,12 @@ int8_t
 	{
 		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++;
diff --git a/src/ft_exit.c b/src/ft_exit.c
index 53af7f9..f0a94a3 100644
--- a/src/ft_exit.c
+++ b/src/ft_exit.c
@@ -33,7 +33,6 @@ static void
 		ft_memdel((void**)&clist->wlist->winptr);
 	ft_memdel((void**)&clist->wlist->wlx);
 	ft_memdel((void**)&clist->wlist);
-	ft_memdel((void**)&clist->errmsg);
 	ft_memdel((void**)&clist);
 }
 
@@ -44,8 +43,6 @@ int
 		mlx_destroy_window(clist->wlist->wlx, clist->wlist->winptr);
 	ft_free_lists(clist);
 	ft_printf("Exiting program\n");
-	if (exit_code < 0 || exit_code > 0)
-		ft_printf("Exit code: %hhu\n", exit_code);
 	exit(exit_code);
 	return (0);
 }
diff --git a/src/ft_get_map.c b/src/ft_get_map.c
index ee23c7a..cb677da 100644
--- a/src/ft_get_map.c
+++ b/src/ft_get_map.c
@@ -53,7 +53,11 @@ static int8_t
 	if (!(clist->mlist->mapl = (char *)ft_nrealloc(clist->mlist->mapl,
 				((clist->mlist->map_w + 1) * i) * sizeof(char),
 				((clist->mlist->map_w + 1) * (i + 1)) * sizeof(char))))
+	{
+		ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE,
+				ft_strlen(FT_ERR_ALLOCATE) + 1);
 		return (-1);
+	}
 	ft_linecpy(line, clist->mlist->mapl, (clist->mlist->map_w + 1) * i);
 	return (0);
 }
@@ -65,12 +69,14 @@ int
 	if (!line[0])
 	{
 		ft_memdel((void**)&line);
+		ft_strlcpy(clist->errmsg, FT_ERR_READ, ft_strlen(FT_ERR_READ) + 1);
 		return (-1);
 	}
 	clist->mlist->map_w = ft_get_line_len(line);
 	if (ft_check_map_line(line, 1, clist) < 0)
 	{
 		ft_memdel((void**)&line);
+		ft_strlcpy(clist->errmsg, FT_ERR_READ, ft_strlen(FT_ERR_READ) + 1);
 		return (-1);
 	}
 	ft_memdel((void**)&clist->mlist->mapl);
diff --git a/src/ft_get_res.c b/src/ft_get_res.c
index 86a3186..e51a66f 100644
--- a/src/ft_get_res.c
+++ b/src/ft_get_res.c
@@ -14,7 +14,7 @@
 #include <cub3d.h>
 
 static int8_t
-	ft_checkdigit(const char *word)
+	ft_checkdigit(const char *word, t_cub *clist)
 {
 	size_t	i;
 
@@ -22,7 +22,11 @@ static int8_t
 	while (ft_isdigit(word[i]))
 		i++;
 	if (i != ft_strlen(word))
+	{
+		ft_strlcpy(clist->errmsg, FT_ERR_RES_ALPHA,
+				ft_strlen(FT_ERR_RES_ALPHA) + 1);
 		return (-1);
+	}
 	return (0);
 }
 
@@ -34,20 +38,23 @@ int8_t
 	wlist = clist->wlist;
 	if (!(*words + 0) || !(*(words + 1)) ||
 		!(*(words + 2)) || (*(words + 3)))
+	{
+		ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1);
 		return (-1);
-	if ((ft_checkdigit(words[1]) < 0) ||
-		(ft_checkdigit(words[2]) < 0))
+	}
+	if ((ft_checkdigit(words[1], clist) < 0) ||
+		(ft_checkdigit(words[2], clist) < 0))
 		return (-1);
 	wlist->x_size = ft_atoi(words[1]);
 	wlist->y_size = ft_atoi(words[2]);
-	if (wlist->x_size < 1
-		|| wlist->y_size < 1)
+	if (wlist->x_size <= 1
+		|| wlist->y_size <= 1)
+	{
+		ft_strlcpy(clist->errmsg, FT_ERR_RES_SMALL,
+					ft_strlen(FT_ERR_RES_SMALL) + 1);
 		return (-1);
+	}
 	if (ft_get_screen_size(wlist) < 0)
 		return (-1);
-	if (wlist->x_size > wlist->x_max_size)
-		wlist->x_size = wlist->x_max_size;
-	if (wlist->y_size > wlist->y_max_size)
-		wlist->y_size = wlist->y_max_size;
 	return (0);
 }
diff --git a/src/ft_get_screen_size.c b/src/ft_get_screen_size.c
index fe368c4..2b437c3 100644
--- a/src/ft_get_screen_size.c
+++ b/src/ft_get_screen_size.c
@@ -24,5 +24,9 @@ int8_t
 		return (-1);
 	wlist->x_max_size = ft_atoi(words[0]);
 	wlist->y_max_size = ft_atoi(words[1]);
+	if (wlist->x_size > wlist->x_max_size)
+		wlist->x_size = wlist->x_max_size;
+	if (wlist->y_size > wlist->y_max_size)
+		wlist->y_size = wlist->y_max_size;
 	return (ft_free_words(words));
 }
diff --git a/src/ft_init_lists.c b/src/ft_init_lists.c
index 459e522..95f0488 100644
--- a/src/ft_init_lists.c
+++ b/src/ft_init_lists.c
@@ -71,9 +71,9 @@ static t_cub
 	if (!(clist = (t_cub*)malloc(sizeof(t_cub))))
 		return (NULL);
 	if (!(clist->plist = ft_init_player()) ||
-		!(clist->mlist = ft_init_map()) ||
-		!(clist->errmsg = ft_calloc(1, sizeof(char))))
+		!(clist->mlist = ft_init_map()))
 		return (NULL);
+	ft_bzero(clist->errmsg, 40);
 	clist->minimap = 0;
 	clist->f_rgb = ft_init_rgb();
 	clist->c_rgb = ft_init_rgb();
diff --git a/src/ft_map_error.c b/src/ft_map_error.c
index cb099dc..5d5ab68 100644
--- a/src/ft_map_error.c
+++ b/src/ft_map_error.c
@@ -19,7 +19,7 @@ int
 {
 	ft_dprintf(STDERR_FILENO, "Error\n");
 	ft_dprintf(STDERR_FILENO,
-				"\033[1;31mMap error: line %3zu: %s\033[0m\n",
+				"\033[1;31mMap error: line %zu: %s\033[0m\n",
 				clist->mlist->line_chk,
 				errmsg);
 	return (ft_exit(4, clist));
diff --git a/src/ft_parse_map.c b/src/ft_parse_map.c
index 0eef871..c4aa011 100644
--- a/src/ft_parse_map.c
+++ b/src/ft_parse_map.c
@@ -90,12 +90,12 @@ void
 	if (fd < 0)
 		ft_no_map_error(clist);
 	ret = 1;
-	while (ret != 12 && ret != -1)
+	while (ret != 12 && ret >= 0)
 		ret = ft_parse_it(fd, clist);
 	(ret == -2) ? (ft_map_error(FT_ERR_ALR_SET, clist)) : 0;
 	(ret == -1) ? (ft_map_error(clist->errmsg, clist)) : 0;
 	if (ft_get_map_core(fd, clist) < 0)
-		ft_map_error(clist);
+		ft_map_error(clist->errmsg, clist);
 	ft_check_map_last_line(clist);
 	ft_get_player_spawn(clist->plist, clist);
 	ft_check_missing(clist);
-- 
cgit v1.2.3