From cf39c418a49808eb63cf4e1b5e2e363455f2da84 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 11 Sep 2020 17:58:05 +0200 Subject: alias utils prepared --- src/m_loop.c | 1 + src/s_lalias.c | 7 ++----- src/u_alias.c | 36 ++++++++++++++++++++++++++++-------- src/u_alias.h | 2 +- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/m_loop.c b/src/m_loop.c index 50357eb..bd4fb1c 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -26,6 +26,7 @@ #include "s_lpipes.h" #include "s_line.h" #include "u_vars.h" +#include "u_alias.h" static void m_parse_and_run_line(char line[], t_msh *msh) diff --git a/src/s_lalias.c b/src/s_lalias.c index b4b9430..eeac08b 100644 --- a/src/s_lalias.c +++ b/src/s_lalias.c @@ -19,15 +19,12 @@ #include "s_struct.h" void - s_lalias_rebind(t_lalias **lalias, - const char newname[], - const char newval[]) + s_lalias_rebind(t_lalias **lalias, const char name[], const char newval[]) { t_lalias *tmp; tmp = *lalias; - while (tmp != NULL && - ft_strncmp(tmp->name, newname, ft_strlen(newname) + 1) != 0) + while (tmp != NULL && ft_strncmp(tmp->name, name, ft_strlen(name) + 1) != 0) { tmp = tmp->next; } diff --git a/src/u_alias.c b/src/u_alias.c index 6253d60..f273a5c 100644 --- a/src/u_alias.c +++ b/src/u_alias.c @@ -11,33 +11,53 @@ /* ************************************************************************** */ #include +#include #include #include "d_define.h" +#include "s_lalias.h" #include "s_struct.h" -void +uint8_t u_get_alias_value(char str[], const char name[], size_t dstsize, t_msh *msh) { t_lalias *ptr; - str[0] = C_NUL; + if (str != NULL) + { + str[0] = C_NUL; + } ptr = msh->alias; - while (ptr != NULL && ft_strncmp(tmp->name, name, ft_strlen(name)) != 0) + while (ptr != NULL && ft_strncmp(ptr->name, name, ft_strlen(name) + 1) != 0) { ptr = ptr->next; } - if (ptr != NULL) + if (ptr == NULL) + { + return (1); + } + if (str != NULL) { - ft_strlcpy(str, tmp->value, dstsize); + ft_strlcpy(str, ptr->val, dstsize); } + return (0); } void u_set_alias_value(const char name[], const char value[], t_msh *msh) { - t_lalias *tmp; + t_lalias *new; - tmp = msh->alias; - while (tmp) + if (u_get_alias_value(NULL, name, 0, msh) != 0) + { + s_lalias_rebind(&msh->alias, name, value); + } + else + { + if ((new = s_lalias_new(name, value)) == NULL) + { + return ; + } + s_lalias_add_front(&msh->alias, new); + } } diff --git a/src/u_alias.h b/src/u_alias.h index c660484..59860db 100644 --- a/src/u_alias.h +++ b/src/u_alias.h @@ -17,7 +17,7 @@ #include "s_struct.h" -void u_get_alias_value(char str[], +uint8_t u_get_alias_value(char str[], const char name[], size_t dstsize, t_msh *msh); -- cgit v1.2.3