PipeWire 1.2.6

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