From e0f0dec2220512812eb5ce1413af1354b4fcee94 Mon Sep 17 00:00:00 2001 From: ngn Date: Sun, 11 Aug 2024 01:42:47 +0300 Subject: [PATCH] update: better way to handle resolve list --- include/all.h | 2 +- include/ctx.h | 7 ++-- include/util.h | 6 --- locale/tr/LC_MESSAGES/libmp.po | 2 +- src/ctx/resolve.c | 53 ++++++++++++++---------- src/util.c | 75 ---------------------------------- 6 files changed, 37 insertions(+), 108 deletions(-) diff --git a/include/all.h b/include/all.h index ac4506b..dbffa4e 100644 --- a/include/all.h +++ b/include/all.h @@ -22,7 +22,7 @@ // clang-format on -#define LM_VERSION "24.01" +#define LM_VERSION "24.02" #include "ctx.h" #include "error.h" diff --git a/include/ctx.h b/include/ctx.h index be71e03..acabb71 100644 --- a/include/ctx.h +++ b/include/ctx.h @@ -23,10 +23,9 @@ typedef struct lm_ctx_list { } lm_ctx_list_t; typedef struct lm_ctx_resolve_list { - lm_pkg_t *resolving; - lm_pkg_t *packages; - lm_pkg_t *cur; - ssize_t count; + lm_pkg_t **packages; + lm_pkg_t *cur; + ssize_t count, index; } lm_ctx_resolve_list_t; typedef struct lm_ctx_update_list { diff --git a/include/util.h b/include/util.h index bd1b0d4..21b58a0 100644 --- a/include/util.h +++ b/include/util.h @@ -47,9 +47,3 @@ bool mkdir_ifnot(char *path); int join_multiple(char *res, const char *base, const char *pth, const char *pth2); int join(char *res, const char *base, const char *pth); char *join_alloc(const char *base, const char *pth); - -bool pkglist_contains(lm_pkg_t *list, lm_pkg_t *pkg); -lm_pkg_t *pkglist_del(lm_pkg_t *list, lm_pkg_t *pkg); -lm_pkg_t *pkglist_add_top(lm_pkg_t *list, lm_pkg_t *pkg); -lm_pkg_t *pkglist_add_end(lm_pkg_t *list, lm_pkg_t *pkg); -void pkglist_free(lm_pkg_t *list); diff --git a/locale/tr/LC_MESSAGES/libmp.po b/locale/tr/LC_MESSAGES/libmp.po index 27d7f54..a4cb59e 100644 --- a/locale/tr/LC_MESSAGES/libmp.po +++ b/locale/tr/LC_MESSAGES/libmp.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-08-10 00:05+0300\n" +"POT-Creation-Date: 2024-08-11 01:42+0300\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" diff --git a/src/ctx/resolve.c b/src/ctx/resolve.c index 69b1b4f..8d691fd 100644 --- a/src/ctx/resolve.c +++ b/src/ctx/resolve.c @@ -7,14 +7,32 @@ #include #include +bool __lm_ctx_resolve_contains(lm_pkg_t *pkg, lm_ctx_resolve_list_t *list){ + if(NULL == list->packages) + return false; + + for(int i = 0; i < list->count; i++){ + if(eq(list->packages[i]->data.name, pkg->data.name)) + return true; + } + + return false; +} + bool __lm_ctx_resolve(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list, lm_pkg_t *pkg, bool resolve_depends){ - if(pkglist_contains(list->packages, pkg)) + if(__lm_ctx_resolve_contains(pkg, list)) return true; - if(!resolve_depends || NULL == pkg->data.depends) - goto end; + if(NULL == list->packages) + list->packages = malloc(sizeof(lm_pkg_t *)*(++list->count)); + else + list->packages = realloc(list->packages, sizeof(lm_pkg_t *)*(++list->count)); - list->resolving = pkglist_add_top(list->resolving, pkg); + list->packages[list->count-1] = pkg; + + if(!resolve_depends || NULL == pkg->data.depends) + return true; + lm_pkg_t *depend = NULL; for(int i = 0; pkg->data.depends[i] != NULL; i++){ @@ -29,19 +47,10 @@ bool __lm_ctx_resolve(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list, lm_pkg_t *pkg, return false; } - if(pkglist_contains(list->resolving, depend)){ - lm_error_set(LM_ERR_DependNotFound, pkg->data.depends[i], pkg->data.name); - return false; - } - if(!__lm_ctx_resolve(ctx, list, depend, resolve_depends)) return false; } - list->resolving = pkglist_del(list->resolving, pkg); -end: - list->packages = pkglist_add_end(list->packages, pkg); - list->count++; return true; } @@ -57,15 +66,11 @@ lm_ctx_resolve_list_t *lm_ctx_resolve(lm_ctx_t *ctx, lm_pkg_t *pkg, bool resolve } if(!__lm_ctx_resolve(ctx, list, pkg, resolve_depends)){ - pkglist_free(list->resolving); - pkglist_free(list->packages); + free(list->packages); free(list); return NULL; } - pkglist_free(list->resolving); - list->resolving = NULL; - return list; } @@ -76,11 +81,16 @@ lm_pkg_t *lm_ctx_resolve_next(lm_ctx_resolve_list_t *list){ } if(NULL == list->cur){ - list->cur = list->packages; + list->index = 0; + list->cur = list->packages[list->index++]; return list->cur; } - list->cur = list->cur->next; + if(list->index >= list->count) + list->cur = NULL; + else + list->cur = list->packages[list->index++]; + return list->cur; } @@ -90,7 +100,8 @@ void lm_ctx_resolve_free(lm_ctx_resolve_list_t *list){ return; } - pkglist_free(list->packages); + free(list->packages); free(list); + return; } diff --git a/src/util.c b/src/util.c index caa03b0..eb0a02e 100644 --- a/src/util.c +++ b/src/util.c @@ -354,81 +354,6 @@ char *join_alloc(const char *base, const char *pth) { return path; } -bool pkglist_contains(lm_pkg_t *list, lm_pkg_t *pkg) { - lm_pkg_t *cur = list; - while (cur) { - if (eq(pkg->data.name, cur->data.name)) - return true; - cur = cur->next; - } - return false; -} - -lm_pkg_t *pkglist_del(lm_pkg_t *list, lm_pkg_t *pkg) { - if (NULL == pkg) { - lm_error_set(LM_ERR_ArgNULL); - return list; - } - - if (NULL == list) - return list; - - if (eq(list->data.name, pkg->data.name)) { - list = NULL; - return list; - } - - lm_pkg_t *cur = list; - lm_pkg_t *found = NULL; - - while (NULL != cur->next) { - if (eq(cur->next->data.name, pkg->data.name)) { - found = cur->next; - cur->next = cur->next->next; - break; - } - cur = cur->next; - } - - free(found); - return list; -} - -lm_pkg_t *pkglist_add_top(lm_pkg_t *list, lm_pkg_t *pkg) { - lm_pkg_t *new = malloc(sizeof(lm_pkg_t)); - memcpy(new, pkg, sizeof(lm_pkg_t)); - new->next = list; - list = new; - return list; -} - -lm_pkg_t *pkglist_add_end(lm_pkg_t *list, lm_pkg_t *pkg) { - lm_pkg_t *new = malloc(sizeof(lm_pkg_t)), *cur = list; - memcpy(new, pkg, sizeof(lm_pkg_t)); - - new->next = NULL; - - if (NULL == cur) { - list = new; - return list; - } - - while (cur->next != NULL) - cur = cur->next; - cur->next = new; - - return list; -} - -void pkglist_free(lm_pkg_t *list) { - lm_pkg_t *cur = list, *old = NULL; - while (cur != NULL) { - old = cur; - cur = cur->next; - free(old); - } -} - bool copy_file(char *dst, char *src) { FILE *dstp = NULL, *srcp = NULL; bool ret = false;