diff --git a/philo_one/Makefile b/philo_one/Makefile index e69de29..8bd3076 100644 --- a/philo_one/Makefile +++ b/philo_one/Makefile @@ -0,0 +1,45 @@ +.PHONY: all clean fclean re + +SRCS = main.c utilc.c + + +SRC_DIR = ./srcs/ +OBJ_DIR = ./obj/ + +OBJS = $(SRCS:%.c=%.o) + +OBJS_FILES = $(addprefix $(OBJ_DIR), $(OBJS)) + +HEADER = philo_header.h + +vpath %.h includes +vpath %.o $(OBJ_DIR) +vpath %.c $(SRC_DIR) + +CC = gcc +RM = rm -f +CFLAGS = -Wall -Wextra -Werror -I ./includes/ + +NAME = philo_one + +all: $(OBJ_DIR) $(NAME) + +norme : + norminette $(addprefix $(SRC_DIR), $(SRCS)) + +$(OBJ_DIR): + mkdir -p obj + +$(NAME): $(OBJS_FILES) + $(CC) $(CFLAGS) -o $(NAME) $(OBJS_FILES) + +clean: + $(RM) $(OBJS_FILES) + +$(OBJ_DIR)%.o: %.c $(HEADER) + $(CC) $(CFLAGS) -c $< -o $@ + +fclean: clean + $(RM) $(NAME) + +re: fclean $(NAME) diff --git a/philo_one/includes/philo_header.h b/philo_one/includes/philo_header.h index 804d75c..2457013 100644 --- a/philo_one/includes/philo_header.h +++ b/philo_one/includes/philo_header.h @@ -6,6 +6,7 @@ # include # include # include +# include typedef struct s_all @@ -17,8 +18,10 @@ typedef struct s_all int number_of_times_each_philosopher_must_eat; } t_all; -void *ft_calloc(size_t num, size_t size); -t_all *structalloc_and_bzero(void); -int parse_and_check_values(char **argv, int quantity); +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); #endif diff --git a/philo_one/srcs/main.c b/philo_one/srcs/main.c index a728aa3..c10c0c5 100644 --- a/philo_one/srcs/main.c +++ b/philo_one/srcs/main.c @@ -1,15 +1,81 @@ #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); } diff --git a/philo_one/srcs/utilc.c b/philo_one/srcs/utilc.c index e201a5f..45eb288 100644 --- a/philo_one/srcs/utilc.c +++ b/philo_one/srcs/utilc.c @@ -1,5 +1,43 @@ #include "../includes/philo_header.h" +int check_f(char ch) +{ + if (ch == '\t' || ch == '\n' || ch == ' ' || \ + ch == '\v' || ch == '\f' || ch == '\r') + return (1); + else + return (0); +} + +int ft_atoi(const char *str) +{ + int i; + unsigned long int result; + int operator; + + i = 0; + operator = 1; + result = 0; + while (check_f(str[i])) + i++; + if (str[i] == '-' || str[i] == '+') + { + if (str[i] == '-') + operator *= -1; + i++; + } + while (str[i] >= '0' && str[i] <= '9') + { + if (result > 922337203685477580 && operator == -1) + return (0); + else if (result > 922337203685477580 && operator == 1) + return (-1); + result = result * 10 + str[i] - '0'; + i++; + } + return (result * operator); +} + void *ft_calloc(size_t num, size_t size) { void *addr; @@ -23,9 +61,10 @@ t_all *structalloc_and_bzero(void) t_all *all; all = ft_calloc(sizeof(t_all), 1); - all->number_of_philosophers = 0; - all->time_to_die = 0; - all->time_to_eat = 0; - all->time_to_sleep = 0; - all->number_of_times_each_philosopher_must_eat = 0; + all->number_of_philosophers = -1; + all->time_to_die = -1; + all->time_to_eat = -1; + all->time_to_sleep = -1; + all->number_of_times_each_philosopher_must_eat = -1; + return(all); }