Home | History | Annotate | Line # | Download | only in libprop
prop_bool.c revision 1.2
      1 /*	$NetBSD: prop_bool.c,v 1.2 2006/05/18 03:05:19 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 #include <prop/prop_bool.h>
     40 #include "prop_object_impl.h"
     41 
     42 struct _prop_bool {
     43 	struct _prop_object	pb_obj;
     44 	boolean_t		pb_value;
     45 };
     46 
     47 _PROP_POOL_INIT(_prop_bool_pool, sizeof(struct _prop_bool), "propbool")
     48 
     49 static void		_prop_bool_free(void *);
     50 static boolean_t	_prop_bool_externalize(
     51 				struct _prop_object_externalize_context *,
     52 				void *);
     53 static boolean_t	_prop_bool_equals(void *, void *);
     54 
     55 static const struct _prop_object_type _prop_object_type_bool = {
     56 	.pot_type	=	PROP_TYPE_BOOL,
     57 	.pot_free	=	_prop_bool_free,
     58 	.pot_extern	=	_prop_bool_externalize,
     59 	.pot_equals	=	_prop_bool_equals,
     60 };
     61 
     62 #define	prop_object_is_bool(x)		\
     63 	((x)->pb_obj.po_type == &_prop_object_type_bool)
     64 
     65 static void
     66 _prop_bool_free(void *v)
     67 {
     68 
     69 	_PROP_POOL_PUT(_prop_bool_pool, v);
     70 }
     71 
     72 static boolean_t
     73 _prop_bool_externalize(struct _prop_object_externalize_context *ctx,
     74 		       void *v)
     75 {
     76 	prop_bool_t pb = v;
     77 
     78 	return (_prop_object_externalize_empty_tag(ctx,
     79 	    pb->pb_value ? "true" : "false"));
     80 }
     81 
     82 static boolean_t
     83 _prop_bool_equals(void *v1, void *v2)
     84 {
     85 	prop_bool_t b1 = v1;
     86 	prop_bool_t b2 = v2;
     87 
     88 	_PROP_ASSERT(prop_object_is_bool(b1));
     89 	_PROP_ASSERT(prop_object_is_bool(b2));
     90 	return (b1->pb_value == b2->pb_value);
     91 }
     92 
     93 static prop_bool_t
     94 _prop_bool_alloc(void)
     95 {
     96 	prop_bool_t pb;
     97 
     98 	pb = _PROP_POOL_GET(_prop_bool_pool);
     99 	if (pb != NULL) {
    100 		_prop_object_init(&pb->pb_obj, &_prop_object_type_bool);
    101 	}
    102 
    103 	return (pb);
    104 }
    105 
    106 /*
    107  * prop_bool_create --
    108  *	Create a prop_bool_t and initialize it with the
    109  *	provided boolean value.
    110  */
    111 prop_bool_t
    112 prop_bool_create(boolean_t val)
    113 {
    114 	prop_bool_t pb;
    115 
    116 	pb = _prop_bool_alloc();
    117 	if (pb != NULL)
    118 		pb->pb_value = val;
    119 	return (pb);
    120 }
    121 
    122 /*
    123  * prop_bool_copy --
    124  *	Copy a prop_bool_t.
    125  */
    126 prop_bool_t
    127 prop_bool_copy(prop_bool_t opb)
    128 {
    129 	prop_bool_t pb;
    130 
    131 	_PROP_ASSERT(prop_object_is_bool(opb));
    132 
    133 	pb = _prop_bool_alloc();
    134 	if (pb != NULL)
    135 		pb->pb_value = opb->pb_value;
    136 	return (pb);
    137 }
    138 
    139 /*
    140  * prop_bool_true --
    141  *	Get the value of a prop_bool_t.
    142  */
    143 boolean_t
    144 prop_bool_true(prop_bool_t pb)
    145 {
    146 
    147 	_PROP_ASSERT(prop_object_is_bool(pb));
    148 	return (pb->pb_value);
    149 }
    150 
    151 /*
    152  * prop_bool_equals --
    153  *	Return TRUE if the boolean values are equivalent.
    154  */
    155 boolean_t
    156 prop_bool_equals(prop_bool_t b1, prop_bool_t b2)
    157 {
    158 
    159 	return (_prop_bool_equals(b1, b2));
    160 }
    161 
    162 /*
    163  * _prop_bool_internalize --
    164  *	Parse a <true/> or <false/> and return the object created from
    165  *	the external representation.
    166  */
    167 prop_object_t
    168 _prop_bool_internalize(struct _prop_object_internalize_context *ctx)
    169 {
    170 	boolean_t val;
    171 
    172 	/* No attributes, and it must be an empty element. */
    173 	if (ctx->poic_tagattr != NULL ||
    174 	    ctx->poic_is_empty_element == FALSE)
    175 	    	return (NULL);
    176 
    177 	if (_PROP_TAG_MATCH(ctx, "true"))
    178 		val = TRUE;
    179 	else {
    180 		_PROP_ASSERT(_PROP_TAG_MATCH(ctx, "false"));
    181 		val = FALSE;
    182 	}
    183 
    184 	return (prop_bool_create(val));
    185 }
    186