From f5c8342a3bb4d58c9b87f21ca4dcc188e5e86bd5 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 14 Mar 2017 21:11:07 +0100 Subject: [PATCH] cliopts now part of libft --- libftasm/Makefile | 4 + libftasm/includes/btree.h | 2 +- libftasm/includes/cliopts.h | 41 ++++++++ libftasm/includes/error.h | 44 ++++++++ libftasm/includes/ft_printf.h | 3 +- libftasm/includes/libft.h | 19 ++-- libftasm/includes/{ft_sys.h => sys.h} | 10 +- libftasm/src/btree/btree_apply_by_level.c | 2 +- libftasm/src/cliopts/cliopts_get.c | 115 +++++++++++++++++++++ libftasm/src/cliopts/cliopts_getdata.c | 32 ++++++ libftasm/src/cliopts/cliopts_has.c | 35 +++++++ libftasm/src/error/error.c | 42 ++++++++ libftasm/src/ft_printf/ft_printf.c | 24 ++++- libftasm/src/get_next_line/get_next_line.c | 3 +- libftasm/src/sstr/ft_sstrdel.c | 2 +- libftasm/src/str/ft_strcmp.c | 2 +- 16 files changed, 358 insertions(+), 22 deletions(-) create mode 100644 libftasm/includes/cliopts.h create mode 100644 libftasm/includes/error.h rename libftasm/includes/{ft_sys.h => sys.h} (82%) create mode 100644 libftasm/src/cliopts/cliopts_get.c create mode 100644 libftasm/src/cliopts/cliopts_getdata.c create mode 100644 libftasm/src/cliopts/cliopts_has.c create mode 100644 libftasm/src/error/error.c diff --git a/libftasm/Makefile b/libftasm/Makefile index fbc1aded..3fafa5ef 100644 --- a/libftasm/Makefile +++ b/libftasm/Makefile @@ -48,6 +48,9 @@ char/ft_isdigit.c\ char/ft_isprint.c\ char/ft_tolower.c\ char/ft_toupper.c\ +cliopts/cliopts_get.c\ +cliopts/cliopts_getdata.c\ +cliopts/cliopts_has.c\ color/ft_color_mk.c\ color/ft_color_mkif.c\ color/ft_color_reset.c\ @@ -60,6 +63,7 @@ dlst/ft_dlstlast.c\ dlst/ft_dlstnew.c\ dlst/ft_dlstrtostr.c\ dlst/ft_dlstsize.c\ +error/error.c\ ft_printf/ft_conversion.c\ ft_printf/ft_fmt_simplify.c\ ft_printf/ft_fmt_validate_conv.c\ diff --git a/libftasm/includes/btree.h b/libftasm/includes/btree.h index ca59a0e1..dd0b2c97 100644 --- a/libftasm/includes/btree.h +++ b/libftasm/includes/btree.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/16 11:13:15 by jhalford #+# #+# */ -/* Updated: 2017/03/07 15:27:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 17:40:20 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/libftasm/includes/cliopts.h b/libftasm/includes/cliopts.h new file mode 100644 index 00000000..765f5464 --- /dev/null +++ b/libftasm/includes/cliopts.h @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cliopts.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/14 20:22:56 by jhalford #+# #+# */ +/* Updated: 2017/03/14 20:47:45 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CLI_OPTS_H +# define CLI_OPTS_H + +# include "libft.h" +# include "error.h" + +typedef struct s_cliopts t_cliopts; +typedef struct s_data_template t_data_template; +typedef long long t_flag; + +struct s_cliopts +{ + char c; + char *str; + t_flag flag_on; + t_flag flag_off; + int (*get)(); +}; + +struct s_data_template +{ + t_flag flag; +}; + +int cliopts_get(char **av, t_cliopts opt_map[], void *data); +char **cliopts_getdata(char **av); +int cliopts_has(char **av, char c); + +#endif diff --git a/libftasm/includes/error.h b/libftasm/includes/error.h new file mode 100644 index 00000000..7c00d0b2 --- /dev/null +++ b/libftasm/includes/error.h @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* error.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/14 15:34:21 by jhalford #+# #+# */ +/* Updated: 2017/03/14 20:25:26 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ERROR_H +# define ERROR_H + +# include "libft.h" +# include + +# define DG_PROTO "{inv}{ran}%5i{yel}%21s {bol}{blu}%-3d{eoc}" +# define DG_ARGS getpid(), getpid(), ft_path_notdir(__FILE__), __LINE__ +# define DG(s, ...) ft_dprintf(STDBUG, DG_PROTO s "{eoc}\n", DG_ARGS, ##__VA_ARGS__) + +# define ERR_PROTO(s, ...) "{red}%s: " s "{eoc}\n", PROGNAME +# define ERR_SET(n, ...) error_set(n, ##__VA_ARGS__) +# define ERRMSG_MAX_SIZE 150 + +enum e_errors +{ + E_NOERR, + E_OPTINVS, + E_OPTINVC, + E_OPTARG, + E_MAX, +}; + +extern char g_error_msg[E_MAX][ERRMSG_MAX_SIZE]; +extern char *g_errmsg; +extern int g_errnum; +extern char **g_argv; + +int error_set(int n, ...); +int ft_perror(void); + +#endif diff --git a/libftasm/includes/ft_printf.h b/libftasm/includes/ft_printf.h index f110dc6c..4f6d169d 100644 --- a/libftasm/includes/ft_printf.h +++ b/libftasm/includes/ft_printf.h @@ -6,12 +6,13 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:22:54 by jhalford #+# #+# */ -/* Updated: 2017/03/10 16:56:01 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 18:07:10 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef FT_PRINTF_H # define FT_PRINTF_H + # include "libft.h" # include diff --git a/libftasm/includes/libft.h b/libftasm/includes/libft.h index cb5ad4d1..d1255b0b 100644 --- a/libftasm/includes/libft.h +++ b/libftasm/includes/libft.h @@ -6,17 +6,13 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:49:04 by jhalford #+# #+# */ -/* Updated: 2017/03/11 13:13:18 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 20:24:05 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef LIBFT_H # define LIBFT_H -# define DG_MSG0 "{inv}{ran}%5i{yel}%21s {bol}{blu}%-3d{eoc}" -# define DG_MSG1 getpid(), getpid(), ft_path_notdir(__FILE__), __LINE__ -# define DG(f, ...) ft_dprintf(STDBUG, DG_MSG0 f "{eoc}\n", DG_MSG1, ##__VA_ARGS__) - # include # include # include @@ -25,16 +21,20 @@ # include # include +# include "error.h" +# include "color.h" +# include "cliopts.h" + # include "lst.h" # include "dlst.h" +# include "btree.h" + # include "str.h" # include "sstr.h" # include "math.h" -# include "btree.h" # include "mytime.h" # include "get_next_line.h" -# include "color.h" -# include "ft_sys.h" +# include "sys.h" typedef struct s_stos t_stos; typedef struct s_stof t_stof; @@ -60,6 +60,9 @@ struct s_itof int ft_printf(const char *format, ...); int ft_dprintf(int fd, const char *format, ...); +int ft_vdprintf(int fd, const char *format, va_list ap); +int ft_asprintf(char **ret, const char *format, ...); +int ft_vasprintf(char **ret, const char *format, va_list ap); void *ft_memset(void *b, int c, size_t len); void ft_bzero(void *s, size_t n); diff --git a/libftasm/includes/ft_sys.h b/libftasm/includes/sys.h similarity index 82% rename from libftasm/includes/ft_sys.h rename to libftasm/includes/sys.h index 5f057611..3d883032 100644 --- a/libftasm/includes/ft_sys.h +++ b/libftasm/includes/sys.h @@ -1,17 +1,17 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_sys.h :+: :+: :+: */ +/* sys.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/02/18 13:09:02 by jhalford #+# #+# */ -/* Updated: 2017/02/18 13:19:06 by jhalford ### ########.fr */ +/* Created: 2017/03/14 17:24:23 by jhalford #+# #+# */ +/* Updated: 2017/03/14 17:24:24 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef FT_SYS_H -# define FT_SYS_H +#ifndef SYS_H +# define SYS_H # define FT_XATTR_SIZE 10000 # define STDIN STDIN_FILENO diff --git a/libftasm/src/btree/btree_apply_by_level.c b/libftasm/src/btree/btree_apply_by_level.c index 09619fee..14e537f7 100644 --- a/libftasm/src/btree/btree_apply_by_level.c +++ b/libftasm/src/btree/btree_apply_by_level.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/08/19 12:06:15 by jhalford #+# #+# */ -/* Updated: 2016/11/16 11:14:28 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 17:25:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/libftasm/src/cliopts/cliopts_get.c b/libftasm/src/cliopts/cliopts_get.c new file mode 100644 index 00000000..e1c85e94 --- /dev/null +++ b/libftasm/src/cliopts/cliopts_get.c @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cliopts_get.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/14 20:04:04 by jhalford #+# #+# */ +/* Updated: 2017/03/14 20:58:37 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* +** void *data must be a structure starting with `int flag` +** to do polymorphism with t_data_template ! +*/ + +# include "cliopts.h" + +static t_cliopts *get_map_long(t_cliopts opt_map[], char *arg) +{ + int i; + + i = -1; + while (opt_map[++i].c) + if (ft_strcmp(opt_map[i].str, arg) == 0) + return (&opt_map[i]); + return (NULL); +} + +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) +{ + t_cliopts *map; + char *arg; + int i; + + arg = **av + 1; + i = 0; + while (arg[i]) + { + if (!(map = get_map_short(opt_map, arg[i]))) + return (ERR_SET(E_OPTINVC, arg[i])); + if (map->get) + { + if (!(arg[i - 1] == '-' && arg[i + 1] == 0)) + return (ERR_SET(E_OPTARG, *arg)); + ++(*av); + if ((map->get)(av, data)) + return (1); + } + ((t_data_template*)data)->flag |= map->flag_on; + ((t_data_template*)data)->flag &= ~map->flag_off; + i++; + } + ++(*av); + return (0); + +} + +static int cliopts_parse_long(char ***av, t_cliopts opt_map[], void *data) +{ + t_cliopts *map; + char *arg; + + arg = **av + 2; + if (!(map = get_map_long(opt_map, arg))) + return (ERR_SET(E_OPTINVS, 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)) + return (1); + } + ++(*av); + return (0); +} + +int cliopts_get(char **av, t_cliopts opt_map[], void *data) +{ + ((t_data_template*)data)->flag = 0; + if (!av) + return (1); + av++; + while (*av) + { + if (ft_strcmp(*av, "--") == 0) + return (0); + else if ((*av)[0] == '-' && (*av)[1] == '-') + { + if (cliopts_parse_long(&av, opt_map, data)) + return (1); + } + else if ((*av)[0] == '-') + { + if (cliopts_parse_short(&av, opt_map, data)) + return (1); + } + else + return (0); + } + return (0); +} diff --git a/libftasm/src/cliopts/cliopts_getdata.c b/libftasm/src/cliopts/cliopts_getdata.c new file mode 100644 index 00000000..caca6575 --- /dev/null +++ b/libftasm/src/cliopts/cliopts_getdata.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cliopts_getdata.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/14 20:35:15 by jhalford #+# #+# */ +/* Updated: 2017/03/14 20:55:01 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cliopts.h" + +char **cliopts_getdata(char **av) +{ + 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); +} diff --git a/libftasm/src/cliopts/cliopts_has.c b/libftasm/src/cliopts/cliopts_has.c new file mode 100644 index 00000000..3d1fc5c5 --- /dev/null +++ b/libftasm/src/cliopts/cliopts_has.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 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); +} diff --git a/libftasm/src/error/error.c b/libftasm/src/error/error.c new file mode 100644 index 00000000..267274ca --- /dev/null +++ b/libftasm/src/error/error.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* error.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/14 16:47:00 by jhalford #+# #+# */ +/* Updated: 2017/03/14 20:26:46 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "error.h" + +char g_error_msglist[E_MAX][ERRMSG_MAX_SIZE] = +{ + "no error", + "invalid option --%s", + "invalid option -%c", + "option '%c' awaits argument(s): please don't combine", +}; + +int g_errnum = 0; +char *g_errmsg = NULL; +char **g_argv; + +int error_set(int n, ...) +{ + va_list ap; + + g_errnum = n; + va_start(ap, n); + ft_strdel(&g_errmsg); + ft_vasprintf(&g_errmsg, g_error_msglist[n], ap); + return (g_errnum); +} + +int ft_perror(void) +{ + ft_dprintf(2, "{red}%s: %s{eoc}\n", g_argv[0], g_errmsg); + return (g_errnum); +} diff --git a/libftasm/src/ft_printf/ft_printf.c b/libftasm/src/ft_printf/ft_printf.c index 4bfe428f..fb813acd 100644 --- a/libftasm/src/ft_printf/ft_printf.c +++ b/libftasm/src/ft_printf/ft_printf.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:33:27 by jhalford #+# #+# */ -/* Updated: 2017/03/11 18:17:48 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 20:24:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,7 +41,26 @@ int ft_dprintf(int fd, const char *format, ...) return (ft_vdprintf(fd, format, ap)); } +int ft_asprintf(char **ret, const char *format, ...) +{ + va_list ap; + va_start(ap, format); + return (ft_vasprintf(ret, format, ap)); +} + int ft_vdprintf(int fd, const char *format, va_list ap) +{ + char *ret; + + ret = NULL; + if (ft_vasprintf(&ret, format, ap)) + return (1); + ft_putstr_fd(ret, fd); + ft_strdel(&ret); + return (0); +} + +int ft_vasprintf(char **ret, const char *format, va_list ap) { char *str; char *tmp; @@ -63,8 +82,7 @@ int ft_vdprintf(int fd, const char *format, va_list ap) final = ft_strjoin(final, (char[]){*str++, 0}); ft_strdel(&tmp); } - ft_putstr_fd(final, fd); - ft_strdel(&final); + *ret = final; return (0); } diff --git a/libftasm/src/get_next_line/get_next_line.c b/libftasm/src/get_next_line/get_next_line.c index 373eddcf..4a65cb55 100644 --- a/libftasm/src/get_next_line/get_next_line.c +++ b/libftasm/src/get_next_line/get_next_line.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/15 13:12:06 by jhalford #+# #+# */ -/* Updated: 2017/02/21 14:20:05 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 17:47:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,7 @@ static void ft_gnlfree(void *a, size_t size) { (void)size; ft_strdel(&((t_save*)a)->str); + free(a); } static t_list *ft_newfd(t_list **head, int fd) diff --git a/libftasm/src/sstr/ft_sstrdel.c b/libftasm/src/sstr/ft_sstrdel.c index f539d73e..c0d618ac 100644 --- a/libftasm/src/sstr/ft_sstrdel.c +++ b/libftasm/src/sstr/ft_sstrdel.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/03 18:04:07 by jhalford #+# #+# */ -/* Updated: 2016/11/03 18:04:08 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 21:09:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/libftasm/src/str/ft_strcmp.c b/libftasm/src/str/ft_strcmp.c index 6349ac8c..de85d971 100644 --- a/libftasm/src/str/ft_strcmp.c +++ b/libftasm/src/str/ft_strcmp.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/08/07 10:49:02 by jhalford #+# #+# */ -/* Updated: 2016/11/03 16:08:51 by jhalford ### ########.fr */ +/* Updated: 2017/03/14 18:09:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */