PipeWire 1.6.8

Loading...
Searching...
No Matches
dict-utils.h
Go to the documentation of this file.
1/* Simple Plugin API */
2/* SPDX-FileCopyrightText: Copyright © 2023 Wim Taymans */
3/* SPDX-License-Identifier: MIT */
4
5#ifndef SPA_PARAM_DICT_UTILS_H
6#define SPA_PARAM_DICT_UTILS_H
7
8#include <float.h>
9
10#include <spa/utils/dict.h>
11#include <spa/pod/builder.h>
12#include <spa/pod/iter.h>
13#include <spa/pod/parser.h>
14#include <spa/pod/compare.h>
15#include <spa/param/dict.h>
16
17#ifdef __cplusplus
18 extern "C" {
19#endif
20
25
26#ifndef SPA_API_DICT_UTILS
27 #ifdef SPA_API_IMPL
28 #define SPA_API_DICT_UTILS SPA_API_IMPL
29 #else
30 #define SPA_API_DICT_UTILS static inline
31 #endif
32#endif
33
34 SPA_API_DICT_UTILS int
35 spa_param_dict_compare(const struct spa_pod *a, const struct spa_pod *b)
36{
37 return spa_pod_memcmp(a, b);
38}
39
40 SPA_API_DICT_UTILS struct spa_pod *
41 spa_param_dict_build_dict(struct spa_pod_builder *builder, uint32_t id, struct spa_dict *dict)
42{
43 struct spa_pod_frame f[2];
44 uint32_t i, n_items;
45
46 spa_pod_builder_push_object(builder, &f[0], SPA_TYPE_OBJECT_ParamDict, id);
47
48 n_items = dict ? dict->n_items : 0;
49
50 spa_pod_builder_prop(builder, SPA_PARAM_DICT_info, SPA_POD_PROP_FLAG_HINT_DICT);
51 spa_pod_builder_push_struct(builder, &f[1]);
52 spa_pod_builder_int(builder, n_items);
53 for (i = 0; i < n_items; i++) {
54 spa_pod_builder_string(builder, dict->items[i].key);
55 spa_pod_builder_string(builder, dict->items[i].value);
56 }
57 spa_pod_builder_pop(builder, &f[1]);
58
59 return (struct spa_pod*)spa_pod_builder_pop(builder, &f[0]);
60}
61
62 SPA_API_DICT_UTILS struct spa_pod *
63 spa_param_dict_build_info(struct spa_pod_builder *builder, uint32_t id, struct spa_param_dict_info *info)
64{
65 struct spa_pod_frame f;
66 spa_pod_builder_push_object(builder, &f, SPA_TYPE_OBJECT_ParamDict, id);
67 spa_pod_builder_add(builder, SPA_PARAM_DICT_info, SPA_POD_PROP_FLAG_HINT_DICT);
68 spa_pod_builder_primitive(builder, info->info);
69 return (struct spa_pod*)spa_pod_builder_pop(builder, &f);
70}
71
72 SPA_API_DICT_UTILS int
73 spa_param_dict_parse(const struct spa_pod *dict, struct spa_param_dict_info *info, size_t size)
74{
75 memset(info, 0, size);
76 return spa_pod_parse_object(dict,
77 SPA_TYPE_OBJECT_ParamDict, NULL,
78 SPA_PARAM_DICT_info, SPA_POD_PodStruct(&info->info));
79}
80
81 SPA_API_DICT_UTILS int
82 spa_param_dict_info_parse(const struct spa_param_dict_info *info, size_t size,
83 struct spa_dict *dict, struct spa_dict_item *items)
84{
85 struct spa_pod_parser prs;
86 uint32_t n, n_items;
87 const char *key, *value;
88 struct spa_pod_frame f[1];
89
90 spa_pod_parser_pod(&prs, info->info);
91 if (spa_pod_parser_push_struct(&prs, &f[0]) < 0 ||
92 spa_pod_parser_get_int(&prs, (int32_t*)&n_items) < 0)
93 return -EINVAL;
94
95 if (items == NULL) {
96 dict->n_items = n_items;
97 return 0;
98 }
99 n_items = SPA_MIN(dict->n_items, n_items);
100
101 for (n = 0; n < n_items; n++) {
102 if (spa_pod_parser_get(&prs,
103 SPA_POD_String(&key),
104 SPA_POD_String(&value),
105 NULL) < 0)
106 break;
107 if (key == NULL || value == NULL)
108 return -EINVAL;
109 items[n].key = key;
110 items[n].value = value;
111 }
112 *dict = SPA_DICT_INIT(items, n);
113 spa_pod_parser_pop(&prs, &f[0]);
114 return 0;
115}
116
120
121#ifdef __cplusplus
122} /* extern "C" */
123#endif
124
125#endif /* SPA_PARAM_DICT_UTILS_H */
builder.h
spa/pod/builder.h
compare.h
spa/pod/compare.h
SPA_DICT_INIT
#define SPA_DICT_INIT(items, n_items)
Definition dict.h:67
SPA_API_DICT_UTILS
#define SPA_API_DICT_UTILS
Definition dict-utils.h:37
spa_param_dict_info_parse
SPA_API_DICT_UTILS int spa_param_dict_info_parse(const struct spa_param_dict_info *info, size_t size, struct spa_dict *dict, struct spa_dict_item *items)
Definition dict-utils.h:89
spa_param_dict_build_info
SPA_API_DICT_UTILS struct spa_pod * spa_param_dict_build_info(struct spa_pod_builder *builder, uint32_t id, struct spa_param_dict_info *info)
Definition dict-utils.h:70
spa_param_dict_parse
SPA_API_DICT_UTILS int spa_param_dict_parse(const struct spa_pod *dict, struct spa_param_dict_info *info, size_t size)
Definition dict-utils.h:80
spa_param_dict_compare
SPA_API_DICT_UTILS int spa_param_dict_compare(const struct spa_pod *a, const struct spa_pod *b)
Definition dict-utils.h:42
spa_param_dict_build_dict
SPA_API_DICT_UTILS struct spa_pod * spa_param_dict_build_dict(struct spa_pod_builder *builder, uint32_t id, struct spa_dict *dict)
Definition dict-utils.h:48
SPA_PARAM_DICT_info
@ SPA_PARAM_DICT_info
Struct( Int: n_items (String: key ...
Definition dict.h:27
spa_pod_builder_pop
SPA_API_POD_BUILDER void * spa_pod_builder_pop(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:213
spa_pod_builder_int
SPA_API_POD_BUILDER int spa_pod_builder_int(struct spa_pod_builder *builder, int32_t val)
Definition builder.h:296
SPA_POD_String
#define SPA_POD_String(val)
Definition vararg.h:102
spa_pod_parser_get_int
SPA_API_POD_PARSER int spa_pod_parser_get_int(struct spa_pod_parser *parser, int32_t *value)
Definition parser.h:236
spa_pod_parser_pop
SPA_API_POD_PARSER int spa_pod_parser_pop(struct spa_pod_parser *parser, struct spa_pod_frame *frame)
Definition parser.h:204
spa_pod_parser_pod
SPA_API_POD_PARSER void spa_pod_parser_pod(struct spa_pod_parser *parser, const struct spa_pod *pod)
Definition parser.h:60
spa_pod_builder_string
SPA_API_POD_BUILDER int spa_pod_builder_string(struct spa_pod_builder *builder, const char *str)
Definition builder.h:351
spa_pod_builder_push_struct
SPA_API_POD_BUILDER int spa_pod_builder_push_struct(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:462
spa_pod_builder_add
SPA_API_POD_BUILDER int spa_pod_builder_add(struct spa_pod_builder *builder,...)
Definition builder.h:713
spa_pod_builder_push_object
SPA_API_POD_BUILDER int spa_pod_builder_push_object(struct spa_pod_builder *builder, struct spa_pod_frame *frame, uint32_t type, uint32_t id)
Definition builder.h:475
spa_pod_parser_push_struct
SPA_API_POD_PARSER int spa_pod_parser_push_struct(struct spa_pod_parser *parser, struct spa_pod_frame *frame)
Definition parser.h:400
spa_pod_parser_get
SPA_API_POD_PARSER int spa_pod_parser_get(struct spa_pod_parser *parser,...)
Definition parser.h:849
spa_pod_builder_primitive
SPA_API_POD_BUILDER int spa_pod_builder_primitive(struct spa_pod_builder *builder, const struct spa_pod *p)
Definition builder.h:250
SPA_POD_PROP_FLAG_HINT_DICT
#define SPA_POD_PROP_FLAG_HINT_DICT
contains a dictionary struct as (Struct( Int : n_items,...
Definition pod.h:235
spa_pod_parse_object
#define spa_pod_parse_object(pod, type, id,...)
Definition parser.h:935
SPA_POD_PodStruct
#define SPA_POD_PodStruct(val)
Definition vararg.h:140
spa_pod_memcmp
SPA_API_POD_COMPARE int spa_pod_memcmp(const struct spa_pod *a, const struct spa_pod *b)
Definition compare.h:90
spa_pod_builder_prop
SPA_API_POD_BUILDER int spa_pod_builder_prop(struct spa_pod_builder *builder, uint32_t key, uint32_t flags)
Definition builder.h:490
SPA_TYPE_OBJECT_ParamDict
@ SPA_TYPE_OBJECT_ParamDict
Definition type.h:99
SPA_MIN
#define SPA_MIN(a, b)
Definition defs.h:165
iter.h
spa/pod/iter.h
dict.h
spa/param/dict.h
parser.h
spa/pod/parser.h
spa_dict_item
Definition dict.h:41
spa_dict_item::key
const char * key
Definition dict.h:42
spa_dict_item::value
const char * value
Definition dict.h:43
spa_dict
Definition dict.h:51
spa_dict::items
const struct spa_dict_item * items
Definition dict.h:56
spa_dict::n_items
uint32_t n_items
Definition dict.h:55
spa_param_dict_info
helper structure for managing info objects
Definition dict.h:35
spa_pod_builder
Definition builder.h:63
spa_pod_frame
Definition body.h:38
spa_pod_parser
Definition parser.h:44
spa_pod
Definition pod.h:57
dict.h
spa/utils/dict.h