ff785981226425d7a42dd34e44e44d2ea617a295
[openvswitch] / lib / smap.c
1 /* Copyright (c) 2012 Nicira, Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License. */
14
15 #include <config.h>
16 #include "smap.h"
17
18 #include <assert.h>
19
20 #include "hash.h"
21 #include "json.h"
22
23 static struct smap_node *smap_add__(struct smap *, char *, void *,
24                                     size_t hash);
25 static struct smap_node *smap_find__(const struct smap *, const char *key,
26                                      size_t key_len, size_t hash);
27 static int compare_nodes_by_key(const void *, const void *);
28 \f
29 /* Public Functions. */
30
31 void
32 smap_init(struct smap *smap)
33 {
34     hmap_init(&smap->map);
35 }
36
37 void
38 smap_destroy(struct smap *smap)
39 {
40     if (smap) {
41         smap_clear(smap);
42         hmap_destroy(&smap->map);
43     }
44 }
45
46 /* Adds 'key' paired with 'value' to 'smap'.  It is the caller's responsibility
47  * to avoid duplicate keys if desirable. */
48 struct smap_node *
49 smap_add(struct smap *smap, const char *key, const char *value)
50 {
51     size_t key_len = strlen(key);
52     return smap_add__(smap, xmemdup0(key, key_len), xstrdup(value),
53                       hash_bytes(key, key_len, 0));
54 }
55
56 /* Attempts to add 'key' to 'smap' associated with 'value'.  If 'key' already
57  * exists in 'smap', does nothing and returns false.  Otherwise, performs the
58  * addition and returns true. */
59 bool
60 smap_add_once(struct smap *smap, const char *key, const char *value)
61 {
62     if (!smap_get(smap, key)) {
63         smap_add(smap, key, value);
64         return true;
65     } else {
66         return false;
67     }
68 }
69
70 /* Adds 'key' paired with a value derived from 'format' (similar to printf).
71  * It is the caller's responsibility to avoid duplicate keys if desirable. */
72 void
73 smap_add_format(struct smap *smap, const char *key, const char *format, ...)
74 {
75     size_t key_len;
76     va_list args;
77     char *value;
78
79     va_start(args, format);
80     value = xvasprintf(format, args);
81     va_end(args);
82
83     key_len = strlen(key);
84     smap_add__(smap, xmemdup0(key, key_len), value,
85                hash_bytes(key, key_len, 0));
86 }
87
88 /* Searches for 'key' in 'smap'.  If it does not already exists, adds it.
89  * Otherwise, changes its value to 'value'. */
90 void
91 smap_replace(struct smap *smap, const char *key, const char *value)
92 {
93     size_t  key_len = strlen(key);
94     size_t hash = hash_bytes(key, key_len, 0);
95
96     struct smap_node *node;
97
98     node = smap_find__(smap, key, key_len, hash);
99     if (node) {
100         free(node->value);
101         node->value = xstrdup(value);
102     } else {
103         smap_add__(smap, xmemdup0(key, key_len), xstrdup(value), hash);
104     }
105 }
106
107 /* If 'key' is in 'smap', removes it.  Otherwise does nothing. */
108 void
109 smap_remove(struct smap *smap, const char *key)
110 {
111     struct smap_node *node = smap_get_node(smap, key);
112
113     if (node) {
114         smap_remove_node(smap, node);
115     }
116 }
117
118 /* Removes 'node' from 'smap'. */
119 void
120 smap_remove_node(struct smap *smap, struct smap_node *node)
121 {
122     hmap_remove(&smap->map, &node->node);
123     free(node->key);
124     free(node->value);
125     free(node);
126 }
127
128 /* Deletes 'node' from 'sh'.  Neither the node's key nor its value is freed;
129  * instead, ownership is transferred to the caller.  Returns the node's key. */
130 char *
131 smap_steal(struct smap *smap, struct smap_node *node)
132 {
133     char *key = node->key;
134
135     hmap_remove(&smap->map, &node->node);
136     free(node);
137     return key;
138 }
139
140 /* Removes all key-value pairs from 'smap'. */
141 void
142 smap_clear(struct smap *smap)
143 {
144     struct smap_node *node, *next;
145
146     SMAP_FOR_EACH_SAFE (node, next, smap) {
147         smap_remove_node(smap, node);
148     }
149 }
150
151 /* Returns the value associated with 'key' in 'smap', or NULL. */
152 const char *
153 smap_get(const struct smap *smap, const char *key)
154 {
155     struct smap_node *node = smap_get_node(smap, key);
156     return node ? node->value : NULL;
157 }
158
159 /* Returns the node associated with 'key' in 'smap', or NULL. */
160 struct smap_node *
161 smap_get_node(const struct smap *smap, const char *key)
162 {
163     size_t key_len = strlen(key);
164     return smap_find__(smap, key, key_len, hash_bytes(key, key_len, 0));
165 }
166
167 /* Gets the value associated with 'key' in 'smap' and converts it to a boolean.
168  * If 'key' is not in 'smap', or its value is neither "true" nor "false",
169  * returns 'def'. */
170 bool
171 smap_get_bool(const struct smap *smap, const char *key, bool def)
172 {
173     const char *value = smap_get(smap, key);
174
175     if (!value) {
176         return def;
177     }
178
179     if (def) {
180         return strcasecmp("false", value) != 0;
181     } else {
182         return !strcasecmp("true", value);
183     }
184 }
185
186 /* Gets the value associated with 'key' in 'smap' and converts it to an int
187  * using atoi().  If 'key' is not in 'smap', returns 'def'. */
188 int
189 smap_get_int(const struct smap *smap, const char *key, int def)
190 {
191     const char *value = smap_get(smap, key);
192
193     return value ? atoi(value) : def;
194 }
195
196 /* Returns true of there are no elements in 'smap'. */
197 bool
198 smap_is_empty(const struct smap *smap)
199 {
200     return hmap_is_empty(&smap->map);
201 }
202
203 /* Returns the number of elements in 'smap'. */
204 size_t
205 smap_count(const struct smap *smap)
206 {
207     return hmap_count(&smap->map);
208 }
209
210 /* Initializes 'dst' as a clone of 'src. */
211 void
212 smap_clone(struct smap *dst, const struct smap *src)
213 {
214     const struct smap_node *node;
215
216     smap_init(dst);
217     SMAP_FOR_EACH (node, src) {
218         smap_add__(dst, xstrdup(node->key), xstrdup(node->value),
219                    node->node.hash);
220     }
221 }
222
223 /* Returns an array of nodes sorted on key or NULL if 'smap' is empty.  The
224  * caller is responsible for freeing this array. */
225 const struct smap_node **
226 smap_sort(const struct smap *smap)
227 {
228     if (smap_is_empty(smap)) {
229         return NULL;
230     } else {
231         const struct smap_node **nodes;
232         struct smap_node *node;
233         size_t i, n;
234
235         n = smap_count(smap);
236         nodes = xmalloc(n * sizeof *nodes);
237         i = 0;
238         SMAP_FOR_EACH (node, smap) {
239             nodes[i++] = node;
240         }
241         assert(i == n);
242
243         qsort(nodes, n, sizeof *nodes, compare_nodes_by_key);
244
245         return nodes;
246     }
247 }
248
249 /* Adds each of the key-value pairs from 'json' (which must be a JSON object
250  * whose values are strings) to 'smap'.
251  *
252  * The caller must have initialized 'smap'.
253  *
254  * The caller retains ownership of 'json' and everything in it. */
255 void
256 smap_from_json(struct smap *smap, const struct json *json)
257 {
258     const struct shash_node *node;
259
260     SHASH_FOR_EACH (node, json_object(json)) {
261         const struct json *value = node->data;
262         smap_add(smap, node->name, json_string(value));
263     }
264 }
265
266 /* Returns a JSON object that maps from the keys in 'smap' to their values.
267  *
268  * The caller owns the returned value and must eventually json_destroy() it.
269  *
270  * The caller retains ownership of 'smap' and everything in it. */
271 struct json *
272 smap_to_json(const struct smap *smap)
273 {
274     const struct smap_node *node;
275     struct json *json;
276
277     json = json_object_create();
278     SMAP_FOR_EACH (node, smap) {
279         json_object_put_string(json, node->key, node->value);
280     }
281     return json;
282 }
283 \f
284 /* Private Helpers. */
285
286 static struct smap_node *
287 smap_add__(struct smap *smap, char *key, void *value, size_t hash)
288 {
289     struct smap_node *node = xmalloc(sizeof *node);
290     node->key = key;
291     node->value = value;
292     hmap_insert(&smap->map, &node->node, hash);
293     return node;
294 }
295
296 static struct smap_node *
297 smap_find__(const struct smap *smap, const char *key, size_t key_len,
298             size_t hash)
299 {
300     struct smap_node *node;
301
302     HMAP_FOR_EACH_WITH_HASH (node, node, hash, &smap->map) {
303         if (!strncmp(node->key, key, key_len) && !node->key[key_len]) {
304             return node;
305         }
306     }
307
308     return NULL;
309 }
310
311 static int
312 compare_nodes_by_key(const void *a_, const void *b_)
313 {
314     const struct smap_node *const *a = a_;
315     const struct smap_node *const *b = b_;
316     return strcmp((*a)->key, (*b)->key);
317 }