aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--Makefile2
-rw-r--r--inc/cub3d.h4
-rw-r--r--inc/cub3d_defines.h18
-rw-r--r--inc/cub3d_structs.h1
-rw-r--r--src/ft_check_ext.c13
-rw-r--r--src/ft_exit.c1
-rw-r--r--src/ft_init_lists.c3
-rw-r--r--src/ft_map_error.c6
-rw-r--r--src/ft_parse_map.c58
-rw-r--r--src/ft_select_get.c2
10 files changed, 63 insertions, 45 deletions
diff --git a/Makefile b/Makefile
index 21177eb..384955d 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,5 @@
default: all
+
#==================================================================================================#
#-------------------------------------------- Shell -----------------------------------------------#
#==================================================================================================#
@@ -51,6 +52,7 @@ SRCS_NAME += ft_init_s_ray.c
SRCS_NAME += ft_init_map.c
SRCS_NAME += ft_tex_init.c
SRCS_NAME += ft_detect.c
+SRCS_NAME += ft_check_ext.c
#--------------------------------------------------------------------------------------------------#
SRCS = $(addprefix ${SRCS_DIR},${SRCS_NAME})
#--------------------------------------------------------------------------------------------------#
diff --git a/inc/cub3d.h b/inc/cub3d.h
index 386f658..55c7a97 100644
--- a/inc/cub3d.h
+++ b/inc/cub3d.h
@@ -25,7 +25,6 @@
** 3: failed mlx init
** 4: map error
** 5: no map
-** 6: not a .cub
*/
int8_t ft_init_cub3d(t_cub **clist);
@@ -55,7 +54,7 @@ int8_t ft_check_map_line(char *line, uint8_t l, t_cub *clist);
size_t ft_get_line_len(char *line);
int ft_missing_error(const char *err, t_cub *clist);
uint8_t ft_free_words(char **words);
-int ft_map_error(t_cub *clist);
+int ft_map_error(const char *errmsg, t_cub *clist);
int ft_init_winlx(t_cub *clist);
void ft_draw_scene(t_cub *clist);
void ft_print_list(t_cub *clist);
@@ -73,5 +72,6 @@ int ft_d_key(t_cub *clist);
int ft_f1_key(t_cub *clist);
int ft_left_key(t_cub *clist);
int ft_right_key(t_cub *clist);
+int8_t ft_check_ext(const char *filep, const char *ext);
# endif
diff --git a/inc/cub3d_defines.h b/inc/cub3d_defines.h
index e6c98c5..6f8de65 100644
--- a/inc/cub3d_defines.h
+++ b/inc/cub3d_defines.h
@@ -1,3 +1,15 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* cub3d_defines.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/15 15:40:25 by rbousset #+# #+# */
+/* Updated: 2020/02/15 15:40:27 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
# ifndef CUB3D_DEFINES_H
# define CUB3D_DEFINES_H
@@ -45,6 +57,12 @@
** ====== ERROR MSG ======
*/
+# define FT_ERR_NOT_A_CUB "given map is not a .cub"
+# define FT_ERR_MAP_L_L "last line is invalid or contains illegal char"
+# define FT_ERR_UNFINISHED "unexpected file end"
+# define FT_ERR_READ "read error"
+# define FT_ERR_ILL_ENTRY "illegal map entry"
+# define FT_ERR_ALR_SET "duplicate entry"
# define FT_ERR_MISS_ELEMENT "Missing element:"
# define FT_ERR_MISS_NORTH "north side texture"
# define FT_ERR_MISS_SOUTH "south side texture"
diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h
index 1bf2bca..fce155e 100644
--- a/inc/cub3d_structs.h
+++ b/inc/cub3d_structs.h
@@ -109,6 +109,7 @@ typedef struct s_map
typedef struct s_cub
{
uint8_t minimap;
+ char *errmsg;
struct s_win *wlist;
struct s_player *plist;
struct s_map *mlist;
diff --git a/src/ft_check_ext.c b/src/ft_check_ext.c
new file mode 100644
index 0000000..f4a1e92
--- /dev/null
+++ b/src/ft_check_ext.c
@@ -0,0 +1,13 @@
+#include <libft.h>
+#include <stdint.h>
+
+int8_t
+ ft_check_ext(const char *filep, const char *ext)
+{
+ const uint8_t extlen = ft_strlen(ext);
+
+ if (ft_strncmp(filep + (ft_strlen(filep) - extlen), ext, extlen))
+ return (-1);
+ else
+ return (0);
+}
diff --git a/src/ft_exit.c b/src/ft_exit.c
index d4b69ba..53af7f9 100644
--- a/src/ft_exit.c
+++ b/src/ft_exit.c
@@ -33,6 +33,7 @@ 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);
}
diff --git a/src/ft_init_lists.c b/src/ft_init_lists.c
index dc50231..459e522 100644
--- a/src/ft_init_lists.c
+++ b/src/ft_init_lists.c
@@ -71,7 +71,8 @@ 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->mlist = ft_init_map()) ||
+ !(clist->errmsg = ft_calloc(1, sizeof(char))))
return (NULL);
clist->minimap = 0;
clist->f_rgb = ft_init_rgb();
diff --git a/src/ft_map_error.c b/src/ft_map_error.c
index 263eee8..cb099dc 100644
--- a/src/ft_map_error.c
+++ b/src/ft_map_error.c
@@ -15,10 +15,12 @@
#include <unistd.h>
int
- ft_map_error(t_cub *clist)
+ ft_map_error(const char *errmsg, t_cub *clist)
{
ft_dprintf(STDERR_FILENO, "Error\n");
ft_dprintf(STDERR_FILENO,
- "\033[1;31mMap error: line %zu\033[0m\n", clist->mlist->line_chk);
+ "\033[1;31mMap error: line %3zu: %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 0e1a1e7..0eef871 100644
--- a/src/ft_parse_map.c
+++ b/src/ft_parse_map.c
@@ -18,32 +18,6 @@
#include <stdint.h>
static void
- ft_check_cub(const char *map_path, t_cub *clist)
-{
- char **words;
- size_t i;
-
- if (!(words = ft_split(map_path, '.')))
- {
- 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_exit(6, clist);
- }
- i = 0;
- while (words[i])
- i++;
- if (ft_strncmp(words[i - 1], "cub", 3))
- {
- 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_exit(6, clist);
- }
- ft_free_words(words);
-}
-
-static void
ft_check_map_last_line(t_cub *clist)
{
t_map *ml;
@@ -58,16 +32,16 @@ static void
while (ml->map[i - 1][j])
{
if (ml->map[i - 1][j] != '1' && ml->map[i - 1][j] != '\0')
- ft_map_error(clist);
+ ft_map_error(FT_ERR_MAP_L_L, clist);
j++;
}
}
static int8_t
- ft_error_here(char *line, t_cub *clist)
+ft_error_here(const char *errmsg, char *line, t_cub *clist)
{
ft_memdel((void**)&line);
- return (ft_map_error(clist));
+ return (ft_map_error(errmsg, clist));
}
static int8_t
@@ -79,9 +53,9 @@ static int8_t
clist->mlist->line_chk += 1;
if ((ret = get_next_line(fd, &line)) < 0)
- return (ft_map_error(clist));
+ return (ft_map_error(FT_ERR_READ, clist));
if (ret == 0)
- return (ft_error_here(line, clist));
+ return (ft_error_here(FT_ERR_UNFINISHED, line, clist));
if (!line[0])
{
ft_memdel((void**)&line);
@@ -89,31 +63,37 @@ static int8_t
}
if (!ft_ischarset("RNSEWFC1\0", line[0])
|| !(words = ft_split(line, ' ')))
- return (ft_error_here(line, clist));
+ return (ft_error_here(FT_ERR_ILL_ENTRY, line, clist));
if ((ret = ft_select_get(words, clist)) == 12)
return ((ft_get_map_first_line(line, clist) < 0) ? (-1) : (12));
ft_memdel((void**)&line);
return (ret);
}
+static void
+ ft_no_map_error(t_cub *clist)
+{
+ ft_dprintf(STDERR_FILENO, "Error\n");
+ ft_dprintf(STDERR_FILENO, "\033[31;1mNo map\033[0m\n");
+ ft_exit(5, clist);
+}
+
void
ft_parse_map(const char *map_path, t_cub *clist)
{
int fd;
int8_t ret;
- ft_check_cub(map_path, clist);
+ if (ft_check_ext(map_path, ".cub") < 0)
+ ft_map_error(FT_ERR_NOT_A_CUB, clist);
fd = open(map_path, O_RDONLY);
if (fd < 0)
- {
- ft_dprintf(STDERR_FILENO, "Error\n");
- ft_dprintf(STDERR_FILENO, "\033[31;1mNo map\033[0m\n");
- ft_exit(5, clist);
- }
+ ft_no_map_error(clist);
ret = 1;
while (ret != 12 && ret != -1)
ret = ft_parse_it(fd, clist);
- (ret == -1) ? (ft_map_error(clist)) : 0;
+ (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_check_map_last_line(clist);
diff --git a/src/ft_select_get.c b/src/ft_select_get.c
index f550d98..4b82430 100644
--- a/src/ft_select_get.c
+++ b/src/ft_select_get.c
@@ -86,7 +86,7 @@ int8_t
if (id < 0 || (*fun_ptr[id])(words, clist) < 0)
{
ft_free_words(words);
- return (-1);
+ return ((id < 0) ? (-2) : (-1));
}
ft_free_words(words);
return (id);