new cliopts from 42sh

This commit is contained in:
Jack Halford 2017-03-26 15:25:13 +02:00
parent 318c4ded18
commit c472ce8e7c
5 changed files with 75 additions and 111 deletions

View file

@ -49,8 +49,7 @@ char/ft_isprint.c\
char/ft_tolower.c\ char/ft_tolower.c\
char/ft_toupper.c\ char/ft_toupper.c\
cliopts/cliopts_get.c\ cliopts/cliopts_get.c\
cliopts/cliopts_getdata.c\ cliopts/cliopts_getmap.c\
cliopts/cliopts_has.c\
color/ft_color_mk.c\ color/ft_color_mk.c\
color/ft_color_mkif.c\ color/ft_color_mkif.c\
color/ft_color_reset.c\ color/ft_color_reset.c\

View file

@ -6,12 +6,12 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 20:22:56 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 #ifndef CLIOPTS_H
# define CLI_OPTS_H # define CLIOPTS_H
# include "libft.h" # include "libft.h"
# include "error.h" # include "error.h"
@ -27,15 +27,17 @@ struct s_cliopts
t_flag flag_on; t_flag flag_on;
t_flag flag_off; t_flag flag_off;
int (*get)(); int (*get)();
int arg_required:1;
}; };
struct s_data_template struct s_data_template
{ {
t_flag flag; t_flag flag;
char **av_data;
}; };
int cliopts_get(char **av, t_cliopts opt_map[], void *data); int cliopts_get(char **av, t_cliopts opt_map[], void *data);
char **cliopts_getdata(char **av); t_cliopts *cliopts_getmap_long(t_cliopts opt_map[], char *arg);
int cliopts_has(char **av, char c); t_cliopts *cliopts_getmap_short(t_cliopts opt_map[], char arg);
#endif #endif

View file

@ -6,83 +6,77 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 20:04:04 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` ** void *data must be a structure starting with `int flag`
** to do polymorphism with t_data_template ! ** 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; if (!av || !*av)
while (opt_map[++i].c)
if (ft_strcmp(opt_map[i].str, arg) == 0)
return (&opt_map[i]);
return (NULL); 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) static int cliopts_parse_short(
{ char ***av, t_cliopts opt_map[], void *data)
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)
{ {
t_cliopts *map; t_cliopts *map;
char *arg; char *arg;
int i; int i;
char *tmp;
arg = **av + 1; arg = **av + 1;
i = 0; i = -1;
while (arg[i]) 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])); 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_on;
((t_data_template*)data)->flag &= ~map->flag_off; ((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; t_cliopts *map;
char *arg; char *arg;
char *tmp;
arg = **av + 2; 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)); return (ERR_SET(E_CO_INVL, arg));
((t_data_template*)data)->flag |= map->flag_on; ((t_data_template*)data)->flag |= map->flag_on;
((t_data_template*)data)->flag &= ~map->flag_off; ((t_data_template*)data)->flag &= ~map->flag_off;
if (map->get) if (map->get)
{ {
++(*av); if (map->arg_required && !(tmp = check_required(av, NULL)))
if ((map->get)(av, data)) return (ERR_SET(E_CO_MISS, *arg));
if ((map->get)(tmp, data))
return (ERR_SET(E_CO_MISSL, arg)); return (ERR_SET(E_CO_MISSL, arg));
} }
++(*av); ++(*av);
@ -96,8 +90,8 @@ int cliopts_get(char **av, t_cliopts opt_map[], void *data)
av++; av++;
while (av && *av) while (av && *av)
{ {
if (ft_strcmp(*av, "--") == 0) if (ft_strcmp(*av, "-") == 0 || (ft_strcmp(*av, "--") == 0 && av++))
return (0); break ;
else if ((*av)[0] == '-' && (*av)[1] == '-') else if ((*av)[0] == '-' && (*av)[1] == '-')
{ {
if (cliopts_parse_long(&av, opt_map, data)) 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); return (1);
} }
else else
return (0); break ;
} }
((t_data_template*)data)->av_data = av;
return (0); return (0);
} }

View file

@ -1,32 +1,35 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* cliopts_getdata.c :+: :+: :+: */ /* cliopts_getmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 20:35:15 by jhalford #+# #+# */ /* Created: 2017/03/25 14:59:03 by jhalford #+# #+# */
/* Updated: 2017/03/14 21:35:19 by jhalford ### ########.fr */ /* Updated: 2017/03/25 15:01:10 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cliopts.h" #include "cliopts.h"
char **cliopts_getdata(char **av) t_cliopts *cliopts_getmap_long(t_cliopts opt_map[], char *arg)
{ {
if (!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); 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);
} }

View file

@ -1,35 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cliopts_has.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}