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