Home | History | Annotate | Line # | Download | only in libprop
prop_dictionary_util.c revision 1.5
      1  1.5    pooka /*	$NetBSD: prop_dictionary_util.c,v 1.5 2012/07/27 09:10:59 pooka Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*-
      4  1.1  thorpej  * Copyright (c) 2006 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.4   bouyer prop_dictionary_get_dict(prop_dictionary_t dict, const char *key, prop_dictionary_t *dp)
     46  1.4   bouyer {
     47  1.4   bouyer 	prop_object_t o;
     48  1.4   bouyer 	o = prop_dictionary_get(dict, key);
     49  1.4   bouyer 	if (o == NULL || prop_object_type(o) != PROP_TYPE_DICTIONARY)
     50  1.4   bouyer 		return false;
     51  1.4   bouyer 	*dp = o;
     52  1.4   bouyer 	return true;
     53  1.4   bouyer 
     54  1.4   bouyer }
     55  1.4   bouyer 
     56  1.4   bouyer bool
     57  1.1  thorpej prop_dictionary_get_bool(prop_dictionary_t dict,
     58  1.1  thorpej 			 const char *key,
     59  1.2  thorpej 			 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.1  thorpej prop_dictionary_set_bool(prop_dictionary_t dict,
     74  1.1  thorpej 			 const char *key,
     75  1.2  thorpej 			 bool val)
     76  1.1  thorpej {
     77  1.1  thorpej 	prop_bool_t b;
     78  1.1  thorpej 	int rv;
     79  1.1  thorpej 
     80  1.1  thorpej 	b = prop_bool_create(val);
     81  1.1  thorpej 	if (b == NULL)
     82  1.2  thorpej 		return (false);
     83  1.1  thorpej 	rv = prop_dictionary_set(dict, key, b);
     84  1.1  thorpej 	prop_object_release(b);
     85  1.1  thorpej 
     86  1.1  thorpej 	return (rv);
     87  1.1  thorpej }
     88  1.1  thorpej 
     89  1.1  thorpej #define	TEMPLATE(size)							\
     90  1.2  thorpej bool								\
     91  1.1  thorpej prop_dictionary_get_int ## size (prop_dictionary_t dict,		\
     92  1.1  thorpej 				 const char *key,			\
     93  1.1  thorpej 				 int ## size ## _t *valp)		\
     94  1.1  thorpej {									\
     95  1.1  thorpej 	prop_number_t num;						\
     96  1.1  thorpej 									\
     97  1.1  thorpej 	num = prop_dictionary_get(dict, key);				\
     98  1.1  thorpej 	if (prop_object_type(num) != PROP_TYPE_NUMBER)			\
     99  1.2  thorpej 		return (false);						\
    100  1.1  thorpej 									\
    101  1.1  thorpej 	if (prop_number_unsigned(num) &&				\
    102  1.1  thorpej 	    prop_number_unsigned_integer_value(num) >			\
    103  1.1  thorpej 	   /*CONSTCOND*/((size) ==  8 ?  INT8_MAX :			\
    104  1.1  thorpej 			 (size) == 16 ? INT16_MAX :			\
    105  1.1  thorpej 			 (size) == 32 ? INT32_MAX : INT64_MAX)) {	\
    106  1.2  thorpej 		return (false);						\
    107  1.1  thorpej 	}								\
    108  1.1  thorpej 									\
    109  1.1  thorpej 	if (prop_number_size(num) > (size))				\
    110  1.2  thorpej 		return (false);						\
    111  1.1  thorpej 									\
    112  1.1  thorpej 	*valp = (int ## size ## _t) prop_number_integer_value(num);	\
    113  1.1  thorpej 									\
    114  1.2  thorpej 	return (true);							\
    115  1.1  thorpej }									\
    116  1.1  thorpej 									\
    117  1.2  thorpej bool								\
    118  1.1  thorpej prop_dictionary_get_uint ## size (prop_dictionary_t dict,		\
    119  1.1  thorpej 				  const char *key,			\
    120  1.1  thorpej 				  uint ## size ## _t *valp)		\
    121  1.1  thorpej {									\
    122  1.1  thorpej 	prop_number_t num;						\
    123  1.1  thorpej 									\
    124  1.1  thorpej 	num = prop_dictionary_get(dict, key);				\
    125  1.1  thorpej 	if (prop_object_type(num) != PROP_TYPE_NUMBER)			\
    126  1.2  thorpej 		return (false);						\
    127  1.1  thorpej 									\
    128  1.2  thorpej 	if (prop_number_unsigned(num) == false &&			\
    129  1.1  thorpej 	    prop_number_integer_value(num) < 0) {			\
    130  1.2  thorpej 		return (false);						\
    131  1.1  thorpej 	}								\
    132  1.1  thorpej 									\
    133  1.1  thorpej 	if (prop_number_size(num) > (size))				\
    134  1.2  thorpej 		return (false);						\
    135  1.1  thorpej 									\
    136  1.1  thorpej 	*valp = (uint ## size ## _t)					\
    137  1.1  thorpej 	    prop_number_unsigned_integer_value(num);			\
    138  1.1  thorpej 									\
    139  1.2  thorpej 	return (true);							\
    140  1.1  thorpej }									\
    141  1.1  thorpej 									\
    142  1.2  thorpej bool								\
    143  1.1  thorpej prop_dictionary_set_int ## size (prop_dictionary_t dict,		\
    144  1.1  thorpej 				 const char *key,			\
    145  1.1  thorpej 				 int ## size ## _t val)			\
    146  1.1  thorpej {									\
    147  1.1  thorpej 	prop_number_t num;						\
    148  1.1  thorpej 	int rv;								\
    149  1.1  thorpej 									\
    150  1.1  thorpej 	num = prop_number_create_integer((int64_t) val);		\
    151  1.1  thorpej 	if (num == NULL)						\
    152  1.2  thorpej 		return (false);						\
    153  1.1  thorpej 	rv = prop_dictionary_set(dict, key, num);			\
    154  1.1  thorpej 	prop_object_release(num);					\
    155  1.1  thorpej 									\
    156  1.1  thorpej 	return (rv);							\
    157  1.1  thorpej }									\
    158  1.1  thorpej 									\
    159  1.2  thorpej bool								\
    160  1.1  thorpej prop_dictionary_set_uint ## size (prop_dictionary_t dict,		\
    161  1.1  thorpej 				  const char *key,			\
    162  1.1  thorpej 				  uint ## size ## _t val)		\
    163  1.1  thorpej {									\
    164  1.1  thorpej 	prop_number_t num;						\
    165  1.1  thorpej 	int rv;								\
    166  1.1  thorpej 									\
    167  1.1  thorpej 	num = prop_number_create_unsigned_integer((uint64_t) val);	\
    168  1.1  thorpej 	if (num == NULL)						\
    169  1.2  thorpej 		return (false);						\
    170  1.1  thorpej 	rv = prop_dictionary_set(dict, key, num);			\
    171  1.1  thorpej 	prop_object_release(num);					\
    172  1.1  thorpej 									\
    173  1.1  thorpej 	return (rv);							\
    174  1.1  thorpej }
    175  1.1  thorpej 
    176  1.1  thorpej TEMPLATE(8)
    177  1.1  thorpej TEMPLATE(16)
    178  1.1  thorpej TEMPLATE(32)
    179  1.1  thorpej TEMPLATE(64)
    180  1.1  thorpej 
    181  1.1  thorpej #undef TEMPLATE
    182  1.1  thorpej 
    183  1.1  thorpej #define	TEMPLATE(variant, qualifier)					\
    184  1.2  thorpej bool								\
    185  1.1  thorpej prop_dictionary_get_cstring ## variant (prop_dictionary_t dict,		\
    186  1.1  thorpej 					const char *key,		\
    187  1.1  thorpej 					qualifier char **cpp)		\
    188  1.1  thorpej {									\
    189  1.1  thorpej 	prop_string_t str;						\
    190  1.1  thorpej 									\
    191  1.1  thorpej 	str = prop_dictionary_get(dict, key);				\
    192  1.1  thorpej 	if (prop_object_type(str) != PROP_TYPE_STRING)			\
    193  1.2  thorpej 		return (false);						\
    194  1.1  thorpej 									\
    195  1.1  thorpej 	*cpp = prop_string_cstring ## variant (str);			\
    196  1.1  thorpej 									\
    197  1.2  thorpej 	return (*cpp == NULL ? false : true);				\
    198  1.1  thorpej }									\
    199  1.1  thorpej 									\
    200  1.2  thorpej bool								\
    201  1.1  thorpej prop_dictionary_set_cstring ## variant (prop_dictionary_t dict,		\
    202  1.1  thorpej 					const char *key,		\
    203  1.1  thorpej 					const char *cp)			\
    204  1.1  thorpej {									\
    205  1.1  thorpej 	prop_string_t str;						\
    206  1.1  thorpej 	int rv;								\
    207  1.1  thorpej 									\
    208  1.1  thorpej 	str = prop_string_create_cstring ## variant (cp);		\
    209  1.1  thorpej 	if (str == NULL)						\
    210  1.2  thorpej 		return (false);						\
    211  1.1  thorpej 	rv = prop_dictionary_set(dict, key, str);			\
    212  1.1  thorpej 	prop_object_release(str);					\
    213  1.1  thorpej 									\
    214  1.1  thorpej 	return (rv);							\
    215  1.1  thorpej }
    216  1.1  thorpej 
    217  1.1  thorpej TEMPLATE(,)
    218  1.1  thorpej TEMPLATE(_nocopy,const)
    219  1.1  thorpej 
    220  1.1  thorpej #undef TEMPLATE
    221  1.4   bouyer 
    222  1.4   bouyer bool
    223  1.4   bouyer prop_dictionary_set_and_rel(prop_dictionary_t dict, const char *key,
    224  1.4   bouyer     prop_object_t po)
    225  1.4   bouyer {
    226  1.4   bouyer 	bool ret;
    227  1.4   bouyer 	if (po == NULL)
    228  1.4   bouyer 		return false;
    229  1.4   bouyer 	ret = prop_dictionary_set(dict, key, po);
    230  1.4   bouyer 	prop_object_release(po);
    231  1.4   bouyer 	return ret;
    232  1.4   bouyer }
    233