xref: /netbsd-src/common/lib/libprop/prop_dictionary_util.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: prop_dictionary_util.c,v 1.2 2007/08/16 16:28:18 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the NetBSD
21  *      Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Utility routines to make it more convenient to work with values
41  * stored in dictionaries.
42  *
43  * Note: There is no special magic going on here.  We use the standard
44  * proplib(3) APIs to do all of this work.  Any application could do
45  * exactly what we're doing here.
46  */
47 
48 #include <prop/proplib.h>
49 #include "prop_object_impl.h"	/* only to hide kernel vs. not-kernel */
50 
51 bool
52 prop_dictionary_get_bool(prop_dictionary_t dict,
53 			 const char *key,
54 			 bool *valp)
55 {
56 	prop_bool_t b;
57 
58 	b = prop_dictionary_get(dict, key);
59 	if (prop_object_type(b) != PROP_TYPE_BOOL)
60 		return (false);
61 
62 	*valp = prop_bool_true(b);
63 
64 	return (true);
65 }
66 
67 bool
68 prop_dictionary_set_bool(prop_dictionary_t dict,
69 			 const char *key,
70 			 bool val)
71 {
72 	prop_bool_t b;
73 	int rv;
74 
75 	b = prop_bool_create(val);
76 	if (b == NULL)
77 		return (false);
78 	rv = prop_dictionary_set(dict, key, b);
79 	prop_object_release(b);
80 
81 	return (rv);
82 }
83 
84 #define	TEMPLATE(size)							\
85 bool								\
86 prop_dictionary_get_int ## size (prop_dictionary_t dict,		\
87 				 const char *key,			\
88 				 int ## size ## _t *valp)		\
89 {									\
90 	prop_number_t num;						\
91 									\
92 	num = prop_dictionary_get(dict, key);				\
93 	if (prop_object_type(num) != PROP_TYPE_NUMBER)			\
94 		return (false);						\
95 									\
96 	if (prop_number_unsigned(num) &&				\
97 	    prop_number_unsigned_integer_value(num) >			\
98 	   /*CONSTCOND*/((size) ==  8 ?  INT8_MAX :			\
99 			 (size) == 16 ? INT16_MAX :			\
100 			 (size) == 32 ? INT32_MAX : INT64_MAX)) {	\
101 		return (false);						\
102 	}								\
103 									\
104 	if (prop_number_size(num) > (size))				\
105 		return (false);						\
106 									\
107 	*valp = (int ## size ## _t) prop_number_integer_value(num);	\
108 									\
109 	return (true);							\
110 }									\
111 									\
112 bool								\
113 prop_dictionary_get_uint ## size (prop_dictionary_t dict,		\
114 				  const char *key,			\
115 				  uint ## size ## _t *valp)		\
116 {									\
117 	prop_number_t num;						\
118 									\
119 	num = prop_dictionary_get(dict, key);				\
120 	if (prop_object_type(num) != PROP_TYPE_NUMBER)			\
121 		return (false);						\
122 									\
123 	if (prop_number_unsigned(num) == false &&			\
124 	    prop_number_integer_value(num) < 0) {			\
125 		return (false);						\
126 	}								\
127 									\
128 	if (prop_number_size(num) > (size))				\
129 		return (false);						\
130 									\
131 	*valp = (uint ## size ## _t)					\
132 	    prop_number_unsigned_integer_value(num);			\
133 									\
134 	return (true);							\
135 }									\
136 									\
137 bool								\
138 prop_dictionary_set_int ## size (prop_dictionary_t dict,		\
139 				 const char *key,			\
140 				 int ## size ## _t val)			\
141 {									\
142 	prop_number_t num;						\
143 	int rv;								\
144 									\
145 	num = prop_number_create_integer((int64_t) val);		\
146 	if (num == NULL)						\
147 		return (false);						\
148 	rv = prop_dictionary_set(dict, key, num);			\
149 	prop_object_release(num);					\
150 									\
151 	return (rv);							\
152 }									\
153 									\
154 bool								\
155 prop_dictionary_set_uint ## size (prop_dictionary_t dict,		\
156 				  const char *key,			\
157 				  uint ## size ## _t val)		\
158 {									\
159 	prop_number_t num;						\
160 	int rv;								\
161 									\
162 	num = prop_number_create_unsigned_integer((uint64_t) val);	\
163 	if (num == NULL)						\
164 		return (false);						\
165 	rv = prop_dictionary_set(dict, key, num);			\
166 	prop_object_release(num);					\
167 									\
168 	return (rv);							\
169 }
170 
171 TEMPLATE(8)
172 TEMPLATE(16)
173 TEMPLATE(32)
174 TEMPLATE(64)
175 
176 #undef TEMPLATE
177 
178 #define	TEMPLATE(variant, qualifier)					\
179 bool								\
180 prop_dictionary_get_cstring ## variant (prop_dictionary_t dict,		\
181 					const char *key,		\
182 					qualifier char **cpp)		\
183 {									\
184 	prop_string_t str;						\
185 									\
186 	str = prop_dictionary_get(dict, key);				\
187 	if (prop_object_type(str) != PROP_TYPE_STRING)			\
188 		return (false);						\
189 									\
190 	*cpp = prop_string_cstring ## variant (str);			\
191 									\
192 	return (*cpp == NULL ? false : true);				\
193 }									\
194 									\
195 bool								\
196 prop_dictionary_set_cstring ## variant (prop_dictionary_t dict,		\
197 					const char *key,		\
198 					const char *cp)			\
199 {									\
200 	prop_string_t str;						\
201 	int rv;								\
202 									\
203 	str = prop_string_create_cstring ## variant (cp);		\
204 	if (str == NULL)						\
205 		return (false);						\
206 	rv = prop_dictionary_set(dict, key, str);			\
207 	prop_object_release(str);					\
208 									\
209 	return (rv);							\
210 }
211 
212 TEMPLATE(,)
213 TEMPLATE(_nocopy,const)
214 
215 #undef TEMPLATE
216