From 2fd013d7583e0388d50ff9895c5e474e2d76f79f Mon Sep 17 00:00:00 2001
From: Rudy Bousset <rbousset@z2r4p3.le-101.fr>
Date: Mon, 17 Feb 2020 15:31:11 +0100
Subject: Better parse for textures and colors

---
 inc/cub3d_defines.h | 14 ++++++---
 map/map_one.cub     | 16 +++++-----
 src/ft_get_colors.c | 87 ++++++++++++++++++++++++++++++++++++++++++-----------
 src/ft_get_res.c    |  1 +
 src/ft_get_sprite.c | 14 +++++++++
 src/ft_get_tex.c    | 52 ++++++++++++++++++++++++++++++++
 6 files changed, 153 insertions(+), 31 deletions(-)

diff --git a/inc/cub3d_defines.h b/inc/cub3d_defines.h
index 14ec61a..e9a9a7d 100644
--- a/inc/cub3d_defines.h
+++ b/inc/cub3d_defines.h
@@ -57,18 +57,22 @@
 ** ====== MAP ERROR MSG ======
 */
 
+#	define FT_ERR_ALLOCATE			"allocation error"
+#	define FT_ERR_READ				"read error"
 #	define FT_ERR_NOT_A_CUB			"given map is not a .cub"
-#	define FT_ERR_ARGS				"too many arguments"
+#	define FT_ERR_ARGS				"too many or to few arguments"
 #	define FT_ERR_RES_SMALL			"resolution is too small"
 #	define FT_ERR_RES_ALPHA			"resolution should be digits only"
-#	define FT_ERR_MAP_L_L			"last line is invalid"
-#	define FT_ERR_UNFINISHED		"no map"
-#	define FT_ERR_READ				"read error"
+#	define FT_ERR_NOT_A_XPM			"given texture is not a .xpm"
+#	define FT_ERR_COLOR_ALPHA		"colors should be digits only"
+#	define FT_ERR_COLOR_MAX			"colors should be maximum 255"
+#	define FT_ERR_COLOR_ARGS		"colors three numbers separated by commas"
 #	define FT_ERR_ILL_ENTRY			"illegal map entry"
+#	define FT_ERR_UNFINISHED		"no map"
 #	define FT_ERR_ALR_SET			"duplicate entry"
-#	define FT_ERR_ALLOCATE			"allocation error"
 #	define FT_ERR_ILL_MAP			"map contains illegal char"
 #	define FT_ERR_MULT_SPAWN		"multiple spawn points"
+#	define FT_ERR_MAP_L_L			"last line is invalid"
 
 /*
 ** ====== MISSING ERROR MSG ======
diff --git a/map/map_one.cub b/map/map_one.cub
index 909b797..0738091 100644
--- a/map/map_one.cub
+++ b/map/map_one.cub
@@ -1,13 +1,13 @@
-R 1200 1
+R 1200 800
 
-NO ./path_to_the_north_texture
-SO ./path_to_the_south_texture
-EA ./path_to_the_east_texture
-WE ./path_to_the_west_texture
-S ./path_to_the_sprite_texture
+NO ./path_to_the_north_texture.xpm
+SO ./path_to_the_south_texture.xpm
+EA ./path_to_the_east_texture.xpm
+WE ./path_to_the_west_texture.xpm
+S ./path_to_the_sprite_texture.xpm
 
 C 30,130,255
-F 150,150,145
+F 150,150,123
 
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
@@ -19,7 +19,7 @@ F 150,150,145
 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 0 1 0 0 1 0 0 0 1
 1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 0 0 0 0 0 0 1 0 0 0 1
 1 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 1
-1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 1 0 0 1 0 W 0 1
+1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 1 0 0 1 0 N 0 1
 1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 1 0 0 0 1
 1 1 1 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 0 1 1 1 1 0 0 0 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
diff --git a/src/ft_get_colors.c b/src/ft_get_colors.c
index 869dd71..3d34284 100644
--- a/src/ft_get_colors.c
+++ b/src/ft_get_colors.c
@@ -13,17 +13,58 @@
 #include <libft.h>
 #include <cub3d.h>
 #include <stddef.h>
+#include <stdint.h>
 
-static int
-	ft_check_digits(const char *word)
+static int8_t
+ft_check_color_digits(char **num, t_cub *clist)
 {
 	size_t	i;
+	uint8_t	j;
 
 	i = 0;
-	while (ft_isdigit(word[i]))
-		i++;
-	if (i != ft_strlen(word))
+	j = 0;
+	while (j < 3)
+	{
+		while (ft_isdigit(num[j][i]))
+			i++;
+		if (i != ft_strlen(num[j]))
+		{
+			ft_free_words(num);
+			ft_strlcpy(clist->errmsg, FT_ERR_COLOR_ALPHA,
+					   ft_strlen(FT_ERR_COLOR_ALPHA) + 1);
+
+			return (-1);
+		}
+		i = 0;
+		j++;
+	}
+	return (0);
+}
+
+static int8_t
+	ft_check_max_int(char **num, t_cub *clist)
+{
+	if (ft_atoi(num[0]) > 255 || ft_atoi(num[1]) > 255
+		|| ft_atoi(num[2]) > 255)
+	{
+		ft_strlcpy(clist->errmsg, FT_ERR_COLOR_MAX,
+				ft_strlen(FT_ERR_COLOR_MAX) + 1);
+		ft_free_words(num);
 		return (-1);
+	}
+	return (0);
+}
+
+static int8_t
+	ft_check_nums_amount(char **num, t_cub *clist)
+{
+	if (!num[0] || !num[1] || !num[2] || num[3])
+	{
+		ft_strlcpy(clist->errmsg, FT_ERR_COLOR_ARGS,
+				ft_strlen(FT_ERR_COLOR_ARGS) + 1);
+		ft_free_words(num);
+		return (-1);
+	}
 	return (0);
 }
 
@@ -33,17 +74,22 @@ int8_t
 	char	**num;
 
 	if (!(*words) || !words[1] || words[2])
+	{
+		ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1);
 		return (-1);
+	}
 	if (!(num = ft_split(words[1], ',')))
-		return (-1);
-	if (!num[0] || !num[1] || !num[2] || num[3] ||
-		ft_check_digits(num[0]) || ft_check_digits(num[1]) ||
-		ft_check_digits(num[2]) || ft_atoi(num[0]) > 255 ||
-		ft_atoi(num[1]) > 255 || ft_atoi(num[2]) > 255)
 	{
-		ft_free_words(num);
+		ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE,
+				ft_strlen(FT_ERR_ALLOCATE) + 1);
 		return (-1);
 	}
+	if (ft_check_nums_amount(num, clist) < 0)
+		return (-1);
+	if (ft_check_color_digits(num, clist) < 0)
+		return (-1);
+	if (ft_check_max_int(num, clist) < 0)
+		return (-1);
 	clist->f_rgb.r = ft_atoi(num[0]);
 	clist->f_rgb.g = ft_atoi(num[1]);
 	clist->f_rgb.b = ft_atoi(num[2]);
@@ -56,18 +102,23 @@ int8_t
 {
 	char	**num;
 
-	if (!(*words) || ft_strcmp(*words, "C") || !words[1] || words[2])
+	if (!(*words) || !words[1] || words[2])
+	{
+		ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1);
 		return (-1);
+	}
 	if (!(num = ft_split(words[1], ',')))
-		return (-1);
-	if (!num[0] || !num[1] || !num[2] || num[3] ||
-		ft_check_digits(num[0]) || ft_check_digits(num[1]) ||
-		ft_check_digits(num[2]) || ft_atoi(num[0]) > 255 ||
-		ft_atoi(num[1]) > 255 || ft_atoi(num[2]) > 255)
 	{
-		ft_free_words(num);
+		ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE,
+				ft_strlen(FT_ERR_ALLOCATE) + 1);
 		return (-1);
 	}
+	if (ft_check_nums_amount(num, clist) < 0)
+		return (-1);
+	if (ft_check_color_digits(num, clist) < 0)
+		return (-1);
+	if (ft_check_max_int(num, clist) < 0)
+		return (-1);
 	clist->c_rgb.r = ft_atoi(num[0]);
 	clist->c_rgb.g = ft_atoi(num[1]);
 	clist->c_rgb.b = ft_atoi(num[2]);
diff --git a/src/ft_get_res.c b/src/ft_get_res.c
index e51a66f..8607f62 100644
--- a/src/ft_get_res.c
+++ b/src/ft_get_res.c
@@ -12,6 +12,7 @@
 
 #include <libft.h>
 #include <cub3d.h>
+#include <stdint.h>
 
 static int8_t
 	ft_checkdigit(const char *word, t_cub *clist)
diff --git a/src/ft_get_sprite.c b/src/ft_get_sprite.c
index cdea85c..791f51f 100644
--- a/src/ft_get_sprite.c
+++ b/src/ft_get_sprite.c
@@ -12,14 +12,28 @@
 
 #include <libft.h>
 #include <cub3d.h>
+#include <stdint.h>
 
 int8_t
 	ft_get_sprite(char **words, t_cub *clist)
 {
 	if (!(*words) || !words[1] || words[2])
+	{
+		ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1);
 		return (-1);
+	}
+	if (ft_check_ext(*(words + 1), ".xpm") < 0)
+	{
+		ft_strlcpy(clist->errmsg, FT_ERR_NOT_A_XPM,
+				ft_strlen(FT_ERR_NOT_A_XPM) + 1);
+		return (-1);
+	}
 	ft_memdel((void**)&clist->mlist->sprite_path);
 	if (!(clist->mlist->sprite_path = ft_strdup(*(words + 1))))
+	{
+		ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE,
+				ft_strlen(FT_ERR_ALLOCATE) + 1);
 		return (-1);
+	}
 	return (0);
 }
diff --git a/src/ft_get_tex.c b/src/ft_get_tex.c
index 1965e72..ad093ed 100644
--- a/src/ft_get_tex.c
+++ b/src/ft_get_tex.c
@@ -18,10 +18,23 @@ int8_t
 	ft_get_tex_no(char **words, t_cub *clist)
 {
 	if (!(*words) || !(*(words + 1)) || (*(words + 2)))
+	{
+		ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1);
 		return (-1);
+	}
+	if (ft_check_ext(*(words + 1), ".xpm") < 0)
+	{
+		ft_strlcpy(clist->errmsg, FT_ERR_NOT_A_XPM,
+				ft_strlen(FT_ERR_NOT_A_XPM) + 1);
+		return (-1);
+	}
 	ft_memdel((void**)&clist->mlist->no_tex_path);
 	if (!(clist->mlist->no_tex_path = ft_strdup(*(words + 1))))
+	{
+		ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE,
+				ft_strlen(FT_ERR_ALLOCATE) + 1);
 		return (-1);
+	}
 	return (0);
 }
 
@@ -29,10 +42,23 @@ int8_t
 	ft_get_tex_so(char **words, t_cub *clist)
 {
 	if (!(*words) || !(*(words + 1)) || (*(words + 2)))
+	{
+		ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1);
+		return (-1);
+	}
+	if (ft_check_ext(*(words + 1), ".xpm") < 0)
+	{
+		ft_strlcpy(clist->errmsg, FT_ERR_NOT_A_XPM,
+				ft_strlen(FT_ERR_NOT_A_XPM) + 1);
 		return (-1);
+	}
 	ft_memdel((void**)&clist->mlist->so_tex_path);
 	if (!(clist->mlist->so_tex_path = ft_strdup(*(words + 1))))
+	{
+		ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE,
+				ft_strlen(FT_ERR_ALLOCATE) + 1);
 		return (-1);
+	}
 	return (0);
 }
 
@@ -40,10 +66,23 @@ int8_t
 	ft_get_tex_ea(char **words, t_cub *clist)
 {
 	if (!(*words) || !(*(words + 1)) || (*(words + 2)))
+	{
+		ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1);
 		return (-1);
+	}
+	if (ft_check_ext(*(words + 1), ".xpm") < 0)
+	{
+		ft_strlcpy(clist->errmsg, FT_ERR_NOT_A_XPM,
+				ft_strlen(FT_ERR_NOT_A_XPM) + 1);
+		return (-1);
+	}
 	ft_memdel((void**)&clist->mlist->ea_tex_path);
 	if (!(clist->mlist->ea_tex_path = ft_strdup(*(words + 1))))
+	{
+		ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE,
+				ft_strlen(FT_ERR_ALLOCATE) + 1);
 		return (-1);
+	}
 	return (0);
 }
 
@@ -51,9 +90,22 @@ int8_t
 	ft_get_tex_we(char **words, t_cub *clist)
 {
 	if (!(*words) || !(*(words + 1)) || (*(words + 2)))
+	{
+		ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1);
+		return (-1);
+	}
+	if (ft_check_ext(*(words + 1), ".xpm") < 0)
+	{
+		ft_strlcpy(clist->errmsg, FT_ERR_NOT_A_XPM,
+				ft_strlen(FT_ERR_NOT_A_XPM) + 1);
 		return (-1);
+	}
 	ft_memdel((void**)&clist->mlist->we_tex_path);
 	if (!(clist->mlist->we_tex_path = ft_strdup(*(words + 1))))
+	{
+		ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE,
+				ft_strlen(FT_ERR_ALLOCATE) + 1);
 		return (-1);
+	}
 	return (0);
 }
-- 
cgit v1.2.3