/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* parsing.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: lfirmin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/21 16:16:19 by lfirmin #+# #+# */ /* Updated: 2025/03/17 04:00:28 by lfirmin ### ########.fr */ /* */ /* ************************************************************************** */ #include "../include/philo.h" int ft_put_and_check(t_data *data, char **av) { int c; c = 0; data->nb_philo = ft_atoll(av[1]); if (data->nb_philo <= 0) (printf("Error : Invalid number of philo, expected 1 "\ "or more, You gave \"%s\"\n", av[1]), c++); data->t_die = ft_atoll(av[2]); if (data->t_die <= 0) (printf("Error : Invalid time for die, expected 1ms "\ "or more, You gave \"%s\"\n", av[2]), c++); data->t_eat = ft_atoll(av[3]); if (data->t_eat < 0 || !av[3][0]) (printf("Error : Invalid time for eat, expected 0ms "\ "or more, You gave \"%s\"\n", av[3]), c++); data->t_sleep = ft_atoll(av[4]); if (data->t_sleep < 0 || !av[4][0]) (printf("Error : Invalid time for sleep, expected 0ms "\ "or more, You gave \"%s\"\n", av[4]), c++); if (av[5] != NULL) ft_put_and_check_2(data, av, &c); return (c); } void ft_put_and_check_2(t_data *data, char **av, int *c) { data->must_eat = ft_atoll(av[5]); if (data->must_eat < 1) (printf("Error : Invalid number of philo must eat, expected 1 or more"\ ", You gave \"%s\"\n", av[5]), (*c)++); } int ft_check_av(char **av) { int i; int c; c = 0; i = 1; while (av[i]) { if (validate_number(av, i) == 1) (printf("Error : argument %d isn't a number\n", i + 1), c++); i++; } return (c); } int validate_number(char **av, int i) { int j; j = 0; while (av[i][j]) { if ((av[i][j] < '0' || av[i][j] > '9') && \ (av[i][j] != '-' && av[i][j] != '+')) return (1); if (av[i][j] == '-' || av[i][j] == '+') { if (av[i][j + 1] < '0' || av[i][j + 1] > '9' || j != 0) return (1); } j++; } return (0); } int parsing(int ac, char **av, t_data *data) { int ret; if (ac < 5 || ac > 6) return (printf("%s\n", USAGE_MESS), 1); ret = ft_check_av(av); if (ret != 0) return (1); ret = ft_put_and_check(data, av); if (ret != 0) return (1); return (0); }