Home | History | Annotate | Line # | Download | only in libprop
prop_bool.c revision 1.1
      1  1.1  thorpej /*	$NetBSD: prop_bool.c,v 1.1 2006/04/27 20:11:27 thorpej 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  * 3. All advertising materials mentioning features or use of this software
     19  1.1  thorpej  *    must display the following acknowledgement:
     20  1.1  thorpej  *      This product includes software developed by the NetBSD
     21  1.1  thorpej  *      Foundation, Inc. and its contributors.
     22  1.1  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  thorpej  *    contributors may be used to endorse or promote products derived
     24  1.1  thorpej  *    from this software without specific prior written permission.
     25  1.1  thorpej  *
     26  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  thorpej  */
     38  1.1  thorpej 
     39  1.1  thorpej #include <prop/prop_bool.h>
     40  1.1  thorpej #include "prop_object_impl.h"
     41  1.1  thorpej 
     42  1.1  thorpej struct _prop_bool {
     43  1.1  thorpej 	struct _prop_object	pb_obj;
     44  1.1  thorpej 	boolean_t		pb_value;
     45  1.1  thorpej };
     46  1.1  thorpej 
     47  1.1  thorpej _PROP_POOL_INIT(_prop_bool_pool, sizeof(struct _prop_bool), "propbool")
     48  1.1  thorpej 
     49  1.1  thorpej #define	prop_object_is_bool(x)	((x)->pb_obj.po_type == PROP_TYPE_BOOL)
     50  1.1  thorpej 
     51  1.1  thorpej static void
     52  1.1  thorpej _prop_bool_free(void *v)
     53  1.1  thorpej {
     54  1.1  thorpej 
     55  1.1  thorpej 	_PROP_POOL_PUT(_prop_bool_pool, v);
     56  1.1  thorpej }
     57  1.1  thorpej 
     58  1.1  thorpej static boolean_t
     59  1.1  thorpej _prop_bool_externalize(struct _prop_object_externalize_context *ctx,
     60  1.1  thorpej 		       void *v)
     61  1.1  thorpej {
     62  1.1  thorpej 	prop_bool_t pb = v;
     63  1.1  thorpej 
     64  1.1  thorpej 	return (_prop_object_externalize_empty_tag(ctx,
     65  1.1  thorpej 	    pb->pb_value ? "true" : "false"));
     66  1.1  thorpej }
     67  1.1  thorpej 
     68  1.1  thorpej static prop_bool_t
     69  1.1  thorpej _prop_bool_alloc(void)
     70  1.1  thorpej {
     71  1.1  thorpej 	prop_bool_t pb;
     72  1.1  thorpej 
     73  1.1  thorpej 	pb = _PROP_POOL_GET(_prop_bool_pool);
     74  1.1  thorpej 	if (pb != NULL) {
     75  1.1  thorpej 		_prop_object_init(&pb->pb_obj);
     76  1.1  thorpej 		pb->pb_obj.po_type = PROP_TYPE_BOOL;
     77  1.1  thorpej 		pb->pb_obj.po_free = _prop_bool_free;
     78  1.1  thorpej 		pb->pb_obj.po_extern = _prop_bool_externalize;
     79  1.1  thorpej 	}
     80  1.1  thorpej 
     81  1.1  thorpej 	return (pb);
     82  1.1  thorpej }
     83  1.1  thorpej 
     84  1.1  thorpej /*
     85  1.1  thorpej  * prop_bool_create --
     86  1.1  thorpej  *	Create a prop_bool_t and initialize it with the
     87  1.1  thorpej  *	provided boolean value.
     88  1.1  thorpej  */
     89  1.1  thorpej prop_bool_t
     90  1.1  thorpej prop_bool_create(boolean_t val)
     91  1.1  thorpej {
     92  1.1  thorpej 	prop_bool_t pb;
     93  1.1  thorpej 
     94  1.1  thorpej 	pb = _prop_bool_alloc();
     95  1.1  thorpej 	if (pb != NULL)
     96  1.1  thorpej 		pb->pb_value = val;
     97  1.1  thorpej 	return (pb);
     98  1.1  thorpej }
     99  1.1  thorpej 
    100  1.1  thorpej /*
    101  1.1  thorpej  * prop_bool_copy --
    102  1.1  thorpej  *	Copy a prop_bool_t.
    103  1.1  thorpej  */
    104  1.1  thorpej prop_bool_t
    105  1.1  thorpej prop_bool_copy(prop_bool_t opb)
    106  1.1  thorpej {
    107  1.1  thorpej 	prop_bool_t pb;
    108  1.1  thorpej 
    109  1.1  thorpej 	_PROP_ASSERT(prop_object_is_bool(opb));
    110  1.1  thorpej 
    111  1.1  thorpej 	pb = _prop_bool_alloc();
    112  1.1  thorpej 	if (pb != NULL)
    113  1.1  thorpej 		pb->pb_value = opb->pb_value;
    114  1.1  thorpej 	return (pb);
    115  1.1  thorpej }
    116  1.1  thorpej 
    117  1.1  thorpej /*
    118  1.1  thorpej  * prop_bool_true --
    119  1.1  thorpej  *	Get the value of a prop_bool_t.
    120  1.1  thorpej  */
    121  1.1  thorpej boolean_t
    122  1.1  thorpej prop_bool_true(prop_bool_t pb)
    123  1.1  thorpej {
    124  1.1  thorpej 
    125  1.1  thorpej 	_PROP_ASSERT(prop_object_is_bool(pb));
    126  1.1  thorpej 	return (pb->pb_value);
    127  1.1  thorpej }
    128  1.1  thorpej 
    129  1.1  thorpej /*
    130  1.1  thorpej  * _prop_bool_internalize --
    131  1.1  thorpej  *	Parse a <true/> or <false/> and return the object created from
    132  1.1  thorpej  *	the external representation.
    133  1.1  thorpej  */
    134  1.1  thorpej prop_object_t
    135  1.1  thorpej _prop_bool_internalize(struct _prop_object_internalize_context *ctx)
    136  1.1  thorpej {
    137  1.1  thorpej 	boolean_t val;
    138  1.1  thorpej 
    139  1.1  thorpej 	/* No attributes, and it must be an empty element. */
    140  1.1  thorpej 	if (ctx->poic_tagattr != NULL ||
    141  1.1  thorpej 	    ctx->poic_is_empty_element == FALSE)
    142  1.1  thorpej 	    	return (NULL);
    143  1.1  thorpej 
    144  1.1  thorpej 	if (_PROP_TAG_MATCH(ctx, "true"))
    145  1.1  thorpej 		val = TRUE;
    146  1.1  thorpej 	else {
    147  1.1  thorpej 		_PROP_ASSERT(_PROP_TAG_MATCH(ctx, "false"));
    148  1.1  thorpej 		val = FALSE;
    149  1.1  thorpej 	}
    150  1.1  thorpej 
    151  1.1  thorpej 	return (prop_bool_create(val));
    152  1.1  thorpej }
    153