gestion erreur parsing
This commit is contained in:
37
srcs/init.c
37
srcs/init.c
@@ -6,7 +6,7 @@
|
||||
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:30:34 by hulamy #+# #+# */
|
||||
/* Updated: 2022/01/26 16:29:38 by hulamy ### ########.fr */
|
||||
/* Updated: 2022/01/26 17:55:34 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -57,22 +57,29 @@ static t_philo *init_chain_philo(t_params *params, t_global *global)
|
||||
static t_params *init_params(int ac, char **av)
|
||||
{
|
||||
t_params *params;
|
||||
int i;
|
||||
|
||||
if (ac == 5 || ac == 6)
|
||||
{
|
||||
params = malloc(sizeof(t_params));
|
||||
if (!params)
|
||||
return (NULL);
|
||||
params->n_phi = ft_atoi(av[1]);
|
||||
params->t_die = ft_atoi(av[2]);
|
||||
params->t_eat = ft_atoi(av[3]);
|
||||
params->t_slp = ft_atoi(av[4]);
|
||||
params->n_eat = -1;
|
||||
if (ac == 6)
|
||||
params->n_eat = ft_atoi(av[5]);
|
||||
}
|
||||
else
|
||||
if (ac < 5 || ac > 6)
|
||||
return (NULL);
|
||||
if (!ft_isdigit_2d_arr(av + 1))
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (av[i])
|
||||
{
|
||||
if (ft_int_overflow(av[i]))
|
||||
return (NULL);
|
||||
i++;
|
||||
}
|
||||
params = malloc(sizeof(t_params));
|
||||
if (!params)
|
||||
return (NULL);
|
||||
params->n_phi = ft_atoi(av[1]);
|
||||
params->t_die = ft_atoi(av[2]);
|
||||
params->t_eat = ft_atoi(av[3]);
|
||||
params->t_slp = ft_atoi(av[4]);
|
||||
params->n_eat = -1;
|
||||
if (ac == 6)
|
||||
params->n_eat = ft_atoi(av[5]);
|
||||
return (params);
|
||||
}
|
||||
|
||||
|
||||
66
srcs/utils.c
66
srcs/utils.c
@@ -1,17 +1,79 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* generic.c :+: :+: :+: */
|
||||
/* utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/01/26 15:30:19 by hulamy #+# #+# */
|
||||
/* Updated: 2022/01/26 15:31:15 by hulamy ### ########.fr */
|
||||
/* Updated: 2022/01/26 17:50:31 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "philo.h"
|
||||
|
||||
int ft_isdigit_2d_arr(char **str)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
while (str[i])
|
||||
{
|
||||
j = 0;
|
||||
while (str[i][j])
|
||||
{
|
||||
if (str[i][j] < '0' || str[i][j] > '9')
|
||||
return (0);
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
size_t ft_strlen(char *str)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (str[i])
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
int ft_strncmp(char *s1, char *s2, size_t n)
|
||||
{
|
||||
size_t i;
|
||||
int res;
|
||||
|
||||
i = 0;
|
||||
res = 0;
|
||||
while (s1[i] && s1[i] == s2[i] && i < n - 1)
|
||||
i++;
|
||||
if (n != 0)
|
||||
res = (unsigned char)s1[i] - (unsigned char)s2[i];
|
||||
return (res);
|
||||
}
|
||||
|
||||
int ft_int_overflow(char *str)
|
||||
{
|
||||
size_t len;
|
||||
char *int_xtrem;
|
||||
|
||||
int_xtrem = "2147483647";
|
||||
if (str[0] == '-')
|
||||
int_xtrem = "2147483648";
|
||||
if (str[0] == '+' || str[0] == '-')
|
||||
str++;
|
||||
len = ft_strlen(str);
|
||||
if (len < 10)
|
||||
return (0);
|
||||
else if (len > 10 || ft_strncmp(str, int_xtrem, len) > 0)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ft_atoi(const char *str)
|
||||
{
|
||||
int i;
|
||||
|
||||
Reference in New Issue
Block a user