cliopts now part of libft
This commit is contained in:
parent
470e6e3432
commit
599ca54b5e
16 changed files with 358 additions and 22 deletions
|
|
@ -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\
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
41
libft/includes/cliopts.h
Normal file
41
libft/includes/cliopts.h
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cliopts.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
44
libft/includes/error.h
Normal file
44
libft/includes/error.h
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* error.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <stdarg.h>
|
||||
|
||||
# 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
|
||||
|
|
@ -6,12 +6,13 @@
|
|||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <stdarg.h>
|
||||
|
||||
|
|
|
|||
|
|
@ -6,17 +6,13 @@
|
|||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <string.h>
|
||||
# include <unistd.h>
|
||||
# include <stdio.h>
|
||||
|
|
@ -25,16 +21,20 @@
|
|||
# include <sys/xattr.h>
|
||||
# include <sys/acl.h>
|
||||
|
||||
# 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);
|
||||
|
|
|
|||
|
|
@ -1,17 +1,17 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_sys.h :+: :+: :+: */
|
||||
/* sys.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
115
libft/src/cliopts/cliopts_get.c
Normal file
115
libft/src/cliopts/cliopts_get.c
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cliopts_get.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
32
libft/src/cliopts/cliopts_getdata.c
Normal file
32
libft/src/cliopts/cliopts_getdata.c
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cliopts_getdata.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
35
libft/src/cliopts/cliopts_has.c
Normal file
35
libft/src/cliopts/cliopts_has.c
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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);
|
||||
}
|
||||
42
libft/src/error/error.c
Normal file
42
libft/src/error/error.c
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* error.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue