/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* philo_utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/16 14:04:06 by narnaud #+# #+# */ /* Updated: 2022/05/20 10:36:06 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" static int mini_atoi(char *nbr) { int ret; ret = 0; while (*nbr >= '0' && *nbr <= '9') { ret = (*nbr - '0') + (10 * ret); nbr++; } return (ret); } t_table *parsing(char **argv) { t_table *table; table = malloc(sizeof(t_table)); table->param[PHILO_AMOUNT] = mini_atoi(argv[0]); table->param[DIE_TIME] = mini_atoi(argv[1]); table->param[EAT_DURATION] = mini_atoi(argv[2]); table->param[SLEEP_DURATION] = mini_atoi(argv[3]); if (argv[4]) table->param[MEALS_AMOUNT] = mini_atoi(argv[4]); else table->param[MEALS_AMOUNT] = 2147483647; table->time = gettime(); table->philos = malloc(sizeof(t_philo) * table->param[PHILO_AMOUNT]); return (table); } int gettime(void) { static struct timeval starttime = {0, 0}; struct timeval actualtime; if (starttime.tv_sec == 0 && starttime.tv_usec == 0) gettimeofday(&starttime, NULL); gettimeofday(&actualtime, NULL); return ((int)((actualtime.tv_sec * 1000 + actualtime.tv_usec / 1000) - (starttime.tv_sec * 1000 + starttime.tv_usec / 1000))); }