xref: /minix3/common/lib/libprop/prop_array_util.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1*f14fb602SLionel Sambuc /*	$NetBSD: prop_array_util.c,v 1.4 2012/07/27 09:10:59 pooka Exp $	*/
26b6d114aSBen Gras 
36b6d114aSBen Gras /*-
46b6d114aSBen Gras  * Copyright (c) 2006 The NetBSD Foundation, Inc.
56b6d114aSBen Gras  * All rights reserved.
66b6d114aSBen Gras  *
76b6d114aSBen Gras  * This code is derived from software contributed to The NetBSD Foundation
86b6d114aSBen Gras  * by Jason R. Thorpe.
96b6d114aSBen Gras  *
106b6d114aSBen Gras  * Redistribution and use in source and binary forms, with or without
116b6d114aSBen Gras  * modification, are permitted provided that the following conditions
126b6d114aSBen Gras  * are met:
136b6d114aSBen Gras  * 1. Redistributions of source code must retain the above copyright
146b6d114aSBen Gras  *    notice, this list of conditions and the following disclaimer.
156b6d114aSBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
166b6d114aSBen Gras  *    notice, this list of conditions and the following disclaimer in the
176b6d114aSBen Gras  *    documentation and/or other materials provided with the distribution.
186b6d114aSBen Gras  *
196b6d114aSBen Gras  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
206b6d114aSBen Gras  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
216b6d114aSBen Gras  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
226b6d114aSBen Gras  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
236b6d114aSBen Gras  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
246b6d114aSBen Gras  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
256b6d114aSBen Gras  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
266b6d114aSBen Gras  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
276b6d114aSBen Gras  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
286b6d114aSBen Gras  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
296b6d114aSBen Gras  * POSSIBILITY OF SUCH DAMAGE.
306b6d114aSBen Gras  */
316b6d114aSBen Gras 
326b6d114aSBen Gras /*
336b6d114aSBen Gras  * Utility routines to make it more convenient to work with values
346b6d114aSBen Gras  * stored in array.
356b6d114aSBen Gras  *
366b6d114aSBen Gras  * Note: There is no special magic going on here.  We use the standard
376b6d114aSBen Gras  * proplib(3) APIs to do all of this work.  Any application could do
386b6d114aSBen Gras  * exactly what we're doing here.
396b6d114aSBen Gras  */
406b6d114aSBen Gras 
416b6d114aSBen Gras #include "prop_object_impl.h" /* hide kernel vs. not-kernel vs. standalone */
42*f14fb602SLionel Sambuc #include <prop/proplib.h>
436b6d114aSBen Gras 
446b6d114aSBen Gras bool
prop_array_get_bool(prop_array_t array,unsigned int indx,bool * valp)456b6d114aSBen Gras prop_array_get_bool(prop_array_t array,
466b6d114aSBen Gras 			 unsigned int indx,
476b6d114aSBen Gras 			 bool *valp)
486b6d114aSBen Gras {
496b6d114aSBen Gras 	prop_bool_t b;
506b6d114aSBen Gras 
516b6d114aSBen Gras 	b = prop_array_get(array, indx);
526b6d114aSBen Gras 	if (prop_object_type(b) != PROP_TYPE_BOOL)
536b6d114aSBen Gras 		return (false);
546b6d114aSBen Gras 
556b6d114aSBen Gras 	*valp = prop_bool_true(b);
566b6d114aSBen Gras 
576b6d114aSBen Gras 	return (true);
586b6d114aSBen Gras }
596b6d114aSBen Gras 
606b6d114aSBen Gras bool
prop_array_set_bool(prop_array_t array,unsigned int indx,bool val)616b6d114aSBen Gras prop_array_set_bool(prop_array_t array,
626b6d114aSBen Gras 			 unsigned int indx,
636b6d114aSBen Gras 			 bool val)
646b6d114aSBen Gras {
656b6d114aSBen Gras 	prop_bool_t b;
666b6d114aSBen Gras 	int rv;
676b6d114aSBen Gras 
686b6d114aSBen Gras 	b = prop_bool_create(val);
696b6d114aSBen Gras 	if (b == NULL)
706b6d114aSBen Gras 		return (false);
716b6d114aSBen Gras 	rv = prop_array_set(array, indx, b);
726b6d114aSBen Gras 	prop_object_release(b);
736b6d114aSBen Gras 
746b6d114aSBen Gras 	return (rv);
756b6d114aSBen Gras }
766b6d114aSBen Gras 
776b6d114aSBen Gras #define	TEMPLATE(size)							\
786b6d114aSBen Gras bool							                \
796b6d114aSBen Gras prop_array_get_int ## size (prop_array_t array,				\
806b6d114aSBen Gras 				 unsigned int indx,			\
816b6d114aSBen Gras 				 int ## size ## _t *valp)		\
826b6d114aSBen Gras {									\
836b6d114aSBen Gras 	prop_number_t num;						\
846b6d114aSBen Gras 									\
856b6d114aSBen Gras 	num = prop_array_get(array, indx);				\
866b6d114aSBen Gras 	if (prop_object_type(num) != PROP_TYPE_NUMBER)			\
876b6d114aSBen Gras 		return (false);						\
886b6d114aSBen Gras 									\
896b6d114aSBen Gras 	if (prop_number_unsigned(num) &&				\
906b6d114aSBen Gras 	    prop_number_unsigned_integer_value(num) >			\
916b6d114aSBen Gras 	   /*CONSTCOND*/((size) ==  8 ?  INT8_MAX :			\
926b6d114aSBen Gras 			 (size) == 16 ? INT16_MAX :			\
936b6d114aSBen Gras 			 (size) == 32 ? INT32_MAX : INT64_MAX)) {	\
946b6d114aSBen Gras 		return (false);						\
956b6d114aSBen Gras 	}								\
966b6d114aSBen Gras 									\
976b6d114aSBen Gras 	if (prop_number_size(num) > (size))				\
986b6d114aSBen Gras 		return (false);						\
996b6d114aSBen Gras 									\
1006b6d114aSBen Gras 	*valp = (int ## size ## _t) prop_number_integer_value(num);	\
1016b6d114aSBen Gras 									\
1026b6d114aSBen Gras 	return (true);							\
1036b6d114aSBen Gras }									\
1046b6d114aSBen Gras 									\
1056b6d114aSBen Gras bool								        \
1066b6d114aSBen Gras prop_array_get_uint ## size (prop_array_t array,		        \
1076b6d114aSBen Gras 				  unsigned int indx,			\
1086b6d114aSBen Gras 				  uint ## size ## _t *valp)		\
1096b6d114aSBen Gras {									\
1106b6d114aSBen Gras 	prop_number_t num;						\
1116b6d114aSBen Gras 									\
1126b6d114aSBen Gras 	num = prop_array_get(array, indx);				\
1136b6d114aSBen Gras 	if (prop_object_type(num) != PROP_TYPE_NUMBER)			\
1146b6d114aSBen Gras 		return (false);						\
1156b6d114aSBen Gras 									\
1166b6d114aSBen Gras 	if (prop_number_unsigned(num) == false &&			\
1176b6d114aSBen Gras 	    prop_number_integer_value(num) < 0) {			\
1186b6d114aSBen Gras 		return (false);						\
1196b6d114aSBen Gras 	}								\
1206b6d114aSBen Gras 									\
1216b6d114aSBen Gras 	if (prop_number_size(num) > (size))				\
1226b6d114aSBen Gras 		return (false);						\
1236b6d114aSBen Gras 									\
1246b6d114aSBen Gras 	*valp = (uint ## size ## _t)					\
1256b6d114aSBen Gras 	    prop_number_unsigned_integer_value(num);			\
1266b6d114aSBen Gras 									\
1276b6d114aSBen Gras 	return (true);							\
1286b6d114aSBen Gras }									\
1296b6d114aSBen Gras 									\
1306b6d114aSBen Gras bool									\
1316b6d114aSBen Gras  prop_array_set_int ## size (prop_array_t array,			\
1326b6d114aSBen Gras 				 unsigned int indx,			\
1336b6d114aSBen Gras 				 int ## size ## _t val)			\
1346b6d114aSBen Gras {									\
1356b6d114aSBen Gras 	prop_number_t num;						\
1366b6d114aSBen Gras 	int rv;								\
1376b6d114aSBen Gras 									\
1386b6d114aSBen Gras 	num = prop_number_create_integer((int64_t) val);		\
1396b6d114aSBen Gras 	if (num == NULL)						\
1406b6d114aSBen Gras 		return (false);						\
1416b6d114aSBen Gras 	rv = prop_array_set(array, indx, num);				\
1426b6d114aSBen Gras 	prop_object_release(num);					\
1436b6d114aSBen Gras 									\
1446b6d114aSBen Gras 	return (rv);							\
1456b6d114aSBen Gras }									\
1466b6d114aSBen Gras 									\
1476b6d114aSBen Gras bool									\
1486b6d114aSBen Gras prop_array_set_uint ## size (prop_array_t array,			\
1496b6d114aSBen Gras 				  unsigned int indx,			\
1506b6d114aSBen Gras 				  uint ## size ## _t val)		\
1516b6d114aSBen Gras {									\
1526b6d114aSBen Gras 	prop_number_t num;						\
1536b6d114aSBen Gras 	int rv;								\
1546b6d114aSBen Gras 									\
1556b6d114aSBen Gras 	num = prop_number_create_unsigned_integer((uint64_t) val);	\
1566b6d114aSBen Gras 	if (num == NULL)						\
1576b6d114aSBen Gras 		return (false);						\
1586b6d114aSBen Gras 	rv = prop_array_set(array, indx, num);				\
1596b6d114aSBen Gras 	prop_object_release(num);					\
1606b6d114aSBen Gras 									\
1616b6d114aSBen Gras 	return (rv);							\
1626b6d114aSBen Gras }									\
1636b6d114aSBen Gras 									\
1646b6d114aSBen Gras bool									\
1656b6d114aSBen Gras prop_array_add_int ## size (prop_array_t array,				\
1666b6d114aSBen Gras 				 int ## size ## _t val)			\
1676b6d114aSBen Gras {									\
1686b6d114aSBen Gras 	prop_number_t num;						\
1696b6d114aSBen Gras 	int rv;								\
1706b6d114aSBen Gras 									\
1716b6d114aSBen Gras 	num = prop_number_create_integer((int64_t) val);		\
1726b6d114aSBen Gras 	if (num == NULL)						\
1736b6d114aSBen Gras 		return (false);						\
1746b6d114aSBen Gras 	rv = prop_array_add(array, num);				\
1756b6d114aSBen Gras 	prop_object_release(num);					\
1766b6d114aSBen Gras 									\
1776b6d114aSBen Gras 	return (rv);							\
1786b6d114aSBen Gras }									\
1796b6d114aSBen Gras 									\
1806b6d114aSBen Gras bool									\
1816b6d114aSBen Gras prop_array_add_uint ## size (prop_array_t array,			\
1826b6d114aSBen Gras 				  uint ## size ## _t val)		\
1836b6d114aSBen Gras {									\
1846b6d114aSBen Gras 	prop_number_t num;						\
1856b6d114aSBen Gras 	int rv;								\
1866b6d114aSBen Gras 									\
1876b6d114aSBen Gras 	num = prop_number_create_integer((int64_t) val);		\
1886b6d114aSBen Gras 	if (num == NULL)						\
1896b6d114aSBen Gras 		return (false);						\
1906b6d114aSBen Gras 	rv = prop_array_add(array, num);				\
1916b6d114aSBen Gras 	prop_object_release(num);					\
1926b6d114aSBen Gras 									\
1936b6d114aSBen Gras 	return (rv);							\
1946b6d114aSBen Gras }
1956b6d114aSBen Gras 
1966b6d114aSBen Gras TEMPLATE(8)
1976b6d114aSBen Gras TEMPLATE(16)
1986b6d114aSBen Gras TEMPLATE(32)
1996b6d114aSBen Gras TEMPLATE(64)
2006b6d114aSBen Gras 
2016b6d114aSBen Gras #undef TEMPLATE
2026b6d114aSBen Gras 
2036b6d114aSBen Gras #define	TEMPLATE(variant, qualifier)					\
2046b6d114aSBen Gras bool								        \
2056b6d114aSBen Gras prop_array_get_cstring ## variant (prop_array_t array,		        \
2066b6d114aSBen Gras 					unsigned int indx,		\
2076b6d114aSBen Gras 					qualifier char **cpp)		\
2086b6d114aSBen Gras {									\
2096b6d114aSBen Gras 	prop_string_t str;						\
2106b6d114aSBen Gras 									\
2116b6d114aSBen Gras 	str = prop_array_get(array, indx);				\
2126b6d114aSBen Gras 	if (prop_object_type(str) != PROP_TYPE_STRING)			\
2136b6d114aSBen Gras 		return (false);						\
2146b6d114aSBen Gras 									\
2156b6d114aSBen Gras 	*cpp = prop_string_cstring ## variant (str);			\
2166b6d114aSBen Gras 									\
2176b6d114aSBen Gras 	return (*cpp == NULL ? false : true);				\
2186b6d114aSBen Gras }									\
2196b6d114aSBen Gras 									\
2206b6d114aSBen Gras bool									\
2216b6d114aSBen Gras prop_array_set_cstring ## variant (prop_array_t array,			\
2226b6d114aSBen Gras 					unsigned int indx,		\
2236b6d114aSBen Gras 					const char *cp)			\
2246b6d114aSBen Gras {									\
2256b6d114aSBen Gras 	prop_string_t str;						\
2266b6d114aSBen Gras 	int rv;								\
2276b6d114aSBen Gras 									\
2286b6d114aSBen Gras 	str = prop_string_create_cstring ## variant (cp);		\
2296b6d114aSBen Gras 	if (str == NULL)						\
2306b6d114aSBen Gras 		return (false);						\
2316b6d114aSBen Gras 	rv = prop_array_set(array, indx, str);				\
2326b6d114aSBen Gras 	prop_object_release(str);					\
2336b6d114aSBen Gras 									\
2346b6d114aSBen Gras 	return (rv);							\
2356b6d114aSBen Gras }
2366b6d114aSBen Gras 
2376b6d114aSBen Gras TEMPLATE(,)
TEMPLATE(_nocopy,const)2386b6d114aSBen Gras TEMPLATE(_nocopy,const)
2396b6d114aSBen Gras 
2406b6d114aSBen Gras #undef TEMPLATE
2416b6d114aSBen Gras 
2426b6d114aSBen Gras bool
2436b6d114aSBen Gras prop_array_add_and_rel(prop_array_t array, prop_object_t po)
2446b6d114aSBen Gras {
2456b6d114aSBen Gras 	bool ret;
2466b6d114aSBen Gras 	if (po == NULL)
2476b6d114aSBen Gras 		return false;
2486b6d114aSBen Gras 	ret = prop_array_add(array, po);
2496b6d114aSBen Gras 	prop_object_release(po);
2506b6d114aSBen Gras 	return ret;
2516b6d114aSBen Gras }
252