add new debug macro
[project/procd.git] / utils.c
1 #include <libubox/avl.h>
2 #include <libubox/avl-cmp.h>
3 #include "utils.h"
4
5 void
6 __blobmsg_list_init(struct blobmsg_list *list, int offset, int len)
7 {
8         avl_init(&list->avl, avl_strcmp, false, NULL);
9         list->node_offset = offset;
10         list->node_len = len;
11 }
12
13 int
14 blobmsg_list_fill(struct blobmsg_list *list, void *data, int len, bool array)
15 {
16         struct avl_tree *tree = &list->avl;
17         struct blobmsg_list_node *node;
18         struct blob_attr *cur;
19         void *ptr;
20         int count = 0;
21         int rem = len;
22
23         __blob_for_each_attr(cur, data, rem) {
24                 if (!blobmsg_check_attr(cur, true))
25                         continue;
26
27                 ptr = calloc(1, list->node_len);
28                 if (!ptr)
29                         return -1;
30
31                 node = (void *) ((char *)ptr + list->node_offset);
32                 if (array)
33                         node->avl.key = blobmsg_data(cur);
34                 else
35                         node->avl.key = blobmsg_name(cur);
36                 node->data = cur;
37                 if (avl_insert(tree, &node->avl)) {
38                         free(ptr);
39                         continue;
40                 }
41
42                 count++;
43         }
44
45         return count;
46 }
47
48 void
49 blobmsg_list_move(struct blobmsg_list *list, struct blobmsg_list *src)
50 {
51         struct blobmsg_list_node *node, *tmp;
52         void *ptr;
53
54         avl_remove_all_elements(&src->avl, node, avl, tmp) {
55                 if (!avl_insert(&list->avl, &node->avl)) {
56                         ptr = ((char *) node - list->node_offset);
57                         free(ptr);
58                 }
59         }
60 }
61
62 void
63 blobmsg_list_free(struct blobmsg_list *list)
64 {
65         struct blobmsg_list_node *node, *tmp;
66         void *ptr;
67
68         avl_remove_all_elements(&list->avl, node, avl, tmp) {
69                 ptr = ((char *) node - list->node_offset);
70                 free(ptr);
71         }
72 }
73
74 bool
75 blobmsg_list_equal(struct blobmsg_list *l1, struct blobmsg_list *l2)
76 {
77         struct blobmsg_list_node *n1, *n2;
78         int count = l1->avl.count;
79
80         if (count != l2->avl.count)
81                 return false;
82
83         n1 = avl_first_element(&l1->avl, n1, avl);
84         n2 = avl_first_element(&l2->avl, n2, avl);
85
86         while (count-- > 0) {
87                 int len;
88
89                 len = blob_len(n1->data);
90                 if (len != blob_len(n2->data))
91                         return false;
92
93                 if (memcmp(n1->data, n2->data, len) != 0)
94                         return false;
95
96                 if (!count)
97                         break;
98
99                 n1 = avl_next_element(n1, avl);
100                 n2 = avl_next_element(n2, avl);
101         }
102
103         return true;
104 }