refactor: Decided to not return char** in function that creates the package description after all, now returns char*.

The method I was trying started to irk me when I thought of creating a test unit for it. Also fixed some other issues I found in the package_to_description function (SHA256SUM section still missing).
GreekStapler 2023-01-25 22:15:07 +01:00
parent 2d6ac2ac19
commit 2ce7a819a9
2 changed files with 49 additions and 27 deletions

View File

@ -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

View File

@ -1,18 +1,30 @@
#include "package.h"
#define BUFF_SIZE 128
#define SMALL_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); \
snprintf(aux, SMALL_BUFF_SIZE, section, pkg_info->field); \
if (buff_size < strlen(description) + SMALL_BUFF_SIZE + 1) { \
description = realloc(description, buff_size * 2);\
buff_size *= 2; \
} \
strcat(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); \
} \
snprintf(aux, SMALL_BUFF_SIZE, section, pkg_info->field->array[i]); i++; \
if (buff_size < strlen(description) + SMALL_BUFF_SIZE + 1) { \
description = realloc(description, buff_size * 2);\
buff_size *= 2; \
} \
strcat(description, aux); \
while (pkg_info->field->array[i] != NULL) { \
snprintf(aux, SMALL_BUFF_SIZE, "\n%s", pkg_info->field->array[i]); i++; \
if (buff_size < strlen(description) + SMALL_BUFF_SIZE + 1) { \
description = realloc(description, buff_size * 2);\
buff_size *= 2; \
} \
strcat(description, aux); \
} \
}
static char *ignored_names[5] = {
@ -109,33 +121,43 @@ Pkg *package_read_archive(const char *pkg_path) {
}
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];
size_t buff_size = 1024;
char aux[SMALL_BUFF_SIZE];
char *description = malloc(sizeof(char) * 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);
// special case for FILENAME
// assuming .pkg.tar.zst; other formats are valid, this should account for that
snprintf(aux, SMALL_BUFF_SIZE, "%%FILENAME%%\n%s-%s-%s.pkg.tar.zst", pkg_info->name, pkg_info->version,
pkg_info->arch);
strcat(description, aux);
ADD_STRING("\n\n%%NAME%%\n%s", name);
ADD_STRING("\n\n%%BASE%%\n%s", base);
ADD_STRING("\n\n%%VERSION%%\n%s", version);
ADD_STRING("\n\n%%DESC%%\n%s", description);
ADD_ARRAY("\n\n%%GROUPS%%\n%s", groups);
ADD_ARRAY("\n\n%%LICENSES%%\n%s", licenses);
ADD_STRING("\n\n%%CSIZE%%\n%ld", csize);
ADD_STRING("\n\n%%ISIZE%%\n%ld", size);
//SHA256SUM
ADD_STRING("\n\n%%URL%%\n%s", url);
ADD_ARRAY("\n\n%%LICENSE%%\n%s", licenses);
ADD_STRING("\n\n%%ARCH%%\n%s", arch);
ADD_STRING("\n\n%%BUILDDATE%%\n%ld", build_date);
ADD_STRING("\n\n%%PACKAGER%%\n%s", packager);
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%%DEPENDS%%\n%s", depends);
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;
snprintf(aux, SMALL_BUFF_SIZE, "\n\n");
strcat(description, aux);
return description;
}