+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
c · 113 lines · 2.7 KB · SPDX: MIT
main
1 /* Simple Plugin API */
2 /* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
3 /* SPDX-License-Identifier: MIT */
4  
5 #ifndef SPA_DICT_H
6 #define SPA_DICT_H
7  
8 #include <string.h>
9  
10 #include <spa/utils/defs.h>
11  
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15  
16 #ifndef SPA_API_DICT
17 #ifdef SPA_API_IMPL
18 #define SPA_API_DICT SPA_API_IMPL
19 #else
20 #define SPA_API_DICT static inline
21 #endif
22 #endif
23  
24 /**
25 * \defgroup spa_dict Dictionary
26 * Dictionary data structure
27 */
28  
29 /**
30 * \addtogroup spa_dict
31 * \{
32 */
33  
34 struct spa_dict_item {
35 const char *key;
36 const char *value;
37 };
38  
39 #define SPA_DICT_ITEM(key,value) ((struct spa_dict_item) { (key), (value) })
40 #define SPA_DICT_ITEM_INIT(key,value) SPA_DICT_ITEM(key,value)
41  
42 struct spa_dict {
43 #define SPA_DICT_FLAG_SORTED (1<<0) /**< items are sorted */
44 uint32_t flags;
45 uint32_t n_items;
46 const struct spa_dict_item *items;
47 };
48  
49 #define SPA_DICT(items,n_items) ((struct spa_dict) { 0, (n_items), (items) })
50 #define SPA_DICT_ARRAY(items) SPA_DICT((items),SPA_N_ELEMENTS(items))
51 #define SPA_DICT_ITEMS(...) SPA_DICT_ARRAY(((struct spa_dict_item[]) { __VA_ARGS__}))
52  
53 #define SPA_DICT_INIT(items,n_items) SPA_DICT(items,n_items)
54 #define SPA_DICT_INIT_ARRAY(items) SPA_DICT_ARRAY(items)
55  
56 #define spa_dict_for_each(item, dict) \
57 for ((item) = (dict)->items; \
58 (item) < &(dict)->items[(dict)->n_items]; \
59 (item)++)
60  
61 SPA_API_DICT int spa_dict_item_compare(const void *i1, const void *i2)
62 {
63 const struct spa_dict_item *it1 = (const struct spa_dict_item *)i1,
64 *it2 = (const struct spa_dict_item *)i2;
65 return strcmp(it1->key, it2->key);
66 }
67  
68 SPA_API_DICT void spa_dict_qsort(struct spa_dict *dict)
69 {
70 if (dict->n_items > 0)
71 qsort((void*)dict->items, dict->n_items, sizeof(struct spa_dict_item),
72 spa_dict_item_compare);
73 SPA_FLAG_SET(dict->flags, SPA_DICT_FLAG_SORTED);
74 }
75  
76 SPA_API_DICT const struct spa_dict_item *spa_dict_lookup_item(const struct spa_dict *dict,
77 const char *key)
78 {
79 const struct spa_dict_item *item;
80  
81 if (SPA_FLAG_IS_SET(dict->flags, SPA_DICT_FLAG_SORTED) &&
82 dict->n_items > 0) {
83 struct spa_dict_item k = SPA_DICT_ITEM_INIT(key, NULL);
84 item = (const struct spa_dict_item *)bsearch(&k,
85 (const void *) dict->items, dict->n_items,
86 sizeof(struct spa_dict_item),
87 spa_dict_item_compare);
88 if (item != NULL)
89 return item;
90 } else {
91 spa_dict_for_each(item, dict) {
92 if (!strcmp(item->key, key))
93 return item;
94 }
95 }
96 return NULL;
97 }
98  
99 SPA_API_DICT const char *spa_dict_lookup(const struct spa_dict *dict, const char *key)
100 {
101 const struct spa_dict_item *item = spa_dict_lookup_item(dict, key);
102 return item ? item->value : NULL;
103 }
104  
105 /**
106 * \}
107 */
108  
109 #ifdef __cplusplus
110 } /* extern "C" */
111 #endif
112  
113 #endif /* SPA_DICT_H */