philosopher/mandatory/parsing.c
Etienne Rey-bethbeder ffb68409b1 Finito philo
2023-03-31 18:23:57 +02:00

51 lines
1.7 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/08 14:59:29 by erey-bet #+# #+# */
/* Updated: 2023/03/31 15:13:30 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
int error(char *s1, char *s2)
{
ft_putstr_fd(s1, 2);
ft_putstr_fd(s2, 2);
write(2, "\n", 1);
return (1);
}
int get_argv(char **argv, long *conf, char *message, int multiplier)
{
if (ft_atoi_check(*argv))
{
*conf = (long)ft_atoi(*argv) * multiplier;
if (*conf < 1)
return (error("Wrong number of ", message));
return (0);
}
return (1);
}
int parsing(char *argv[], t_config *conf)
{
if (!get_argv(&argv[1], (long *)&conf->nbr_philo, "philosopher", 1)
&& !get_argv(&argv[2], &conf->time_die, "time to die", 1000)
&& !get_argv(&argv[3], &conf->time_eat, "time to eat", 1000)
&& !get_argv(&argv[4], &conf->time_sleep, "time to sleep", 1000))
{
conf->must_eat = -1;
if (!argv[5])
return (0);
if (!get_argv(&argv[5], &conf->must_eat, "must eat time", 1))
return (0);
return (1);
}
return (1);
}