42_cube3D/map/parsing_header.c

93 lines
1.6 KiB
C
Raw Normal View History

2023-05-03 07:09:06 -04:00
#include "./parsing_private.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_ultimate(1, list);
return (0);
}
if (ft_tablen((const void **) list) != 2)
{
ft_freer_ultimate(1, list);
return (0);
}
if (get_token(list[0]) == 0)
{
ft_freer_ultimate(1, list);
return (0);
}
ft_freer_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;
size_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 = 0;
i = 0;
while (y < 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);
}
y++;
i++;
}
return (header);
}