diff --git a/philo_one/Makefile b/philo_one/Makefile index 8bd3076..942b953 100644 --- a/philo_one/Makefile +++ b/philo_one/Makefile @@ -1,6 +1,6 @@ .PHONY: all clean fclean re -SRCS = main.c utilc.c +SRCS = main.c utilc.c parser.c SRC_DIR = ./srcs/ diff --git a/philo_one/includes/philo_header.h b/philo_one/includes/philo_header.h index 2457013..90b7684 100644 --- a/philo_one/includes/philo_header.h +++ b/philo_one/includes/philo_header.h @@ -22,6 +22,6 @@ void *ft_calloc(size_t num, size_t size); t_all *structalloc_and_bzero(void); int parse_and_check_values(char **argv, int quantity); int ft_atoi(const char *str); -int check_f(char ch); +int check_f(char ch); #endif diff --git a/philo_one/obj/main.o b/philo_one/obj/main.o new file mode 100644 index 0000000..330829f Binary files /dev/null and b/philo_one/obj/main.o differ diff --git a/philo_one/obj/parser.o b/philo_one/obj/parser.o new file mode 100644 index 0000000..01a56e7 Binary files /dev/null and b/philo_one/obj/parser.o differ diff --git a/philo_one/obj/utilc.o b/philo_one/obj/utilc.o new file mode 100644 index 0000000..ec09fb9 Binary files /dev/null and b/philo_one/obj/utilc.o differ diff --git a/philo_one/philo_one b/philo_one/philo_one new file mode 100755 index 0000000..d5a26e7 Binary files /dev/null and b/philo_one/philo_one differ diff --git a/philo_one/srcs/main.c b/philo_one/srcs/main.c index c10c0c5..3a36598 100644 --- a/philo_one/srcs/main.c +++ b/philo_one/srcs/main.c @@ -1,81 +1,14 @@ #include "../includes/philo_header.h" -int check_param(t_all *all, int optional) -{ - if (optional) - { - if (all->number_of_philosophers <= 0) - return(1); - if (all->time_to_die <= 0) - return(1); - if (all->time_to_eat <= 0) - return(1); - if (all->time_to_sleep <= 0) - return(1); - if (all->number_of_times_each_philosopher_must_eat <= 0) - return(1); - } - else - { - if (all->number_of_philosophers <= 0) - return(1); - if (all->time_to_die <= 0) - return(1); - if (all->time_to_eat <= 0) - return(1); - if (all->time_to_sleep <= 0) - return(1); - } - return (0); -} - -int parse_param(t_all *all, int optional, char **argv) -{ - if (optional) - { - all->number_of_philosophers = ft_atoi(argv[1]); - all->time_to_die = ft_atoi(argv[2]); - all->time_to_eat = ft_atoi(argv[3]); - all->time_to_sleep = ft_atoi(argv[4]); - all->number_of_times_each_philosopher_must_eat = ft_atoi(argv[5]); - return (check_param(all, 1)); - } - else - { - all->number_of_philosophers = ft_atoi(argv[1]); - all->time_to_die = ft_atoi(argv[2]); - all->time_to_eat = ft_atoi(argv[3]); - all->time_to_sleep = ft_atoi(argv[4]); - return (check_param(all, 0)); - } -} - -int parse_and_check_values(char **argv, int quantity) -{ - t_all *all; - - all = structalloc_and_bzero(); - if (quantity == 5) - { - printf("ok standart run\n"); - parse_param(all, 0, argv); - return(1); - } - else if (quantity == 6) - { - printf("ok run with optional\n"); - parse_param(all, 1, argv); - return(1); - } - else - { - printf("error please use argument 5 (optional) or 4 (standart) \n"); - free(all); - return(0); - } -} - int main(int argc, char **argv) { - parse_and_check_values(argv, argc); + if (parse_and_check_values(argv, argc)) + ; + else + { + printf("exit because error\n"); + return (1); + } + + return(0); } diff --git a/philo_one/srcs/parser.c b/philo_one/srcs/parser.c new file mode 100644 index 0000000..2c9ddc9 --- /dev/null +++ b/philo_one/srcs/parser.c @@ -0,0 +1,90 @@ +#include "../includes/philo_header.h" + +int check_param(t_all *all, int optional) +{ + if (optional) + { + if (all->number_of_philosophers <= 0) + return(1); + if (all->time_to_die <= 0) + return(1); + if (all->time_to_eat <= 0) + return(1); + if (all->time_to_sleep <= 0) + return(1); + if (all->number_of_times_each_philosopher_must_eat <= 0) + return(1); + } + else + { + if (all->number_of_philosophers <= 0) + return(1); + if (all->time_to_die <= 0) + return(1); + if (all->time_to_eat <= 0) + return(1); + if (all->time_to_sleep <= 0) + return(1); + } + return (0); +} + +int parse_param(t_all *all, int optional, char **argv) +{ + if (optional) + { + all->number_of_philosophers = ft_atoi(argv[1]); + all->time_to_die = ft_atoi(argv[2]); + all->time_to_eat = ft_atoi(argv[3]); + all->time_to_sleep = ft_atoi(argv[4]); + all->number_of_times_each_philosopher_must_eat = ft_atoi(argv[5]); + return (check_param(all, 1)); + } + else + { + all->number_of_philosophers = ft_atoi(argv[1]); + all->time_to_die = ft_atoi(argv[2]); + all->time_to_eat = ft_atoi(argv[3]); + all->time_to_sleep = ft_atoi(argv[4]); + return (check_param(all, 0)); + } +} + +int parse_and_check_values(char **argv, int quantity) +{ + t_all *all; + + all = structalloc_and_bzero(); + if (quantity == 5) + { + if (!parse_param(all, 0, argv)) + { + printf("ok standart run\n"); + return(1); + } + else + { + printf("bad arg\n"); + return (0); + } + } + else if (quantity == 6) + { + if (!parse_param(all, 1, argv)) + { + printf("ok run with optional\n"); + return(1); + } + else + { + printf("bad arg\n"); + return (0); + } + } + else + { + printf("error please use argument 5 (optional) or 4 (standart) \n"); + free(all); + return(0); + } +}