/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   valid_arguments.c                                  :+:    :+:            */
/*                                                     +:+                    */
/*   By: whaffman <whaffman@student.codam.nl>         +#+                     */
/*                                                   +#+                      */
/*   Created: 2025/04/24 11:28:55 by whaffman      #+#    #+#                 */
/*   Updated: 2025/04/24 11:38:05 by whaffman      ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include "cub3d.h"
#include "libft.h"

static int	valid_extension(char *filename)
{
	int		len;

	len = ft_strlen(filename);
	if (len < 4)
		return (FAILURE);
	if (!ft_strncmp(filename + len - 4, ".cub", 4))
		return (SUCCESS);
	return (FAILURE);
}

int	valid_arguments(int argc, char **argv)
{
	int		fd;

	if (argc != 2)
		return (ft_putstr_fd("Usage: ./cub3d <map.cub>\n", 2), FAILURE);
	if (!valid_extension(argv[1]))
	{
		ft_putstr_fd("Error: Invalid file extension. Expected .cub\n", 2);
		return (FAILURE);
	}
	fd = open(argv[1], O_RDONLY);
	if (fd < 0)
		return (perror("Error opening file"), FAILURE);
	if (read(fd, NULL, 0) < 0)
	{
		perror("Error reading file");
		close(fd);
		return (FAILURE);
	}
	close(open(argv[1], O_RDONLY));
	return (SUCCESS);
}
