/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   main.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: bchanot <bchanot@students.42.fr>           +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2016/11/02 07:13:20 by bchanot           #+#    #+#             */
/*   Updated: 2016/11/14 11:10:31 by bchanot          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "ft_select.h"

static void	sl_display_selected(t_inf inf)
{
	int	cpt;
	int	end;
	int	checker;

	cpt = 0;
	while (cpt < inf.nb)
	{
		checker = cpt + 1;
		while (checker < inf.nb)
		{
			if (inf.selected[checker])
				break ;
			checker++;
		}
		end = checker == inf.nb ? 0 : 1;
		if (inf.selected[cpt])
		{
			ft_putstr(inf.args[cpt]);
			if (end)
				ft_putchar(' ');
		}
		cpt++;
	}
}

int			main(int ac, char **av)
{
	t_inf	inf;

	if (ac < 2)
		ft_exit_err("ft_select: Usage: ft_select [arguments]");
	sl_init_inf(ac, av, &inf);
	sl_set_inf(&inf);
	sl_set_signal();
	if (sl_launch_loop(&inf))
		sl_display_selected(inf);
	ft_memdel2((void ***)&inf.args);
	ft_memdel((void **)&inf.selected);
	return (0);
}
