PipeWire  0.3.59
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
29 extern "C" {
30 #endif
31 
32 #include <string.h>
33 
34 #include <spa/utils/defs.h>
35 
46 struct spa_dict_item {
47  const char *key;
48  const char *value;
49 };
50 
51 #define SPA_DICT_ITEM_INIT(key,value) ((struct spa_dict_item) { (key), (value) })
52 
53 struct spa_dict {
54 #define SPA_DICT_FLAG_SORTED (1<<0)
55  uint32_t flags;
56  uint32_t n_items;
57  const struct spa_dict_item *items;
58 };
59 
60 #define SPA_DICT_INIT(items,n_items) ((struct spa_dict) { 0, (n_items), (items) })
61 #define SPA_DICT_INIT_ARRAY(items) ((struct spa_dict) { 0, SPA_N_ELEMENTS(items), (items) })
62 
63 #define spa_dict_for_each(item, dict) \
64  for ((item) = (dict)->items; \
65  (item) < &(dict)->items[(dict)->n_items]; \
66  (item)++)
67 
68 static inline int spa_dict_item_compare(const void *i1, const void *i2)
69 {
70  const struct spa_dict_item *it1 = (const struct spa_dict_item *)i1,
71  *it2 = (const struct spa_dict_item *)i2;
72  return strcmp(it1->key, it2->key);
73 }
74 
75 static inline void spa_dict_qsort(struct spa_dict *dict)
76 {
77  if (dict->n_items > 0)
78  qsort((void*)dict->items, dict->n_items, sizeof(struct spa_dict_item),
81 }
82 
83 static inline const struct spa_dict_item *spa_dict_lookup_item(const struct spa_dict *dict,
84  const char *key)
85 {
86  const struct spa_dict_item *item;
87 
89  dict->n_items > 0) {
90  struct spa_dict_item k = SPA_DICT_ITEM_INIT(key, NULL);
91  item = (const struct spa_dict_item *)bsearch(&k,
92  (const void *) dict->items, dict->n_items,
93  sizeof(struct spa_dict_item),
95  if (item != NULL)
96  return item;
97  } else {
98  spa_dict_for_each(item, dict) {
99  if (!strcmp(item->key, key))
100  return item;
101  }
102  }
103  return NULL;
104 }
105 
106 static inline const char *spa_dict_lookup(const struct spa_dict *dict, const char *key)
107 {
108  const struct spa_dict_item *item = spa_dict_lookup_item(dict, key);
109  return item ? item->value : NULL;
110 }
111 
116 #ifdef __cplusplus
117 } /* extern "C" */
118 #endif
119 
120 #endif /* SPA_DICT_H */
spa/utils/defs.h
static const char * spa_dict_lookup(const struct spa_dict *dict, const char *key)
Definition: dict.h:115
#define SPA_DICT_ITEM_INIT(key, value)
Definition: dict.h:57
static void spa_dict_qsort(struct spa_dict *dict)
Definition: dict.h:84
static int spa_dict_item_compare(const void *i1, const void *i2)
Definition: dict.h:77
#define SPA_DICT_FLAG_SORTED
items are sorted
Definition: dict.h:61
#define spa_dict_for_each(item, dict)
Definition: dict.h:72
static const struct spa_dict_item * spa_dict_lookup_item(const struct spa_dict *dict, const char *key)
Definition: dict.h:92
#define SPA_FLAG_SET(field, flag)
Definition: defs.h:95
#define SPA_FLAG_IS_SET(field, flag)
Definition: defs.h:92
spa/utils/string.h
Definition: dict.h:51
const char * key
Definition: dict.h:52
const char * value
Definition: dict.h:53
Definition: dict.h:59
const struct spa_dict_item * items
Definition: dict.h:64
uint32_t n_items
Definition: dict.h:63
uint32_t flags
Definition: dict.h:62