prop_array_util.c revision 1.1.4.2 1 1.1.4.2 wrstuden /* $NetBSD: prop_array_util.c,v 1.1.4.2 2008/06/23 05:01:56 wrstuden Exp $ */
2 1.1.4.2 wrstuden
3 1.1.4.2 wrstuden /*-
4 1.1.4.2 wrstuden * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 1.1.4.2 wrstuden * All rights reserved.
6 1.1.4.2 wrstuden *
7 1.1.4.2 wrstuden * This code is derived from software contributed to The NetBSD Foundation
8 1.1.4.2 wrstuden * by Jason R. Thorpe.
9 1.1.4.2 wrstuden *
10 1.1.4.2 wrstuden * Redistribution and use in source and binary forms, with or without
11 1.1.4.2 wrstuden * modification, are permitted provided that the following conditions
12 1.1.4.2 wrstuden * are met:
13 1.1.4.2 wrstuden * 1. Redistributions of source code must retain the above copyright
14 1.1.4.2 wrstuden * notice, this list of conditions and the following disclaimer.
15 1.1.4.2 wrstuden * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.4.2 wrstuden * notice, this list of conditions and the following disclaimer in the
17 1.1.4.2 wrstuden * documentation and/or other materials provided with the distribution.
18 1.1.4.2 wrstuden *
19 1.1.4.2 wrstuden * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.4.2 wrstuden * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.4.2 wrstuden * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.4.2 wrstuden * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.4.2 wrstuden * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.4.2 wrstuden * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.4.2 wrstuden * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.4.2 wrstuden * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.4.2 wrstuden * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.4.2 wrstuden * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.4.2 wrstuden * POSSIBILITY OF SUCH DAMAGE.
30 1.1.4.2 wrstuden */
31 1.1.4.2 wrstuden
32 1.1.4.2 wrstuden /*
33 1.1.4.2 wrstuden * Utility routines to make it more convenient to work with values
34 1.1.4.2 wrstuden * stored in array.
35 1.1.4.2 wrstuden *
36 1.1.4.2 wrstuden * Note: There is no special magic going on here. We use the standard
37 1.1.4.2 wrstuden * proplib(3) APIs to do all of this work. Any application could do
38 1.1.4.2 wrstuden * exactly what we're doing here.
39 1.1.4.2 wrstuden */
40 1.1.4.2 wrstuden
41 1.1.4.2 wrstuden #include <prop/proplib.h>
42 1.1.4.2 wrstuden #include "prop_object_impl.h" /* hide kernel vs. not-kernel vs. standalone */
43 1.1.4.2 wrstuden
44 1.1.4.2 wrstuden bool
45 1.1.4.2 wrstuden prop_array_get_bool(prop_array_t array,
46 1.1.4.2 wrstuden unsigned int indx,
47 1.1.4.2 wrstuden bool *valp)
48 1.1.4.2 wrstuden {
49 1.1.4.2 wrstuden prop_bool_t b;
50 1.1.4.2 wrstuden
51 1.1.4.2 wrstuden b = prop_array_get(array, indx);
52 1.1.4.2 wrstuden if (prop_object_type(b) != PROP_TYPE_BOOL)
53 1.1.4.2 wrstuden return (false);
54 1.1.4.2 wrstuden
55 1.1.4.2 wrstuden *valp = prop_bool_true(b);
56 1.1.4.2 wrstuden
57 1.1.4.2 wrstuden return (true);
58 1.1.4.2 wrstuden }
59 1.1.4.2 wrstuden
60 1.1.4.2 wrstuden bool
61 1.1.4.2 wrstuden prop_array_set_bool(prop_array_t array,
62 1.1.4.2 wrstuden unsigned int indx,
63 1.1.4.2 wrstuden bool val)
64 1.1.4.2 wrstuden {
65 1.1.4.2 wrstuden prop_bool_t b;
66 1.1.4.2 wrstuden int rv;
67 1.1.4.2 wrstuden
68 1.1.4.2 wrstuden b = prop_bool_create(val);
69 1.1.4.2 wrstuden if (b == NULL)
70 1.1.4.2 wrstuden return (false);
71 1.1.4.2 wrstuden rv = prop_array_set(array, indx, b);
72 1.1.4.2 wrstuden prop_object_release(b);
73 1.1.4.2 wrstuden
74 1.1.4.2 wrstuden return (rv);
75 1.1.4.2 wrstuden }
76 1.1.4.2 wrstuden
77 1.1.4.2 wrstuden #define TEMPLATE(size) \
78 1.1.4.2 wrstuden bool \
79 1.1.4.2 wrstuden prop_array_get_int ## size (prop_array_t array, \
80 1.1.4.2 wrstuden unsigned int indx, \
81 1.1.4.2 wrstuden int ## size ## _t *valp) \
82 1.1.4.2 wrstuden { \
83 1.1.4.2 wrstuden prop_number_t num; \
84 1.1.4.2 wrstuden \
85 1.1.4.2 wrstuden num = prop_array_get(array, indx); \
86 1.1.4.2 wrstuden if (prop_object_type(num) != PROP_TYPE_NUMBER) \
87 1.1.4.2 wrstuden return (false); \
88 1.1.4.2 wrstuden \
89 1.1.4.2 wrstuden if (prop_number_unsigned(num) && \
90 1.1.4.2 wrstuden prop_number_unsigned_integer_value(num) > \
91 1.1.4.2 wrstuden /*CONSTCOND*/((size) == 8 ? INT8_MAX : \
92 1.1.4.2 wrstuden (size) == 16 ? INT16_MAX : \
93 1.1.4.2 wrstuden (size) == 32 ? INT32_MAX : INT64_MAX)) { \
94 1.1.4.2 wrstuden return (false); \
95 1.1.4.2 wrstuden } \
96 1.1.4.2 wrstuden \
97 1.1.4.2 wrstuden if (prop_number_size(num) > (size)) \
98 1.1.4.2 wrstuden return (false); \
99 1.1.4.2 wrstuden \
100 1.1.4.2 wrstuden *valp = (int ## size ## _t) prop_number_integer_value(num); \
101 1.1.4.2 wrstuden \
102 1.1.4.2 wrstuden return (true); \
103 1.1.4.2 wrstuden } \
104 1.1.4.2 wrstuden \
105 1.1.4.2 wrstuden bool \
106 1.1.4.2 wrstuden prop_array_get_uint ## size (prop_array_t array, \
107 1.1.4.2 wrstuden unsigned int indx, \
108 1.1.4.2 wrstuden uint ## size ## _t *valp) \
109 1.1.4.2 wrstuden { \
110 1.1.4.2 wrstuden prop_number_t num; \
111 1.1.4.2 wrstuden \
112 1.1.4.2 wrstuden num = prop_array_get(array, indx); \
113 1.1.4.2 wrstuden if (prop_object_type(num) != PROP_TYPE_NUMBER) \
114 1.1.4.2 wrstuden return (false); \
115 1.1.4.2 wrstuden \
116 1.1.4.2 wrstuden if (prop_number_unsigned(num) == false && \
117 1.1.4.2 wrstuden prop_number_integer_value(num) < 0) { \
118 1.1.4.2 wrstuden return (false); \
119 1.1.4.2 wrstuden } \
120 1.1.4.2 wrstuden \
121 1.1.4.2 wrstuden if (prop_number_size(num) > (size)) \
122 1.1.4.2 wrstuden return (false); \
123 1.1.4.2 wrstuden \
124 1.1.4.2 wrstuden *valp = (uint ## size ## _t) \
125 1.1.4.2 wrstuden prop_number_unsigned_integer_value(num); \
126 1.1.4.2 wrstuden \
127 1.1.4.2 wrstuden return (true); \
128 1.1.4.2 wrstuden } \
129 1.1.4.2 wrstuden \
130 1.1.4.2 wrstuden bool \
131 1.1.4.2 wrstuden prop_array_set_int ## size (prop_array_t array, \
132 1.1.4.2 wrstuden unsigned int indx, \
133 1.1.4.2 wrstuden int ## size ## _t val) \
134 1.1.4.2 wrstuden { \
135 1.1.4.2 wrstuden prop_number_t num; \
136 1.1.4.2 wrstuden int rv; \
137 1.1.4.2 wrstuden \
138 1.1.4.2 wrstuden num = prop_number_create_integer((int64_t) val); \
139 1.1.4.2 wrstuden if (num == NULL) \
140 1.1.4.2 wrstuden return (false); \
141 1.1.4.2 wrstuden rv = prop_array_set(array, indx, num); \
142 1.1.4.2 wrstuden prop_object_release(num); \
143 1.1.4.2 wrstuden \
144 1.1.4.2 wrstuden return (rv); \
145 1.1.4.2 wrstuden } \
146 1.1.4.2 wrstuden \
147 1.1.4.2 wrstuden bool \
148 1.1.4.2 wrstuden prop_array_set_uint ## size (prop_array_t array, \
149 1.1.4.2 wrstuden unsigned int indx, \
150 1.1.4.2 wrstuden uint ## size ## _t val) \
151 1.1.4.2 wrstuden { \
152 1.1.4.2 wrstuden prop_number_t num; \
153 1.1.4.2 wrstuden int rv; \
154 1.1.4.2 wrstuden \
155 1.1.4.2 wrstuden num = prop_number_create_unsigned_integer((uint64_t) val); \
156 1.1.4.2 wrstuden if (num == NULL) \
157 1.1.4.2 wrstuden return (false); \
158 1.1.4.2 wrstuden rv = prop_array_set(array, indx, num); \
159 1.1.4.2 wrstuden prop_object_release(num); \
160 1.1.4.2 wrstuden \
161 1.1.4.2 wrstuden return (rv); \
162 1.1.4.2 wrstuden }
163 1.1.4.2 wrstuden
164 1.1.4.2 wrstuden TEMPLATE(8)
165 1.1.4.2 wrstuden TEMPLATE(16)
166 1.1.4.2 wrstuden TEMPLATE(32)
167 1.1.4.2 wrstuden TEMPLATE(64)
168 1.1.4.2 wrstuden
169 1.1.4.2 wrstuden #undef TEMPLATE
170 1.1.4.2 wrstuden
171 1.1.4.2 wrstuden #define TEMPLATE(variant, qualifier) \
172 1.1.4.2 wrstuden bool \
173 1.1.4.2 wrstuden prop_array_get_cstring ## variant (prop_array_t array, \
174 1.1.4.2 wrstuden unsigned int indx, \
175 1.1.4.2 wrstuden qualifier char **cpp) \
176 1.1.4.2 wrstuden { \
177 1.1.4.2 wrstuden prop_string_t str; \
178 1.1.4.2 wrstuden \
179 1.1.4.2 wrstuden str = prop_array_get(array, indx); \
180 1.1.4.2 wrstuden if (prop_object_type(str) != PROP_TYPE_STRING) \
181 1.1.4.2 wrstuden return (false); \
182 1.1.4.2 wrstuden \
183 1.1.4.2 wrstuden *cpp = prop_string_cstring ## variant (str); \
184 1.1.4.2 wrstuden \
185 1.1.4.2 wrstuden return (*cpp == NULL ? false : true); \
186 1.1.4.2 wrstuden } \
187 1.1.4.2 wrstuden \
188 1.1.4.2 wrstuden bool \
189 1.1.4.2 wrstuden prop_array_set_cstring ## variant (prop_array_t array, \
190 1.1.4.2 wrstuden unsigned int indx, \
191 1.1.4.2 wrstuden const char *cp) \
192 1.1.4.2 wrstuden { \
193 1.1.4.2 wrstuden prop_string_t str; \
194 1.1.4.2 wrstuden int rv; \
195 1.1.4.2 wrstuden \
196 1.1.4.2 wrstuden str = prop_string_create_cstring ## variant (cp); \
197 1.1.4.2 wrstuden if (str == NULL) \
198 1.1.4.2 wrstuden return (false); \
199 1.1.4.2 wrstuden rv = prop_array_set(array, indx, str); \
200 1.1.4.2 wrstuden prop_object_release(str); \
201 1.1.4.2 wrstuden \
202 1.1.4.2 wrstuden return (rv); \
203 1.1.4.2 wrstuden }
204 1.1.4.2 wrstuden
205 1.1.4.2 wrstuden TEMPLATE(,)
206 1.1.4.2 wrstuden TEMPLATE(_nocopy,const)
207 1.1.4.2 wrstuden
208 1.1.4.2 wrstuden #undef TEMPLATE
209