From b09a640e1dd7b30ff05093e14190da99bb3cac32 Mon Sep 17 00:00:00 2001 From: forever2k38 Date: Mon, 31 Aug 2026 01:44:47 +0200 Subject: feat: repos array --- src/config.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'src/config.c') diff --git a/src/config.c b/src/config.c index 54a505a..dd44129 100644 --- a/src/config.c +++ b/src/config.c @@ -2,6 +2,8 @@ #include "lauxlib.h" #include "lualib.h" +#include "stdlib.h" +#include "string.h" int pow_open_config(pow_config *cfg, char *path) { int err; @@ -46,14 +48,70 @@ int pow_open_config(pow_config *cfg, char *path) { lua_rawgeti(L, -1, i); if (lua_istable(L, -1)) { + pow_repo *repo = malloc(sizeof(pow_repo)); + + if (!repo) { + lua_pop(L, 1); + + printf("malloc for repo failed, what the fuck? something really bad " + "happened i guess\n"); + + return 1; + } + lua_getfield(L, -1, "path"); if (lua_isstring(L, -1)) { const char *path = lua_tostring(L, -1); + const char *path_dup = strdup(path); + + if (!path_dup) { + lua_pop(L, 1); + lua_pop(L, 1); + + free(repo); + + printf( + "malloc for path failed, what the fuck? something really bad " + "happened i guess\n"); + + return 1; + } + + repo->path = path_dup; + printf("%s\n", path); + } else { + printf("invalid (or non existent) path for repo\n"); + + lua_pop(L, 1); + lua_pop(L, 1); + + free(repo); + + continue; } + pow_repo **repos = + realloc(cfg->repos, (cfg->repos_count + 1) * sizeof(*repos)); + + if (!repos) { + printf("failed realloc! bad things coming\n"); + + lua_pop(L, 1); + lua_pop(L, 1); + + free(repo->path); + free(repo); + + return 1; + } + + repos[cfg->repos_count++] = repo; + + cfg->repos = repos; + lua_pop(L, 1); } @@ -63,3 +121,15 @@ int pow_open_config(pow_config *cfg, char *path) { return 0; } + +int pow_close_config(pow_config *cfg) { + for (size_t i = 0; i < cfg->repos_count; i++) { + free(cfg->repos[i]->path); + free(cfg->repos[i]); + } + + free(cfg->repos); + lua_close(cfg->L); + + return 0; +} -- cgit v1.3