diff --git a/Makefile b/Makefile index 0d0b06c..b622464 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,8 @@ COMPLEX_DIR = radix FLAGS_DIR = flags +CHECKER_DIR = checker + INCLUDES = headers #============================ @@ -33,6 +35,8 @@ FLAGS_FILES = algorithms_sort.c flag.c bench.c PARSING = ft_atoi.c parsing.c ft_strncmp.c ft_split.c ft_strlen.c ft_substr.c checker.c ft_itoa.c ft_isdigit.c \ ft_strjoin.c ft_strlcat.c ft_strlcpy.c parsing_2.c +CHECKER_FILES = check_error.c verif_flag.c verif_is_digit.c verif_overflow.c verif_double.c + STACK_UTILS = push.c rev_rotate.c rotate.c stack_add.c stack_remove.c stacks_len.c swap.c print_stacks.c MEDIUM_ALGO = utils_medium.c utils_struct_tab.c utils_medium_two.c sort_utils.c sort_utils_two.c medium_algo.c @@ -48,7 +52,7 @@ ALGO_UTILS = check_order.c compare.c iterate.c pre_sort.c ALL_FILES = $(SRC) $(STACK_UTILS_DIR)/$(STACK_UTILS) $(PARS_DIR)/$(PARSING) \ $(ALGO_DIR)/$(MEDIUM_DIR)/$(MEDIUM_ALGO) $(ALGO_UTILS_DIR)/$(ALGO_UTILS) \ $(INSERT_DIR)/$(INSERTION) $(ALGO_DIR)/$(COMPLEX_DIR)/$(COMPLEX_ALGO) \ - $(FLAGS_DIR)/$(FLAGS_FILES) + $(FLAGS_DIR)/$(FLAGS_FILES) $(CHECKER_DIR)/$(CHECKER_FILES) OBJ_DIR = obj @@ -95,6 +99,9 @@ $(OBJ_DIR)/%.o: $(ALGO_UTILS_DIR)/%.c | $(OBJ_DIR) $(OBJ_DIR)/%.o: $(FLAGS_DIR)/%.c | $(OBJ_DIR) $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ +$(OBJ_DIR)/%.o: $(CHECKER_DIR)/%.c | $(OBJ_DIR) + $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ + $(OBJ_DIR)/%.o: %.c | $(OBJ_DIR) $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ diff --git a/checker/check_error.c b/checker/check_error.c new file mode 100644 index 0000000..b549333 --- /dev/null +++ b/checker/check_error.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check_error.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mteriier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/09 09:31:50 by mteriier #+# #+# */ +/* Updated: 2026/01/09 09:31:51 by mteriier ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "check_error.h" + +int check_error(char **tab, int mod) +{ + if (!verif_flag(tab, mod)) + return (0); + if (!verif_is_digit(tab, mod)) + return (0); + if (!verif_overflow(tab, mod)) + return (0); + if (!verif_double(tab, mod)) + return (0); + return (1); +} + + diff --git a/checker/verif_double.c b/checker/verif_double.c new file mode 100644 index 0000000..bdc7ccc --- /dev/null +++ b/checker/verif_double.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* verif_double.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mteriier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/09 10:41:42 by mteriier #+# #+# */ +/* Updated: 2026/01/09 10:41:43 by mteriier ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "parsing.h" +#include "check_error.h" + +static int is_double_in_tab(char **tab, int nb, int pos) +{ + int i; + + i = pos; + while (tab[i]) + { + if (nb == ft_atoi(tab[i])) + return (0); + i++; + } + return (1); +} + +int verif_double(char **tab, int mod) +{ + int i; + int tmp; + + i = wich_mod(mod); + while (tab[i]) + { + if (tab[i + 1]) + { + tmp = ft_atoi(tab[i]); + if (!is_double_in_tab(tab, tmp, i + 1)) + return (0); + } + i++; + } + return (1); +} diff --git a/checker/verif_flag.c b/checker/verif_flag.c new file mode 100644 index 0000000..8ad3d76 --- /dev/null +++ b/checker/verif_flag.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* verif_flag.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mteriier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/09 09:51:11 by mteriier #+# #+# */ +/* Updated: 2026/01/09 09:51:12 by mteriier ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "parsing.h" + +static int is_exist_flag(char **tab, int pos) +{ + int verif; + + verif = 0; + if (ft_strncmp(tab[pos], "--bench", 7) + || ft_strncmp(tab[pos], "--simple", 8) + || ft_strncmp(tab[pos], "--medium", 8) + || ft_strncmp(tab[pos], "--complex", 9)) + verif = 1; + return (verif); +} + +static int verif_exist_flag(char **tab, int mod) +{ + int verif; + + verif = 0; + if (mod == 1) + verif = (is_exist_flag(tab, 1)); + else if (mod == 2) + { + if (is_exist_flag(tab, 1) && is_exist_flag(tab, 2)) + verif = 1; + } + return (verif); +} + +static int verif_double_flag(char **tab, int mod) +{ + int verif; + + verif = 1; + if (mod == 2 && ft_strncmp(tab[1], tab[2], 9)) + verif = 0; + return (verif); +} + +int verif_flag(char **tab, int mod) +{ + if (mod == 0) + return (1); + if (verif_double_flag(tab, mod) && verif_exist_flag(tab, mod)) + return (1); + return (0); +} \ No newline at end of file diff --git a/checker/verif_is_digit.c b/checker/verif_is_digit.c new file mode 100644 index 0000000..65b4e0a --- /dev/null +++ b/checker/verif_is_digit.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* verif_is_digit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mteriier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/09 10:25:23 by mteriier #+# #+# */ +/* Updated: 2026/01/09 10:25:24 by mteriier ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "parsing.h" + +static int scan_str_is_digit(char *tab) +{ + int i; + + i = 0; + while (tab[i]) + { + if (!ft_isdigit(tab[i]) && (tab[i] == '-' && !ft_isdigit(tab[i + 1]))) + return (0); + i++; + } + return (1); +} + +int verif_is_digit(char **tab, int mod) +{ + int i; + + i = wich_mod(mod); + while (tab[i]) + { + if (!scan_str_is_digit(tab[i])) + return (0); + i++; + } + return (1); +} diff --git a/checker/verif_overflow.c b/checker/verif_overflow.c new file mode 100644 index 0000000..d9aa159 --- /dev/null +++ b/checker/verif_overflow.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* verif_overflow.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mteriier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/09 10:30:39 by mteriier #+# #+# */ +/* Updated: 2026/01/09 10:30:40 by mteriier ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "parsing.h" + +static int verif_atoi(const char *nptr) +{ + size_t i; + int tmp; + int before; + + i = 0; + tmp = 0; + before = 0; + while ((nptr[i] >= 9 && nptr[i] <= 13) || nptr[i] == ' ') + i++; + if (nptr[i] == '-' || nptr[i] == '+') + i++; + while (nptr[i] >= '0' && nptr[i] <= '9') + { + before = tmp; + tmp = tmp * 10 + nptr[i] - '0'; + if (before > tmp) + return (0); + i++; + } + return (1); +} + +int verif_overflow(char **tab, int mod) +{ + int i; + + i = wich_mod(mod); + while (tab[i]) + { + if (!verif_atoi(tab[i])) + return (0); + i++; + } + return (1); +} diff --git a/headers/check_error.h b/headers/check_error.h new file mode 100644 index 0000000..a9a506a --- /dev/null +++ b/headers/check_error.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check_error.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mteriier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/09 10:16:35 by mteriier #+# #+# */ +/* Updated: 2026/01/09 10:16:37 by mteriier ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CHECK_ERROR_H +# define CHECK_ERROR_H + +int verif_flag(char **tab, int mod); +int check_error(char **tab, int mod); +int verif_is_digit(char **tab, int mod); +int verif_overflow(char **tab, int mod); +int verif_double(char **tab, int mod); + +#endif diff --git a/headers/parsing.h b/headers/parsing.h index 72b0410..8925ce6 100644 --- a/headers/parsing.h +++ b/headers/parsing.h @@ -17,6 +17,7 @@ # include "push_swap.h" int ft_atoi(const char *nptr); +int wich_mod(int mod); t_stacks *init_stacks(int argc, char **argv, int mod); int ft_strncmp(const char *s1, const char *s2, int n); size_t ft_strlen(const char *s); diff --git a/headers/push_swap.h b/headers/push_swap.h index 495fef0..c039fe4 100644 --- a/headers/push_swap.h +++ b/headers/push_swap.h @@ -79,7 +79,7 @@ int check_order(t_stack *stack); void insertion(t_stacks *stacks, int len); int test2(char **argv); /* TEST FILE */ -int test1(int argc, char **argv); +int test1(char **tab, int len, int mod); /* RADIX */ void radix(t_stacks *stacks); diff --git a/main.c b/main.c index 13a55be..4c8434e 100644 --- a/main.c +++ b/main.c @@ -12,16 +12,25 @@ #include "push_swap.h" #include "parsing.h" +#include "check_error.h" +#include "flags.h" #include int main(int argc, char **argv) { - // if (!checker(argc, argv)) - // { - // write(2, "Error !\n", 8); - // return (1); - // } - if (argc > 1) - test1(argc, argv); + char **tab; + int mod; + int len; + + tab = split_all(join_all(argc, argv)); + if (!tab) + return (0); + len = len_split(tab); + mod = calcul_mod(len, tab); + if (check_error(tab, mod)) + test1(tab, len, mod); + else + write(2, "Error\n", 7); + free_tab(tab); return (0); } diff --git a/parsing/parsing.c b/parsing/parsing.c index 787aa3b..3a80a9c 100644 --- a/parsing/parsing.c +++ b/parsing/parsing.c @@ -14,7 +14,7 @@ #include "parsing.h" #include -static int wich_mod(int mod) +int wich_mod(int mod) { if (mod == 0) return (1); diff --git a/test_one.c b/test_one.c index 8faa47a..88a33bb 100644 --- a/test_one.c +++ b/test_one.c @@ -16,19 +16,11 @@ #include "medium_headers.h" #include -int test1(int argc, char **argv) +int test1(char **tab, int len, int mod) { t_stacks *stacks; - int mod; - char **tab; - int len; stacks = NULL; - tab = split_all(join_all(argc, argv)); - if (!tab) - return (0); - len = len_split(tab); - mod = calcul_mod(len, tab); if (mod == -1) return (0); stacks = init_stacks(len, tab, mod);