]>
Commit | Line | Data |
---|---|---|
bc8d7dfa | 1 | /* Copyright (c) 2012, 2014, 2015, 2016 Nicira, Inc. |
79f1cbe9 EJ |
2 | * |
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: | |
6 | * | |
7 | * http://www.apache.org/licenses/LICENSE-2.0 | |
8 | * | |
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. */ | |
14 | ||
15 | #ifndef SMAP_H | |
16 | #define SMAP_H 1 | |
17 | ||
b4f6e930 | 18 | #include <netinet/in.h> |
79f1cbe9 EJ |
19 | #include "hmap.h" |
20 | ||
cccc1356 | 21 | struct json; |
d65467f1 | 22 | struct uuid; |
cccc1356 | 23 | |
79f1cbe9 EJ |
24 | /* A map from string to string. */ |
25 | struct smap { | |
26 | struct hmap map; /* Contains "struct smap_node"s. */ | |
27 | }; | |
28 | ||
29 | struct smap_node { | |
30 | struct hmap_node node; /* In struct smap's 'map' hmap. */ | |
31 | char *key; | |
32 | char *value; | |
33 | }; | |
34 | ||
35 | #define SMAP_INITIALIZER(SMAP) { HMAP_INITIALIZER(&(SMAP)->map) } | |
36 | ||
bc8d7dfa BP |
37 | #define SMAP_FOR_EACH(SMAP_NODE, SMAP) \ |
38 | HMAP_FOR_EACH_INIT (SMAP_NODE, node, &(SMAP)->map, \ | |
39 | BUILD_ASSERT_TYPE(SMAP_NODE, struct smap_node *), \ | |
40 | BUILD_ASSERT_TYPE(SMAP, struct smap *)) | |
41 | ||
42 | #define SMAP_FOR_EACH_SAFE(SMAP_NODE, NEXT, SMAP) \ | |
43 | HMAP_FOR_EACH_SAFE_INIT ( \ | |
44 | SMAP_NODE, NEXT, node, &(SMAP)->map, \ | |
45 | BUILD_ASSERT_TYPE(SMAP_NODE, struct smap_node *), \ | |
46 | BUILD_ASSERT_TYPE(NEXT, struct smap_node *), \ | |
47 | BUILD_ASSERT_TYPE(SMAP, struct smap *)) | |
79f1cbe9 | 48 | |
aaf881c6 BP |
49 | /* Initializer for an immutable struct smap 'SMAP' that contains a single |
50 | * 'KEY'-'VALUE' pair, e.g. | |
51 | * | |
52 | * const struct smap smap = SMAP1_CONST1(&smap, "key", "value"); | |
53 | * | |
54 | * An smap initialized this way must not be modified or destroyed. | |
55 | * | |
56 | * The 'KEY' argument is evaluated multiple times. | |
57 | */ | |
58 | #define SMAP_CONST1(SMAP, KEY, VALUE) { \ | |
59 | HMAP_CONST1(&(SMAP)->map, \ | |
60 | (&(struct smap_node) SMAP_NODE_INIT(KEY, VALUE).node)) \ | |
61 | } | |
62 | #define SMAP_NODE_INIT(KEY, VALUE) { \ | |
63 | HMAP_NODE_INIT(hash_string(KEY, 0)), \ | |
64 | CONST_CAST(char *, KEY), \ | |
65 | CONST_CAST(char *, VALUE) } | |
66 | ||
67 | ||
79f1cbe9 EJ |
68 | void smap_init(struct smap *); |
69 | void smap_destroy(struct smap *); | |
70 | ||
71 | struct smap_node *smap_add(struct smap *, const char *, const char *); | |
ff929935 | 72 | struct smap_node *smap_add_nocopy(struct smap *, char *, char *); |
79f1cbe9 EJ |
73 | bool smap_add_once(struct smap *, const char *, const char *); |
74 | void smap_add_format(struct smap *, const char *key, const char *, ...) | |
cab50449 | 75 | OVS_PRINTF_FORMAT(3, 4); |
9835576b | 76 | void smap_add_ipv6(struct smap *, const char *, struct in6_addr *); |
79f1cbe9 EJ |
77 | void smap_replace(struct smap *, const char *, const char *); |
78 | ||
79 | void smap_remove(struct smap *, const char *); | |
51c82a49 | 80 | void smap_remove_node(struct smap *, struct smap_node *); |
57c8677b | 81 | void smap_steal(struct smap *, struct smap_node *, char **keyp, char **valuep); |
79f1cbe9 EJ |
82 | void smap_clear(struct smap *); |
83 | ||
84 | const char *smap_get(const struct smap *, const char *); | |
85 | struct smap_node *smap_get_node(const struct smap *, const char *); | |
86 | bool smap_get_bool(const struct smap *smap, const char *key, bool def); | |
87 | int smap_get_int(const struct smap *smap, const char *key, int def); | |
d65467f1 | 88 | bool smap_get_uuid(const struct smap *, const char *key, struct uuid *); |
79f1cbe9 EJ |
89 | |
90 | bool smap_is_empty(const struct smap *); | |
91 | size_t smap_count(const struct smap *); | |
92 | ||
4512aaa7 | 93 | void smap_clone(struct smap *dst, const struct smap *src); |
79f1cbe9 EJ |
94 | const struct smap_node **smap_sort(const struct smap *); |
95 | ||
cccc1356 BP |
96 | void smap_from_json(struct smap *, const struct json *); |
97 | struct json *smap_to_json(const struct smap *); | |
98 | ||
7962b7f0 RB |
99 | bool smap_equal(const struct smap *, const struct smap *); |
100 | ||
79f1cbe9 | 101 | #endif /* smap.h */ |