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