42_cube3D/map/parsing.c

100 lines
1.6 KiB
C
Raw Normal View History

2023-05-03 07:09:06 -04:00
#include "./parsing_private.h"
2023-05-03 10:33:57 -04:00
#include "map.h"
#include <stddef.h>
2023-05-05 10:49:04 -04:00
#include <stdlib.h>
2023-05-03 10:33:57 -04:00
static ssize_t get_nb_line(const char *path)
{
int fd;
char readed[1];
size_t i;
fd = open(path, O_RDONLY);
if (fd == -1)
return (-1);
i = 1;
while (read(fd, readed, 1))
{
if (readed[0] == '\n')
i++;
}
close(fd);
return (i);
}
2023-05-03 10:33:57 -04:00
static char **read_map(const char *path)
{
int fd;
size_t i;
char **map;
map = malloc(sizeof(char *) * (get_nb_line(path) + 1));
if (map == NULL)
return (NULL);
fd = open(path, O_RDONLY);
if (fd == -1)
{
free(map);
return (NULL);
}
i = 0;
map[i] = get_next_line(fd);
while (map[i] != NULL)
{
map[i][ft_strlen(map[i]) - 1] = '\0';
i++;
map[i] = get_next_line(fd);
}
return (map);
}
2023-05-03 07:09:06 -04:00
2023-05-05 10:49:04 -04:00
void header_freer(char ***header)
{
size_t i;
size_t j;
i = 0;
while (header[i] != NULL)
{
j = 0;
while (header[i][j] != NULL)
{
free(header[i][j]);
j++;
}
free(header[i]);
i++;
}
free(header);
}
int map_parsing(const char *path, t_map *map)
2023-05-03 07:09:06 -04:00
{
2023-05-03 10:33:57 -04:00
char **file_content;
char ***header;
size_t header_size;
2023-05-03 07:09:06 -04:00
2023-05-03 10:33:57 -04:00
if (parsing_meta(path))
2023-05-05 10:49:04 -04:00
return (1);
ft_bzero(map, sizeof(t_map));
2023-05-03 10:33:57 -04:00
file_content = read_map(path);
if (file_content == NULL)
{
ft_eprintf("map: file error");
2023-05-05 10:49:04 -04:00
return (1);
}
2023-05-03 10:33:57 -04:00
header = get_header((const char **) file_content, &header_size);
if (header_is_valid(header, map) == 0)
{
2023-05-05 10:49:04 -04:00
header_freer(header);
2023-05-03 10:33:57 -04:00
ft_freer_tab_ultimate(1, file_content);
2023-05-05 10:49:04 -04:00
return (1);
2023-05-03 10:33:57 -04:00
}
2023-05-05 10:49:04 -04:00
header_freer(header);
map->map = get_body((const char **) file_content, header_size);
ft_freer_tab_ultimate(1, file_content);
if (body_is_valid(map) == 0)
return (1);
return (0);
2023-05-03 07:09:06 -04:00
}