42_Philosopher/parsing.c
Camille Chauvet a37cbf88f3 init
2023-04-13 13:00:39 +00:00

49 lines
848 B
C

#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);
return (0);
}