42_Philosopher/philo/parsing.c
Camille Chauvet aefb6ceec0 bozosujet
2023-05-17 13:28:42 +02:00

63 lines
1.8 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:34:57 by cchauvet #+# #+# */
/* Updated: 2023/04/27 11:34:58 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "utils/utils.h"
#include "./data.h"
#include <stdbool.h>
#include <stddef.h>
#include <unistd.h>
static bool check_amount_of_argument(size_t n)
{
return (n < 4 || n > 5);
}
static bool check_value(char **av, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
if (ft_isnum(av[i]) == 0)
return (1);
i++;
}
return (0);
}
static void set_value(t_data *data, char **args, size_t n)
{
data->nb_philos = ft_atoi(args[0]);
data->life_expectency = ft_atoi(args[1]);
data->eat_time = ft_atoi(args[2]);
data->sleep_time = ft_atoi(args[3]);
if (n == 5)
data->nb_meals = ft_atoi(args[4]);
else
data->nb_meals = -1;
}
bool parsing(t_data *data, char **args, size_t n)
{
if (check_amount_of_argument(n)
|| check_value(args, n))
{
write(2, "Argument error !\n", 16);
return (1);
}
set_value(data, args, n);
if (data->nb_philos == 0)
write(2, "Argument error !\n", 16);
return (data->nb_philos == 0);
}