88 lines
1.7 KiB
C
88 lines
1.7 KiB
C
#include "../includes/philo_header.h"
|
|
|
|
int check_param(t_all *all, int optional)
|
|
{
|
|
if (optional)
|
|
{
|
|
if (all->number_of_philosophers <= 0)
|
|
return(1);
|
|
if (all->time_to_die <= 0)
|
|
return(1);
|
|
if (all->time_to_eat <= 0)
|
|
return(1);
|
|
if (all->time_to_sleep <= 0)
|
|
return(1);
|
|
if (all->number_of_times_each_philosopher_must_eat <= 0)
|
|
return(1);
|
|
}
|
|
else
|
|
{
|
|
if (all->number_of_philosophers <= 0)
|
|
return(1);
|
|
if (all->time_to_die <= 0)
|
|
return(1);
|
|
if (all->time_to_eat <= 0)
|
|
return(1);
|
|
if (all->time_to_sleep <= 0)
|
|
return(1);
|
|
}
|
|
return (0);
|
|
}
|
|
|
|
int parse_param(t_all *all, int optional, char **argv)
|
|
{
|
|
all->smert = 0;
|
|
if (optional)
|
|
{
|
|
all->number_of_philosophers = ft_atoi(argv[1]);
|
|
all->time_to_die = ft_atoi(argv[2]);
|
|
all->time_to_eat = ft_atoi(argv[3]);
|
|
all->time_to_sleep = ft_atoi(argv[4]);
|
|
all->number_of_times_each_philosopher_must_eat = ft_atoi(argv[5]);
|
|
all->opt = 1;
|
|
return (check_param(all, 1));
|
|
}
|
|
else
|
|
{
|
|
all->number_of_philosophers = ft_atoi(argv[1]);
|
|
all->time_to_die = ft_atoi(argv[2]);
|
|
all->time_to_eat = ft_atoi(argv[3]);
|
|
all->time_to_sleep = ft_atoi(argv[4]);
|
|
all->number_of_times_each_philosopher_must_eat = 0;
|
|
all->opt = 0;
|
|
return (check_param(all, 0));
|
|
}
|
|
}
|
|
|
|
int parse_and_check_values(char **argv, int quantity)
|
|
{
|
|
t_all *all;
|
|
int bool;
|
|
|
|
if (quantity == 5)
|
|
bool = 0;
|
|
if (quantity == 6)
|
|
bool = 1;
|
|
all = structalloc_and_bzero();
|
|
if (quantity == 5 || quantity == 6)
|
|
{
|
|
if (!parse_param(all, bool, argv))
|
|
{
|
|
if (runner(all) == 2)
|
|
return (2);
|
|
return(1);
|
|
}
|
|
else
|
|
{
|
|
printf("bad arg\n");
|
|
return (0);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
printf("error please use argument 5 (optional) or 4 (standart)\n");
|
|
free(all);
|
|
return(0);
|
|
}
|
|
}
|