PipeWire: spa/utils/dict.h Source File

PipeWire 1.4.7

Loading...
Searching...
No Matches
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
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
28
33
34struct 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)
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
108
109#ifdef __cplusplus
110} /* extern "C" */
111#endif
112
113#endif /* SPA_DICT_H */
defs.h
spa/utils/defs.h
spa_dict_qsort
SPA_API_DICT void spa_dict_qsort(struct spa_dict *dict)
Definition dict.h:83
spa_dict_lookup_item
SPA_API_DICT const struct spa_dict_item * spa_dict_lookup_item(const struct spa_dict *dict, const char *key)
Definition dict.h:91
SPA_DICT_ITEM_INIT
#define SPA_DICT_ITEM_INIT(key, value)
Definition dict.h:49
spa_dict_lookup
SPA_API_DICT const char * spa_dict_lookup(const struct spa_dict *dict, const char *key)
Definition dict.h:114
spa_dict_item_compare
SPA_API_DICT int spa_dict_item_compare(const void *i1, const void *i2)
Definition dict.h:76
SPA_DICT_FLAG_SORTED
#define SPA_DICT_FLAG_SORTED
items are sorted
Definition dict.h:53
spa_dict_for_each
#define spa_dict_for_each(item, dict)
Definition dict.h:71
SPA_FLAG_SET
#define SPA_FLAG_SET(field, flag)
Definition defs.h:93
SPA_FLAG_IS_SET
#define SPA_FLAG_IS_SET(field, flag)
Definition defs.h:90
string.h
spa/utils/string.h
spa_dict_item
Definition dict.h:41
spa_dict_item::key
const char * key
Definition dict.h:42
spa_dict_item::value
const char * value
Definition dict.h:43
spa_dict
Definition dict.h:51
spa_dict::items
const struct spa_dict_item * items
Definition dict.h:56
spa_dict::n_items
uint32_t n_items
Definition dict.h:55
spa_dict::flags
uint32_t flags
Definition dict.h:54
SPA_API_DICT
#define SPA_API_DICT
Definition dict.h:27