prop_dictionary_util.c revision 1.9.8.1 1 1.9.8.1 perseant /* $NetBSD: prop_dictionary_util.c,v 1.9.8.1 2025/08/02 05:18:35 perseant Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.6 thorpej * Copyright (c) 2006, 2020 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej *
19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.1 thorpej */
31 1.1 thorpej
32 1.1 thorpej /*
33 1.1 thorpej * Utility routines to make it more convenient to work with values
34 1.1 thorpej * stored in dictionaries.
35 1.1 thorpej *
36 1.1 thorpej * Note: There is no special magic going on here. We use the standard
37 1.1 thorpej * proplib(3) APIs to do all of this work. Any application could do
38 1.1 thorpej * exactly what we're doing here.
39 1.1 thorpej */
40 1.1 thorpej
41 1.5 pooka #include "prop_object_impl.h" /* only to hide kernel vs. not-kernel */
42 1.1 thorpej #include <prop/proplib.h>
43 1.1 thorpej
44 1.9.8.1 perseant _PROP_EXPORT bool
45 1.6 thorpej prop_dictionary_get_dict(prop_dictionary_t dict, const char *key,
46 1.6 thorpej prop_dictionary_t *dp)
47 1.4 bouyer {
48 1.4 bouyer prop_object_t o;
49 1.6 thorpej
50 1.4 bouyer o = prop_dictionary_get(dict, key);
51 1.6 thorpej if (prop_object_type(o) != PROP_TYPE_DICTIONARY)
52 1.4 bouyer return false;
53 1.4 bouyer *dp = o;
54 1.4 bouyer return true;
55 1.4 bouyer
56 1.4 bouyer }
57 1.4 bouyer
58 1.9.8.1 perseant _PROP_EXPORT bool
59 1.6 thorpej prop_dictionary_get_bool(prop_dictionary_t dict, const char *key, bool *valp)
60 1.1 thorpej {
61 1.1 thorpej prop_bool_t b;
62 1.1 thorpej
63 1.1 thorpej b = prop_dictionary_get(dict, key);
64 1.1 thorpej if (prop_object_type(b) != PROP_TYPE_BOOL)
65 1.2 thorpej return (false);
66 1.9 riastrad
67 1.1 thorpej *valp = prop_bool_true(b);
68 1.1 thorpej
69 1.2 thorpej return (true);
70 1.1 thorpej }
71 1.1 thorpej
72 1.9.8.1 perseant _PROP_EXPORT bool
73 1.6 thorpej prop_dictionary_set_bool(prop_dictionary_t dict, const char *key, bool val)
74 1.1 thorpej {
75 1.1 thorpej
76 1.6 thorpej return prop_dictionary_set_and_rel(dict, key, prop_bool_create(val));
77 1.1 thorpej }
78 1.1 thorpej
79 1.6 thorpej #define TEMPLATE(name, typ) \
80 1.9.8.1 perseant _PROP_EXPORT bool \
81 1.6 thorpej prop_dictionary_get_ ## name (prop_dictionary_t dict, \
82 1.6 thorpej const char *key, \
83 1.6 thorpej typ *valp) \
84 1.1 thorpej { \
85 1.6 thorpej return prop_number_ ## name ## _value( \
86 1.6 thorpej prop_dictionary_get(dict, key), valp); \
87 1.6 thorpej }
88 1.6 thorpej TEMPLATE(schar, signed char)
89 1.6 thorpej TEMPLATE(short, short)
90 1.6 thorpej TEMPLATE(int, int)
91 1.6 thorpej TEMPLATE(long, long)
92 1.6 thorpej TEMPLATE(longlong, long long)
93 1.6 thorpej TEMPLATE(intptr, intptr_t)
94 1.6 thorpej TEMPLATE(int8, int8_t)
95 1.6 thorpej TEMPLATE(int16, int16_t)
96 1.6 thorpej TEMPLATE(int32, int32_t)
97 1.6 thorpej TEMPLATE(int64, int64_t)
98 1.6 thorpej
99 1.6 thorpej TEMPLATE(uchar, unsigned char)
100 1.6 thorpej TEMPLATE(ushort, unsigned short)
101 1.6 thorpej TEMPLATE(uint, unsigned int)
102 1.6 thorpej TEMPLATE(ulong, unsigned long)
103 1.6 thorpej TEMPLATE(ulonglong, unsigned long long)
104 1.6 thorpej TEMPLATE(uintptr, uintptr_t)
105 1.6 thorpej TEMPLATE(uint8, uint8_t)
106 1.6 thorpej TEMPLATE(uint16, uint16_t)
107 1.6 thorpej TEMPLATE(uint32, uint32_t)
108 1.6 thorpej TEMPLATE(uint64, uint64_t)
109 1.1 thorpej
110 1.1 thorpej #undef TEMPLATE
111 1.1 thorpej
112 1.6 thorpej static bool
113 1.6 thorpej prop_dictionary_set_signed_number(prop_dictionary_t dict, const char *key,
114 1.6 thorpej intmax_t val)
115 1.6 thorpej {
116 1.6 thorpej return prop_dictionary_set_and_rel(dict, key,
117 1.6 thorpej prop_number_create_signed(val));
118 1.6 thorpej }
119 1.6 thorpej
120 1.6 thorpej static bool
121 1.6 thorpej prop_dictionary_set_unsigned_number(prop_dictionary_t dict, const char *key,
122 1.6 thorpej uintmax_t val)
123 1.6 thorpej {
124 1.7 christos /*LINTED: for conversion from 'long long' to 'long'*/ \
125 1.6 thorpej return prop_dictionary_set_and_rel(dict, key,
126 1.6 thorpej prop_number_create_unsigned(val));
127 1.6 thorpej }
128 1.6 thorpej
129 1.6 thorpej #define TEMPLATE(name, which, typ) \
130 1.9.8.1 perseant _PROP_EXPORT bool \
131 1.6 thorpej prop_dictionary_set_ ## name (prop_dictionary_t dict, \
132 1.6 thorpej const char *key, \
133 1.6 thorpej typ val) \
134 1.1 thorpej { \
135 1.7 christos /*LINTED: for conversion from long long to 'long'*/ \
136 1.6 thorpej return prop_dictionary_set_ ## which ## _number(dict, key, val);\
137 1.1 thorpej }
138 1.1 thorpej
139 1.6 thorpej #define STEMPLATE(name, typ) TEMPLATE(name, signed, typ)
140 1.6 thorpej #define UTEMPLATE(name, typ) TEMPLATE(name, unsigned, typ)
141 1.6 thorpej
142 1.6 thorpej STEMPLATE(schar, signed char)
143 1.6 thorpej STEMPLATE(short, short)
144 1.6 thorpej STEMPLATE(int, int)
145 1.6 thorpej STEMPLATE(long, long)
146 1.6 thorpej STEMPLATE(longlong, long long)
147 1.6 thorpej STEMPLATE(intptr, intptr_t)
148 1.6 thorpej STEMPLATE(int8, int8_t)
149 1.6 thorpej STEMPLATE(int16, int16_t)
150 1.6 thorpej STEMPLATE(int32, int32_t)
151 1.6 thorpej STEMPLATE(int64, int64_t)
152 1.6 thorpej
153 1.6 thorpej UTEMPLATE(uchar, unsigned char)
154 1.6 thorpej UTEMPLATE(ushort, unsigned short)
155 1.6 thorpej UTEMPLATE(uint, unsigned int)
156 1.6 thorpej UTEMPLATE(ulong, unsigned long)
157 1.6 thorpej UTEMPLATE(ulonglong, unsigned long long)
158 1.6 thorpej UTEMPLATE(uintptr, uintptr_t)
159 1.6 thorpej UTEMPLATE(uint8, uint8_t)
160 1.6 thorpej UTEMPLATE(uint16, uint16_t)
161 1.6 thorpej UTEMPLATE(uint32, uint32_t)
162 1.6 thorpej UTEMPLATE(uint64, uint64_t)
163 1.1 thorpej
164 1.6 thorpej #undef STEMPLATE
165 1.6 thorpej #undef UTEMPLATE
166 1.1 thorpej #undef TEMPLATE
167 1.4 bouyer
168 1.9.8.1 perseant _PROP_EXPORT bool
169 1.6 thorpej prop_dictionary_get_string(prop_dictionary_t dict, const char *key,
170 1.6 thorpej const char **cpp)
171 1.6 thorpej {
172 1.6 thorpej prop_string_t str;
173 1.6 thorpej const char *cp;
174 1.6 thorpej
175 1.6 thorpej str = prop_dictionary_get(dict, key);
176 1.6 thorpej if (prop_object_type(str) != PROP_TYPE_STRING)
177 1.6 thorpej return (false);
178 1.6 thorpej
179 1.6 thorpej cp = prop_string_value(str);
180 1.6 thorpej if (cp == NULL)
181 1.6 thorpej return (false);
182 1.9 riastrad
183 1.6 thorpej *cpp = cp;
184 1.6 thorpej return (true);
185 1.6 thorpej }
186 1.6 thorpej
187 1.9.8.1 perseant _PROP_EXPORT bool
188 1.6 thorpej prop_dictionary_set_string(prop_dictionary_t dict, const char *key,
189 1.6 thorpej const char *cp)
190 1.6 thorpej {
191 1.6 thorpej return prop_dictionary_set_and_rel(dict, key,
192 1.6 thorpej prop_string_create_copy(cp));
193 1.6 thorpej }
194 1.6 thorpej
195 1.9.8.1 perseant _PROP_EXPORT bool
196 1.6 thorpej prop_dictionary_set_string_nocopy(prop_dictionary_t dict,
197 1.6 thorpej const char *key,
198 1.6 thorpej const char *cp)
199 1.6 thorpej {
200 1.6 thorpej return prop_dictionary_set_and_rel(dict, key,
201 1.6 thorpej prop_string_create_nocopy(cp));
202 1.6 thorpej }
203 1.6 thorpej
204 1.9.8.1 perseant _PROP_EXPORT bool
205 1.6 thorpej prop_dictionary_get_data(prop_dictionary_t dict, const char *key,
206 1.6 thorpej const void **vp, size_t *sizep)
207 1.6 thorpej {
208 1.6 thorpej prop_data_t data;
209 1.6 thorpej const void *v;
210 1.6 thorpej
211 1.6 thorpej data = prop_dictionary_get(dict, key);
212 1.6 thorpej if (prop_object_type(data) != PROP_TYPE_DATA)
213 1.6 thorpej return (false);
214 1.6 thorpej
215 1.6 thorpej v = prop_data_value(data);
216 1.6 thorpej if (v == NULL)
217 1.6 thorpej return (false);
218 1.6 thorpej
219 1.6 thorpej *vp = v;
220 1.6 thorpej if (sizep != NULL)
221 1.6 thorpej *sizep = prop_data_size(data);
222 1.6 thorpej return (true);
223 1.6 thorpej }
224 1.6 thorpej
225 1.9.8.1 perseant _PROP_EXPORT bool
226 1.6 thorpej prop_dictionary_set_data(prop_dictionary_t dict, const char *key,
227 1.6 thorpej const void *v, size_t size)
228 1.6 thorpej {
229 1.6 thorpej return prop_dictionary_set_and_rel(dict, key,
230 1.6 thorpej prop_data_create_copy(v, size));
231 1.6 thorpej }
232 1.6 thorpej
233 1.9.8.1 perseant _PROP_EXPORT bool
234 1.6 thorpej prop_dictionary_set_data_nocopy(prop_dictionary_t dict, const char *key,
235 1.6 thorpej const void *v, size_t size)
236 1.6 thorpej {
237 1.6 thorpej return prop_dictionary_set_and_rel(dict, key,
238 1.6 thorpej prop_data_create_nocopy(v, size));
239 1.6 thorpej }
240 1.6 thorpej
241 1.6 thorpej _PROP_DEPRECATED(prop_dictionary_get_cstring,
242 1.6 thorpej "this program uses prop_dictionary_get_cstring(), "
243 1.6 thorpej "which is deprecated; use prop_dictionary_get_string() and copy instead.")
244 1.9.8.1 perseant _PROP_EXPORT bool
245 1.6 thorpej prop_dictionary_get_cstring(prop_dictionary_t dict,
246 1.6 thorpej const char *key,
247 1.6 thorpej char **cpp)
248 1.6 thorpej {
249 1.6 thorpej prop_string_t str;
250 1.6 thorpej char *cp;
251 1.6 thorpej size_t len;
252 1.6 thorpej bool rv;
253 1.6 thorpej
254 1.6 thorpej str = prop_dictionary_get(dict, key);
255 1.6 thorpej if (prop_object_type(str) != PROP_TYPE_STRING)
256 1.6 thorpej return (false);
257 1.6 thorpej
258 1.6 thorpej len = prop_string_size(str);
259 1.6 thorpej cp = _PROP_MALLOC(len + 1, M_TEMP);
260 1.6 thorpej if (cp == NULL)
261 1.6 thorpej return (false);
262 1.6 thorpej
263 1.6 thorpej rv = prop_string_copy_value(str, cp, len + 1);
264 1.6 thorpej if (rv)
265 1.6 thorpej *cpp = cp;
266 1.6 thorpej else
267 1.6 thorpej _PROP_FREE(cp, M_TEMP);
268 1.6 thorpej
269 1.6 thorpej return (rv);
270 1.6 thorpej }
271 1.6 thorpej
272 1.6 thorpej _PROP_DEPRECATED(prop_string_get_cstring_nocopy,
273 1.6 thorpej "this program uses prop_string_get_cstring_nocopy(), "
274 1.6 thorpej "which is deprecated; use prop_dictionary_get_string() instead.")
275 1.9.8.1 perseant _PROP_EXPORT bool
276 1.6 thorpej prop_dictionary_get_cstring_nocopy(prop_dictionary_t dict,
277 1.6 thorpej const char *key,
278 1.6 thorpej const char **cpp)
279 1.6 thorpej {
280 1.6 thorpej return prop_dictionary_get_string(dict, key, cpp);
281 1.6 thorpej }
282 1.6 thorpej
283 1.6 thorpej _PROP_DEPRECATED(prop_dictionary_set_cstring,
284 1.6 thorpej "this program uses prop_dictionary_set_cstring(), "
285 1.6 thorpej "which is deprecated; use prop_dictionary_set_string() instead.")
286 1.9.8.1 perseant _PROP_EXPORT bool
287 1.6 thorpej prop_dictionary_set_cstring(prop_dictionary_t dict,
288 1.6 thorpej const char *key,
289 1.6 thorpej const char *cp)
290 1.6 thorpej {
291 1.6 thorpej return prop_dictionary_set_string(dict, key, cp);
292 1.6 thorpej }
293 1.6 thorpej
294 1.6 thorpej _PROP_DEPRECATED(prop_dictionary_set_cstring_nocopy,
295 1.6 thorpej "this program uses prop_dictionary_set_cstring_nocopy(), "
296 1.6 thorpej "which is deprecated; use prop_dictionary_set_string_nocopy() instead.")
297 1.9.8.1 perseant _PROP_EXPORT bool
298 1.6 thorpej prop_dictionary_set_cstring_nocopy(prop_dictionary_t dict,
299 1.6 thorpej const char *key,
300 1.6 thorpej const char *cp)
301 1.6 thorpej {
302 1.6 thorpej return prop_dictionary_set_string_nocopy(dict, key, cp);
303 1.6 thorpej }
304 1.6 thorpej
305 1.9.8.1 perseant _PROP_EXPORT bool
306 1.4 bouyer prop_dictionary_set_and_rel(prop_dictionary_t dict, const char *key,
307 1.6 thorpej prop_object_t po)
308 1.4 bouyer {
309 1.6 thorpej bool rv;
310 1.6 thorpej
311 1.4 bouyer if (po == NULL)
312 1.4 bouyer return false;
313 1.6 thorpej rv = prop_dictionary_set(dict, key, po);
314 1.4 bouyer prop_object_release(po);
315 1.6 thorpej return rv;
316 1.4 bouyer }
317