more list handling
[project/uci.git] / list.c
1 /*
2  * libuci - Library for the Unified Configuration Interface
3  * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
4  *
5  * this program is free software; you can redistribute it and/or modify
6  * it under the terms of the gnu lesser general public license version 2.1
7  * as published by the free software foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 /* initialize a list head/item */
16 static inline void uci_list_init(struct uci_list *ptr)
17 {
18         ptr->prev = ptr;
19         ptr->next = ptr;
20 }
21
22 /* inserts a new list entry between two consecutive entries */
23 static inline void __uci_list_add(struct uci_list *prev, struct uci_list *next, struct uci_list *ptr)
24 {
25         prev->next = ptr;
26         next->prev = ptr;
27         ptr->prev = prev;
28         ptr->next = next;
29 }
30
31 /* inserts a new list entry at the tail of the list */
32 static inline void uci_list_add(struct uci_list *head, struct uci_list *ptr)
33 {
34         /* NB: head->prev points at the tail */
35         __uci_list_add(head->prev, head, ptr);
36 }
37
38 static inline void uci_list_del(struct uci_list *ptr)
39 {
40         struct uci_list *next, *prev;
41
42         next = ptr->next;
43         prev = ptr->prev;
44
45         prev->next = next;
46         next->prev = prev;
47 }
48
49 static struct uci_config *uci_alloc_file(struct uci_context *ctx, const char *name)
50 {
51         struct uci_config *cfg;
52
53         cfg = (struct uci_config *) uci_malloc(ctx, sizeof(struct uci_config));
54         uci_list_init(&cfg->list);
55         uci_list_init(&cfg->sections);
56         cfg->name = uci_strdup(ctx, name);
57         cfg->ctx = ctx;
58
59         return cfg;
60 }
61
62 static void uci_drop_file(struct uci_config *cfg)
63 {
64         /* TODO: free children */
65         uci_list_del(&cfg->list);
66         if (cfg->name)
67                 free(cfg->name);
68         free(cfg);
69 }