Added memorybook to parsing and cleaned most of the files

This commit is contained in:
Philippe BLAGOJEVIC
2022-05-04 14:38:34 +02:00
parent e564e7c8e9
commit 362668fe35
231 changed files with 11474 additions and 405 deletions

View File

@@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pblagoje <pblagoje@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/18 12:37:48 by pblagoje #+# #+# */
/* Updated: 2022/05/04 13:05:47 by pblagoje ### ########.fr */
/* */
/* ************************************************************************** */
#include "cube3d.h"
int check_borders(t_map *map)
{
int x;
int y;
y = 0;
while (map->content[y] != NULL)
{
x = 0;
while (map->content[y][x] != '\0' && map->content[y][x] != '\n')
{
if (y == 0 || y == map->size_y - 1)
if (map->content[y][x] != '1' && map->content[y][x] != ' ')
mb_exit("Error\nInvalid map borders.\n", EXIT_FAILURE);
if (x == 0 || x == map->size_x - 1)
if (map->content[y][x] != '1' && map->content[y][x] != ' ')
mb_exit("Error\nInvalid map borders.\n", EXIT_FAILURE);
x++;
}
y++;
}
return (EXIT_SUCCESS);
}
int check_map(t_map *map)
{
if (check_borders(map) || check_content(map))
mb_exit("Error\nInvalid map content.\n", EXIT_FAILURE);
return (EXIT_SUCCESS);
}

View File

@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_map.c :+: :+: :+: */
/* check_map_content.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pblagoje <pblagoje@student.42.fr> +#+ +:+ +#+ */
/* By: pblagoje <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/18 12:37:48 by pblagoje #+# #+# */
/* Updated: 2022/05/04 00:51:54 by simplonco ### ########.fr */
/* Created: 2022/05/04 13:04:44 by pblagoje #+# #+# */
/* Updated: 2022/05/04 13:05:34 by pblagoje ### ########.fr */
/* */
/* ************************************************************************** */
@@ -73,9 +73,9 @@ int check_content(t_map *map)
while (map->content[y][x] != '\0' && map->content[y][x] != '\n')
{
if (map->content[y][x] == ' ' && check_spaces(map, y, x))
return (EXIT_FAILURE);
mb_exit("Error\nInvalid space positions.\n", EXIT_FAILURE);
else if (!ft_strchr(" 01SNWE", map->content[y][x]))
return (EXIT_FAILURE);
mb_exit("Error\nInvalid map characters.\n", EXIT_FAILURE);
else if (ft_strchr("SNWE", map->content[y][x]) \
&& set_player(map, y, x, map->content[y][x]))
count++;
@@ -84,40 +84,6 @@ int check_content(t_map *map)
y++;
}
if (count != 1)
return (EXIT_FAILURE);
return (EXIT_SUCCESS);
}
int check_borders(t_map *map)
{
int x;
int y;
y = 0;
while (map->content[y] != NULL)
{
x = 0;
while (map->content[y][x] != '\0' && map->content[y][x] != '\n')
{
if (y == 0 || y == map->size_y - 1)
if (map->content[y][x] != '1' && map->content[y][x] != ' ')
return (EXIT_FAILURE);
if (x == 0 || x == map->size_x - 1)
if (map->content[y][x] != '1' && map->content[y][x] != ' ')
return (EXIT_FAILURE);
x++;
}
y++;
}
return (EXIT_SUCCESS);
}
int check_map(t_map *map)
{
if (check_borders(map) || check_content(map))
{
ft_putstr_fd("Error\nThe map characters are invalid.\n", 2);
return (EXIT_FAILURE);
}
mb_exit("Error\nToo many player starting positions.\n", EXIT_FAILURE);
return (EXIT_SUCCESS);
}

View File

@@ -6,7 +6,7 @@
/* By: pblagoje <pblagoje@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/16 20:54:37 by pblagoje #+# #+# */
/* Updated: 2022/05/02 15:45:01 by simplonco ### ########.fr */
/* Updated: 2022/05/04 13:01:16 by pblagoje ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,21 +17,25 @@ int set_path(t_txt *txt, char *path, char identifier)
if (identifier == 'N' && txt->txt_north == NULL)
{
txt->txt_north = ft_strdup(path);
mb_add(txt->txt_north);
return (EXIT_SUCCESS);
}
else if (identifier == 'S' && txt->txt_south == NULL)
{
txt->txt_south = ft_strdup(path);
mb_add(txt->txt_south);
return (EXIT_SUCCESS);
}
else if (identifier == 'W' && txt->txt_west == NULL)
{
txt->txt_west = ft_strdup(path);
mb_add(txt->txt_west);
return (EXIT_SUCCESS);
}
else if (identifier == 'E' && txt->txt_east == NULL)
{
txt->txt_east = ft_strdup(path);
mb_add(txt->txt_east);
return (EXIT_SUCCESS);
}
return (EXIT_FAILURE);
@@ -45,21 +49,21 @@ static int check_path(t_txt *txt, char *element, char identifier)
while (element && *element && ft_strchr(" \t\r", *element))
element++;
path = ft_substr(element, 0, ft_strlen(element));
mb_add(path);
fd = open(path, O_RDONLY);
if (fd == -1 || check_extension(path, ".xpm"))
{
ft_putstr_fd("Error\nInvalid texture file.\n", 2);
free(path);
mb_free(path);
close(fd);
return (EXIT_FAILURE);
mb_exit("Error\nInvalid texture file.\n", EXIT_FAILURE);
}
if (set_path(txt, path, identifier) == EXIT_FAILURE)
{
free(path);
mb_free(path);
close(fd);
return (EXIT_FAILURE);
mb_exit("Error\nCouldn't save texture paths.\n", EXIT_FAILURE);
}
free(path);
mb_free(path);
close(fd);
return (EXIT_SUCCESS);
}
@@ -81,6 +85,7 @@ static int check_element(t_game *game, char *element)
if (ft_strnstr("F C ", identifier, 5) && \
!check_rgb(&(game->txt), element, identifier[0]))
return (EXIT_SUCCESS);
mb_exit("Error\nInvalid textures and/or RGB elements.\n", EXIT_FAILURE);
return (EXIT_FAILURE);
}
@@ -98,15 +103,16 @@ int check_elements(t_game *game, char *file)
while (count < TOTAL_ELEMENTS)
{
get_next_line(fd, &line);
mb_add(line);
if (line && *line != '\n' && !check_element(game, line))
count++;
else if (!line || (*line != '\n' && *line != '\0'))
{
free(line);
mb_free(line);
close(fd);
return (-1);
}
free(line);
mb_free(line);
line = NULL;
}
return (fd);

View File

@@ -6,7 +6,7 @@
/* By: pblagoje <pblagoje@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/15 16:11:24 by pblagoje #+# #+# */
/* Updated: 2022/05/01 22:53:25 by simplonco ### ########.fr */
/* Updated: 2022/05/04 12:58:13 by pblagoje ### ########.fr */
/* */
/* ************************************************************************** */
@@ -51,7 +51,7 @@ static void set_rgb(int *plan, char **rgb)
*plan = 0 << 24 | r << 16 | g << 8 | b;
}
void ft_free_2d(char **str)
/*void ft_free_2d(char **str)
{
int i;
@@ -62,14 +62,14 @@ void ft_free_2d(char **str)
i++;
}
free(str);
}
}*/
int check_rgb(t_txt *txt, char *elem, char identifier)
{
char **rgb;
rgb = ft_split(elem, ',');
mb_add_2d((void**)rgb, ft_strlen_2d(rgb));
mb_add_2d((void **)rgb, ft_strlen_2d(rgb));
if (!rgb || ft_strlen_2d(rgb) != 3 || elem[ft_strlen(elem) - 1] == ',')
mb_exit("Error\nInvalid RGB code.\n", EXIT_FAILURE);
if ((identifier != 'F' && identifier != 'C') || check_colors(rgb))

View File

@@ -6,7 +6,7 @@
/* By: pblagoje <pblagoje@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/15 16:03:50 by pblagoje #+# #+# */
/* Updated: 2022/04/23 18:57:37 by pblagoje ### ########.fr */
/* Updated: 2022/05/04 13:14:21 by pblagoje ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,11 +17,12 @@ int size_map(t_map *map, int fd)
char *line;
get_next_line(fd, &line);
mb_add(line);
while (line)
{
if ((*line == '\n' || *line == '\0') && map->size_x)
{
free(line);
mb_free(line);
return (EXIT_FAILURE);
}
else if (*line != '\n' && (int)ft_strlen(line) > map->size_x)
@@ -32,9 +33,10 @@ int size_map(t_map *map, int fd)
}
if (*line != '\n' && *line != '\0')
map->size_y++;
free(line);
mb_free(line);
line = NULL;
get_next_line(fd, &line);
mb_add(line);
}
return (EXIT_SUCCESS);
}
@@ -50,8 +52,8 @@ int find_map(t_map *map, char *file)
return (-1);
while (count < TOTAL_ELEMENTS + 1)
{
free(map->tmp_str);
get_next_line(fd, &map->tmp_str);
mb_add(map->tmp_str);
if (!map->tmp_str)
{
close(fd);
@@ -88,22 +90,25 @@ int fill_map(t_map *map, int fd)
map->content = (char **)mb_alloc((map->size_y + 1) * sizeof(char *));
if (!map->content)
{
free(map->tmp_str);
mb_free(map->tmp_str);
return (EXIT_FAILURE);
}
i = -1;
while (++i < map->size_y)
{
if (i > 0)
{
get_next_line(fd, &map->tmp_str);
mb_add(map->tmp_str);
}
map->content[i] = (char *)mb_alloc((map->size_x + 1) * sizeof(char));
if (!map->content[i])
{
free(map->tmp_str);
mb_free(map->tmp_str);
return (EXIT_FAILURE);
}
fill_row(map->content[i], map->tmp_str, map->size_x);
free(map->tmp_str);
mb_free(map->tmp_str);
map->tmp_str = NULL;
}
map->content[i] = NULL;
@@ -116,20 +121,20 @@ int init_parsing(t_game *game, char *file)
fd = check_elements(game, file);
if (fd == -1)
return (EXIT_FAILURE);
mb_exit("Error\nInvalid .cub file.\n", EXIT_FAILURE);
if (size_map(&(game->map), fd))
{
close(fd);
return (EXIT_FAILURE);
mb_exit("Error\nInvalid map format.\n", EXIT_FAILURE);
}
close(fd);
fd = find_map(&(game->map), file);
if (fd == -1)
return (EXIT_FAILURE);
mb_exit("Error\nCouldn't locate the map.\n", EXIT_FAILURE);
if (fill_map(&(game->map), fd) == EXIT_FAILURE)
{
close(fd);
return (EXIT_FAILURE);
mb_exit("Error\nCouldn't save the map.\n", EXIT_FAILURE);
}
close(fd);
return (EXIT_SUCCESS);