From 54b82706a01049e4ed5e4d417f50f642c701105b Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Tue, 15 Apr 2025 18:02:06 -0500 Subject: Finish prototype scanning code --- src/ll.c | 68 +++++++++++++++++++++++++++++++++----------------------------- src/ll.h | 6 ++++-- src/main.c | 22 +++++++++++--------- 3 files changed, 53 insertions(+), 43 deletions(-) (limited to 'src') diff --git a/src/ll.c b/src/ll.c index 5e67244..d25818a 100644 --- a/src/ll.c +++ b/src/ll.c @@ -1,6 +1,7 @@ #include "ll.h" #include "shared.h" +#include #include #include #include @@ -32,20 +33,6 @@ dllnode * dllnode_init(void *data, dll_freecb fcb) { return n; } -int dllnode_free(dllnode **n) { - if(!n) - RETURNWERR(EINVAL, 1); - if(!(*n)) - RETURNWERR(EINVAL, 1); - - if((*n)->freecb != NULL) - (*n)->freecb((*n)->data); - free(*n); - *n = NULL; - - return 0; -} - dlinkedlist * dlinkedlist_init(void) { dlinkedlist *ll = xcalloc(1, sizeof(*ll)); ll->end = NULL; @@ -55,21 +42,23 @@ dlinkedlist * dlinkedlist_init(void) { return ll; } -int dlinkedlist_free(dlinkedlist **ll) { +void dlinkedlist_free(dlinkedlist *ll) { if(!ll) - RETURNWERR(EINVAL, 1); - if(!(*ll)) - RETURNWERR(EINVAL, 1); + return; + + for(dllnode *current = ll->start, *next; current != NULL; ) { + next = current->next; - for(dllnode *n; (*ll)->start != NULL;) { - n = (*ll)->start->next; - dllnode_free(&((*ll)->start)); - (*ll)->start = n; + // This used to be a function but was causing issues for some inscrutable reason + if(current->freecb != NULL) + current->freecb(current->data); + free(current); + + current = next; } - free(*ll); - *ll = NULL; + free(ll); - return 0; + return; } int dlinkedlist_size(const dlinkedlist * const ll) { @@ -107,14 +96,14 @@ int dlinkedlist_xxxend(dlinkedlist * const ll, void *data, dll_freecb fcb, char dllnode *n = dllnode_init(data, fcb); switch (op) { case 'a': - n->prev = ll->end; - ll->end->next = n; + n->prev = (ll->end); + (ll->end)->next = n; ll->end = n; break; case 'p': - n->next = ll->start; - ll->start->prev = n; + n->next = (ll->start); + (ll->start)->prev = n; ll->start = n; break; @@ -176,8 +165,8 @@ int dlinkedlist_insert(dlinkedlist * const ll, void *data, dll_freecb fcb, int i if(!current) XALLOC_EXIT(" somehow managed to pull a null node from dlinkedlist_getnode(... , ... , ... , %d)", , (index)); - current->prev->next = new; - new->prev = current->prev; + (current->prev)->next = new; + new->prev = (current->prev); new->next = current; current->prev = new; @@ -206,7 +195,10 @@ int dlinkedlist_remove(dlinkedlist * const ll, int index) { current->next->prev = current->prev; } - dllnode_free(¤t); + if(current->freecb != NULL) + current->freecb(current->data); + free(current); + ll->size--; return 0; @@ -218,4 +210,16 @@ void* dlinkedlist_get(const dlinkedlist * const ll, int index) { return NULL; return n->data; +} + +int dlinkedlist_foreach(dlinkedlist *ll, int (*callback)(void*)) { + if(!ll) + RETURNWERR(EINVAL, -1); + if(!callback) + RETURNWERR(EINVAL, -1); + + for(dllnode *p = ll->start; p != NULL; p = p->next) + callback(p->data); + + return 0; } \ No newline at end of file diff --git a/src/ll.h b/src/ll.h index 497d47b..1824b49 100644 --- a/src/ll.h +++ b/src/ll.h @@ -21,11 +21,11 @@ /* TODO: Implement a way to register a set of alloc functions to a linked list so I can give it arenas for memory allocation // instead of just xcalloc */ -typedef int (*dll_freecb)(void*); +typedef void (*dll_freecb)(void*); typedef struct dlinked dlinkedlist; dlinkedlist * dlinkedlist_init(void); -int dlinkedlist_free(dlinkedlist **ll); +void dlinkedlist_free(dlinkedlist *ll); int dlinkedlist_append(dlinkedlist * const ll, void *data, dll_freecb fcb); int dlinkedlist_prepend(dlinkedlist * const ll, void *data, dll_freecb fcb); int dlinkedlist_insert(dlinkedlist * const ll, void *data, dll_freecb fcb, int index); @@ -35,4 +35,6 @@ int dlinkedlist_remove(dlinkedlist * const ll, int index); int dlinkedlist_size(const dlinkedlist * const ll); #define dlinkedlist_isempty(ll) (dlinkedlist_size((ll)) == 0) +int dlinkedlist_foreach(dlinkedlist *ll, int (*callback)(void*)); + #endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index cacbc4f..abae0a9 100644 --- a/src/main.c +++ b/src/main.c @@ -22,6 +22,13 @@ int selector(const struct dirent *ent) { return 1; } +int printnames(void *data) { + struct dirent *node = (struct dirent *)data; + + printf("%s\n", (node) ? node->d_name : "null"); + return 0; +} + int main() { // error(1, ENOTSUP, "No main file lol"); @@ -32,16 +39,13 @@ int main() { if(numentries < 0) error(1, errno, "Ran into error scanning dir"); - dlinkedlist *ll = dlinkedlist_init(); - for(int i = 0; i < numentries; i++) { - if(dlinkedlist_append(ll, (void *)namelist[i], free) != 0) - error(1, errno, "Could not add file entry to linked list"); - - free(namelist[i]); - } + dlinkedlist *list = dlinkedlist_init(); + for(int i = 0; i < numentries; i++) + dlinkedlist_append(list, (void *)(namelist[i]), free); free(namelist); - - dlinkedlist_free(&ll); + + dlinkedlist_foreach(list, printnames); + dlinkedlist_free(list); return 0; } \ No newline at end of file -- cgit v1.2.3