1 /* Copyright (c) 2012, 2014 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. */
23 static struct smap_node *smap_add__(struct smap *, char *, void *,
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 *);
29 /* Public Functions. */
32 smap_init(struct smap *smap)
34 hmap_init(&smap->map);
38 smap_destroy(struct smap *smap)
42 hmap_destroy(&smap->map);
46 /* Adds 'key' paired with 'value' to 'smap'. It is the caller's responsibility
47 * to avoid duplicate keys if desirable. */
49 smap_add(struct smap *smap, const char *key, const char *value)
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));
56 /* Adds 'key' paired with 'value' to 'smap'. Takes ownership of 'key' and
57 * 'value' (which will eventually be freed with free()). It is the caller's
58 * responsibility to avoid duplicate keys if desirable. */
60 smap_add_nocopy(struct smap *smap, char *key, char *value)
62 return smap_add__(smap, key, value, hash_bytes(key, strlen(key), 0));
65 /* Attempts to add 'key' to 'smap' associated with 'value'. If 'key' already
66 * exists in 'smap', does nothing and returns false. Otherwise, performs the
67 * addition and returns true. */
69 smap_add_once(struct smap *smap, const char *key, const char *value)
71 if (!smap_get(smap, key)) {
72 smap_add(smap, key, value);
79 /* Adds 'key' paired with a value derived from 'format' (similar to printf).
80 * It is the caller's responsibility to avoid duplicate keys if desirable. */
82 smap_add_format(struct smap *smap, const char *key, const char *format, ...)
88 va_start(args, format);
89 value = xvasprintf(format, args);
92 key_len = strlen(key);
93 smap_add__(smap, xmemdup0(key, key_len), value,
94 hash_bytes(key, key_len, 0));
97 /* Searches for 'key' in 'smap'. If it does not already exists, adds it.
98 * Otherwise, changes its value to 'value'. */
100 smap_replace(struct smap *smap, const char *key, const char *value)
102 size_t key_len = strlen(key);
103 size_t hash = hash_bytes(key, key_len, 0);
105 struct smap_node *node;
107 node = smap_find__(smap, key, key_len, hash);
110 node->value = xstrdup(value);
112 smap_add__(smap, xmemdup0(key, key_len), xstrdup(value), hash);
116 /* If 'key' is in 'smap', removes it. Otherwise does nothing. */
118 smap_remove(struct smap *smap, const char *key)
120 struct smap_node *node = smap_get_node(smap, key);
123 smap_remove_node(smap, node);
127 /* Removes 'node' from 'smap'. */
129 smap_remove_node(struct smap *smap, struct smap_node *node)
131 hmap_remove(&smap->map, &node->node);
137 /* Deletes 'node' from 'smap'.
139 * If 'keyp' is nonnull, stores the node's key in '*keyp' and transfers
140 * ownership to the caller. Otherwise, frees the node's key. Similarly for
141 * 'valuep' and the node's value. */
143 smap_steal(struct smap *smap, struct smap_node *node,
144 char **keyp, char **valuep)
153 *valuep = node->value;
158 hmap_remove(&smap->map, &node->node);
162 /* Removes all key-value pairs from 'smap'. */
164 smap_clear(struct smap *smap)
166 struct smap_node *node, *next;
168 SMAP_FOR_EACH_SAFE (node, next, smap) {
169 smap_remove_node(smap, node);
173 /* Returns the value associated with 'key' in 'smap', or NULL. */
175 smap_get(const struct smap *smap, const char *key)
177 struct smap_node *node = smap_get_node(smap, key);
178 return node ? node->value : NULL;
181 /* Returns the node associated with 'key' in 'smap', or NULL. */
183 smap_get_node(const struct smap *smap, const char *key)
185 size_t key_len = strlen(key);
186 return smap_find__(smap, key, key_len, hash_bytes(key, key_len, 0));
189 /* Gets the value associated with 'key' in 'smap' and converts it to a boolean.
190 * If 'key' is not in 'smap', or its value is neither "true" nor "false",
193 smap_get_bool(const struct smap *smap, const char *key, bool def)
195 const char *value = smap_get(smap, key);
202 return strcasecmp("false", value) != 0;
204 return !strcasecmp("true", value);
208 /* Gets the value associated with 'key' in 'smap' and converts it to an int
209 * using atoi(). If 'key' is not in 'smap', returns 'def'. */
211 smap_get_int(const struct smap *smap, const char *key, int def)
213 const char *value = smap_get(smap, key);
215 return value ? atoi(value) : def;
218 /* Returns true of there are no elements in 'smap'. */
220 smap_is_empty(const struct smap *smap)
222 return hmap_is_empty(&smap->map);
225 /* Returns the number of elements in 'smap'. */
227 smap_count(const struct smap *smap)
229 return hmap_count(&smap->map);
232 /* Initializes 'dst' as a clone of 'src. */
234 smap_clone(struct smap *dst, const struct smap *src)
236 const struct smap_node *node;
239 SMAP_FOR_EACH (node, src) {
240 smap_add__(dst, xstrdup(node->key), xstrdup(node->value),
245 /* Returns an array of nodes sorted on key or NULL if 'smap' is empty. The
246 * caller is responsible for freeing this array. */
247 const struct smap_node **
248 smap_sort(const struct smap *smap)
250 if (smap_is_empty(smap)) {
253 const struct smap_node **nodes;
254 struct smap_node *node;
257 n = smap_count(smap);
258 nodes = xmalloc(n * sizeof *nodes);
260 SMAP_FOR_EACH (node, smap) {
265 qsort(nodes, n, sizeof *nodes, compare_nodes_by_key);
271 /* Adds each of the key-value pairs from 'json' (which must be a JSON object
272 * whose values are strings) to 'smap'.
274 * The caller must have initialized 'smap'.
276 * The caller retains ownership of 'json' and everything in it. */
278 smap_from_json(struct smap *smap, const struct json *json)
280 const struct shash_node *node;
282 SHASH_FOR_EACH (node, json_object(json)) {
283 const struct json *value = node->data;
284 smap_add(smap, node->name, json_string(value));
288 /* Returns a JSON object that maps from the keys in 'smap' to their values.
290 * The caller owns the returned value and must eventually json_destroy() it.
292 * The caller retains ownership of 'smap' and everything in it. */
294 smap_to_json(const struct smap *smap)
296 const struct smap_node *node;
299 json = json_object_create();
300 SMAP_FOR_EACH (node, smap) {
301 json_object_put_string(json, node->key, node->value);
306 /* Private Helpers. */
308 static struct smap_node *
309 smap_add__(struct smap *smap, char *key, void *value, size_t hash)
311 struct smap_node *node = xmalloc(sizeof *node);
314 hmap_insert(&smap->map, &node->node, hash);
318 static struct smap_node *
319 smap_find__(const struct smap *smap, const char *key, size_t key_len,
322 struct smap_node *node;
324 HMAP_FOR_EACH_WITH_HASH (node, node, hash, &smap->map) {
325 if (!strncmp(node->key, key, key_len) && !node->key[key_len]) {
334 compare_nodes_by_key(const void *a_, const void *b_)
336 const struct smap_node *const *a = a_;
337 const struct smap_node *const *b = b_;
338 return strcmp((*a)->key, (*b)->key);