42_Philosopher/philo/parsing.c

65 lines
1.8 KiB
C
Raw Normal View History

2023-04-27 08:05:14 -04:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:34:57 by cchauvet #+# #+# */
2023-05-17 07:44:38 -04:00
/* Updated: 2023/05/17 13:43:29 by cchauvet ### ########.fr */
2023-04-27 08:05:14 -04:00
/* */
/* ************************************************************************** */
2023-04-13 09:00:39 -04:00
#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)
{
2023-05-17 07:44:38 -04:00
size_t i;
2023-04-13 09:00:39 -04:00
i = 0;
while (i < n)
{
if (ft_isnum(av[i]) == 0)
return (1);
2023-05-17 07:44:38 -04:00
if (!(ft_atoi(av[i]) > 0))
return (1);
2023-04-13 09:00:39 -04:00
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)
2023-04-19 09:51:35 -04:00
write(2, "Argument error !\n", 16);
return (data->nb_philos == 0);
2023-04-13 09:00:39 -04:00
}