aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRudy Bousset <rbousset@z2r4p3.le-101.fr>2020-02-17 15:31:11 +0100
committerRudy Bousset <rbousset@z2r4p3.le-101.fr>2020-02-17 15:31:11 +0100
commit2fd013d7583e0388d50ff9895c5e474e2d76f79f (patch)
tree435e687675c620c93e44823dea606a30e20f3db4 /src
parentWork in progress, res now (diff)
download42-cub3d-2fd013d7583e0388d50ff9895c5e474e2d76f79f.tar.gz
42-cub3d-2fd013d7583e0388d50ff9895c5e474e2d76f79f.tar.bz2
42-cub3d-2fd013d7583e0388d50ff9895c5e474e2d76f79f.tar.xz
42-cub3d-2fd013d7583e0388d50ff9895c5e474e2d76f79f.tar.zst
42-cub3d-2fd013d7583e0388d50ff9895c5e474e2d76f79f.zip
Better parse for textures and colors
Diffstat (limited to 'src')
-rw-r--r--src/ft_get_colors.c87
-rw-r--r--src/ft_get_res.c1
-rw-r--r--src/ft_get_sprite.c14
-rw-r--r--src/ft_get_tex.c52
4 files changed, 136 insertions, 18 deletions
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);
}