From a5ae25cd9d1da63d919641fb0f11f9f0302e43d2 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Sun, 26 Mar 2017 15:25:13 +0200 Subject: [PATCH] new cliopts from 42sh --- libftasm/Makefile | 3 +- libftasm/includes/cliopts.h | 18 ++-- libftasm/src/cliopts/cliopts_get.c | 89 +++++++++---------- .../{cliopts_getdata.c => cliopts_getmap.c} | 41 +++++---- libftasm/src/cliopts/cliopts_has.c | 35 -------- 5 files changed, 75 insertions(+), 111 deletions(-) rename libftasm/src/cliopts/{cliopts_getdata.c => cliopts_getmap.c} (56%) delete mode 100644 libftasm/src/cliopts/cliopts_has.c diff --git a/libftasm/Makefile b/libftasm/Makefile index 3fafa5ef..fb38f8d4 100644 --- a/libftasm/Makefile +++ b/libftasm/Makefile @@ -49,8 +49,7 @@ char/ft_isprint.c\ char/ft_tolower.c\ char/ft_toupper.c\ cliopts/cliopts_get.c\ -cliopts/cliopts_getdata.c\ -cliopts/cliopts_has.c\ +cliopts/cliopts_getmap.c\ color/ft_color_mk.c\ color/ft_color_mkif.c\ color/ft_color_reset.c\ diff --git a/libftasm/includes/cliopts.h b/libftasm/includes/cliopts.h index 2de89bd8..f70f2345 100644 --- a/libftasm/includes/cliopts.h +++ b/libftasm/includes/cliopts.h @@ -6,12 +6,12 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 20:22:56 by jhalford #+# #+# */ -/* Updated: 2017/03/15 19:00:17 by jhalford ### ########.fr */ +/* Updated: 2017/03/26 15:25:54 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef CLI_OPTS_H -# define CLI_OPTS_H +#ifndef CLIOPTS_H +# define CLIOPTS_H # include "libft.h" # include "error.h" @@ -20,22 +20,24 @@ typedef struct s_cliopts t_cliopts; typedef struct s_data_template t_data_template; typedef long long t_flag; -struct s_cliopts +struct s_cliopts { char c; char *str; t_flag flag_on; t_flag flag_off; int (*get)(); + int arg_required:1; }; -struct s_data_template +struct s_data_template { t_flag flag; + char **av_data; }; -int cliopts_get(char **av, t_cliopts opt_map[], void *data); -char **cliopts_getdata(char **av); -int cliopts_has(char **av, char c); +int cliopts_get(char **av, t_cliopts opt_map[], void *data); +t_cliopts *cliopts_getmap_long(t_cliopts opt_map[], char *arg); +t_cliopts *cliopts_getmap_short(t_cliopts opt_map[], char arg); #endif diff --git a/libftasm/src/cliopts/cliopts_get.c b/libftasm/src/cliopts/cliopts_get.c index 9aaea51f..37094636 100644 --- a/libftasm/src/cliopts/cliopts_get.c +++ b/libftasm/src/cliopts/cliopts_get.c @@ -6,83 +6,77 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 20:04:04 by jhalford #+# #+# */ -/* Updated: 2017/03/15 21:18:20 by jhalford ### ########.fr */ +/* Updated: 2017/03/26 15:27:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ /* - ** void *data must be a structure starting with `int flag` - ** to do polymorphism with t_data_template ! - */ +** void *data must be a structure starting with `int flag` +** to do polymorphism with t_data_template ! +*/ -# include "cliopts.h" +#include "cliopts.h" -static t_cliopts *get_map_long(t_cliopts opt_map[], char *arg) +static char *check_required(char ***av, char *arg) { - int i; + char *ret; - i = -1; - while (opt_map[++i].c) - if (ft_strcmp(opt_map[i].str, arg) == 0) - return (&opt_map[i]); - return (NULL); + if (!av || !*av) + return (NULL); + if (!arg || !*arg || !*(arg + 1)) + return (*++(*av)); + ret = arg + 1; + return (ret); } -static t_cliopts *get_map_short(t_cliopts opt_map[], char arg) -{ - int i; - - i = -1; - while (opt_map[++i].c) - if (opt_map[i].c == arg) - return (&opt_map[i]); - return (NULL); -} - -static int cliopts_parse_short(char ***av, t_cliopts opt_map[], void *data) +static int cliopts_parse_short( + char ***av, t_cliopts opt_map[], void *data) { t_cliopts *map; char *arg; int i; + char *tmp; arg = **av + 1; - i = 0; - while (arg[i]) + i = -1; + while (arg[++i] && !(tmp = NULL)) { - if (!(map = get_map_short(opt_map, arg[i]))) + if (!(map = cliopts_getmap_short(opt_map, arg[i]))) return (ERR_SET(E_CO_INV, arg[i])); - if (map->get) - { - if (!(arg[i - 1] == '-' && arg[i + 1] == 0)) - return (ERR_SET(E_CO_MULT, *arg)); - ++(*av); - if ((map->get)(av, data)) - return (ERR_SET(E_CO_MISS, *arg)); - break; - } ((t_data_template*)data)->flag |= map->flag_on; ((t_data_template*)data)->flag &= ~map->flag_off; - i++; + if (map->get) + { + if (map->arg_required && !(tmp = check_required(av, arg + i))) + return (ERR_SET(E_CO_MISS, *arg)); + tmp = tmp ? tmp : **av; + if ((map->get)(tmp, data)) + return (ERR_SET(E_CO_MISS, *arg)); + if (map->arg_required) + break ; + } } - ++(*av); - return (0); - + return (++(*av) ? 0 : 0); } -static int cliopts_parse_long(char ***av, t_cliopts opt_map[], void *data) +static int cliopts_parse_long( + char ***av, t_cliopts opt_map[], void *data) { t_cliopts *map; char *arg; + char *tmp; arg = **av + 2; - if (!(map = get_map_long(opt_map, arg))) + tmp = NULL; + if (!(map = cliopts_getmap_long(opt_map, arg))) return (ERR_SET(E_CO_INVL, arg)); ((t_data_template*)data)->flag |= map->flag_on; ((t_data_template*)data)->flag &= ~map->flag_off; if (map->get) { - ++(*av); - if ((map->get)(av, data)) + if (map->arg_required && !(tmp = check_required(av, NULL))) + return (ERR_SET(E_CO_MISS, *arg)); + if ((map->get)(tmp, data)) return (ERR_SET(E_CO_MISSL, arg)); } ++(*av); @@ -96,8 +90,8 @@ int cliopts_get(char **av, t_cliopts opt_map[], void *data) av++; while (av && *av) { - if (ft_strcmp(*av, "--") == 0) - return (0); + if (ft_strcmp(*av, "-") == 0 || (ft_strcmp(*av, "--") == 0 && av++)) + break ; else if ((*av)[0] == '-' && (*av)[1] == '-') { if (cliopts_parse_long(&av, opt_map, data)) @@ -109,7 +103,8 @@ int cliopts_get(char **av, t_cliopts opt_map[], void *data) return (1); } else - return (0); + break ; } + ((t_data_template*)data)->av_data = av; return (0); } diff --git a/libftasm/src/cliopts/cliopts_getdata.c b/libftasm/src/cliopts/cliopts_getmap.c similarity index 56% rename from libftasm/src/cliopts/cliopts_getdata.c rename to libftasm/src/cliopts/cliopts_getmap.c index 1beb10cd..189e22ba 100644 --- a/libftasm/src/cliopts/cliopts_getdata.c +++ b/libftasm/src/cliopts/cliopts_getmap.c @@ -1,32 +1,35 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* cliopts_getdata.c :+: :+: :+: */ +/* cliopts_getmap.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/03/14 20:35:15 by jhalford #+# #+# */ -/* Updated: 2017/03/14 21:35:19 by jhalford ### ########.fr */ +/* Created: 2017/03/25 14:59:03 by jhalford #+# #+# */ +/* Updated: 2017/03/25 15:01:10 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "cliopts.h" -char **cliopts_getdata(char **av) +t_cliopts *cliopts_getmap_long(t_cliopts opt_map[], char *arg) { - if (!av) - return (NULL); - av++; - while (*av) - { - if (ft_strcmp(*av, "--") == 0) - return (av + 1); - else if ((*av)[0] == '-' && (*av)[1] == '-') - av++; - else if ((*av)[0] == '-') - av++; - else - return (av); - } - return (av); + int i; + + i = -1; + while (opt_map[++i].c) + if (!ft_strcmp(opt_map[i].str, arg)) + return (&opt_map[i]); + return (NULL); +} + +t_cliopts *cliopts_getmap_short(t_cliopts opt_map[], char arg) +{ + int i; + + i = -1; + while (opt_map[++i].c) + if (opt_map[i].c == arg) + return (&opt_map[i]); + return (NULL); } diff --git a/libftasm/src/cliopts/cliopts_has.c b/libftasm/src/cliopts/cliopts_has.c deleted file mode 100644 index 3d1fc5c5..00000000 --- a/libftasm/src/cliopts/cliopts_has.c +++ /dev/null @@ -1,35 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* cliopts_has.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/03/14 20:03:18 by jhalford #+# #+# */ -/* Updated: 2017/03/14 20:24:39 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "cliopts.h" - -int cliopts_has(char **av, char c) -{ - if (!av) - return (0); - while (*av) - { - if (ft_strcmp(*av, "--") == 0) - return (0); - else if ((*av)[0] == '-' && (*av)[1] == '-') - av++; - else if ((*av)[0] == '-') - { - if (ft_strchr(*av + 1, c)) - return (1); - av++; - } - else - return (0); - } - return (0); -}