42_cube3D/map/parsing_header.c
Camille Chauvet b95f3ee1af clean: norm
2023-05-16 19:12:54 +02:00

105 lines
2.5 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_header.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/16 16:28:10 by cchauvet #+# #+# */
/* Updated: 2023/05/16 16:54:16 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "./parsing_private.h"
#include <sys/types.h>
int get_token(const char *key)
{
if (ft_strcmp(key, "NO") == 0)
return (1);
if (ft_strcmp(key, "SO") == 0)
return (3);
if (ft_strcmp(key, "WE") == 0)
return (2);
if (ft_strcmp(key, "EA") == 0)
return (4);
if (ft_strcmp(key, "F") == 0)
return (5);
if (ft_strcmp(key, "C") == 0)
return (6);
return (0);
}
static int is_header(const char *line)
{
char **list;
list = ft_split(line, ' ');
if (list == NULL)
{
ft_eprintf("malloc failed");
ft_freer_tab_ultimate(1, list);
return (0);
}
if (ft_tablen((const void **) list) != 2)
{
ft_freer_tab_ultimate(1, list);
return (0);
}
if (get_token(list[0]) == 0)
{
ft_freer_tab_ultimate(1, list);
return (0);
}
ft_freer_tab_ultimate(1, list);
return (1);
}
static size_t get_header_size(const char **file_content, size_t *header_size)
{
ssize_t i;
size_t len;
len = 0;
i = -1;
while (file_content[++i] != NULL)
{
if (ft_contain_only(file_content[i], ' '))
continue ;
if (!is_header(file_content[i]))
break ;
len++;
}
*header_size = i;
return (len);
}
char ***get_header(const char **file_content, size_t *header_size)
{
char ***header;
size_t len;
size_t i;
ssize_t y;
len = get_header_size(file_content, header_size);
header = malloc(sizeof(char *) * (len + 1));
if (header == NULL)
return (NULL);
header[len] = NULL;
y = -1;
i = 0;
while (++y < (ssize_t) len)
{
while (ft_contain_only(file_content[i], ' '))
i++;
header[y] = ft_split(file_content[i], ' ');
if (header[y] == NULL)
{
ft_cancel((void **) header, y);
return (NULL);
}
i++;
}
return (header);
}