/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* map_checker.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: tchampio #include #include "../../includes/libft.h" void print_mapdata(const t_mapdata *data) { ft_printf(BOLD CYAN "=== Map Data ===\n" RESET); ft_printf(BOLD "Filename: " RESET "%s\n", data->filename); ft_printf(BOLD "NO Texture: " RESET "%s\n", data->NO_texture); ft_printf(BOLD "SO Texture: " RESET "%s\n", data->SO_texture); ft_printf(BOLD "WE Texture: " RESET "%s\n", data->WE_texture); ft_printf(BOLD "EA Texture: " RESET "%s\n", data->EA_texture); ft_printf(BOLD "Validity: " RESET); if (data->isvalid) ft_printf(GREEN "VALID\n" RESET); else ft_printf(RED "INVALID\n" RESET); if (!data->isvalid && data->error[0] != '\0') { ft_printf(BOLD RED "Error: " RESET "%s\n", data->error); } ft_printf(CYAN "=================\n" RESET); } bool set_textures(char *line, t_mapdata *map) { char **tab; int i; tab = ft_split(line, ' '); i = 0; if (tab[0][0] == '1') return (false); if (tab[0] && tab[1]) { if (!ft_strncmp(tab[0], "NO", 3) || !ft_strncmp(tab[0], "no", 3)) map->NO_texture = ft_strdup(tab[1]); if (!ft_strncmp(tab[0], "SO", 3) || !ft_strncmp(tab[0], "so", 3)) map->SO_texture = ft_strdup(tab[1]); if (!ft_strncmp(tab[0], "WE", 3) || !ft_strncmp(tab[0], "we", 3)) map->WE_texture = ft_strdup(tab[1]); if (!ft_strncmp(tab[0], "EA", 3) || !ft_strncmp(tab[0], "ea", 3)) map->EA_texture = ft_strdup(tab[1]); } while (tab[i]) free(tab[i++]); free(tab); return (true); } bool add_textures(int fd, t_mapdata *map) { char *line; line = get_next_line(fd); while (line) { if (!set_textures(line, map)) return (free(line), false); free(line); line = get_next_line(fd); } print_mapdata(map); return (true); } 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); if (!add_textures(fd, map)) return (close(fd), ft_strlcpy(map->error, "Map started before all the textures", 37), false); close(fd); return (true); }