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