]>
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> |
a62a46a2 | 19 | #include "hash.h" |
ee89ea7b | 20 | #include "openvswitch/hmap.h" |
79f1cbe9 | 21 | |
cccc1356 | 22 | struct json; |
d65467f1 | 23 | struct uuid; |
cccc1356 | 24 | |
79f1cbe9 EJ |
25 | /* A map from string to string. */ |
26 | struct smap { | |
27 | struct hmap map; /* Contains "struct smap_node"s. */ | |
28 | }; | |
29 | ||
30 | struct smap_node { | |
31 | struct hmap_node node; /* In struct smap's 'map' hmap. */ | |
32 | char *key; | |
33 | char *value; | |
34 | }; | |
35 | ||
36 | #define SMAP_INITIALIZER(SMAP) { HMAP_INITIALIZER(&(SMAP)->map) } | |
37 | ||
bc8d7dfa BP |
38 | #define SMAP_FOR_EACH(SMAP_NODE, SMAP) \ |
39 | HMAP_FOR_EACH_INIT (SMAP_NODE, node, &(SMAP)->map, \ | |
40 | BUILD_ASSERT_TYPE(SMAP_NODE, struct smap_node *), \ | |
41 | BUILD_ASSERT_TYPE(SMAP, struct smap *)) | |
42 | ||
43 | #define SMAP_FOR_EACH_SAFE(SMAP_NODE, NEXT, SMAP) \ | |
44 | HMAP_FOR_EACH_SAFE_INIT ( \ | |
45 | SMAP_NODE, NEXT, node, &(SMAP)->map, \ | |
46 | BUILD_ASSERT_TYPE(SMAP_NODE, struct smap_node *), \ | |
47 | BUILD_ASSERT_TYPE(NEXT, struct smap_node *), \ | |
48 | BUILD_ASSERT_TYPE(SMAP, struct smap *)) | |
79f1cbe9 | 49 | |
aaf881c6 BP |
50 | /* Initializer for an immutable struct smap 'SMAP' that contains a single |
51 | * 'KEY'-'VALUE' pair, e.g. | |
52 | * | |
53 | * const struct smap smap = SMAP1_CONST1(&smap, "key", "value"); | |
54 | * | |
55 | * An smap initialized this way must not be modified or destroyed. | |
56 | * | |
64982ba2 | 57 | * The 'KEY', 'K1', 'K2' arguments are evaluated multiple times. |
aaf881c6 | 58 | */ |
64982ba2 BP |
59 | #define SMAP_CONST1(SMAP, KEY, VALUE) (const struct smap) { \ |
60 | HMAP_CONST(&(SMAP)->map, 1, SMAP_NODE(KEY, VALUE, NULL)) \ | |
61 | } | |
62 | #define SMAP_CONST2(SMAP, K1, V1, K2, V2) (const struct smap) { \ | |
63 | HMAP_CONST(&(SMAP)->map, 2, \ | |
64 | SMAP_NODE(K1, V1, SMAP_NODE(K2, V2, NULL))) \ | |
65 | } | |
66 | #define SMAP_NODE(KEY, VALUE, NEXT) \ | |
67 | &(struct smap_node) { \ | |
68 | .node = { \ | |
69 | .hash = hash_string(KEY, 0), \ | |
70 | .next = (NEXT), \ | |
71 | }, \ | |
72 | .key = CONST_CAST(char *, KEY), \ | |
73 | .value = CONST_CAST(char *, VALUE), \ | |
74 | }.node | |
aaf881c6 BP |
75 | |
76 | ||
79f1cbe9 EJ |
77 | void smap_init(struct smap *); |
78 | void smap_destroy(struct smap *); | |
79 | ||
80 | struct smap_node *smap_add(struct smap *, const char *, const char *); | |
ff929935 | 81 | struct smap_node *smap_add_nocopy(struct smap *, char *, char *); |
79f1cbe9 EJ |
82 | bool smap_add_once(struct smap *, const char *, const char *); |
83 | void smap_add_format(struct smap *, const char *key, const char *, ...) | |
cab50449 | 84 | OVS_PRINTF_FORMAT(3, 4); |
9835576b | 85 | void smap_add_ipv6(struct smap *, const char *, struct in6_addr *); |
79f1cbe9 EJ |
86 | void smap_replace(struct smap *, const char *, const char *); |
87 | ||
88 | void smap_remove(struct smap *, const char *); | |
51c82a49 | 89 | void smap_remove_node(struct smap *, struct smap_node *); |
57c8677b | 90 | void smap_steal(struct smap *, struct smap_node *, char **keyp, char **valuep); |
79f1cbe9 EJ |
91 | void smap_clear(struct smap *); |
92 | ||
f99f67bd BP |
93 | const char *smap_get(const struct smap *, const char *key); |
94 | const char *smap_get_def(const struct smap *, const char *key, | |
95 | const char *def); | |
79f1cbe9 EJ |
96 | struct smap_node *smap_get_node(const struct smap *, const char *); |
97 | bool smap_get_bool(const struct smap *smap, const char *key, bool def); | |
98 | int smap_get_int(const struct smap *smap, const char *key, int def); | |
13c1637f BP |
99 | unsigned long long int smap_get_ullong(const struct smap *, const char *key, |
100 | unsigned long long def); | |
d65467f1 | 101 | bool smap_get_uuid(const struct smap *, const char *key, struct uuid *); |
79f1cbe9 EJ |
102 | |
103 | bool smap_is_empty(const struct smap *); | |
104 | size_t smap_count(const struct smap *); | |
105 | ||
4512aaa7 | 106 | void smap_clone(struct smap *dst, const struct smap *src); |
79f1cbe9 EJ |
107 | const struct smap_node **smap_sort(const struct smap *); |
108 | ||
cccc1356 BP |
109 | void smap_from_json(struct smap *, const struct json *); |
110 | struct json *smap_to_json(const struct smap *); | |
111 | ||
7962b7f0 RB |
112 | bool smap_equal(const struct smap *, const struct smap *); |
113 | ||
79f1cbe9 | 114 | #endif /* smap.h */ |