From 640e2914bf42b58faabd6b837f69c1378bd38dd4 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sun, 20 Nov 2022 19:58:25 +0100 Subject: [PATCH] feat(package.c): some cleanup --- Makefile | 2 +- src/package/c/dynarray.c | 28 ++++++++++++++++------- src/package/c/dynarray.h | 9 ++++++-- src/package/c/package.c | 4 ++++ src/package/c/package.h | 1 + src/package/c/package_info.c | 43 ++++++++++++++---------------------- src/package/c/package_info.h | 2 +- 7 files changed, 50 insertions(+), 39 deletions(-) diff --git a/Makefile b/Makefile index 5927d41..fbcabfe 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ # =====CONFIG===== SRC_DIR := src -SOURCES != find '$(SRC_DIR)' -iname '*.v' +SOURCES != find '$(SRC_DIR)' -type f \( -iname '*.v' -or -iname '*.c' -or -iname '*.h' \) V_PATH ?= v # We need to use GCC because TCC doesn't like the way we use C bindings diff --git a/src/package/c/dynarray.c b/src/package/c/dynarray.c index a70feaf..480c08f 100644 --- a/src/package/c/dynarray.c +++ b/src/package/c/dynarray.c @@ -29,15 +29,27 @@ void dynarray_add(DynArray *da, const char *s) { da->size++; } -void dynarray_free(DynArray **ptp) { - DynArray *da = *ptp; - - for (size_t i = 0; i < da->size; i++) { - free(da->array[i]); +void dynarray_free(DynArray *da) { + if (da == NULL) { + return; } - free(da->array); - free(da); + if (da->array != NULL) { + for (size_t i = 0; i < da->size; i++) { + free(da->array[i]); + } - *ptp = NULL; + free(da->array); + } + + free(da); +} + +char **dynarray_convert(DynArray *da) { + char **array = da->array; + + da->array = NULL; + dynarray_free(da); + + return array; } diff --git a/src/package/c/dynarray.h b/src/package/c/dynarray.h index 73f5a0a..e552ec4 100644 --- a/src/package/c/dynarray.h +++ b/src/package/c/dynarray.h @@ -8,7 +8,12 @@ typedef struct dyn_array DynArray; DynArray *dynarray_init(size_t initial_capacity); void dynarray_add(DynArray *da, const char * s); -char ** dynarray_get_array(DynArray *da); -void dynarray_free(DynArray **ptp); +void dynarray_free(DynArray *da); + +/** + * Convert a DynArray into an array by freeing all its surrounding components + * and returning the underlying array pointer. + */ +char **dynarray_convert(DynArray *da); #endif diff --git a/src/package/c/package.c b/src/package/c/package.c index d2e3c94..a098abb 100644 --- a/src/package/c/package.c +++ b/src/package/c/package.c @@ -92,3 +92,7 @@ Pkg *package_read_archive(const char *pkg_path) { return pkg; } + +char *package_to_description(Pkg *pkg) { + +} diff --git a/src/package/c/package.h b/src/package/c/package.h index 76ec5ac..09e91bd 100644 --- a/src/package/c/package.h +++ b/src/package/c/package.h @@ -21,5 +21,6 @@ typedef struct pkg { Pkg *package_read_archive(const char *pkg_path); void package_free(Pkg ** ptp); +char *package_to_description(Pkg *pkg); #endif diff --git a/src/package/c/package_info.c b/src/package/c/package_info.c index 79d8720..1202485 100644 --- a/src/package/c/package_info.c +++ b/src/package/c/package_info.c @@ -3,24 +3,10 @@ #include "package_info.h" PkgInfo *package_info_init() { - PkgInfo *pkg_info = calloc(1, sizeof(PkgInfo)); - - pkg_info->groups = dynarray_init(4); - pkg_info->licenses = dynarray_init(4); - pkg_info->replaces = dynarray_init(4); - pkg_info->depends = dynarray_init(4); - pkg_info->conflicts = dynarray_init(4); - pkg_info->provides = dynarray_init(4); - pkg_info->optdepends = dynarray_init(4); - pkg_info->makedepends = dynarray_init(4); - pkg_info->checkdepends = dynarray_init(4); - - return pkg_info; + return calloc(1, sizeof(PkgInfo)); } -void package_info_free(PkgInfo **ptp) { - PkgInfo *pkg_info = *ptp; - +void package_info_free(PkgInfo *pkg_info) { FREE_STRING(pkg_info->name); FREE_STRING(pkg_info->base); FREE_STRING(pkg_info->version); @@ -30,17 +16,17 @@ void package_info_free(PkgInfo **ptp) { FREE_STRING(pkg_info->packager); FREE_STRING(pkg_info->pgpsig); - dynarray_free(&pkg_info->groups); - dynarray_free(&pkg_info->licenses); - dynarray_free(&pkg_info->replaces); - dynarray_free(&pkg_info->depends); - dynarray_free(&pkg_info->conflicts); - dynarray_free(&pkg_info->provides); - dynarray_free(&pkg_info->optdepends); - dynarray_free(&pkg_info->makedepends); - dynarray_free(&pkg_info->checkdepends); + dynarray_free(pkg_info->groups); + dynarray_free(pkg_info->licenses); + dynarray_free(pkg_info->replaces); + dynarray_free(pkg_info->depends); + dynarray_free(pkg_info->conflicts); + dynarray_free(pkg_info->provides); + dynarray_free(pkg_info->optdepends); + dynarray_free(pkg_info->makedepends); + dynarray_free(pkg_info->checkdepends); - *ptp = NULL; + free(pkg_info); } /** @@ -72,7 +58,10 @@ static inline void trim_spaces_back(char *ptr) { #define PKG_INFO_STRING(key, field) if (strcmp(key_ptr, key) == 0) { pkg_info->field = strdup(value_ptr); goto advance; } #define PKG_INFO_INT(key, field) if (strcmp(key_ptr, key) == 0) { pkg_info->field = atoi(value_ptr); goto advance; } -#define PKG_INFO_ARRAY(key, field) if (strcmp(key_ptr, key) == 0) { dynarray_add(pkg_info->field, value_ptr); goto advance; } +#define PKG_INFO_ARRAY(key, field) if (strcmp(key_ptr, key) == 0) { \ + if (pkg_info->field == NULL) { pkg_info->field = dynarray_init(4); } \ + dynarray_add(pkg_info->field, value_ptr); goto advance; \ +} int package_info_parse(PkgInfo *pkg_info, char *pkg_info_str) { char *offset_ptr, *equals_ptr, *key_ptr, *value_ptr; diff --git a/src/package/c/package_info.h b/src/package/c/package_info.h index c3df790..d71386e 100644 --- a/src/package/c/package_info.h +++ b/src/package/c/package_info.h @@ -34,6 +34,6 @@ typedef struct pkg_info { PkgInfo *package_info_init(); int package_info_parse(PkgInfo *pkg_info, char *pkg_info_str); -void package_info_free(PkgInfo **ptp); +void package_info_free(PkgInfo *pkg_info); #endif