Connected parsing to rest of the game
This commit is contained in:
136
srcs/parsing/init_parsing.c
Normal file
136
srcs/parsing/init_parsing.c
Normal file
@@ -0,0 +1,136 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* init_parsing.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cube3d.h"
|
||||
|
||||
int size_map(t_map *map, int fd)
|
||||
{
|
||||
char *line;
|
||||
|
||||
get_next_line(fd, &line);
|
||||
while (line)
|
||||
{
|
||||
if ((*line == '\n' || *line == '\0') && map->size_x)
|
||||
{
|
||||
free(line);
|
||||
return (EXIT_FAILURE);
|
||||
}
|
||||
else if (*line != '\n' && (int)ft_strlen(line) > map->size_x)
|
||||
{
|
||||
map->size_x = ft_strlen(line);
|
||||
if (line[map->size_x - 1] != '\n')
|
||||
map->size_x++;
|
||||
}
|
||||
if (*line != '\n' && *line != '\0')
|
||||
map->size_y++;
|
||||
free(line);
|
||||
line = NULL;
|
||||
get_next_line(fd, &line);
|
||||
}
|
||||
return (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
int find_map(t_map *map, char *file)
|
||||
{
|
||||
int fd;
|
||||
int count;
|
||||
|
||||
fd = open(file, O_RDONLY);
|
||||
count = 0;
|
||||
if (fd == -1)
|
||||
return (-1);
|
||||
while (count < TOTAL_ELEMENTS + 1)
|
||||
{
|
||||
free(map->tmp_str);
|
||||
get_next_line(fd, &map->tmp_str);
|
||||
if (!map->tmp_str)
|
||||
{
|
||||
close(fd);
|
||||
return (-1);
|
||||
}
|
||||
if (map->tmp_str[0] != '\n' && map->tmp_str[0] != '\0')
|
||||
count++;
|
||||
}
|
||||
return (fd);
|
||||
}
|
||||
|
||||
void fill_row(char *row, char *line, int width)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (line != NULL && line[i] != '\0' && line[i] != '\n')
|
||||
{
|
||||
row[i] = line[i];
|
||||
i++;
|
||||
}
|
||||
while (i < width - 1)
|
||||
{
|
||||
row[i] = ' ';
|
||||
i++;
|
||||
}
|
||||
row[i] = '\0';
|
||||
}
|
||||
|
||||
int fill_map(t_map *map, int fd)
|
||||
{
|
||||
int i;
|
||||
|
||||
map->content = (char **)mb_alloc((map->size_y + 1) * sizeof(char *));
|
||||
if (!map->content)
|
||||
{
|
||||
free(map->tmp_str);
|
||||
return (EXIT_FAILURE);
|
||||
}
|
||||
i = -1;
|
||||
while (++i < map->size_y)
|
||||
{
|
||||
if (i > 0)
|
||||
get_next_line(fd, &map->tmp_str);
|
||||
map->content[i] = (char *)mb_alloc((map->size_x + 1) * sizeof(char));
|
||||
if (!map->content[i])
|
||||
{
|
||||
free(map->tmp_str);
|
||||
return (EXIT_FAILURE);
|
||||
}
|
||||
fill_row(map->content[i], map->tmp_str, map->size_x);
|
||||
free(map->tmp_str);
|
||||
map->tmp_str = NULL;
|
||||
}
|
||||
map->content[i] = NULL;
|
||||
return (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
int init_parsing(t_game *game, char *file)
|
||||
{
|
||||
int fd;
|
||||
|
||||
fd = check_elements(game, file);
|
||||
if (fd == -1)
|
||||
return (EXIT_FAILURE);
|
||||
if (size_map(&(game->map), fd))
|
||||
{
|
||||
close(fd);
|
||||
return (EXIT_FAILURE);
|
||||
}
|
||||
close(fd);
|
||||
fd = find_map(&(game->map), file);
|
||||
if (fd == -1)
|
||||
return (EXIT_FAILURE);
|
||||
if (fill_map(&(game->map), fd) == EXIT_FAILURE)
|
||||
{
|
||||
close(fd);
|
||||
return (EXIT_FAILURE);
|
||||
}
|
||||
close(fd);
|
||||
return (EXIT_SUCCESS);
|
||||
}
|
||||
Reference in New Issue
Block a user