refactor: started a codestyle

main
Jef Roosens 2022-12-03 23:27:44 +01:00
parent 65d67b2c01
commit 3b4a56bd89
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
11 changed files with 128 additions and 120 deletions

View File

@ -1,17 +1,25 @@
cmake_minimum_required(VERSION 3.20)
project(cieter C)
project(cieter C)
set(CMAKE_C_STANDARD 17)
find_package(LibArchive REQUIRED)
include_directories(include)
file(GLOB SRCS src/*.c)
file(GLOB CIETER_SRCS src/cieter/**.c)
if(CMAKE_BUILD_TYPE STREQUAL Release)
add_compile_options(-O3 -flto)
endif()
add_executable(cieter ${SRCS})
target_link_libraries(cieter LibArchive::LibArchive)
add_library(cieter SHARED ${CIETER_SRCS})
option(BUILD_CIETERD "Build cieterd binary" ON)
if (BUILD_CIETERD)
file(GLOB CIETERD_SRCS src/cieterd/**.c)
add_executable(cieterd ${CIETERD_SRCS})
target_link_libraries(cieterd cieter LibArchive::LibArchive)
endif()

View File

@ -0,0 +1,19 @@
#ifndef CIETER_DYNARRAY
#define CIETER_DYNARRAY
#include <stdlib.h>
#include <string.h>
typedef struct dyn_array CieterDynArray;
CieterDynArray *cieter_dynarray_init(size_t initial_capacity);
void cieter_dynarray_add(CieterDynArray *da, const char * s);
void cieter_dynarray_free(CieterDynArray *da);
/**
* Convert a DynArray into an array by freeing all its surrounding components
* and returning the underlying array pointer.
*/
char **cieter_dynarray_convert(CieterDynArray *da);
#endif

View File

@ -0,0 +1,26 @@
#ifndef CIETER_PACKAGE
#define CIETER_PACKAGE
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include "archive.h"
#include "archive_entry.h"
#include "cieter_package_info.h"
#include "cieter_dynarray.h"
typedef struct pkg {
char *path;
CieterPkgInfo *info;
CieterDynArray *files;
int compression;
} CieterPkg;
CieterPkg *package_read_archive(const char *pkg_path);
void package_free(CieterPkg ** ptp);
char *package_to_description(CieterPkg *pkg);
#endif

View File

@ -0,0 +1,39 @@
#ifndef CIETER_PACKAGE_INFO
#define CIETER_PACKAGE_INFO
#define FREE_STRING(sp) if (sp != NULL) free(sp)
#include <stdint.h>
#include "cieter_dynarray.h"
typedef struct pkg_info {
char *name;
char *base;
char *version;
char *description;
int64_t size;
int64_t csize;
char *url;
char *arch;
int64_t build_date;
char *packager;
char *pgpsig;
int64_t pgpsigsize;
CieterDynArray *groups;
CieterDynArray *licenses;
CieterDynArray *replaces;
CieterDynArray *depends;
CieterDynArray *conflicts;
CieterDynArray *provides;
CieterDynArray *optdepends;
CieterDynArray *makedepends;
CieterDynArray *checkdepends;
} CieterPkgInfo;
CieterPkgInfo *package_info_init();
int package_info_parse(CieterPkgInfo *pkg_info, char *pkg_info_str);
void package_info_free(CieterPkgInfo *pkg_info);
#endif

View File

@ -1,19 +0,0 @@
#ifndef CIETER_DYNARRAY
#define CIETER_DYNARRAY
#include <stdlib.h>
#include <string.h>
typedef struct dyn_array DynArray;
DynArray *dynarray_init(size_t initial_capacity);
void dynarray_add(DynArray *da, const char * s);
void dynarray_free(DynArray *da);
/**
* Convert a DynArray into an array by freeing all its surrounding components
* and returning the underlying array pointer.
*/
char **dynarray_convert(DynArray *da);
#endif

View File

@ -1,26 +0,0 @@
#ifndef CIETER_PACKAGE
#define CIETER_PACKAGE
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include "archive.h"
#include "archive_entry.h"
#include "package_info.h"
#include "dynarray.h"
typedef struct pkg {
char *path;
PkgInfo *info;
DynArray *files;
int compression;
} Pkg;
Pkg *package_read_archive(const char *pkg_path);
void package_free(Pkg ** ptp);
char *package_to_description(Pkg *pkg);
#endif

View File

@ -1,39 +0,0 @@
#ifndef CIETER_PACKAGE_INFO
#define CIETER_PACKAGE_INFO
#define FREE_STRING(sp) if (sp != NULL) free(sp)
#include <stdint.h>
#include "dynarray.h"
typedef struct pkg_info {
char *name;
char *base;
char *version;
char *description;
int64_t size;
int64_t csize;
char *url;
char *arch;
int64_t build_date;
char *packager;
char *pgpsig;
int64_t pgpsigsize;
DynArray *groups;
DynArray *licenses;
DynArray *replaces;
DynArray *depends;
DynArray *conflicts;
DynArray *provides;
DynArray *optdepends;
DynArray *makedepends;
DynArray *checkdepends;
} PkgInfo;
PkgInfo *package_info_init();
int package_info_parse(PkgInfo *pkg_info, char *pkg_info_str);
void package_info_free(PkgInfo *pkg_info);
#endif

View File

@ -1,4 +1,4 @@
#include "dynarray.h"
#include "cieter_dynarray.h"
struct dyn_array {
char **array;
@ -6,15 +6,15 @@ struct dyn_array {
size_t capacity;
};
DynArray *dynarray_init(size_t initial_capacity) {
DynArray *da = malloc(sizeof(DynArray));
CieterDynArray *cieter_dynarray_init(size_t initial_capacity) {
CieterDynArray *da = malloc(sizeof(CieterDynArray));
da->size = 0;
da->capacity = initial_capacity;
return da;
}
void dynarray_add(DynArray *da, const char *s) {
void cieter_dynarray_add(CieterDynArray *da, const char *s) {
// An empty dynarray does not have an allocated internal array yet
if (da->size == 0) {
da->array = malloc(sizeof(char*) * da->capacity);
@ -29,7 +29,7 @@ void dynarray_add(DynArray *da, const char *s) {
da->size++;
}
void dynarray_free(DynArray *da) {
void cieter_dynarray_free(CieterDynArray *da) {
if (da == NULL) {
return;
}
@ -45,11 +45,11 @@ void dynarray_free(DynArray *da) {
free(da);
}
char **dynarray_convert(DynArray *da) {
char **cieter_dynarray_convert(CieterDynArray *da) {
char **array = da->array;
da->array = NULL;
dynarray_free(da);
cieter_dynarray_free(da);
return array;
}

View File

@ -1,4 +1,4 @@
#include "package.h"
#include "cieter_package.h"
static char *ignored_names[5] = {
".BUILDINFO",
@ -9,11 +9,11 @@ static char *ignored_names[5] = {
};
static int ignored_words_len = sizeof(ignored_names) / sizeof(char *);
Pkg *package_init() {
return calloc(sizeof(PkgInfo), 1);
CieterPkg *package_init() {
return calloc(sizeof(CieterPkgInfo), 1);
}
Pkg *package_read_archive(const char *pkg_path) {
CieterPkg *package_read_archive(const char *pkg_path) {
struct archive *a = archive_read_new();
struct archive_entry *entry = archive_entry_new();
@ -36,8 +36,8 @@ Pkg *package_read_archive(const char *pkg_path) {
int compression_code = archive_filter_code(a, 0);
const char *path_name;
PkgInfo *pkg_info;
DynArray *files = dynarray_init(16);
CieterPkgInfo *pkg_info;
CieterDynArray *files = cieter_dynarray_init(16);
while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
path_name = archive_entry_pathname(entry);
@ -52,7 +52,7 @@ Pkg *package_read_archive(const char *pkg_path) {
}
if (!ignore) {
dynarray_add(files, path_name);
cieter_dynarray_add(files, path_name);
}
if (strcmp(path_name, ".PKGINFO") == 0) {
@ -84,7 +84,7 @@ Pkg *package_read_archive(const char *pkg_path) {
archive_entry_free(entry);
// Create final return value
Pkg *pkg = package_init();
CieterPkg *pkg = package_init();
pkg->path = strdup(pkg_path);
pkg->info = pkg_info;
pkg->files = files;
@ -93,6 +93,6 @@ Pkg *package_read_archive(const char *pkg_path) {
return pkg;
}
char *package_to_description(Pkg *pkg) {
char *package_to_description(CieterPkg *pkg) {
}

View File

@ -1,12 +1,12 @@
#include <stdbool.h>
#include "package_info.h"
#include "cieter_package_info.h"
PkgInfo *package_info_init() {
return calloc(1, sizeof(PkgInfo));
CieterPkgInfo *package_info_init() {
return calloc(1, sizeof(CieterPkgInfo));
}
void package_info_free(PkgInfo *pkg_info) {
void package_info_free(CieterPkgInfo *pkg_info) {
FREE_STRING(pkg_info->name);
FREE_STRING(pkg_info->base);
FREE_STRING(pkg_info->version);
@ -16,15 +16,15 @@ void package_info_free(PkgInfo *pkg_info) {
FREE_STRING(pkg_info->packager);
FREE_STRING(pkg_info->pgpsig);
dynarray_free(pkg_info->groups);
dynarray_free(pkg_info->licenses);
dynarray_free(pkg_info->replaces);
dynarray_free(pkg_info->depends);
dynarray_free(pkg_info->conflicts);
dynarray_free(pkg_info->provides);
dynarray_free(pkg_info->optdepends);
dynarray_free(pkg_info->makedepends);
dynarray_free(pkg_info->checkdepends);
cieter_dynarray_free(pkg_info->groups);
cieter_dynarray_free(pkg_info->licenses);
cieter_dynarray_free(pkg_info->replaces);
cieter_dynarray_free(pkg_info->depends);
cieter_dynarray_free(pkg_info->conflicts);
cieter_dynarray_free(pkg_info->provides);
cieter_dynarray_free(pkg_info->optdepends);
cieter_dynarray_free(pkg_info->makedepends);
cieter_dynarray_free(pkg_info->checkdepends);
free(pkg_info);
}
@ -59,11 +59,11 @@ 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_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) { \
if (pkg_info->field == NULL) { pkg_info->field = dynarray_init(4); } \
dynarray_add(pkg_info->field, value_ptr); goto advance; \
if (pkg_info->field == NULL) { pkg_info->field = cieter_dynarray_init(4); } \
cieter_dynarray_add(pkg_info->field, value_ptr); goto advance; \
}
int package_info_parse(PkgInfo *pkg_info, char *pkg_info_str) {
int package_info_parse(CieterPkgInfo *pkg_info, char *pkg_info_str) {
char *offset_ptr, *equals_ptr, *key_ptr, *value_ptr;
bool end = false;