Added filetype checking

This commit is contained in:
Theo Champion 2025-06-04 19:16:59 +02:00
parent 8f99d23431
commit 935370ac63
5 changed files with 58 additions and 27 deletions

View file

@ -1,6 +1,7 @@
CC=cc CC=cc
CFLAGS=-Wall -Wextra -Werror -g -fsanitize=address -fno-omit-frame-pointer -I mlx 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)) OBJECTS=$(patsubst src/%.c,objects/%.o,$(SOURCEFILES))
OBJDIR=objects OBJDIR=objects
NAME=cub3d NAME=cub3d

15
includes/maputils.h Normal file
View file

@ -0,0 +1,15 @@
#ifndef MAPUTILS_H
# define MAPUTILS_H
# include <stdbool.h>
typedef struct s_mapdata
{
char *filename;
bool isvalid;
char error[1024];
} t_mapdata;
bool check_cubfile(char *filename, t_mapdata *map);
#endif

0
objects/map/.gitkeep Normal file
View file

View file

@ -12,7 +12,7 @@
#include "../includes/libft.h" #include "../includes/libft.h"
#include "../includes/mlx.h" #include "../includes/mlx.h"
//#include "../includes/filecheck.h" #include "../includes/maputils.h"
#include <stdbool.h> #include <stdbool.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
@ -26,12 +26,6 @@ typedef struct s_mlx_data
int endian; int endian;
} t_mlx_data; } 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) 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; *(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) int main(int argc, char **argv)
{ {
void *mlx; void *mlx;

40
src/map/map_checker.c Normal file
View file

@ -0,0 +1,40 @@
#include "../../includes/maputils.h"
#include <fcntl.h>
#include <unistd.h>
#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);
}