/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   parse_file.c                                       :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: gbrochar <gbrochar@student.42.fr>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/12/22 12:38:27 by gbrochar          #+#    #+#             */
/*   Updated: 2020/12/22 18:59:30 by gbrochar         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "scop.h"

int					parse_file(t_env *e)
{
	FILE			*fp;
	char			*buffer;

	fp = fopen(e->file_name, "r");
	if (!fp)
		return (FAILURE);
	buffer = (char *)malloc(sizeof(char) * 4096);
	while (fgets(buffer, 4096, fp))
	{
		if (parse_line(e, buffer) == FAILURE)
			return (FAILURE);
	}
	free(buffer);
	fclose(fp);
	return (SUCCESS);
}

void				set_params(t_env *e, int argc, char **argv)
{
	if (argc >= 2)
		e->file_name = strdup(argv[1]);
	else
		e->file_name = strdup("resources/42.obj");
	if (argc >= 3)
		e->window.width = atof(argv[2]);
	if (argc >= 4)
		e->window.height = atof(argv[3]);
	if (argc >= 5)
		e->camera.fov = atof(argv[4]);
	e->camera.aspect = (double)e->window.width / (double)e->window.height;
}

int					parse(t_env *e, int argc, char **argv)
{
	set_params(e, argc, argv);
	return (parse_file(e));
}
