BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file BStringMap.c
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29  
30 #include <stdlib.h>
31 #include <string.h>
32  
33 #include <misc/offset.h>
34 #include <misc/compare.h>
35  
36 #include <stringmap/BStringMap.h>
37  
38 static int string_comparator (void *unused, char **str1, char **str2)
39 {
40 int c = strcmp(*str1, *str2);
41 return B_COMPARE(c, 0);
42 }
43  
44 static void free_entry (BStringMap *o, struct BStringMap_entry *e)
45 {
46 BAVL_Remove(&o->tree, &e->tree_node);
47 free(e->value);
48 free(e->key);
49 free(e);
50 }
51  
52 void BStringMap_Init (BStringMap *o)
53 {
54 // init tree
55 BAVL_Init(&o->tree, OFFSET_DIFF(struct BStringMap_entry, key, tree_node), (BAVL_comparator)string_comparator, NULL);
56  
57 DebugObject_Init(&o->d_obj);
58 }
59  
60 int BStringMap_InitCopy (BStringMap *o, const BStringMap *src)
61 {
62 BStringMap_Init(o);
63  
64 const char *key = BStringMap_First(src);
65 while (key) {
66 if (!BStringMap_Set(o, key, BStringMap_Get(src, key))) {
67 goto fail1;
68 }
69 key = BStringMap_Next(src, key);
70 }
71  
72 return 1;
73  
74 fail1:
75 BStringMap_Free(o);
76 return 0;
77 }
78  
79 void BStringMap_Free (BStringMap *o)
80 {
81 DebugObject_Free(&o->d_obj);
82  
83 // free entries
84 BAVLNode *tree_node;
85 while (tree_node = BAVL_GetFirst(&o->tree)) {
86 struct BStringMap_entry *e = UPPER_OBJECT(tree_node, struct BStringMap_entry, tree_node);
87 free_entry(o, e);
88 }
89 }
90  
91 const char * BStringMap_Get (const BStringMap *o, const char *key)
92 {
93 DebugObject_Access(&o->d_obj);
94 ASSERT(key)
95  
96 // lookup
97 BAVLNode *tree_node = BAVL_LookupExact(&o->tree, &key);
98 if (!tree_node) {
99 return NULL;
100 }
101 struct BStringMap_entry *e = UPPER_OBJECT(tree_node, struct BStringMap_entry, tree_node);
102  
103 return e->value;
104 }
105  
106 int BStringMap_Set (BStringMap *o, const char *key, const char *value)
107 {
108 DebugObject_Access(&o->d_obj);
109 ASSERT(key)
110 ASSERT(value)
111  
112 // alloc entry
113 struct BStringMap_entry *e = malloc(sizeof(*e));
114 if (!e) {
115 goto fail0;
116 }
117  
118 // alloc and set key
119 if (!(e->key = malloc(strlen(key) + 1))) {
120 goto fail1;
121 }
122 strcpy(e->key, key);
123  
124 // alloc and set value
125 if (!(e->value = malloc(strlen(value) + 1))) {
126 goto fail2;
127 }
128 strcpy(e->value, value);
129  
130 // try inserting to tree
131 BAVLNode *ex_tree_node;
132 if (!BAVL_Insert(&o->tree, &e->tree_node, &ex_tree_node)) {
133 // remove existing entry
134 struct BStringMap_entry *ex_e = UPPER_OBJECT(ex_tree_node, struct BStringMap_entry, tree_node);
135 free_entry(o, ex_e);
136  
137 // insert new node
138 ASSERT_EXECUTE(BAVL_Insert(&o->tree, &e->tree_node, NULL))
139 }
140  
141 return 1;
142  
143 fail2:
144 free(e->key);
145 fail1:
146 free(e);
147 fail0:
148 return 0;
149 }
150  
151 void BStringMap_Unset (BStringMap *o, const char *key)
152 {
153 DebugObject_Access(&o->d_obj);
154 ASSERT(key)
155  
156 // lookup
157 BAVLNode *tree_node = BAVL_LookupExact(&o->tree, &key);
158 if (!tree_node) {
159 return;
160 }
161 struct BStringMap_entry *e = UPPER_OBJECT(tree_node, struct BStringMap_entry, tree_node);
162  
163 // remove
164 free_entry(o, e);
165 }
166  
167 const char * BStringMap_First (const BStringMap *o)
168 {
169 DebugObject_Access(&o->d_obj);
170  
171 // get first
172 BAVLNode *tree_node = BAVL_GetFirst(&o->tree);
173 if (!tree_node) {
174 return NULL;
175 }
176 struct BStringMap_entry *e = UPPER_OBJECT(tree_node, struct BStringMap_entry, tree_node);
177  
178 return e->key;
179 }
180  
181 const char * BStringMap_Next (const BStringMap *o, const char *key)
182 {
183 DebugObject_Access(&o->d_obj);
184 ASSERT(key)
185 ASSERT(BAVL_LookupExact(&o->tree, &key))
186  
187 // get entry
188 struct BStringMap_entry *e = UPPER_OBJECT(BAVL_LookupExact(&o->tree, &key), struct BStringMap_entry, tree_node);
189  
190 // get next
191 BAVLNode *tree_node = BAVL_GetNext(&o->tree, &e->tree_node);
192 if (!tree_node) {
193 return NULL;
194 }
195 struct BStringMap_entry *next_e = UPPER_OBJECT(tree_node, struct BStringMap_entry, tree_node);
196  
197 return next_e->key;
198 }