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