refactor: move dynarray into own module

Jef Roosens 2023-02-23 09:54:50 +01:00
parent e5f0ac8dec
commit caee502382
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
7 changed files with 69 additions and 71 deletions

View File

@ -0,0 +1,34 @@
#ifndef VIETER_DYNARRAY
#define VIETER_DYNARRAY
#include <stdlib.h>
#include <string.h>
typedef struct vieter_dynarray {
char **array;
size_t capacity;
size_t size;
} vieter_dynarray;
/*
* Allocate a dynamic array.
*/
vieter_dynarray *vieter_dynarray_init(size_t initial_capacity);
/*
* Initialise array (if it's not already initialised) and insert a string.
*/
void vieter_dynarray_add(vieter_dynarray *da, const char *s);
/*
* Deallocate dynamic array.
*/
void vieter_dynarray_free(vieter_dynarray *da);
/*
* Convert a vieter_dynarray into an array by freeing all its surrounding
* components and returning the underlying array pointer.
*/
char **vieter_dynarray_convert(vieter_dynarray *da);
#endif

View File

@ -1,15 +1,15 @@
#include "vieter_package_dynarray.h"
#include "vieter_dynarray.h"
vieter_package_dynarray *vieter_package_dynarray_init(size_t initial_capacity) {
vieter_package_dynarray *da = malloc(sizeof(vieter_package_dynarray));
vieter_dynarray *vieter_dynarray_init(size_t initial_capacity) {
vieter_dynarray *da = malloc(sizeof(vieter_dynarray));
da->size = 0;
da->capacity = initial_capacity;
return da;
}
void vieter_package_dynarray_add(vieter_package_dynarray *da, const char *s) {
// An empty vieter_package_dynarray does not have an allocated internal array
void vieter_dynarray_add(vieter_dynarray *da, const char *s) {
// An empty vieter_dynarray does not have an allocated internal array
// yet
if (da->size == 0) {
da->array = malloc(sizeof(char *) * da->capacity);
@ -32,7 +32,7 @@ void vieter_package_dynarray_add(vieter_package_dynarray *da, const char *s) {
da->size++;
}
void vieter_package_dynarray_free(vieter_package_dynarray *da) {
void vieter_dynarray_free(vieter_dynarray *da) {
if (da == NULL) {
return;
}
@ -48,11 +48,11 @@ void vieter_package_dynarray_free(vieter_package_dynarray *da) {
free(da);
}
char **vieter_package_dynarray_convert(vieter_package_dynarray *da) {
char **vieter_dynarray_convert(vieter_dynarray *da) {
char **array = da->array;
da->array = NULL;
vieter_package_dynarray_free(da);
vieter_dynarray_free(da);
return array;
}

View File

@ -66,8 +66,8 @@ vieter_package_error vieter_package_read_archive(vieter_package *pkg,
const char *path_name;
vieter_package_info *pkg_info = NULL;
vieter_package_dynarray *files = vieter_package_dynarray_init(16);
vieter_package_dynarray_add(files, "%FILES%");
vieter_dynarray *files = vieter_dynarray_init(16);
vieter_dynarray_add(files, "%FILES%");
while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
path_name = archive_entry_pathname(entry);
@ -82,7 +82,7 @@ vieter_package_error vieter_package_read_archive(vieter_package *pkg,
}
if (!ignore) {
vieter_package_dynarray_add(files, path_name);
vieter_dynarray_add(files, path_name);
}
if (strcmp(path_name, ".PKGINFO") == 0) {
@ -257,7 +257,7 @@ char *vieter_package_to_description(vieter_package *pkg) {
void vieter_package_free(vieter_package **ptp) {
FREE_STRING((*ptp)->path);
vieter_package_info_free((*ptp)->info);
vieter_package_dynarray_free((*ptp)->files);
vieter_dynarray_free((*ptp)->files);
free(*ptp);
*ptp = NULL;
}

View File

@ -1,36 +0,0 @@
#ifndef VIETER_DYNARRAY
#define VIETER_DYNARRAY
#include <stdlib.h>
#include <string.h>
#include "vieter_package.h"
typedef struct vieter_package_dynarray vieter_package_dynarray;
struct vieter_package_dynarray {
char **array;
size_t capacity;
size_t size;
};
/*
* Allocate a dynamic array.
*/
vieter_package_dynarray *vieter_package_dynarray_init(size_t initial_capacity);
/*
* Initialise array (if it's not already initialised) and insert a string.
*/
void vieter_package_dynarray_add(vieter_package_dynarray *da, const char * s);
/*
* Deallocate dynamic array.
*/
void vieter_package_dynarray_free(vieter_package_dynarray *da);
/*
* Convert a vieter_package_dynarray into an array by freeing all its surrounding components
* and returning the underlying array pointer.
*/
char **vieter_package_dynarray_convert(vieter_package_dynarray *da);
#endif

View File

@ -26,9 +26,9 @@
tail_ptr = strchr(value_ptr, '\n'); \
tail_ptr[0] = '\0'; \
if (pkg_info->field == NULL) { \
pkg_info->field = vieter_package_dynarray_init(4); \
pkg_info->field = vieter_dynarray_init(4); \
} \
vieter_package_dynarray_add(pkg_info->field, value_ptr); \
vieter_dynarray_add(pkg_info->field, value_ptr); \
tail_ptr[0] = '\n'; \
value_ptr = tail_ptr; \
} \
@ -48,15 +48,15 @@ void vieter_package_info_free(vieter_package_info *pkg_info) {
FREE_STRING(pkg_info->packager);
FREE_STRING(pkg_info->pgpsig);
vieter_package_dynarray_free(pkg_info->groups);
vieter_package_dynarray_free(pkg_info->licenses);
vieter_package_dynarray_free(pkg_info->replaces);
vieter_package_dynarray_free(pkg_info->depends);
vieter_package_dynarray_free(pkg_info->conflicts);
vieter_package_dynarray_free(pkg_info->provides);
vieter_package_dynarray_free(pkg_info->optdepends);
vieter_package_dynarray_free(pkg_info->makedepends);
vieter_package_dynarray_free(pkg_info->checkdepends);
vieter_dynarray_free(pkg_info->groups);
vieter_dynarray_free(pkg_info->licenses);
vieter_dynarray_free(pkg_info->replaces);
vieter_dynarray_free(pkg_info->depends);
vieter_dynarray_free(pkg_info->conflicts);
vieter_dynarray_free(pkg_info->provides);
vieter_dynarray_free(pkg_info->optdepends);
vieter_dynarray_free(pkg_info->makedepends);
vieter_dynarray_free(pkg_info->checkdepends);
free(pkg_info);
}

View File

@ -6,7 +6,7 @@
#include <stdint.h>
#include "vieter_package.h"
#include "vieter_package_dynarray.h"
#include "vieter_dynarray.h"
typedef struct vieter_package_info {
@ -23,15 +23,15 @@ typedef struct vieter_package_info {
char *pgpsig;
int64_t pgpsigsize;
vieter_package_dynarray *groups;
vieter_package_dynarray *licenses;
vieter_package_dynarray *replaces;
vieter_package_dynarray *depends;
vieter_package_dynarray *conflicts;
vieter_package_dynarray *provides;
vieter_package_dynarray *optdepends;
vieter_package_dynarray *makedepends;
vieter_package_dynarray *checkdepends;
vieter_dynarray *groups;
vieter_dynarray *licenses;
vieter_dynarray *replaces;
vieter_dynarray *depends;
vieter_dynarray *conflicts;
vieter_dynarray *provides;
vieter_dynarray *optdepends;
vieter_dynarray *makedepends;
vieter_dynarray *checkdepends;
} vieter_package_info;
/*

View File

@ -1,10 +1,10 @@
#include "vieter_package.h"
#include "vieter_package_info.h"
#include "vieter_package_dynarray.h"
#include "vieter_dynarray.h"
struct vieter_package {
char *path;
vieter_package_info *info;
vieter_package_dynarray *files;
vieter_dynarray *files;
int compression;
};