From: Felix Fietkau Date: Sat, 19 Jan 2008 18:46:29 +0000 (+0100) Subject: add some more list handling X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fuci.git;a=commitdiff_plain;h=4cec5755115eb3c48dc50dfcc5558f71f00d59c6 add some more list handling --- diff --git a/err.h b/err.h index 2800526..469164c 100644 --- a/err.h +++ b/err.h @@ -13,7 +13,7 @@ */ /* - * functions for debug and error handling + * functions for debug and error handling, for internal use only */ #ifdef DEBUG @@ -60,4 +60,3 @@ } \ } while (0) - diff --git a/libuci.c b/libuci.c index c3a8fc0..4dfffff 100644 --- a/libuci.c +++ b/libuci.c @@ -42,7 +42,7 @@ static const char *uci_errstr[] = { static void *uci_malloc(struct uci_context *ctx, size_t size) { void *ptr; - + ptr = malloc(size); if (!ptr) UCI_THROW(ctx, UCI_ERR_MEM); @@ -63,6 +63,20 @@ static void *uci_realloc(struct uci_context *ctx, void *ptr, size_t size) return ptr; } +/* + * UCI wrapper for strdup, which uses exception handling + */ +static char *uci_strdup(struct uci_context *ctx, const char *str) +{ + char *ptr; + + ptr = strdup(str); + if (!ptr) + UCI_THROW(ctx, UCI_ERR_MEM); + + return ptr; +} + #include "list.c" #include "parse.c" @@ -71,10 +85,11 @@ static void *uci_realloc(struct uci_context *ctx, void *ptr, size_t size) struct uci_context *uci_alloc(void) { struct uci_context *ctx; - + ctx = (struct uci_context *) malloc(sizeof(struct uci_context)); memset(ctx, 0, sizeof(struct uci_context)); - + uci_list_init(&ctx->root); + return ctx; } @@ -93,7 +108,7 @@ void uci_perror(struct uci_context *ctx, const char *str) err = UCI_ERR_INVAL; else err = ctx->errno; - + if ((err < 0) || (err >= UCI_ERR_LAST)) err = UCI_ERR_UNKNOWN; diff --git a/libuci.h b/libuci.h index e4990a2..3427526 100644 --- a/libuci.h +++ b/libuci.h @@ -50,7 +50,7 @@ extern struct uci_context *uci_alloc(void); * uci_perror: Print the last uci error that occured * @ctx: uci context * @str: string to print before the error message - */ + */ extern void uci_perror(struct uci_context *ctx, const char *str); /** @@ -74,7 +74,7 @@ int uci_cleanup(struct uci_context *ctx); struct uci_context { struct uci_list root; - + /* for error handling only */ struct uci_parse_context *pctx; @@ -127,7 +127,6 @@ struct uci_option #define uci_list_entry(type, ptr) \ ((struct uci_#type *) ((char *)(ptr) - offsetof(struct uci_#type,list))) - #define uci_foreach_entry(type, list, ptr) \ for(ptr = uci_list_entry(type, (list)->next); \ &ptr->list != list; \ diff --git a/list.c b/list.c index 43b58f4..f681611 100644 --- a/list.c +++ b/list.c @@ -12,3 +12,39 @@ * GNU General Public License for more details. */ +/* initialize a list head/item */ +static inline void uci_list_init(struct uci_list *ptr) +{ + ptr->prev = ptr; + ptr->next = ptr; +} + +/* inserts a new list entry between two consecutive entries */ +static inline void __uci_list_add(struct uci_list *prev, struct uci_list *next, struct uci_list *ptr) +{ + prev->next = ptr; + next->prev = ptr; + ptr->prev = prev; + ptr->next = next; +} + +/* inserts a new list entry at the tail of the list */ +static inline void uci_list_add(struct uci_list *head, struct uci_list *ptr) +{ + /* NB: head->prev points at the tail */ + __uci_list_add(head->prev, head, ptr); +} + + +static struct uci_config *uci_add_file(struct uci_context *ctx, const char *name) +{ + struct uci_config *cfg; + + cfg = (struct uci_config *) uci_malloc(ctx, sizeof(struct uci_config)); + uci_list_init(&cfg->list); + uci_list_init(&cfg->sections); + cfg->name = uci_strdup(ctx, name); + uci_list_add(&ctx->root, &cfg->list); + + return cfg; +}