1 /* Copyright (c) 2012 Nicira, Inc.
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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. */
21 static struct smap_node *smap_add__(struct smap *, char *, void *,
23 static struct smap_node *smap_find__(const struct smap *, const char *key,
24 size_t key_len, size_t hash);
25 static int compare_nodes_by_key(const void *, const void *);
27 /* Public Functions. */
30 smap_init(struct smap *smap)
32 hmap_init(&smap->map);
36 smap_destroy(struct smap *smap)
40 hmap_destroy(&smap->map);
44 /* Adds 'key' paired with 'value' to 'smap'. It is the caller's responsibility
45 * to avoid duplicate keys if desirable. */
47 smap_add(struct smap *smap, const char *key, const char *value)
49 size_t key_len = strlen(key);
50 return smap_add__(smap, xmemdup0(key, key_len), xstrdup(value),
51 hash_bytes(key, key_len, 0));
54 /* Attempts to add 'key' to 'smap' associated with 'value'. If 'key' already
55 * exists in 'smap', does nothing and returns false. Otherwise, performs the
56 * addition and returns true. */
58 smap_add_once(struct smap *smap, const char *key, const char *value)
60 if (!smap_get(smap, key)) {
61 smap_add(smap, key, value);
68 /* Adds 'key' paired with a value derived from 'format' (similar to printf).
69 * It is the caller's responsibility to avoid duplicate keys if desirable. */
71 smap_add_format(struct smap *smap, const char *key, const char *format, ...)
77 va_start(args, format);
78 value = xvasprintf(format, args);
81 key_len = strlen(key);
82 smap_add__(smap, xmemdup0(key, key_len), value,
83 hash_bytes(key, key_len, 0));
86 /* Searches for 'key' in 'smap'. If it does not already exists, adds it.
87 * Otherwise, changes its value to 'value'. */
89 smap_replace(struct smap *smap, const char *key, const char *value)
91 size_t key_len = strlen(key);
92 size_t hash = hash_bytes(key, key_len, 0);
94 struct smap_node *node;
96 node = smap_find__(smap, key, key_len, hash);
99 node->value = xstrdup(value);
101 smap_add__(smap, xmemdup0(key, key_len), xstrdup(value), hash);
105 /* If 'key' is in 'smap', removes it. Otherwise does nothing. */
107 smap_remove(struct smap *smap, const char *key)
109 struct smap_node *node = smap_get_node(smap, key);
112 smap_remove_node(smap, node);
116 /* Removes 'node' from 'smap'. */
118 smap_remove_node(struct smap *smap, struct smap_node *node)
120 hmap_remove(&smap->map, &node->node);
126 /* Deletes 'node' from 'smap'.
128 * If 'keyp' is nonnull, stores the node's key in '*keyp' and transfers
129 * ownership to the caller. Otherwise, frees the node's key. Similarly for
130 * 'valuep' and the node's value. */
132 smap_steal(struct smap *smap, struct smap_node *node,
133 char **keyp, char **valuep)
142 *valuep = node->value;
147 hmap_remove(&smap->map, &node->node);
151 /* Removes all key-value pairs from 'smap'. */
153 smap_clear(struct smap *smap)
155 struct smap_node *node, *next;
157 SMAP_FOR_EACH_SAFE (node, next, smap) {
158 smap_remove_node(smap, node);
162 /* Returns the value associated with 'key' in 'smap', or NULL. */
164 smap_get(const struct smap *smap, const char *key)
166 struct smap_node *node = smap_get_node(smap, key);
167 return node ? node->value : NULL;
170 /* Returns the node associated with 'key' in 'smap', or NULL. */
172 smap_get_node(const struct smap *smap, const char *key)
174 size_t key_len = strlen(key);
175 return smap_find__(smap, key, key_len, hash_bytes(key, key_len, 0));
178 /* Gets the value associated with 'key' in 'smap' and converts it to a boolean.
179 * If 'key' is not in 'smap', or its value is neither "true" nor "false",
182 smap_get_bool(const struct smap *smap, const char *key, bool def)
184 const char *value = smap_get(smap, key);
191 return strcasecmp("false", value) != 0;
193 return !strcasecmp("true", value);
197 /* Gets the value associated with 'key' in 'smap' and converts it to an int
198 * using atoi(). If 'key' is not in 'smap', returns 'def'. */
200 smap_get_int(const struct smap *smap, const char *key, int def)
202 const char *value = smap_get(smap, key);
204 return value ? atoi(value) : def;
207 /* Returns true of there are no elements in 'smap'. */
209 smap_is_empty(const struct smap *smap)
211 return hmap_is_empty(&smap->map);
214 /* Returns the number of elements in 'smap'. */
216 smap_count(const struct smap *smap)
218 return hmap_count(&smap->map);
221 /* Initializes 'dst' as a clone of 'src. */
223 smap_clone(struct smap *dst, const struct smap *src)
225 const struct smap_node *node;
228 SMAP_FOR_EACH (node, src) {
229 smap_add__(dst, xstrdup(node->key), xstrdup(node->value),
234 /* Returns an array of nodes sorted on key or NULL if 'smap' is empty. The
235 * caller is responsible for freeing this array. */
236 const struct smap_node **
237 smap_sort(const struct smap *smap)
239 if (smap_is_empty(smap)) {
242 const struct smap_node **nodes;
243 struct smap_node *node;
246 n = smap_count(smap);
247 nodes = xmalloc(n * sizeof *nodes);
249 SMAP_FOR_EACH (node, smap) {
254 qsort(nodes, n, sizeof *nodes, compare_nodes_by_key);
260 /* Adds each of the key-value pairs from 'json' (which must be a JSON object
261 * whose values are strings) to 'smap'.
263 * The caller must have initialized 'smap'.
265 * The caller retains ownership of 'json' and everything in it. */
267 smap_from_json(struct smap *smap, const struct json *json)
269 const struct shash_node *node;
271 SHASH_FOR_EACH (node, json_object(json)) {
272 const struct json *value = node->data;
273 smap_add(smap, node->name, json_string(value));
277 /* Returns a JSON object that maps from the keys in 'smap' to their values.
279 * The caller owns the returned value and must eventually json_destroy() it.
281 * The caller retains ownership of 'smap' and everything in it. */
283 smap_to_json(const struct smap *smap)
285 const struct smap_node *node;
288 json = json_object_create();
289 SMAP_FOR_EACH (node, smap) {
290 json_object_put_string(json, node->key, node->value);
295 /* Private Helpers. */
297 static struct smap_node *
298 smap_add__(struct smap *smap, char *key, void *value, size_t hash)
300 struct smap_node *node = xmalloc(sizeof *node);
303 hmap_insert(&smap->map, &node->node, hash);
307 static struct smap_node *
308 smap_find__(const struct smap *smap, const char *key, size_t key_len,
311 struct smap_node *node;
313 HMAP_FOR_EACH_WITH_HASH (node, node, hash, &smap->map) {
314 if (!strncmp(node->key, key, key_len) && !node->key[key_len]) {
323 compare_nodes_by_key(const void *a_, const void *b_)
325 const struct smap_node *const *a = a_;
326 const struct smap_node *const *b = b_;
327 return strcmp((*a)->key, (*b)->key);