Debian Codegraph @

3rdparty/pipewire/spa/utils/dict.h

raw
1 /* Simple Plugin API
2 *
3 * Copyright © 2018 Wim Taymans
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #ifndef SPA_DICT_H
26 #define SPA_DICT_H
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 #include <string.h>
33
34 #include <spa/utils/defs.h>
35
36 struct spa_dict_item {
37 const char *key;
38 const char *value;
39 };
40
41 #define SPA_DICT_ITEM_INIT(key,value) (struct spa_dict_item) { key, value }
42
43 struct spa_dict {
44 #define SPA_DICT_FLAG_SORTED (1<<0) /**< items are sorted */
45 uint32_t flags;
46 uint32_t n_items;
47 const struct spa_dict_item *items;
48 };
49
50 #define SPA_DICT_INIT(items,n_items) (struct spa_dict) { 0, n_items, items }
51 #define SPA_DICT_INIT_ARRAY(items) (struct spa_dict) { 0, SPA_N_ELEMENTS(items), items }
52
53 #define spa_dict_for_each(item, dict) \
54 for ((item) = (dict)->items; \
55 (item) < &(dict)->items[(dict)->n_items]; \
56 (item)++)
57
58 static inline int spa_dict_item_compare(const void *i1, const void *i2)
59 {
60 const struct spa_dict_item *it1 = (const struct spa_dict_item *)i1,
61 *it2 = (const struct spa_dict_item *)i2;
62 return strcmp(it1->key, it2->key);
63 }
64
65 static inline void spa_dict_qsort(struct spa_dict *dict)
66 {
67 qsort((void*)dict->items, dict->n_items, sizeof(struct spa_dict_item),
68 spa_dict_item_compare);
69 SPA_FLAG_SET(dict->flags, SPA_DICT_FLAG_SORTED);
70 }
71
72 static inline const struct spa_dict_item *spa_dict_lookup_item(const struct spa_dict *dict,
73 const char *key)
74 {
75 const struct spa_dict_item *item;
76
77 if (SPA_FLAG_IS_SET(dict->flags, SPA_DICT_FLAG_SORTED)) {
78 struct spa_dict_item k = SPA_DICT_ITEM_INIT(key, NULL);
79 item = (const struct spa_dict_item *)bsearch(&k,
80 (const void *) dict->items, dict->n_items,
81 sizeof(struct spa_dict_item),
82 spa_dict_item_compare);
83 if (item != NULL)
84 return item;
85 } else {
86 spa_dict_for_each(item, dict) {
87 if (!strcmp(item->key, key))
88 return item;
89 }
90 }
91 return NULL;
92 }
93
94 static inline const char *spa_dict_lookup(const struct spa_dict *dict, const char *key)
95 {
96 const struct spa_dict_item *item = spa_dict_lookup_item(dict, key);
97 return item ? item->value : NULL;
98 }
99
100 #ifdef __cplusplus
101 } /* extern "C" */
102 #endif
103
104 #endif /* SPA_DICT_H */