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) cmake_minimum_required(VERSION 3.20)
project(cieter C)
project(cieter C)
set(CMAKE_C_STANDARD 17) set(CMAKE_C_STANDARD 17)
find_package(LibArchive REQUIRED) find_package(LibArchive REQUIRED)
include_directories(include) include_directories(include)
file(GLOB SRCS src/*.c) file(GLOB CIETER_SRCS src/cieter/**.c)
if(CMAKE_BUILD_TYPE STREQUAL Release) if(CMAKE_BUILD_TYPE STREQUAL Release)
add_compile_options(-O3 -flto) add_compile_options(-O3 -flto)
endif() endif()
add_executable(cieter ${SRCS}) add_library(cieter SHARED ${CIETER_SRCS})
target_link_libraries(cieter LibArchive::LibArchive)
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 { struct dyn_array {
char **array; char **array;
@ -6,15 +6,15 @@ struct dyn_array {
size_t capacity; size_t capacity;
}; };
DynArray *dynarray_init(size_t initial_capacity) { CieterDynArray *cieter_dynarray_init(size_t initial_capacity) {
DynArray *da = malloc(sizeof(DynArray)); CieterDynArray *da = malloc(sizeof(CieterDynArray));
da->size = 0; da->size = 0;
da->capacity = initial_capacity; da->capacity = initial_capacity;
return da; 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 // An empty dynarray does not have an allocated internal array yet
if (da->size == 0) { if (da->size == 0) {
da->array = malloc(sizeof(char*) * da->capacity); da->array = malloc(sizeof(char*) * da->capacity);
@ -29,7 +29,7 @@ void dynarray_add(DynArray *da, const char *s) {
da->size++; da->size++;
} }
void dynarray_free(DynArray *da) { void cieter_dynarray_free(CieterDynArray *da) {
if (da == NULL) { if (da == NULL) {
return; return;
} }
@ -45,11 +45,11 @@ void dynarray_free(DynArray *da) {
free(da); free(da);
} }
char **dynarray_convert(DynArray *da) { char **cieter_dynarray_convert(CieterDynArray *da) {
char **array = da->array; char **array = da->array;
da->array = NULL; da->array = NULL;
dynarray_free(da); cieter_dynarray_free(da);
return array; return array;
} }

View File

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

View File

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