PipeWire 0.3.38
utils/dict.h
Go to the documentation of this file.
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
29extern "C" {
30#endif
31
32#include <string.h>
33
34#include <spa/utils/defs.h>
35
42 const char *key;
43 const char *value;
44};
45
46#define SPA_DICT_ITEM_INIT(key,value) (struct spa_dict_item) { key, value }
47
48struct spa_dict {
49#define SPA_DICT_FLAG_SORTED (1<<0)
50 uint32_t flags;
51 uint32_t n_items;
52 const struct spa_dict_item *items;
53};
54
55#define SPA_DICT_INIT(items,n_items) (struct spa_dict) { 0, n_items, items }
56#define SPA_DICT_INIT_ARRAY(items) (struct spa_dict) { 0, SPA_N_ELEMENTS(items), items }
57
58#define spa_dict_for_each(item, dict) \
59 for ((item) = (dict)->items; \
60 (item) < &(dict)->items[(dict)->n_items]; \
61 (item)++)
62
63/* static */ inline int spa_dict_item_compare(const void *i1, const void *i2)
64{
65 const struct spa_dict_item *it1 = (const struct spa_dict_item *)i1,
66 *it2 = (const struct spa_dict_item *)i2;
67 return strcmp(it1->key, it2->key);
68}
69
70/* static */ inline void spa_dict_qsort(struct spa_dict *dict)
71{
72 qsort((void*)dict->items, dict->n_items, sizeof(struct spa_dict_item),
75}
76
77/* static */ inline const struct spa_dict_item *spa_dict_lookup_item(const struct spa_dict *dict,
78 const char *key)
79{
80 const struct spa_dict_item *item;
81
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),
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/* static */ inline 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
109#ifdef __cplusplus
110} /* extern "C" */
111#endif
112
113#endif /* SPA_DICT_H */
#define SPA_FLAG_SET(field, flag)
Definition: defs.h:74
#define SPA_DICT_ITEM_INIT(key, value)
Definition: utils/dict.h:46
const char * spa_dict_lookup(const struct spa_dict *dict, const char *key)
Definition: utils/dict.h:99
const struct spa_dict_item * spa_dict_lookup_item(const struct spa_dict *dict, const char *key)
Definition: utils/dict.h:77
void spa_dict_qsort(struct spa_dict *dict)
Definition: utils/dict.h:70
#define SPA_FLAG_IS_SET(field, flag)
Definition: defs.h:73
#define spa_dict_for_each(item, dict)
Definition: utils/dict.h:58
int spa_dict_item_compare(const void *i1, const void *i2)
Definition: utils/dict.h:63
Definition: impl-metadata.c:50
char * value
Definition: impl-metadata.c:54
char * key
Definition: impl-metadata.c:52
Definition: utils/dict.h:41
const char * key
Definition: utils/dict.h:42
const char * value
Definition: utils/dict.h:43
Definition: utils/dict.h:48
const struct spa_dict_item * items
Definition: utils/dict.h:52
uint32_t n_items
Definition: utils/dict.h:51
uint32_t flags
Definition: utils/dict.h:50
#define SPA_DICT_FLAG_SORTED
items are sorted
Definition: utils/dict.h:49