prop_dictionary_util.c revision 1.7 1 1.7 christos /* $NetBSD: prop_dictionary_util.c,v 1.7 2020/06/14 21:31:01 christos 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.2 thorpej 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.4 bouyer 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.1 thorpej
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.2 thorpej 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.6 thorpej 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.6 thorpej 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.7 christos /*###155 [lint] warning conversion to 'unsigned long' due to prototype, arg #3 [259]%%%*/
158 1.7 christos /*###155 [lint] warning conversion from 'unsigned long long' to 'unsigned long' may lose accuracy, arg #3 [298]%%%*/
159 1.6 thorpej UTEMPLATE(ulonglong, unsigned long long)
160 1.6 thorpej UTEMPLATE(uintptr, uintptr_t)
161 1.6 thorpej UTEMPLATE(uint8, uint8_t)
162 1.6 thorpej UTEMPLATE(uint16, uint16_t)
163 1.6 thorpej UTEMPLATE(uint32, uint32_t)
164 1.6 thorpej UTEMPLATE(uint64, uint64_t)
165 1.1 thorpej
166 1.6 thorpej #undef STEMPLATE
167 1.6 thorpej #undef UTEMPLATE
168 1.1 thorpej #undef TEMPLATE
169 1.4 bouyer
170 1.4 bouyer bool
171 1.6 thorpej prop_dictionary_get_string(prop_dictionary_t dict, const char *key,
172 1.6 thorpej const char **cpp)
173 1.6 thorpej {
174 1.6 thorpej prop_string_t str;
175 1.6 thorpej const char *cp;
176 1.6 thorpej
177 1.6 thorpej str = prop_dictionary_get(dict, key);
178 1.6 thorpej if (prop_object_type(str) != PROP_TYPE_STRING)
179 1.6 thorpej return (false);
180 1.6 thorpej
181 1.6 thorpej cp = prop_string_value(str);
182 1.6 thorpej if (cp == NULL)
183 1.6 thorpej return (false);
184 1.6 thorpej
185 1.6 thorpej *cpp = cp;
186 1.6 thorpej return (true);
187 1.6 thorpej }
188 1.6 thorpej
189 1.6 thorpej bool
190 1.6 thorpej prop_dictionary_set_string(prop_dictionary_t dict, const char *key,
191 1.6 thorpej const char *cp)
192 1.6 thorpej {
193 1.6 thorpej return prop_dictionary_set_and_rel(dict, key,
194 1.6 thorpej prop_string_create_copy(cp));
195 1.6 thorpej }
196 1.6 thorpej
197 1.6 thorpej bool
198 1.6 thorpej prop_dictionary_set_string_nocopy(prop_dictionary_t dict,
199 1.6 thorpej const char *key,
200 1.6 thorpej const char *cp)
201 1.6 thorpej {
202 1.6 thorpej return prop_dictionary_set_and_rel(dict, key,
203 1.6 thorpej prop_string_create_nocopy(cp));
204 1.6 thorpej }
205 1.6 thorpej
206 1.6 thorpej bool
207 1.6 thorpej prop_dictionary_get_data(prop_dictionary_t dict, const char *key,
208 1.6 thorpej const void **vp, size_t *sizep)
209 1.6 thorpej {
210 1.6 thorpej prop_data_t data;
211 1.6 thorpej const void *v;
212 1.6 thorpej
213 1.6 thorpej data = prop_dictionary_get(dict, key);
214 1.6 thorpej if (prop_object_type(data) != PROP_TYPE_DATA)
215 1.6 thorpej return (false);
216 1.6 thorpej
217 1.6 thorpej v = prop_data_value(data);
218 1.6 thorpej if (v == NULL)
219 1.6 thorpej return (false);
220 1.6 thorpej
221 1.6 thorpej *vp = v;
222 1.6 thorpej if (sizep != NULL)
223 1.6 thorpej *sizep = prop_data_size(data);
224 1.6 thorpej return (true);
225 1.6 thorpej }
226 1.6 thorpej
227 1.6 thorpej bool
228 1.6 thorpej prop_dictionary_set_data(prop_dictionary_t dict, const char *key,
229 1.6 thorpej const void *v, size_t size)
230 1.6 thorpej {
231 1.6 thorpej return prop_dictionary_set_and_rel(dict, key,
232 1.6 thorpej prop_data_create_copy(v, size));
233 1.6 thorpej }
234 1.6 thorpej
235 1.6 thorpej bool
236 1.6 thorpej prop_dictionary_set_data_nocopy(prop_dictionary_t dict, const char *key,
237 1.6 thorpej const void *v, size_t size)
238 1.6 thorpej {
239 1.6 thorpej return prop_dictionary_set_and_rel(dict, key,
240 1.6 thorpej prop_data_create_nocopy(v, size));
241 1.6 thorpej }
242 1.6 thorpej
243 1.6 thorpej _PROP_DEPRECATED(prop_dictionary_get_cstring,
244 1.6 thorpej "this program uses prop_dictionary_get_cstring(), "
245 1.6 thorpej "which is deprecated; use prop_dictionary_get_string() and copy instead.")
246 1.6 thorpej bool
247 1.6 thorpej prop_dictionary_get_cstring(prop_dictionary_t dict,
248 1.6 thorpej const char *key,
249 1.6 thorpej char **cpp)
250 1.6 thorpej {
251 1.6 thorpej prop_string_t str;
252 1.6 thorpej char *cp;
253 1.6 thorpej size_t len;
254 1.6 thorpej bool rv;
255 1.6 thorpej
256 1.6 thorpej str = prop_dictionary_get(dict, key);
257 1.6 thorpej if (prop_object_type(str) != PROP_TYPE_STRING)
258 1.6 thorpej return (false);
259 1.6 thorpej
260 1.6 thorpej len = prop_string_size(str);
261 1.6 thorpej cp = _PROP_MALLOC(len + 1, M_TEMP);
262 1.6 thorpej if (cp == NULL)
263 1.6 thorpej return (false);
264 1.6 thorpej
265 1.6 thorpej rv = prop_string_copy_value(str, cp, len + 1);
266 1.6 thorpej if (rv)
267 1.6 thorpej *cpp = cp;
268 1.6 thorpej else
269 1.6 thorpej _PROP_FREE(cp, M_TEMP);
270 1.6 thorpej
271 1.6 thorpej return (rv);
272 1.6 thorpej }
273 1.6 thorpej
274 1.6 thorpej _PROP_DEPRECATED(prop_string_get_cstring_nocopy,
275 1.6 thorpej "this program uses prop_string_get_cstring_nocopy(), "
276 1.6 thorpej "which is deprecated; use prop_dictionary_get_string() instead.")
277 1.6 thorpej bool
278 1.6 thorpej prop_dictionary_get_cstring_nocopy(prop_dictionary_t dict,
279 1.6 thorpej const char *key,
280 1.6 thorpej const char **cpp)
281 1.6 thorpej {
282 1.6 thorpej return prop_dictionary_get_string(dict, key, cpp);
283 1.6 thorpej }
284 1.6 thorpej
285 1.6 thorpej _PROP_DEPRECATED(prop_dictionary_set_cstring,
286 1.6 thorpej "this program uses prop_dictionary_set_cstring(), "
287 1.6 thorpej "which is deprecated; use prop_dictionary_set_string() instead.")
288 1.6 thorpej bool
289 1.6 thorpej prop_dictionary_set_cstring(prop_dictionary_t dict,
290 1.6 thorpej const char *key,
291 1.6 thorpej const char *cp)
292 1.6 thorpej {
293 1.6 thorpej return prop_dictionary_set_string(dict, key, cp);
294 1.6 thorpej }
295 1.6 thorpej
296 1.6 thorpej _PROP_DEPRECATED(prop_dictionary_set_cstring_nocopy,
297 1.6 thorpej "this program uses prop_dictionary_set_cstring_nocopy(), "
298 1.6 thorpej "which is deprecated; use prop_dictionary_set_string_nocopy() instead.")
299 1.6 thorpej bool
300 1.6 thorpej prop_dictionary_set_cstring_nocopy(prop_dictionary_t dict,
301 1.6 thorpej const char *key,
302 1.6 thorpej const char *cp)
303 1.6 thorpej {
304 1.6 thorpej return prop_dictionary_set_string_nocopy(dict, key, cp);
305 1.6 thorpej }
306 1.6 thorpej
307 1.6 thorpej bool
308 1.4 bouyer prop_dictionary_set_and_rel(prop_dictionary_t dict, const char *key,
309 1.6 thorpej prop_object_t po)
310 1.4 bouyer {
311 1.6 thorpej bool rv;
312 1.6 thorpej
313 1.4 bouyer if (po == NULL)
314 1.4 bouyer return false;
315 1.6 thorpej rv = prop_dictionary_set(dict, key, po);
316 1.4 bouyer prop_object_release(po);
317 1.6 thorpej return rv;
318 1.4 bouyer }
319