diff options
| author | David Moc <personal@cdatgoose.org> | 2026-03-09 17:40:18 +0100 |
|---|---|---|
| committer | David Moc <personal@cdatgoose.org> | 2026-03-09 17:40:18 +0100 |
| commit | 1af8b8a568ba1782c7f54c575dd7cbe352e0d4a4 (patch) | |
| tree | 098fa8507bf30537a1b4cfa0b2389f5f142b17cb /src/lib/arglist.h | |
Diffstat (limited to 'src/lib/arglist.h')
| -rw-r--r-- | src/lib/arglist.h | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/lib/arglist.h b/src/lib/arglist.h new file mode 100644 index 0000000..ecd4455 --- /dev/null +++ b/src/lib/arglist.h @@ -0,0 +1,51 @@ +#ifndef _CB_ARGLIST_H +#define _CB_ARGLIST_H + +#include <stdarg.h> +#include <stdlib.h> +#include <string.h> + +typedef struct { + char **list; + int count; + int capacity; +} CB_ARGLIST; + +static inline CB_ARGLIST *arglist_new() { + CB_ARGLIST *arglist = (CB_ARGLIST*) malloc(sizeof(CB_ARGLIST)); + arglist->count = 0; + arglist->capacity = 8; + arglist->list = (char**) malloc(sizeof(char *) * arglist->capacity); + return arglist; +} + +static inline void arglist_append(CB_ARGLIST *arglist, ...) { + va_list args; + va_start(args, arglist); + char *arg; + while ((arg = va_arg(args, char *)) != NULL) { + if (arglist->count >= arglist->capacity) { + arglist->capacity *= 2; + arglist->list = (char**) realloc(arglist->list, sizeof(char *) * arglist->capacity); + } + arglist->list[arglist->count++] = strdup(arg); + } + va_end(args); +} + +static inline void arglist_append_array(CB_ARGLIST *arglist, const char **arr) { + for (int i = 0; arr[i] != NULL; i++) { + arglist_append(arglist, arr[i], NULL); + } +} + +static inline void arglist_free(CB_ARGLIST *arglist) { + for (int i = 0; i < arglist->count; i++) { + free(arglist->list[i]); + } + free(arglist->list); + free(arglist); +} + +#endif // _CB_ARGLIST_H + |
