From 935370ac63514cfb4293067d0694a8ca4bd04d45 Mon Sep 17 00:00:00 2001 From: Theo Champion Date: Wed, 4 Jun 2025 19:16:59 +0200 Subject: [PATCH] Added filetype checking --- Makefile | 3 ++- includes/maputils.h | 15 +++++++++++++++ objects/map/.gitkeep | 0 src/main.c | 27 +-------------------------- src/map/map_checker.c | 40 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 27 deletions(-) create mode 100644 includes/maputils.h create mode 100644 objects/map/.gitkeep create mode 100644 src/map/map_checker.c diff --git a/Makefile b/Makefile index 4a7b47e..fbdc7c0 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ CC=cc CFLAGS=-Wall -Wextra -Werror -g -fsanitize=address -fno-omit-frame-pointer -I mlx -SOURCEFILES=src/main.c +SOURCEFILES=src/main.c \ + src/map/map_checker.c OBJECTS=$(patsubst src/%.c,objects/%.o,$(SOURCEFILES)) OBJDIR=objects NAME=cub3d diff --git a/includes/maputils.h b/includes/maputils.h new file mode 100644 index 0000000..fe2a8a1 --- /dev/null +++ b/includes/maputils.h @@ -0,0 +1,15 @@ +#ifndef MAPUTILS_H +# define MAPUTILS_H + +# include + +typedef struct s_mapdata +{ + char *filename; + bool isvalid; + char error[1024]; +} t_mapdata; + +bool check_cubfile(char *filename, t_mapdata *map); + +#endif diff --git a/objects/map/.gitkeep b/objects/map/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/main.c b/src/main.c index 21b686f..101f94d 100644 --- a/src/main.c +++ b/src/main.c @@ -12,7 +12,7 @@ #include "../includes/libft.h" #include "../includes/mlx.h" -//#include "../includes/filecheck.h" +#include "../includes/maputils.h" #include #include #include @@ -26,12 +26,6 @@ typedef struct s_mlx_data int endian; } t_mlx_data; -typedef struct s_mapdata -{ - char *filename; - bool isvalid; - char error[1024]; -} t_mapdata; void my_mlx_pixel_put(t_mlx_data *data, int x, int y, int color) { @@ -41,25 +35,6 @@ void my_mlx_pixel_put(t_mlx_data *data, int x, int y, int color) *(unsigned int*)dst = color; } -bool check_filename(char *file) -{ - (void)file; - return (false); -} - -bool check_cubfile(char *file, t_mapdata *map) -{ - int fd; - - fd = open(file, O_RDONLY); - if (fd < 0) - return (ft_strlcpy(map->error, "Can't open file", 16), false); - if (!check_filename(file)) - return (ft_strlcpy(map->error, "File is not a .cub file", 25), false); - close(fd); - return (true); -} - int main(int argc, char **argv) { void *mlx; diff --git a/src/map/map_checker.c b/src/map/map_checker.c new file mode 100644 index 0000000..108daf5 --- /dev/null +++ b/src/map/map_checker.c @@ -0,0 +1,40 @@ +#include "../../includes/maputils.h" +#include +#include +#include "../../includes/libft.h" + +bool check_filename(char *file) +{ + int filename_size; + int i; + int j; + char end[5]; + + filename_size = ft_strlen(file); + i = filename_size; + j = 4; + ft_bzero(end, 5); + while (i > (filename_size - 5)) + { + end[j] = file[i]; + i--; + j--; + } + if (ft_strncmp(end, ".cub", 5)) + return (false); + else + return (true); +} + +bool check_cubfile(char *file, t_mapdata *map) +{ + int fd; + + if (!check_filename(file)) + return (ft_strlcpy(map->error, "File is not a .cub file", 25), false); + fd = open(file, O_RDONLY); + if (fd < 0) + return (ft_strlcpy(map->error, "Can't open file", 16), false); + close(fd); + return (true); +}