PipeWire 1.4.11
Loading...
Searching...
No Matches
compare.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_POD_COMPARE_H
6#define SPA_POD_COMPARE_H
7
8#ifdef __cplusplus
9extern "C" {
10#endif
11
12#include <stdarg.h>
13#include <errno.h>
14#include <stdint.h>
15#include <stddef.h>
16#include <stdio.h>
17#include <string.h>
18
19#include <spa/param/props.h>
20#include <spa/pod/iter.h>
21#include <spa/pod/builder.h>
22
23#ifndef SPA_API_POD_COMPARE
24 #ifdef SPA_API_IMPL
25 #define SPA_API_POD_COMPARE SPA_API_IMPL
26 #else
27 #define SPA_API_POD_COMPARE static inline
28 #endif
29#endif
30
35
36SPA_API_POD_COMPARE int spa_pod_compare_value(uint32_t type, const void *r1, const void *r2, uint32_t size)
37{
38 switch (type) {
39 case SPA_TYPE_None:
40 return 0;
41 case SPA_TYPE_Bool:
42 return SPA_CMP(!!*(int32_t *)r1, !!*(int32_t *)r2);
44 return SPA_CMP(*(uint32_t *)r1, *(uint32_t *)r2);
45 case SPA_TYPE_Int:
46 return SPA_CMP(*(int32_t *)r1, *(int32_t *)r2);
47 case SPA_TYPE_Long:
48 return SPA_CMP(*(int64_t *)r1, *(int64_t *)r2);
49 case SPA_TYPE_Float:
50 return SPA_CMP(*(float *)r1, *(float *)r2);
51 case SPA_TYPE_Double:
52 return SPA_CMP(*(double *)r1, *(double *)r2);
53 case SPA_TYPE_String:
54 return strncmp((char *)r1, (char *)r2, size);
55 case SPA_TYPE_Bytes:
56 return memcmp((char *)r1, (char *)r2, size);
58 {
59 const struct spa_rectangle *rec1 = (struct spa_rectangle *) r1,
60 *rec2 = (struct spa_rectangle *) r2;
61 if (rec1->width == rec2->width && rec1->height == rec2->height)
62 return 0;
63 else if (rec1->width < rec2->width || rec1->height < rec2->height)
64 return -1;
65 else
66 return 1;
67 }
69 {
70 const struct spa_fraction *f1 = (struct spa_fraction *) r1,
71 *f2 = (struct spa_fraction *) r2;
72 uint64_t n1, n2;
73 n1 = ((uint64_t) f1->num) * f2->denom;
74 n2 = ((uint64_t) f2->num) * f1->denom;
75 return SPA_CMP(n1, n2);
76 }
77 default:
78 break;
79 }
80 return 0;
81}
82
83SPA_API_POD_COMPARE int spa_pod_compare(const struct spa_pod *pod1,
84 const struct spa_pod *pod2)
85{
86 int res = 0;
87 uint32_t n_vals1, n_vals2;
88 uint32_t choice1, choice2;
89
90 spa_return_val_if_fail(pod1 != NULL, -EINVAL);
91 spa_return_val_if_fail(pod2 != NULL, -EINVAL);
92
93 pod1 = spa_pod_get_values(pod1, &n_vals1, &choice1);
94 pod2 = spa_pod_get_values(pod2, &n_vals2, &choice2);
95
96 if (n_vals1 != n_vals2)
97 return -EINVAL;
98
99 if (SPA_POD_TYPE(pod1) != SPA_POD_TYPE(pod2))
100 return -EINVAL;
101
102 if (n_vals1 < 1)
103 return -EINVAL; /* empty choice */
104
105 switch (SPA_POD_TYPE(pod1)) {
106 case SPA_TYPE_Struct:
107 {
108 const struct spa_pod *p1, *p2;
109 size_t p1s, p2s;
110
111 p1 = (const struct spa_pod*)SPA_POD_BODY_CONST(pod1);
112 p1s = SPA_POD_BODY_SIZE(pod1);
113 p2 = (const struct spa_pod*)SPA_POD_BODY_CONST(pod2);
114 p2s = SPA_POD_BODY_SIZE(pod2);
115
116 while (true) {
117 if (!spa_pod_is_inside(pod1, p1s, p1) ||
118 !spa_pod_is_inside(pod2, p2s, p2))
119 return -EINVAL;
120
121 if ((res = spa_pod_compare(p1, p2)) != 0)
122 return res;
123
124 p1 = (const struct spa_pod*)spa_pod_next(p1);
125 p2 = (const struct spa_pod*)spa_pod_next(p2);
126 }
127 break;
128 }
129 case SPA_TYPE_Object:
130 {
131 const struct spa_pod_prop *p1, *p2;
132 const struct spa_pod_object *o1, *o2;
133
134 o1 = (const struct spa_pod_object*)pod1;
135 o2 = (const struct spa_pod_object*)pod2;
136
137 p2 = NULL;
138 SPA_POD_OBJECT_FOREACH(o1, p1) {
139 if ((p2 = spa_pod_object_find_prop(o2, p2, p1->key)) == NULL)
140 return 1;
141 if ((res = spa_pod_compare(&p1->value, &p2->value)) != 0)
142 return res;
143 }
144 p1 = NULL;
145 SPA_POD_OBJECT_FOREACH(o2, p2) {
146 if ((p1 = spa_pod_object_find_prop(o1, p1, p2->key)) == NULL)
147 return -1;
148 }
149 break;
150 }
151 case SPA_TYPE_Array:
152 {
153 if (SPA_POD_BODY_SIZE(pod1) != SPA_POD_BODY_SIZE(pod2))
154 return -EINVAL;
155 res = memcmp(SPA_POD_BODY(pod1), SPA_POD_BODY(pod2), SPA_POD_BODY_SIZE(pod2));
156 break;
157 }
158 default:
159 if (SPA_POD_BODY_SIZE(pod1) != SPA_POD_BODY_SIZE(pod2))
160 return -EINVAL;
162 SPA_POD_BODY(pod1), SPA_POD_BODY(pod2),
163 SPA_POD_BODY_SIZE(pod1));
164 break;
165 }
166 return res;
167}
168
172
173#ifdef __cplusplus
174}
175#endif
176
177#endif
spa/pod/builder.h
#define SPA_API_POD_COMPARE
Definition compare.h:34
uint32_t int int res
Definition core.h:433
#define SPA_POD_BODY_CONST(pod)
Definition pod.h:41
SPA_API_POD_COMPARE int spa_pod_compare(const struct spa_pod *pod1, const struct spa_pod *pod2)
Definition compare.h:90
SPA_API_POD_ITER struct spa_pod * spa_pod_get_values(const struct spa_pod *pod, uint32_t *n_vals, uint32_t *choice)
Definition iter.h:363
SPA_API_POD_ITER const struct spa_pod_prop * spa_pod_object_find_prop(const struct spa_pod_object *pod, const struct spa_pod_prop *start, uint32_t key)
Definition iter.h:404
SPA_API_POD_COMPARE int spa_pod_compare_value(uint32_t type, const void *r1, const void *r2, uint32_t size)
Definition compare.h:43
#define SPA_POD_OBJECT_FOREACH(obj, iter)
Definition iter.h:124
#define SPA_POD_BODY(pod)
Definition pod.h:39
#define SPA_POD_TYPE(pod)
Definition pod.h:28
SPA_API_POD_ITER void * spa_pod_next(const void *iter)
Definition iter.h:52
#define SPA_POD_BODY_SIZE(pod)
Definition pod.h:26
SPA_API_POD_ITER bool spa_pod_is_inside(const void *pod, uint32_t size, const void *iter)
Definition iter.h:44
@ SPA_TYPE_Int
Definition type.h:45
@ SPA_TYPE_Rectangle
Definition type.h:51
@ SPA_TYPE_Long
Definition type.h:46
@ SPA_TYPE_Bool
Definition type.h:43
@ SPA_TYPE_Bytes
Definition type.h:50
@ SPA_TYPE_Object
Definition type.h:56
@ SPA_TYPE_Float
Definition type.h:47
@ SPA_TYPE_Fraction
Definition type.h:52
@ SPA_TYPE_None
Definition type.h:42
@ SPA_TYPE_Double
Definition type.h:48
@ SPA_TYPE_Id
Definition type.h:44
@ SPA_TYPE_Array
Definition type.h:54
@ SPA_TYPE_String
Definition type.h:49
@ SPA_TYPE_Struct
Definition type.h:55
#define spa_return_val_if_fail(expr, val)
Definition defs.h:456
#define SPA_CMP(a, b)
3-way comparison.
Definition defs.h:209
spa/pod/iter.h
spa/utils/string.h
Definition defs.h:137
uint32_t num
Definition defs.h:138
uint32_t denom
Definition defs.h:139
Definition pod.h:183
Definition pod.h:208
uint32_t key
key of property, list of valid keys depends on the object type
Definition pod.h:209
struct spa_pod value
Definition pod.h:226
Definition pod.h:43
Definition defs.h:116
uint32_t width
Definition defs.h:117
uint32_t height
Definition defs.h:118