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 'sh'. Neither the node's key nor its value is freed;
129 * instead, ownership is transferred to the caller. Returns the node's key. */
131 smap_steal(struct smap *smap, struct smap_node *node)
133 char *key = node->key;
135 hmap_remove(&smap->map, &node->node);
140 /* Removes all key-value pairs from 'smap'. */
142 smap_clear(struct smap *smap)
144 struct smap_node *node, *next;
146 SMAP_FOR_EACH_SAFE (node, next, smap) {
147 smap_remove_node(smap, node);
151 /* Returns the value associated with 'key' in 'smap', or NULL. */
153 smap_get(const struct smap *smap, const char *key)
155 struct smap_node *node = smap_get_node(smap, key);
156 return node ? node->value : NULL;
159 /* Returns the node associated with 'key' in 'smap', or NULL. */
161 smap_get_node(const struct smap *smap, const char *key)
163 size_t key_len = strlen(key);
164 return smap_find__(smap, key, key_len, hash_bytes(key, key_len, 0));
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",
171 smap_get_bool(const struct smap *smap, const char *key, bool def)
173 const char *value = smap_get(smap, key);
180 return strcasecmp("false", value) != 0;
182 return !strcasecmp("true", value);
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'. */
189 smap_get_int(const struct smap *smap, const char *key, int def)
191 const char *value = smap_get(smap, key);
193 return value ? atoi(value) : def;
196 /* Returns true of there are no elements in 'smap'. */
198 smap_is_empty(const struct smap *smap)
200 return hmap_is_empty(&smap->map);
203 /* Returns the number of elements in 'smap'. */
205 smap_count(const struct smap *smap)
207 return hmap_count(&smap->map);
210 /* Initializes 'dst' as a clone of 'src. */
212 smap_clone(struct smap *dst, const struct smap *src)
214 const struct smap_node *node;
217 SMAP_FOR_EACH (node, src) {
218 smap_add__(dst, xstrdup(node->key), xstrdup(node->value),
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)
228 if (smap_is_empty(smap)) {
231 const struct smap_node **nodes;
232 struct smap_node *node;
235 n = smap_count(smap);
236 nodes = xmalloc(n * sizeof *nodes);
238 SMAP_FOR_EACH (node, smap) {
243 qsort(nodes, n, sizeof *nodes, compare_nodes_by_key);
249 /* Adds each of the key-value pairs from 'json' (which must be a JSON object
250 * whose values are strings) to 'smap'.
252 * The caller must have initialized 'smap'.
254 * The caller retains ownership of 'json' and everything in it. */
256 smap_from_json(struct smap *smap, const struct json *json)
258 const struct shash_node *node;
260 SHASH_FOR_EACH (node, json_object(json)) {
261 const struct json *value = node->data;
262 smap_add(smap, node->name, json_string(value));
266 /* Returns a JSON object that maps from the keys in 'smap' to their values.
268 * The caller owns the returned value and must eventually json_destroy() it.
270 * The caller retains ownership of 'smap' and everything in it. */
272 smap_to_json(const struct smap *smap)
274 const struct smap_node *node;
277 json = json_object_create();
278 SMAP_FOR_EACH (node, smap) {
279 json_object_put_string(json, node->key, node->value);
284 /* Private Helpers. */
286 static struct smap_node *
287 smap_add__(struct smap *smap, char *key, void *value, size_t hash)
289 struct smap_node *node = xmalloc(sizeof *node);
292 hmap_insert(&smap->map, &node->node, hash);
296 static struct smap_node *
297 smap_find__(const struct smap *smap, const char *key, size_t key_len,
300 struct smap_node *node;
302 HMAP_FOR_EACH_WITH_HASH (node, node, hash, &smap->map) {
303 if (!strncmp(node->key, key, key_len) && !node->key[key_len]) {
312 compare_nodes_by_key(const void *a_, const void *b_)
314 const struct smap_node *const *a = a_;
315 const struct smap_node *const *b = b_;
316 return strcmp((*a)->key, (*b)->key);