From dcd2a41e545cbee2a4487e28f684d19f9aa79877 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 26 Aug 2020 18:17:02 +0200 Subject: Err check in progress --- src/b_sqb_err.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/b_sqb_err.c (limited to 'src/b_sqb_err.c') diff --git a/src/b_sqb_err.c b/src/b_sqb_err.c new file mode 100644 index 0000000..1755082 --- /dev/null +++ b/src/b_sqb_err.c @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_sqb_err.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +#include "d_define.h" +#include "u_utils.h" + +static uint8_t + b_file_err(const uint64_t argc, + char *argv[]) +{ + if (argc > 3) + { + ft_dprintf(STDERR_FILENO, "[: %s: unexpected operator\n", argv[1]); + return (1); + } + return (0); +} + +static uint8_t + b_double_file_err(const uint64_t argc, + char *argv[]) +{ + if (argc > 4) + { + ft_dprintf(STDERR_FILENO, "[: %s: unexpected operator\n", argv[2]); + return (1); + } + else if (argc == 3) + { + ft_dprintf(STDERR_FILENO, "[: %s: argument expected\n", argv[1]); + return (1); + } + return (0); +} + +static uint8_t + b_str_err(const uint64_t argc, + char *argv[]) +{ + if (argc > 4) + { + ft_dprintf(STDERR_FILENO, "[: %s: unexpected operator\n", argv[2]); + return (1); + } + else if (argc == 3) + { + ft_dprintf(STDERR_FILENO, "[: %s: argument expected\n", argv[1]); + return (1); + } + return (0); +} + +uint8_t + b_sqb_err_check(uint8_t id, + char *argv[]) +{ + const uint64_t argc = u_builtins_get_argc((const char**)argv); + + if (id < FT_ID_SQB_NT && id != FT_ID_SQB_Z && id != FT_ID_SQB_N) + { + if (b_file_err(argc, argv) != 0) + return (1); + } + else if (id == FT_ID_SQB_NT || id == FT_ID_SQB_OT) + { + if (b_double_file_err(argc, argv) != 0) + return (1); + } + else if (id == FT_ID_SQB_N || id == FT_ID_SQB_Z || + id == FT_ID_SQB_EQUA || id == FT_ID_SQB_DIFF) + { + if (b_str_err(argc, argv) != 0) + return (1); + } + return (0); +} -- cgit v1.2.3 From f7ed540c183e6a20f1956901ee6ebd908745ccb8 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 26 Aug 2020 18:26:44 +0200 Subject: Err check in progress --- src/b_sqb_err.c | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) (limited to 'src/b_sqb_err.c') diff --git a/src/b_sqb_err.c b/src/b_sqb_err.c index 1755082..eac6a8a 100644 --- a/src/b_sqb_err.c +++ b/src/b_sqb_err.c @@ -18,8 +18,8 @@ #include "u_utils.h" static uint8_t - b_file_err(const uint64_t argc, - char *argv[]) + b_single_op(const uint64_t argc, + char *argv[]) { if (argc > 3) { @@ -30,8 +30,8 @@ static uint8_t } static uint8_t - b_double_file_err(const uint64_t argc, - char *argv[]) + b_double_op(const uint64_t argc, + char *argv[]) { if (argc > 4) { @@ -47,19 +47,9 @@ static uint8_t } static uint8_t - b_str_err(const uint64_t argc, - char *argv[]) + b_bad_nbr(char *argv[]) { - if (argc > 4) - { - ft_dprintf(STDERR_FILENO, "[: %s: unexpected operator\n", argv[2]); - return (1); - } - else if (argc == 3) - { - ft_dprintf(STDERR_FILENO, "[: %s: argument expected\n", argv[1]); - return (1); - } + (void)argv; return (0); } @@ -69,20 +59,19 @@ uint8_t { const uint64_t argc = u_builtins_get_argc((const char**)argv); - if (id < FT_ID_SQB_NT && id != FT_ID_SQB_Z && id != FT_ID_SQB_N) + if (id < FT_ID_SQB_NT) { - if (b_file_err(argc, argv) != 0) + if (b_single_op(argc, argv) != 0) return (1); } - else if (id == FT_ID_SQB_NT || id == FT_ID_SQB_OT) + else if (id >= FT_ID_SQB_NT) { - if (b_double_file_err(argc, argv) != 0) + if (b_double_op(argc, argv) != 0) return (1); } - else if (id == FT_ID_SQB_N || id == FT_ID_SQB_Z || - id == FT_ID_SQB_EQUA || id == FT_ID_SQB_DIFF) + if (id >= FT_ID_SQB_EQ) { - if (b_str_err(argc, argv) != 0) + if (b_bad_nbr(argv) != 0) return (1); } return (0); -- cgit v1.2.3 From eb40600dc1e7e4c61547173953958d50bf6a89b3 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 26 Aug 2020 18:39:48 +0200 Subject: Err check done --- src/b_sqb_err.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 11 deletions(-) (limited to 'src/b_sqb_err.c') diff --git a/src/b_sqb_err.c b/src/b_sqb_err.c index eac6a8a..6c68841 100644 --- a/src/b_sqb_err.c +++ b/src/b_sqb_err.c @@ -30,17 +30,24 @@ static uint8_t } static uint8_t - b_double_op(const uint64_t argc, + b_expect_arg(const uint64_t argc, char *argv[]) { - if (argc > 4) + if (argc == 3) { - ft_dprintf(STDERR_FILENO, "[: %s: unexpected operator\n", argv[2]); + ft_dprintf(STDERR_FILENO, "[: %s: argument expected\n", argv[1]); return (1); } - else if (argc == 3) + return (0); +} + +static uint8_t + b_double_op(const uint64_t argc, + char *argv[]) +{ + if (argc > 4) { - ft_dprintf(STDERR_FILENO, "[: %s: argument expected\n", argv[1]); + ft_dprintf(STDERR_FILENO, "[: %s: unexpected operator\n", argv[2]); return (1); } return (0); @@ -49,7 +56,30 @@ static uint8_t static uint8_t b_bad_nbr(char *argv[]) { - (void)argv; + char *ptr; + + if (ft_ischarset("+-0123456789", argv[0][0]) == FALSE || + ft_ischarset("+-0123456789", argv[2][0]) == FALSE) + return (1); + ptr = argv[0] + 1; + while (*ptr != '\0') + { + if (ft_isdigit(*ptr) == FALSE) + { + ft_dprintf(STDERR_FILENO, "[: %s: bad number\n", argv[0]); + return (1); + } + ptr++; + } + ptr = argv[2] + 1; + while (*ptr != '\0') + { + if (ft_isdigit(*ptr) == FALSE) + ft_dprintf(STDERR_FILENO, "[: %s: bad number\n", argv[2]); + if (ft_isdigit(*ptr) == FALSE) + return (1); + ptr++; + } return (0); } @@ -59,6 +89,16 @@ uint8_t { const uint64_t argc = u_builtins_get_argc((const char**)argv); + if (id >= FT_ID_SQB_NT) + { + if (b_expect_arg(argc, argv) != 0) + return (1); + } + if (id >= FT_ID_SQB_EQ) + { + if (b_bad_nbr(argv) != 0) + return (1); + } if (id < FT_ID_SQB_NT) { if (b_single_op(argc, argv) != 0) @@ -69,10 +109,5 @@ uint8_t if (b_double_op(argc, argv) != 0) return (1); } - if (id >= FT_ID_SQB_EQ) - { - if (b_bad_nbr(argv) != 0) - return (1); - } return (0); } -- cgit v1.2.3