e612ac7bad7749bf1a8212cf98af2e91ad177704
[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 /* Removes all key-value pairs from 'smap'. */
129 void
130 smap_clear(struct smap *smap)
131 {
132     struct smap_node *node, *next;
133
134     SMAP_FOR_EACH_SAFE (node, next, smap) {
135         smap_remove_node(smap, node);
136     }
137 }
138
139 /* Returns the value associated with 'key' in 'smap', or NULL. */
140 const char *
141 smap_get(const struct smap *smap, const char *key)
142 {
143     struct smap_node *node = smap_get_node(smap, key);
144     return node ? node->value : NULL;
145 }
146
147 /* Returns the node associated with 'key' in 'smap', or NULL. */
148 struct smap_node *
149 smap_get_node(const struct smap *smap, const char *key)
150 {
151     size_t key_len = strlen(key);
152     return smap_find__(smap, key, key_len, hash_bytes(key, key_len, 0));
153 }
154
155 /* Gets the value associated with 'key' in 'smap' and converts it to a boolean.
156  * If 'key' is not in 'smap', or its value is neither "true" nor "false",
157  * returns 'def'. */
158 bool
159 smap_get_bool(const struct smap *smap, const char *key, bool def)
160 {
161     const char *value = smap_get(smap, key);
162
163     if (!value) {
164         return def;
165     }
166
167     if (def) {
168         return strcasecmp("false", value) != 0;
169     } else {
170         return !strcasecmp("true", value);
171     }
172 }
173
174 /* Gets the value associated with 'key' in 'smap' and converts it to an int
175  * using atoi().  If 'key' is not in 'smap', returns 'def'. */
176 int
177 smap_get_int(const struct smap *smap, const char *key, int def)
178 {
179     const char *value = smap_get(smap, key);
180
181     return value ? atoi(value) : def;
182 }
183
184 /* Returns true of there are no elements in 'smap'. */
185 bool
186 smap_is_empty(const struct smap *smap)
187 {
188     return hmap_is_empty(&smap->map);
189 }
190
191 /* Returns the number of elements in 'smap'. */
192 size_t
193 smap_count(const struct smap *smap)
194 {
195     return hmap_count(&smap->map);
196 }
197
198 /* Initializes 'dst' as a clone of 'src. */
199 void
200 smap_clone(struct smap *dst, const struct smap *src)
201 {
202     const struct smap_node *node;
203
204     smap_init(dst);
205     SMAP_FOR_EACH (node, src) {
206         smap_add__(dst, xstrdup(node->key), xstrdup(node->value),
207                    node->node.hash);
208     }
209 }
210
211 /* Returns an array of nodes sorted on key or NULL if 'smap' is empty.  The
212  * caller is responsible for freeing this array. */
213 const struct smap_node **
214 smap_sort(const struct smap *smap)
215 {
216     if (smap_is_empty(smap)) {
217         return NULL;
218     } else {
219         const struct smap_node **nodes;
220         struct smap_node *node;
221         size_t i, n;
222
223         n = smap_count(smap);
224         nodes = xmalloc(n * sizeof *nodes);
225         i = 0;
226         SMAP_FOR_EACH (node, smap) {
227             nodes[i++] = node;
228         }
229         assert(i == n);
230
231         qsort(nodes, n, sizeof *nodes, compare_nodes_by_key);
232
233         return nodes;
234     }
235 }
236
237 /* Adds each of the key-value pairs from 'json' (which must be a JSON object
238  * whose values are strings) to 'smap'.
239  *
240  * The caller must have initialized 'smap'.
241  *
242  * The caller retains ownership of 'json' and everything in it. */
243 void
244 smap_from_json(struct smap *smap, const struct json *json)
245 {
246     const struct shash_node *node;
247
248     SHASH_FOR_EACH (node, json_object(json)) {
249         const struct json *value = node->data;
250         smap_add(smap, node->name, json_string(value));
251     }
252 }
253
254 /* Returns a JSON object that maps from the keys in 'smap' to their values.
255  *
256  * The caller owns the returned value and must eventually json_destroy() it.
257  *
258  * The caller retains ownership of 'smap' and everything in it. */
259 struct json *
260 smap_to_json(const struct smap *smap)
261 {
262     const struct smap_node *node;
263     struct json *json;
264
265     json = json_object_create();
266     SMAP_FOR_EACH (node, smap) {
267         json_object_put_string(json, node->key, node->value);
268     }
269     return json;
270 }
271 \f
272 /* Private Helpers. */
273
274 static struct smap_node *
275 smap_add__(struct smap *smap, char *key, void *value, size_t hash)
276 {
277     struct smap_node *node = xmalloc(sizeof *node);
278     node->key = key;
279     node->value = value;
280     hmap_insert(&smap->map, &node->node, hash);
281     return node;
282 }
283
284 static struct smap_node *
285 smap_find__(const struct smap *smap, const char *key, size_t key_len,
286             size_t hash)
287 {
288     struct smap_node *node;
289
290     HMAP_FOR_EACH_WITH_HASH (node, node, hash, &smap->map) {
291         if (!strncmp(node->key, key, key_len) && !node->key[key_len]) {
292             return node;
293         }
294     }
295
296     return NULL;
297 }
298
299 static int
300 compare_nodes_by_key(const void *a_, const void *b_)
301 {
302     const struct smap_node *const *a = a_;
303     const struct smap_node *const *b = b_;
304     return strcmp((*a)->key, (*b)->key);
305 }