/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   init_tab_item_path.c                               :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: yantoine <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/01/25 16:03:09 by yantoine          #+#    #+#             */
/*   Updated: 2024/02/21 21:12:07 by yantoine         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */
#include "../so_long.h"

static	int	count_number_line(char *file_path)
{
	char	*line;
	int		fd;
	int		nb;

	nb = 0;
	fd = open(file_path, O_RDONLY);
	if (fd == -1)
	{
		ft_printf("Erreur ouverture fichier\n");
		return (-1);
	}
	while (1)
	{
		line = get_next_line(fd);
		if (!line)
			break ;
		free(line);
		nb++;
	}
	close(fd);
	return (nb);
}

char	**init_tab_item_path(char *file_path)
{
	char	*line;
	char	**tab_item_path;
	int		fd;
	int		count;
	int		nb_line;

	count = 0;
	fd = open(file_path, O_RDONLY);
	nb_line = count_number_line(file_path);
	tab_item_path = malloc((nb_line + 1) * sizeof(char *));
	if (tab_item_path == NULL || fd == -1)
		return (NULL);
	while (1)
	{
		line = get_next_line(fd);
		if (!line)
			break ;
		line = anti_bn(line);
		tab_item_path[count] = line;
		count++;
	}
	tab_item_path[count] = NULL;
	close(fd);
	return (tab_item_path);
}
