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. */
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 /* 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. */
60 smap_add_once(struct smap *smap, const char *key, const char *value)
62 if (!smap_get(smap, key)) {
63 smap_add(smap, key, value);
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. */
73 smap_add_format(struct smap *smap, const char *key, const char *format, ...)
79 va_start(args, format);
80 value = xvasprintf(format, args);
83 key_len = strlen(key);
84 smap_add__(smap, xmemdup0(key, key_len), value,
85 hash_bytes(key, key_len, 0));
88 /* Searches for 'key' in 'smap'. If it does not already exists, adds it.
89 * Otherwise, changes its value to 'value'. */
91 smap_replace(struct smap *smap, const char *key, const char *value)
93 size_t key_len = strlen(key);
94 size_t hash = hash_bytes(key, key_len, 0);
96 struct smap_node *node;
98 node = smap_find__(smap, key, key_len, hash);
101 node->value = xstrdup(value);
103 smap_add__(smap, xmemdup0(key, key_len), xstrdup(value), hash);
107 /* If 'key' is in 'smap', removes it. Otherwise does nothing. */
109 smap_remove(struct smap *smap, const char *key)
111 struct smap_node *node = smap_get_node(smap, key);
114 smap_remove_node(smap, node);
118 /* Removes 'node' from 'smap'. */
120 smap_remove_node(struct smap *smap, struct smap_node *node)
122 hmap_remove(&smap->map, &node->node);
128 /* Deletes 'node' from 'smap'.
130 * If 'keyp' is nonnull, stores the node's key in '*keyp' and transfers
131 * ownership to the caller. Otherwise, frees the node's key. Similarly for
132 * 'valuep' and the node's value. */
134 smap_steal(struct smap *smap, struct smap_node *node,
135 char **keyp, char **valuep)
144 *valuep = node->value;
149 hmap_remove(&smap->map, &node->node);
153 /* Removes all key-value pairs from 'smap'. */
155 smap_clear(struct smap *smap)
157 struct smap_node *node, *next;
159 SMAP_FOR_EACH_SAFE (node, next, smap) {
160 smap_remove_node(smap, node);
164 /* Returns the value associated with 'key' in 'smap', or NULL. */
166 smap_get(const struct smap *smap, const char *key)
168 struct smap_node *node = smap_get_node(smap, key);
169 return node ? node->value : NULL;
172 /* Returns the node associated with 'key' in 'smap', or NULL. */
174 smap_get_node(const struct smap *smap, const char *key)
176 size_t key_len = strlen(key);
177 return smap_find__(smap, key, key_len, hash_bytes(key, key_len, 0));
180 /* Gets the value associated with 'key' in 'smap' and converts it to a boolean.
181 * If 'key' is not in 'smap', or its value is neither "true" nor "false",
184 smap_get_bool(const struct smap *smap, const char *key, bool def)
186 const char *value = smap_get(smap, key);
193 return strcasecmp("false", value) != 0;
195 return !strcasecmp("true", value);
199 /* Gets the value associated with 'key' in 'smap' and converts it to an int
200 * using atoi(). If 'key' is not in 'smap', returns 'def'. */
202 smap_get_int(const struct smap *smap, const char *key, int def)
204 const char *value = smap_get(smap, key);
206 return value ? atoi(value) : def;
209 /* Returns true of there are no elements in 'smap'. */
211 smap_is_empty(const struct smap *smap)
213 return hmap_is_empty(&smap->map);
216 /* Returns the number of elements in 'smap'. */
218 smap_count(const struct smap *smap)
220 return hmap_count(&smap->map);
223 /* Initializes 'dst' as a clone of 'src. */
225 smap_clone(struct smap *dst, const struct smap *src)
227 const struct smap_node *node;
230 SMAP_FOR_EACH (node, src) {
231 smap_add__(dst, xstrdup(node->key), xstrdup(node->value),
236 /* Returns an array of nodes sorted on key or NULL if 'smap' is empty. The
237 * caller is responsible for freeing this array. */
238 const struct smap_node **
239 smap_sort(const struct smap *smap)
241 if (smap_is_empty(smap)) {
244 const struct smap_node **nodes;
245 struct smap_node *node;
248 n = smap_count(smap);
249 nodes = xmalloc(n * sizeof *nodes);
251 SMAP_FOR_EACH (node, smap) {
256 qsort(nodes, n, sizeof *nodes, compare_nodes_by_key);
262 /* Adds each of the key-value pairs from 'json' (which must be a JSON object
263 * whose values are strings) to 'smap'.
265 * The caller must have initialized 'smap'.
267 * The caller retains ownership of 'json' and everything in it. */
269 smap_from_json(struct smap *smap, const struct json *json)
271 const struct shash_node *node;
273 SHASH_FOR_EACH (node, json_object(json)) {
274 const struct json *value = node->data;
275 smap_add(smap, node->name, json_string(value));
279 /* Returns a JSON object that maps from the keys in 'smap' to their values.
281 * The caller owns the returned value and must eventually json_destroy() it.
283 * The caller retains ownership of 'smap' and everything in it. */
285 smap_to_json(const struct smap *smap)
287 const struct smap_node *node;
290 json = json_object_create();
291 SMAP_FOR_EACH (node, smap) {
292 json_object_put_string(json, node->key, node->value);
297 /* Private Helpers. */
299 static struct smap_node *
300 smap_add__(struct smap *smap, char *key, void *value, size_t hash)
302 struct smap_node *node = xmalloc(sizeof *node);
305 hmap_insert(&smap->map, &node->node, hash);
309 static struct smap_node *
310 smap_find__(const struct smap *smap, const char *key, size_t key_len,
313 struct smap_node *node;
315 HMAP_FOR_EACH_WITH_HASH (node, node, hash, &smap->map) {
316 if (!strncmp(node->key, key, key_len) && !node->key[key_len]) {
325 compare_nodes_by_key(const void *a_, const void *b_)
327 const struct smap_node *const *a = a_;
328 const struct smap_node *const *b = b_;
329 return strcmp((*a)->key, (*b)->key);