95 lines
1.8 KiB
C
95 lines
1.8 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)
|
|
{
|
|
if (optional)
|
|
{
|
|
all->number_of_philosophers = ft_atoi(argv[1]) - 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]);
|
|
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]);
|
|
return (check_param(all, 0));
|
|
}
|
|
}
|
|
|
|
int parse_and_check_values(char **argv, int quantity)
|
|
{
|
|
t_all *all;
|
|
|
|
all = structalloc_and_bzero();
|
|
if (quantity == 5)
|
|
{
|
|
if (!parse_param(all, 0, argv))
|
|
{
|
|
printf("ok standart run\n");
|
|
if (runner(all, 0) == 2) // TODO это завершение симуляции
|
|
return (2);
|
|
return(1);
|
|
}
|
|
else
|
|
{
|
|
printf("bad arg\n");
|
|
return (0);
|
|
}
|
|
}
|
|
else if (quantity == 6)
|
|
{
|
|
if (!parse_param(all, 1, argv))
|
|
{
|
|
printf("ok run with optional\n");
|
|
if (runner(all, 1) == 2) // TODO тоже пофиксить;
|
|
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);
|
|
}
|
|
}
|