From 62d1081d380bef51521fe6acde1235b12247bbe7 Mon Sep 17 00:00:00 2001 From: GreekStapler Date: Wed, 25 Jan 2023 17:17:18 +0100 Subject: [PATCH] refactor: Created function that will return the char ** (originally char *) that will be used to make the description file. Also removed free that was resulting in a crash. --- include/package.h | 2 +- src/package/package.c | 49 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/include/package.h b/include/package.h index 09e91bd..dfb3a34 100644 --- a/include/package.h +++ b/include/package.h @@ -21,6 +21,6 @@ typedef struct pkg { Pkg *package_read_archive(const char *pkg_path); void package_free(Pkg ** ptp); -char *package_to_description(Pkg *pkg); +char **package_to_description(Pkg *pkg); #endif diff --git a/src/package/package.c b/src/package/package.c index a098abb..3b9c6f9 100644 --- a/src/package/package.c +++ b/src/package/package.c @@ -1,5 +1,20 @@ #include "package.h" +#define BUFF_SIZE 128 + +#define ADD_STRING(section, field) if (pkg_info->field != 0) { \ + snprintf(aux, BUFF_SIZE, section, pkg_info->field); \ + dynarray_add(description, aux); \ +} +#define ADD_ARRAY(section, field) i = 0; if (pkg_info->field != NULL) { \ + snprintf(aux, BUFF_SIZE, section, pkg_info->field->array[i]); i++; \ + dynarray_add(description, aux); \ + while (pkg_info->field->array[i] != NULL) { \ + snprintf(aux, BUFF_SIZE, "\n%s", pkg_info->field->array[i]); i++; \ + dynarray_add(description, aux); \ + } \ +} + static char *ignored_names[5] = { ".BUILDINFO", ".INSTALL", @@ -7,7 +22,7 @@ static char *ignored_names[5] = { ".PKGINFO", ".CHANGELOG" }; -static int ignored_words_len = sizeof(ignored_names) / sizeof(char *); +static size_t ignored_words_len = sizeof(ignored_names) / sizeof(char *); Pkg *package_init() { return calloc(sizeof(PkgInfo), 1); @@ -38,6 +53,7 @@ Pkg *package_read_archive(const char *pkg_path) { PkgInfo *pkg_info; DynArray *files = dynarray_init(16); + dynarray_add(files, "%FILES%"); while (archive_read_next_header(a, &entry) == ARCHIVE_OK) { path_name = archive_entry_pathname(entry); @@ -81,7 +97,6 @@ Pkg *package_read_archive(const char *pkg_path) { pkg_info->csize = stats.st_size; archive_read_free(a); - archive_entry_free(entry); // Create final return value Pkg *pkg = package_init(); @@ -93,6 +108,34 @@ Pkg *package_read_archive(const char *pkg_path) { return pkg; } -char *package_to_description(Pkg *pkg) { +char **package_to_description(Pkg *pkg) { + PkgInfo *pkg_info = pkg->info; + + DynArray *description = dynarray_init(16); + + char aux[BUFF_SIZE]; + int i; + ADD_STRING("\n\n%%NAME%%\n%s", name); + ADD_STRING("-%s", version); + ADD_STRING("\n\n%%PKGBASE%%\n%s", base); + ADD_STRING("\n\n%%DESCRIPTION%%\n%s", description); + ADD_STRING("\n\n%%SIZE%%\n%ld", size); + ADD_STRING("\n\n%%CSIZE%%\n%ld", csize); + ADD_STRING("\n\n%%URL%%\n%s", url); + ADD_STRING("\n\n%%ARCH%%\n%s", arch); + ADD_STRING("\n\n%%BUILD_DATE%%\n%ld", build_date); + ADD_STRING("\n\n%%PACKAGER%%\n%s", packager); + + ADD_ARRAY("\n\n%%GROUPS%%\n%s", groups); + ADD_ARRAY("\n\n%%LICENSES%%\n%s", licenses); + ADD_ARRAY("\n\n%%REPLACES%%\n%s", replaces); + ADD_ARRAY("\n\n%%DEPENDS%%\n%s", depends); + ADD_ARRAY("\n\n%%CONFLICTS%%\n%s", conflicts); + ADD_ARRAY("\n\n%%PROVIDES%%\n%s", provides); + ADD_ARRAY("\n\n%%OPTDEPENDS%%\n%s", optdepends); + ADD_ARRAY("\n\n%%MAKEDEPENDS%%\n%s", makedepends); + ADD_ARRAY("\n\n%%CHECKDEPENDS%%\n%s", checkdepends); + + return description; }