PipeWire: spa/utils/dict.h Source File

PipeWire 1.0.3

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
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) })
32
33 struct 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};
39
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) })
42
43#define spa_dict_for_each(item, dict) \
44 for ((item) = (dict)->items; \
45 (item) < &(dict)->items[(dict)->n_items]; \
46 (item)++)
47
48 static 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),
59 spa_dict_item_compare);
60 SPA_FLAG_SET(dict->flags, SPA_DICT_FLAG_SORTED);
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
68 if (SPA_FLAG_IS_SET(dict->flags, SPA_DICT_FLAG_SORTED) &&
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),
74 spa_dict_item_compare);
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 */
defs.h
spa/utils/defs.h
SPA_DICT_ITEM_INIT
#define SPA_DICT_ITEM_INIT(key, value)
Definition dict.h:37
spa_dict_qsort
static void spa_dict_qsort(struct spa_dict *dict)
Definition dict.h:64
spa_dict_item_compare
static int spa_dict_item_compare(const void *i1, const void *i2)
Definition dict.h:57
spa_dict_lookup_item
static const struct spa_dict_item * spa_dict_lookup_item(const struct spa_dict *dict, const char *key)
Definition dict.h:72
spa_dict_lookup
static const char * spa_dict_lookup(const struct spa_dict *dict, const char *key)
Definition dict.h:95
SPA_DICT_FLAG_SORTED
#define SPA_DICT_FLAG_SORTED
items are sorted
Definition dict.h:41
spa_dict_for_each
#define spa_dict_for_each(item, dict)
Definition dict.h:52
SPA_FLAG_SET
#define SPA_FLAG_SET(field, flag)
Definition defs.h:79
SPA_FLAG_IS_SET
#define SPA_FLAG_IS_SET(field, flag)
Definition defs.h:76
string.h
spa/utils/string.h
spa_dict_item
Definition dict.h:31
spa_dict_item::key
const char * key
Definition dict.h:32
spa_dict_item::value
const char * value
Definition dict.h:33
spa_dict
Definition dict.h:39
spa_dict::items
const struct spa_dict_item * items
Definition dict.h:44
spa_dict::n_items
uint32_t n_items
Definition dict.h:43
spa_dict::flags
uint32_t flags
Definition dict.h:42