feat(package.c): some cleanup

Jef Roosens 2022-11-20 19:58:25 +01:00
parent 3c0422b998
commit 640e2914bf
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
7 changed files with 50 additions and 39 deletions

View File

@ -1,6 +1,6 @@
# =====CONFIG===== # =====CONFIG=====
SRC_DIR := src 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 V_PATH ?= v
# We need to use GCC because TCC doesn't like the way we use C bindings # We need to use GCC because TCC doesn't like the way we use C bindings

View File

@ -29,15 +29,27 @@ void dynarray_add(DynArray *da, const char *s) {
da->size++; da->size++;
} }
void dynarray_free(DynArray **ptp) { void dynarray_free(DynArray *da) {
DynArray *da = *ptp; if (da == NULL) {
return;
for (size_t i = 0; i < da->size; i++) {
free(da->array[i]);
} }
free(da->array); if (da->array != NULL) {
free(da); 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;
} }

View File

@ -8,7 +8,12 @@ typedef struct dyn_array DynArray;
DynArray *dynarray_init(size_t initial_capacity); DynArray *dynarray_init(size_t initial_capacity);
void dynarray_add(DynArray *da, const char * s); void dynarray_add(DynArray *da, const char * s);
char ** dynarray_get_array(DynArray *da); void dynarray_free(DynArray *da);
void dynarray_free(DynArray **ptp);
/**
* Convert a DynArray into an array by freeing all its surrounding components
* and returning the underlying array pointer.
*/
char **dynarray_convert(DynArray *da);
#endif #endif

View File

@ -92,3 +92,7 @@ Pkg *package_read_archive(const char *pkg_path) {
return pkg; return pkg;
} }
char *package_to_description(Pkg *pkg) {
}

View File

@ -21,5 +21,6 @@ typedef struct pkg {
Pkg *package_read_archive(const char *pkg_path); Pkg *package_read_archive(const char *pkg_path);
void package_free(Pkg ** ptp); void package_free(Pkg ** ptp);
char *package_to_description(Pkg *pkg);
#endif #endif

View File

@ -3,24 +3,10 @@
#include "package_info.h" #include "package_info.h"
PkgInfo *package_info_init() { PkgInfo *package_info_init() {
PkgInfo *pkg_info = calloc(1, sizeof(PkgInfo)); return 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;
} }
void package_info_free(PkgInfo **ptp) { void package_info_free(PkgInfo *pkg_info) {
PkgInfo *pkg_info = *ptp;
FREE_STRING(pkg_info->name); FREE_STRING(pkg_info->name);
FREE_STRING(pkg_info->base); FREE_STRING(pkg_info->base);
FREE_STRING(pkg_info->version); FREE_STRING(pkg_info->version);
@ -30,17 +16,17 @@ void package_info_free(PkgInfo **ptp) {
FREE_STRING(pkg_info->packager); FREE_STRING(pkg_info->packager);
FREE_STRING(pkg_info->pgpsig); FREE_STRING(pkg_info->pgpsig);
dynarray_free(&pkg_info->groups); dynarray_free(pkg_info->groups);
dynarray_free(&pkg_info->licenses); dynarray_free(pkg_info->licenses);
dynarray_free(&pkg_info->replaces); dynarray_free(pkg_info->replaces);
dynarray_free(&pkg_info->depends); dynarray_free(pkg_info->depends);
dynarray_free(&pkg_info->conflicts); dynarray_free(pkg_info->conflicts);
dynarray_free(&pkg_info->provides); dynarray_free(pkg_info->provides);
dynarray_free(&pkg_info->optdepends); dynarray_free(pkg_info->optdepends);
dynarray_free(&pkg_info->makedepends); dynarray_free(pkg_info->makedepends);
dynarray_free(&pkg_info->checkdepends); 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_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_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) { int package_info_parse(PkgInfo *pkg_info, char *pkg_info_str) {
char *offset_ptr, *equals_ptr, *key_ptr, *value_ptr; char *offset_ptr, *equals_ptr, *key_ptr, *value_ptr;

View File

@ -34,6 +34,6 @@ typedef struct pkg_info {
PkgInfo *package_info_init(); PkgInfo *package_info_init();
int package_info_parse(PkgInfo *pkg_info, char *pkg_info_str); 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 #endif