lib

morphux C utility library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/lib

commit 745b5a618b1e35630e34894e5d6a67eaec2df131
parent 8605096e15de6f1ed76ea062c3269a5d98556d83
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Thu,  8 Jun 2017 18:08:11 +0200

Add(Lists): New structure: vector_string_t:

Can be used for simple name -> value with strings

Diffstat:
Minc/m_list.h | 17+++++++++++++++++
Msrc/m_list.c | 41+++++++++++++++++++++++++++++++++++++++++
2 files changed, 58 insertions(+), 0 deletions(-)

diff --git a/inc/m_list.h b/inc/m_list.h @@ -32,6 +32,11 @@ typedef struct list_s { struct list_s *head; /*!< Head of the list */ } mlist_t; +typedef struct vector_string_s { + char *str1; + char *str2; +} vector_string_t; + /* Defines */ # define list_add(ptr, member, sizeZ) ptr = list_add_member(ptr, member, sizeZ); @@ -152,4 +157,16 @@ void *list_get(mlist_t *list, void *member, size_t size); mlist_t *list_remove(mlist_t *list, void *member, size_t size, int (*free_fn)(void *member)); +/*! + * \brief Init a vector_string_t with two default values + */ +vector_string_t *vector_string_init(const char *str1, const char *str2); + +/*! + * \brief Free a vector_string_t structure + * + * \note Can be used as a list_free callback + */ +int vector_string_free(void *magic); + #endif diff --git a/src/m_list.c b/src/m_list.c @@ -239,3 +239,44 @@ mlist_t *list_remove(mlist_t *list, void *member, size_t size, free(tmp2); return list; } + +vector_string_t *vector_string_init(const char *str1, const char *str2) { + vector_string_t *ret; + + ret = malloc(sizeof(*ret)); + if (ret == NULL) + return NULL; + + ret->str1 = NULL; + ret->str2 = NULL; + + if (str1 != NULL) + ret->str1 = strdup(str1); + if (str2 != NULL) + ret->str2 = strdup(str2); + + if (str1 == NULL || str2 == NULL) + goto cleanup; + + return ret; + +cleanup: + if (ret->str1 != NULL) + free(ret->str1); + if (ret->str2 != NULL) + free(ret->str2); + + free(ret); + return NULL; +} + +int vector_string_free(void *magic) { + vector_string_t *ptr = magic; + + if (ptr != NULL) + { + free(ptr->str1); + free(ptr->str2); + } + return 1; +}