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.

GreekStapler 2023-01-25 17:17:18 +01:00
parent d252c55fb0
commit 62d1081d38
2 changed files with 47 additions and 4 deletions

View File

@ -21,6 +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); char **package_to_description(Pkg *pkg);
#endif #endif

View File

@ -1,5 +1,20 @@
#include "package.h" #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] = { static char *ignored_names[5] = {
".BUILDINFO", ".BUILDINFO",
".INSTALL", ".INSTALL",
@ -7,7 +22,7 @@ static char *ignored_names[5] = {
".PKGINFO", ".PKGINFO",
".CHANGELOG" ".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() { Pkg *package_init() {
return calloc(sizeof(PkgInfo), 1); return calloc(sizeof(PkgInfo), 1);
@ -38,6 +53,7 @@ Pkg *package_read_archive(const char *pkg_path) {
PkgInfo *pkg_info; PkgInfo *pkg_info;
DynArray *files = dynarray_init(16); DynArray *files = dynarray_init(16);
dynarray_add(files, "%FILES%");
while (archive_read_next_header(a, &entry) == ARCHIVE_OK) { while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
path_name = archive_entry_pathname(entry); path_name = archive_entry_pathname(entry);
@ -81,7 +97,6 @@ Pkg *package_read_archive(const char *pkg_path) {
pkg_info->csize = stats.st_size; pkg_info->csize = stats.st_size;
archive_read_free(a); archive_read_free(a);
archive_entry_free(entry);
// Create final return value // Create final return value
Pkg *pkg = package_init(); Pkg *pkg = package_init();
@ -93,6 +108,34 @@ Pkg *package_read_archive(const char *pkg_path) {
return pkg; 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;
} }