new cliopts from 42sh
This commit is contained in:
parent
07a27ee936
commit
a5ae25cd9d
5 changed files with 75 additions and 111 deletions
|
|
@ -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\
|
||||
|
|
|
|||
|
|
@ -6,12 +6,12 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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"
|
||||
|
|
@ -27,15 +27,17 @@ struct s_cliopts
|
|||
t_flag flag_on;
|
||||
t_flag flag_off;
|
||||
int (*get)();
|
||||
int arg_required:1;
|
||||
};
|
||||
|
||||
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);
|
||||
t_cliopts *cliopts_getmap_long(t_cliopts opt_map[], char *arg);
|
||||
t_cliopts *cliopts_getmap_short(t_cliopts opt_map[], char arg);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -17,72 +17,66 @@
|
|||
|
||||
#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]);
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,32 +1,35 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cliopts_getdata.c :+: :+: :+: */
|
||||
/* cliopts_getmap.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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)
|
||||
int i;
|
||||
|
||||
i = -1;
|
||||
while (opt_map[++i].c)
|
||||
if (!ft_strcmp(opt_map[i].str, arg))
|
||||
return (&opt_map[i]);
|
||||
return (NULL);
|
||||
av++;
|
||||
while (*av)
|
||||
}
|
||||
|
||||
t_cliopts *cliopts_getmap_short(t_cliopts opt_map[], char arg)
|
||||
{
|
||||
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 (opt_map[i].c == arg)
|
||||
return (&opt_map[i]);
|
||||
return (NULL);
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
Loading…
Reference in a new issue