diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/ll.c | 68 | ||||
| -rw-r--r-- | src/ll.h | 6 | ||||
| -rw-r--r-- | src/main.c | 22 |
3 files changed, 53 insertions, 43 deletions
| @@ -1,6 +1,7 @@ | |||
| 1 | #include "ll.h" | 1 | #include "ll.h" |
| 2 | #include "shared.h" | 2 | #include "shared.h" |
| 3 | 3 | ||
| 4 | #include <asm-generic/errno-base.h> | ||
| 4 | #include <stddef.h> | 5 | #include <stddef.h> |
| 5 | #include <stdlib.h> | 6 | #include <stdlib.h> |
| 6 | #include <errno.h> | 7 | #include <errno.h> |
| @@ -32,20 +33,6 @@ dllnode * dllnode_init(void *data, dll_freecb fcb) { | |||
| 32 | return n; | 33 | return n; |
| 33 | } | 34 | } |
| 34 | 35 | ||
| 35 | int dllnode_free(dllnode **n) { | ||
| 36 | if(!n) | ||
| 37 | RETURNWERR(EINVAL, 1); | ||
| 38 | if(!(*n)) | ||
| 39 | RETURNWERR(EINVAL, 1); | ||
| 40 | |||
| 41 | if((*n)->freecb != NULL) | ||
| 42 | (*n)->freecb((*n)->data); | ||
| 43 | free(*n); | ||
| 44 | *n = NULL; | ||
| 45 | |||
| 46 | return 0; | ||
| 47 | } | ||
| 48 | |||
| 49 | dlinkedlist * dlinkedlist_init(void) { | 36 | dlinkedlist * dlinkedlist_init(void) { |
| 50 | dlinkedlist *ll = xcalloc(1, sizeof(*ll)); | 37 | dlinkedlist *ll = xcalloc(1, sizeof(*ll)); |
| 51 | ll->end = NULL; | 38 | ll->end = NULL; |
| @@ -55,21 +42,23 @@ dlinkedlist * dlinkedlist_init(void) { | |||
| 55 | return ll; | 42 | return ll; |
| 56 | } | 43 | } |
| 57 | 44 | ||
| 58 | int dlinkedlist_free(dlinkedlist **ll) { | 45 | void dlinkedlist_free(dlinkedlist *ll) { |
| 59 | if(!ll) | 46 | if(!ll) |
| 60 | RETURNWERR(EINVAL, 1); | 47 | return; |
| 61 | if(!(*ll)) | 48 | |
| 62 | RETURNWERR(EINVAL, 1); | 49 | for(dllnode *current = ll->start, *next; current != NULL; ) { |
| 50 | next = current->next; | ||
| 63 | 51 | ||
| 64 | for(dllnode *n; (*ll)->start != NULL;) { | 52 | // This used to be a function but was causing issues for some inscrutable reason |
| 65 | n = (*ll)->start->next; | 53 | if(current->freecb != NULL) |
| 66 | dllnode_free(&((*ll)->start)); | 54 | current->freecb(current->data); |
| 67 | (*ll)->start = n; | 55 | free(current); |
| 56 | |||
| 57 | current = next; | ||
| 68 | } | 58 | } |
| 69 | free(*ll); | 59 | free(ll); |
| 70 | *ll = NULL; | ||
| 71 | 60 | ||
| 72 | return 0; | 61 | return; |
| 73 | } | 62 | } |
| 74 | 63 | ||
| 75 | int dlinkedlist_size(const dlinkedlist * const ll) { | 64 | int dlinkedlist_size(const dlinkedlist * const ll) { |
| @@ -107,14 +96,14 @@ int dlinkedlist_xxxend(dlinkedlist * const ll, void *data, dll_freecb fcb, char | |||
| 107 | dllnode *n = dllnode_init(data, fcb); | 96 | dllnode *n = dllnode_init(data, fcb); |
| 108 | switch (op) { | 97 | switch (op) { |
| 109 | case 'a': | 98 | case 'a': |
| 110 | n->prev = ll->end; | 99 | n->prev = (ll->end); |
| 111 | ll->end->next = n; | 100 | (ll->end)->next = n; |
| 112 | ll->end = n; | 101 | ll->end = n; |
| 113 | break; | 102 | break; |
| 114 | 103 | ||
| 115 | case 'p': | 104 | case 'p': |
| 116 | n->next = ll->start; | 105 | n->next = (ll->start); |
| 117 | ll->start->prev = n; | 106 | (ll->start)->prev = n; |
| 118 | ll->start = n; | 107 | ll->start = n; |
| 119 | break; | 108 | break; |
| 120 | 109 | ||
| @@ -176,8 +165,8 @@ int dlinkedlist_insert(dlinkedlist * const ll, void *data, dll_freecb fcb, int i | |||
| 176 | if(!current) | 165 | if(!current) |
| 177 | XALLOC_EXIT("<dlinkedlist_insert> somehow managed to pull a null node from dlinkedlist_getnode(... , ... , ... , %d)", , (index)); | 166 | XALLOC_EXIT("<dlinkedlist_insert> somehow managed to pull a null node from dlinkedlist_getnode(... , ... , ... , %d)", , (index)); |
| 178 | 167 | ||
| 179 | current->prev->next = new; | 168 | (current->prev)->next = new; |
| 180 | new->prev = current->prev; | 169 | new->prev = (current->prev); |
| 181 | new->next = current; | 170 | new->next = current; |
| 182 | current->prev = new; | 171 | current->prev = new; |
| 183 | 172 | ||
| @@ -206,7 +195,10 @@ int dlinkedlist_remove(dlinkedlist * const ll, int index) { | |||
| 206 | current->next->prev = current->prev; | 195 | current->next->prev = current->prev; |
| 207 | } | 196 | } |
| 208 | 197 | ||
| 209 | dllnode_free(¤t); | 198 | if(current->freecb != NULL) |
| 199 | current->freecb(current->data); | ||
| 200 | free(current); | ||
| 201 | |||
| 210 | ll->size--; | 202 | ll->size--; |
| 211 | 203 | ||
| 212 | return 0; | 204 | return 0; |
| @@ -218,4 +210,16 @@ void* dlinkedlist_get(const dlinkedlist * const ll, int index) { | |||
| 218 | return NULL; | 210 | return NULL; |
| 219 | 211 | ||
| 220 | return n->data; | 212 | return n->data; |
| 213 | } | ||
| 214 | |||
| 215 | int dlinkedlist_foreach(dlinkedlist *ll, int (*callback)(void*)) { | ||
| 216 | if(!ll) | ||
| 217 | RETURNWERR(EINVAL, -1); | ||
| 218 | if(!callback) | ||
| 219 | RETURNWERR(EINVAL, -1); | ||
| 220 | |||
| 221 | for(dllnode *p = ll->start; p != NULL; p = p->next) | ||
| 222 | callback(p->data); | ||
| 223 | |||
| 224 | return 0; | ||
| 221 | } \ No newline at end of file | 225 | } \ No newline at end of file |
| @@ -21,11 +21,11 @@ | |||
| 21 | /* TODO: Implement a way to register a set of alloc functions to a linked list so I can give it arenas for memory allocation | 21 | /* TODO: Implement a way to register a set of alloc functions to a linked list so I can give it arenas for memory allocation |
| 22 | // instead of just xcalloc */ | 22 | // instead of just xcalloc */ |
| 23 | 23 | ||
| 24 | typedef int (*dll_freecb)(void*); | 24 | typedef void (*dll_freecb)(void*); |
| 25 | typedef struct dlinked dlinkedlist; | 25 | typedef struct dlinked dlinkedlist; |
| 26 | 26 | ||
| 27 | dlinkedlist * dlinkedlist_init(void); | 27 | dlinkedlist * dlinkedlist_init(void); |
| 28 | int dlinkedlist_free(dlinkedlist **ll); | 28 | void dlinkedlist_free(dlinkedlist *ll); |
| 29 | int dlinkedlist_append(dlinkedlist * const ll, void *data, dll_freecb fcb); | 29 | int dlinkedlist_append(dlinkedlist * const ll, void *data, dll_freecb fcb); |
| 30 | int dlinkedlist_prepend(dlinkedlist * const ll, void *data, dll_freecb fcb); | 30 | int dlinkedlist_prepend(dlinkedlist * const ll, void *data, dll_freecb fcb); |
| 31 | int dlinkedlist_insert(dlinkedlist * const ll, void *data, dll_freecb fcb, int index); | 31 | 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); | |||
| 35 | int dlinkedlist_size(const dlinkedlist * const ll); | 35 | int dlinkedlist_size(const dlinkedlist * const ll); |
| 36 | #define dlinkedlist_isempty(ll) (dlinkedlist_size((ll)) == 0) | 36 | #define dlinkedlist_isempty(ll) (dlinkedlist_size((ll)) == 0) |
| 37 | 37 | ||
| 38 | int dlinkedlist_foreach(dlinkedlist *ll, int (*callback)(void*)); | ||
| 39 | |||
| 38 | #endif \ No newline at end of file | 40 | #endif \ No newline at end of file |
| @@ -22,6 +22,13 @@ int selector(const struct dirent *ent) { | |||
| 22 | return 1; | 22 | return 1; |
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | int printnames(void *data) { | ||
| 26 | struct dirent *node = (struct dirent *)data; | ||
| 27 | |||
| 28 | printf("%s\n", (node) ? node->d_name : "null"); | ||
| 29 | return 0; | ||
| 30 | } | ||
| 31 | |||
| 25 | int main() { | 32 | int main() { |
| 26 | // error(1, ENOTSUP, "No main file lol"); | 33 | // error(1, ENOTSUP, "No main file lol"); |
| 27 | 34 | ||
| @@ -32,16 +39,13 @@ int main() { | |||
| 32 | if(numentries < 0) | 39 | if(numentries < 0) |
| 33 | error(1, errno, "Ran into error scanning dir"); | 40 | error(1, errno, "Ran into error scanning dir"); |
| 34 | 41 | ||
| 35 | dlinkedlist *ll = dlinkedlist_init(); | 42 | dlinkedlist *list = dlinkedlist_init(); |
| 36 | for(int i = 0; i < numentries; i++) { | 43 | for(int i = 0; i < numentries; i++) |
| 37 | if(dlinkedlist_append(ll, (void *)namelist[i], free) != 0) | 44 | dlinkedlist_append(list, (void *)(namelist[i]), free); |
| 38 | error(1, errno, "Could not add file entry to linked list"); | ||
| 39 | |||
| 40 | free(namelist[i]); | ||
| 41 | } | ||
| 42 | free(namelist); | 45 | free(namelist); |
| 43 | 46 | ||
| 44 | dlinkedlist_free(&ll); | 47 | dlinkedlist_foreach(list, printnames); |
| 48 | dlinkedlist_free(list); | ||
| 45 | 49 | ||
| 46 | return 0; | 50 | return 0; |
| 47 | } \ No newline at end of file | 51 | } \ No newline at end of file |
