summaryrefslogtreecommitdiffstats
path: root/third_party/pipewire/spa/utils/dict.h
blob: 272420ec3d46e468ad22c7793bc87f8b6e1cacff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* Simple Plugin API
 *
 * Copyright © 2018 Wim Taymans
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#ifndef SPA_DICT_H
#define SPA_DICT_H

#ifdef __cplusplus
extern "C" {
#endif

#include <string.h>

#include <spa/utils/defs.h>

struct spa_dict_item {
	const char *key;
	const char *value;
};

#define SPA_DICT_ITEM_INIT(key,value) (struct spa_dict_item) { key, value }

struct spa_dict {
#define SPA_DICT_FLAG_SORTED	(1<<0)		/**< items are sorted */
	uint32_t flags;
	uint32_t n_items;
	const struct spa_dict_item *items;
};

#define SPA_DICT_INIT(items,n_items) (struct spa_dict) { 0, n_items, items }
#define SPA_DICT_INIT_ARRAY(items) (struct spa_dict) { 0, SPA_N_ELEMENTS(items), items }

#define spa_dict_for_each(item, dict)				\
	for ((item) = (dict)->items;				\
	     (item) < &(dict)->items[(dict)->n_items];		\
	     (item)++)

static inline int spa_dict_item_compare(const void *i1, const void *i2)
{
	const struct spa_dict_item *it1 = (const struct spa_dict_item *)i1,
	      *it2 = (const struct spa_dict_item *)i2;
	return strcmp(it1->key, it2->key);
}

static inline void spa_dict_qsort(struct spa_dict *dict)
{
	qsort((void*)dict->items, dict->n_items, sizeof(struct spa_dict_item),
			spa_dict_item_compare);
	SPA_FLAG_SET(dict->flags, SPA_DICT_FLAG_SORTED);
}

static inline const struct spa_dict_item *spa_dict_lookup_item(const struct spa_dict *dict,
							       const char *key)
{
	const struct spa_dict_item *item;

	if (SPA_FLAG_IS_SET(dict->flags, SPA_DICT_FLAG_SORTED)) {
		struct spa_dict_item k = SPA_DICT_ITEM_INIT(key, NULL);
		item = (const struct spa_dict_item *)bsearch(&k,
				(const void *) dict->items, dict->n_items,
				sizeof(struct spa_dict_item),
				spa_dict_item_compare);
		if (item != NULL)
			return item;
	} else {
		spa_dict_for_each(item, dict) {
			if (!strcmp(item->key, key))
				return item;
		}
	}
	return NULL;
}

static inline const char *spa_dict_lookup(const struct spa_dict *dict, const char *key)
{
	const struct spa_dict_item *item = spa_dict_lookup_item(dict, key);
	return item ? item->value : NULL;
}

#ifdef __cplusplus
}  /* extern "C" */
#endif

#endif /* SPA_DICT_H */