PipeWire 0.3.77
Loading...
Searching...
No Matches
utils/dict.h
Go to the documentation of this file.
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#ifdef __cplusplus
9extern "C" {
10#endif
11
12#include <string.h>
13
14#include <spa/utils/defs.h>
15
26struct spa_dict_item {
27 const char *key;
28 const char *value;
29};
30
31#define SPA_DICT_ITEM_INIT(key,value) ((struct spa_dict_item) { (key), (value) })
33struct spa_dict {
34#define SPA_DICT_FLAG_SORTED (1<<0)
35 uint32_t flags;
36 uint32_t n_items;
37 const struct spa_dict_item *items;
38};
40#define SPA_DICT_INIT(items,n_items) ((struct spa_dict) { 0, (n_items), (items) })
41#define SPA_DICT_INIT_ARRAY(items) ((struct spa_dict) { 0, SPA_N_ELEMENTS(items), (items) })
43#define spa_dict_for_each(item, dict) \
44 for ((item) = (dict)->items; \
45 (item) < &(dict)->items[(dict)->n_items]; \
46 (item)++)
47
48static inline int spa_dict_item_compare(const void *i1, const void *i2)
49{
50 const struct spa_dict_item *it1 = (const struct spa_dict_item *)i1,
51 *it2 = (const struct spa_dict_item *)i2;
52 return strcmp(it1->key, it2->key);
53}
54
55static inline void spa_dict_qsort(struct spa_dict *dict)
56{
57 if (dict->n_items > 0)
58 qsort((void*)dict->items, dict->n_items, sizeof(struct spa_dict_item),
61}
62
63static inline const struct spa_dict_item *spa_dict_lookup_item(const struct spa_dict *dict,
64 const char *key)
65{
66 const struct spa_dict_item *item;
67
69 dict->n_items > 0) {
70 struct spa_dict_item k = SPA_DICT_ITEM_INIT(key, NULL);
71 item = (const struct spa_dict_item *)bsearch(&k,
72 (const void *) dict->items, dict->n_items,
73 sizeof(struct spa_dict_item),
75 if (item != NULL)
76 return item;
77 } else {
78 spa_dict_for_each(item, dict) {
79 if (!strcmp(item->key, key))
80 return item;
81 }
82 }
83 return NULL;
84}
85
86static inline const char *spa_dict_lookup(const struct spa_dict *dict, const char *key)
87{
88 const struct spa_dict_item *item = spa_dict_lookup_item(dict, key);
89 return item ? item->value : NULL;
90}
91
96#ifdef __cplusplus
97} /* extern "C" */
98#endif
99
100#endif /* SPA_DICT_H */
spa/utils/defs.h
#define SPA_DICT_ITEM_INIT(key, value)
Definition: utils/dict.h:37
static void spa_dict_qsort(struct spa_dict *dict)
Definition: utils/dict.h:64
static int spa_dict_item_compare(const void *i1, const void *i2)
Definition: utils/dict.h:57
static const struct spa_dict_item * spa_dict_lookup_item(const struct spa_dict *dict, const char *key)
Definition: utils/dict.h:72
static const char * spa_dict_lookup(const struct spa_dict *dict, const char *key)
Definition: utils/dict.h:95
#define SPA_DICT_FLAG_SORTED
items are sorted
Definition: utils/dict.h:41
#define spa_dict_for_each(item, dict)
Definition: utils/dict.h:52
#define SPA_FLAG_SET(field, flag)
Definition: defs.h:79
#define SPA_FLAG_IS_SET(field, flag)
Definition: defs.h:76
spa/utils/string.h
Definition: utils/dict.h:31
const char * key
Definition: utils/dict.h:32
const char * value
Definition: utils/dict.h:33
Definition: utils/dict.h:39
const struct spa_dict_item * items
Definition: utils/dict.h:44
uint32_t n_items
Definition: utils/dict.h:43
uint32_t flags
Definition: utils/dict.h:42