42_cube3D/map/parsing_header.c

105 lines
2.5 KiB
C
Raw Permalink Normal View History

2023-05-16 13:12:54 -04:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 */
/* */
/* ************************************************************************** */
2023-05-03 07:09:06 -04:00
#include "./parsing_private.h"
2023-05-16 13:12:54 -04:00
#include <sys/types.h>
2023-05-03 07:09:06 -04:00
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");
2023-05-05 10:49:04 -04:00
ft_freer_tab_ultimate(1, list);
2023-05-03 07:09:06 -04:00
return (0);
}
if (ft_tablen((const void **) list) != 2)
{
2023-05-05 10:49:04 -04:00
ft_freer_tab_ultimate(1, list);
2023-05-03 07:09:06 -04:00
return (0);
}
if (get_token(list[0]) == 0)
{
2023-05-05 10:49:04 -04:00
ft_freer_tab_ultimate(1, list);
2023-05-03 07:09:06 -04:00
return (0);
}
2023-05-05 10:49:04 -04:00
ft_freer_tab_ultimate(1, list);
2023-05-03 07:09:06 -04:00
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], ' '))
2023-05-16 13:12:54 -04:00
continue ;
2023-05-03 07:09:06 -04:00
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;
2023-05-16 13:12:54 -04:00
ssize_t y;
2023-05-03 07:09:06 -04:00
len = get_header_size(file_content, header_size);
header = malloc(sizeof(char *) * (len + 1));
if (header == NULL)
return (NULL);
header[len] = NULL;
2023-05-16 13:12:54 -04:00
y = -1;
2023-05-03 07:09:06 -04:00
i = 0;
2023-05-16 13:12:54 -04:00
while (++y < (ssize_t) len)
2023-05-03 07:09:06 -04:00
{
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);
}