]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/smap.c
1 /* Copyright (c) 2012, 2014, 2015 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. */
25 static struct smap_node
*smap_add__(struct smap
*, char *, void *,
27 static struct smap_node
*smap_find__(const struct smap
*, const char *key
,
28 size_t key_len
, size_t hash
);
29 static int compare_nodes_by_key(const void *, const void *);
31 /* Public Functions. */
34 smap_init(struct smap
*smap
)
36 hmap_init(&smap
->map
);
40 smap_destroy(struct smap
*smap
)
44 hmap_destroy(&smap
->map
);
48 /* Adds 'key' paired with 'value' to 'smap'. It is the caller's responsibility
49 * to avoid duplicate keys if desirable. */
51 smap_add(struct smap
*smap
, const char *key
, const char *value
)
53 size_t key_len
= strlen(key
);
54 return smap_add__(smap
, xmemdup0(key
, key_len
), xstrdup(value
),
55 hash_bytes(key
, key_len
, 0));
58 /* Adds 'key' paired with 'value' to 'smap'. Takes ownership of 'key' and
59 * 'value' (which will eventually be freed with free()). It is the caller's
60 * responsibility to avoid duplicate keys if desirable. */
62 smap_add_nocopy(struct smap
*smap
, char *key
, char *value
)
64 return smap_add__(smap
, key
, value
, hash_bytes(key
, strlen(key
), 0));
67 /* Attempts to add 'key' to 'smap' associated with 'value'. If 'key' already
68 * exists in 'smap', does nothing and returns false. Otherwise, performs the
69 * addition and returns true. */
71 smap_add_once(struct smap
*smap
, const char *key
, const char *value
)
73 if (!smap_get(smap
, key
)) {
74 smap_add(smap
, key
, value
);
81 /* Adds 'key' paired with a value derived from 'format' (similar to printf).
82 * It is the caller's responsibility to avoid duplicate keys if desirable. */
84 smap_add_format(struct smap
*smap
, const char *key
, const char *format
, ...)
90 va_start(args
, format
);
91 value
= xvasprintf(format
, args
);
94 key_len
= strlen(key
);
95 smap_add__(smap
, xmemdup0(key
, key_len
), value
,
96 hash_bytes(key
, key_len
, 0));
99 /* Adds 'key' paired with a string representation of 'addr'. It is the
100 * caller's responsibility to avoid duplicate keys if desirable. */
102 smap_add_ipv6(struct smap
*smap
, const char *key
, struct in6_addr
*addr
)
104 char buf
[INET6_ADDRSTRLEN
];
105 ipv6_string_mapped(buf
, addr
);
106 smap_add(smap
, key
, buf
);
109 /* Searches for 'key' in 'smap'. If it does not already exists, adds it.
110 * Otherwise, changes its value to 'value'. */
112 smap_replace(struct smap
*smap
, const char *key
, const char *value
)
114 size_t key_len
= strlen(key
);
115 size_t hash
= hash_bytes(key
, key_len
, 0);
117 struct smap_node
*node
;
119 node
= smap_find__(smap
, key
, key_len
, hash
);
122 node
->value
= xstrdup(value
);
124 smap_add__(smap
, xmemdup0(key
, key_len
), xstrdup(value
), hash
);
128 /* If 'key' is in 'smap', removes it. Otherwise does nothing. */
130 smap_remove(struct smap
*smap
, const char *key
)
132 struct smap_node
*node
= smap_get_node(smap
, key
);
135 smap_remove_node(smap
, node
);
139 /* Removes 'node' from 'smap'. */
141 smap_remove_node(struct smap
*smap
, struct smap_node
*node
)
143 hmap_remove(&smap
->map
, &node
->node
);
149 /* Deletes 'node' from 'smap'.
151 * If 'keyp' is nonnull, stores the node's key in '*keyp' and transfers
152 * ownership to the caller. Otherwise, frees the node's key. Similarly for
153 * 'valuep' and the node's value. */
155 smap_steal(struct smap
*smap
, struct smap_node
*node
,
156 char **keyp
, char **valuep
)
165 *valuep
= node
->value
;
170 hmap_remove(&smap
->map
, &node
->node
);
174 /* Removes all key-value pairs from 'smap'. */
176 smap_clear(struct smap
*smap
)
178 struct smap_node
*node
, *next
;
180 SMAP_FOR_EACH_SAFE (node
, next
, smap
) {
181 smap_remove_node(smap
, node
);
185 /* Returns the value associated with 'key' in 'smap', or NULL. */
187 smap_get(const struct smap
*smap
, const char *key
)
189 struct smap_node
*node
= smap_get_node(smap
, key
);
190 return node
? node
->value
: NULL
;
193 /* Returns the node associated with 'key' in 'smap', or NULL. */
195 smap_get_node(const struct smap
*smap
, const char *key
)
197 size_t key_len
= strlen(key
);
198 return smap_find__(smap
, key
, key_len
, hash_bytes(key
, key_len
, 0));
201 /* Gets the value associated with 'key' in 'smap' and converts it to a boolean.
202 * If 'key' is not in 'smap', or its value is neither "true" nor "false",
205 smap_get_bool(const struct smap
*smap
, const char *key
, bool def
)
207 const char *value
= smap_get(smap
, key
);
214 return strcasecmp("false", value
) != 0;
216 return !strcasecmp("true", value
);
220 /* Gets the value associated with 'key' in 'smap' and converts it to an int
221 * using atoi(). If 'key' is not in 'smap', returns 'def'. */
223 smap_get_int(const struct smap
*smap
, const char *key
, int def
)
225 const char *value
= smap_get(smap
, key
);
227 return value
? atoi(value
) : def
;
230 /* Gets the value associated with 'key' in 'smap' and converts it to a UUID
231 * using uuid_from_string(). Returns true if successful, false if 'key' is not
232 * in 'smap' or if 'key' does not have the correct syntax for a UUID. */
234 smap_get_uuid(const struct smap
*smap
, const char *key
, struct uuid
*uuid
)
236 const char *value
= smap_get(smap
, key
);
237 return value
&& uuid_from_string(uuid
, value
);
240 /* Returns true of there are no elements in 'smap'. */
242 smap_is_empty(const struct smap
*smap
)
244 return hmap_is_empty(&smap
->map
);
247 /* Returns the number of elements in 'smap'. */
249 smap_count(const struct smap
*smap
)
251 return hmap_count(&smap
->map
);
254 /* Initializes 'dst' as a clone of 'src. */
256 smap_clone(struct smap
*dst
, const struct smap
*src
)
258 const struct smap_node
*node
;
261 SMAP_FOR_EACH (node
, src
) {
262 smap_add__(dst
, xstrdup(node
->key
), xstrdup(node
->value
),
267 /* Returns an array of nodes sorted on key or NULL if 'smap' is empty. The
268 * caller is responsible for freeing this array. */
269 const struct smap_node
**
270 smap_sort(const struct smap
*smap
)
272 if (smap_is_empty(smap
)) {
275 const struct smap_node
**nodes
;
276 struct smap_node
*node
;
279 n
= smap_count(smap
);
280 nodes
= xmalloc(n
* sizeof *nodes
);
282 SMAP_FOR_EACH (node
, smap
) {
287 qsort(nodes
, n
, sizeof *nodes
, compare_nodes_by_key
);
293 /* Adds each of the key-value pairs from 'json' (which must be a JSON object
294 * whose values are strings) to 'smap'.
296 * The caller must have initialized 'smap'.
298 * The caller retains ownership of 'json' and everything in it. */
300 smap_from_json(struct smap
*smap
, const struct json
*json
)
302 const struct shash_node
*node
;
304 SHASH_FOR_EACH (node
, json_object(json
)) {
305 const struct json
*value
= node
->data
;
306 smap_add(smap
, node
->name
, json_string(value
));
310 /* Returns a JSON object that maps from the keys in 'smap' to their values.
312 * The caller owns the returned value and must eventually json_destroy() it.
314 * The caller retains ownership of 'smap' and everything in it. */
316 smap_to_json(const struct smap
*smap
)
318 const struct smap_node
*node
;
321 json
= json_object_create();
322 SMAP_FOR_EACH (node
, smap
) {
323 json_object_put_string(json
, node
->key
, node
->value
);
328 /* Returns true if the two maps are equal, meaning that they have the same set
329 * of key-value pairs.
332 smap_equal(const struct smap
*smap1
, const struct smap
*smap2
)
334 if (smap_count(smap1
) != smap_count(smap2
)) {
338 const struct smap_node
*node
;
339 SMAP_FOR_EACH (node
, smap1
) {
340 const char *value2
= smap_get(smap2
, node
->key
);
341 if (!value2
|| strcmp(node
->value
, value2
)) {
348 /* Private Helpers. */
350 static struct smap_node
*
351 smap_add__(struct smap
*smap
, char *key
, void *value
, size_t hash
)
353 struct smap_node
*node
= xmalloc(sizeof *node
);
356 hmap_insert(&smap
->map
, &node
->node
, hash
);
360 static struct smap_node
*
361 smap_find__(const struct smap
*smap
, const char *key
, size_t key_len
,
364 struct smap_node
*node
;
366 HMAP_FOR_EACH_WITH_HASH (node
, node
, hash
, &smap
->map
) {
367 if (!strncmp(node
->key
, key
, key_len
) && !node
->key
[key_len
]) {
376 compare_nodes_by_key(const void *a_
, const void *b_
)
378 const struct smap_node
*const *a
= a_
;
379 const struct smap_node
*const *b
= b_
;
380 return strcmp((*a
)->key
, (*b
)->key
);