Home | History | Annotate | Line # | Download | only in libprop
prop_ingest.c revision 1.1.14.1
      1  1.1.14.1  wrstuden /*	$NetBSD: prop_ingest.c,v 1.1.14.1 2007/09/30 03:38:47 wrstuden 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/proplib.h>
     40       1.1   thorpej #include "prop_object_impl.h"
     41       1.1   thorpej 
     42       1.1   thorpej struct _prop_ingest_context {
     43       1.1   thorpej 	prop_ingest_error_t	pic_error;
     44       1.1   thorpej 	prop_type_t		pic_type;
     45       1.1   thorpej 	const char *		pic_key;
     46       1.1   thorpej 	void *			pic_private;
     47       1.1   thorpej };
     48       1.1   thorpej 
     49       1.1   thorpej /*
     50       1.1   thorpej  * prop_ingest_context_alloc --
     51       1.1   thorpej  *	Allocate and initialize an ingest context.
     52       1.1   thorpej  */
     53       1.1   thorpej prop_ingest_context_t
     54       1.1   thorpej prop_ingest_context_alloc(void *private)
     55       1.1   thorpej {
     56       1.1   thorpej 	prop_ingest_context_t ctx;
     57       1.1   thorpej 
     58       1.1   thorpej 	ctx = _PROP_MALLOC(sizeof(*ctx), M_TEMP);
     59       1.1   thorpej 	if (ctx != NULL) {
     60       1.1   thorpej 		ctx->pic_error = PROP_INGEST_ERROR_NO_ERROR;
     61       1.1   thorpej 		ctx->pic_type = PROP_TYPE_UNKNOWN;
     62       1.1   thorpej 		ctx->pic_key = NULL;
     63       1.1   thorpej 		ctx->pic_private = private;
     64       1.1   thorpej 	}
     65       1.1   thorpej 	return (ctx);
     66       1.1   thorpej }
     67       1.1   thorpej 
     68       1.1   thorpej /*
     69       1.1   thorpej  * prop_ingest_context_free --
     70       1.1   thorpej  *	Free an ingest context.
     71       1.1   thorpej  */
     72       1.1   thorpej void
     73       1.1   thorpej prop_ingest_context_free(prop_ingest_context_t ctx)
     74       1.1   thorpej {
     75       1.1   thorpej 
     76       1.1   thorpej 	_PROP_FREE(ctx, M_TEMP);
     77       1.1   thorpej }
     78       1.1   thorpej 
     79       1.1   thorpej /*
     80       1.1   thorpej  * prop_ingest_context_error --
     81       1.1   thorpej  *	Get the error code from an ingest context.
     82       1.1   thorpej  */
     83       1.1   thorpej prop_ingest_error_t
     84       1.1   thorpej prop_ingest_context_error(prop_ingest_context_t ctx)
     85       1.1   thorpej {
     86       1.1   thorpej 
     87       1.1   thorpej 	return (ctx->pic_error);
     88       1.1   thorpej }
     89       1.1   thorpej 
     90       1.1   thorpej /*
     91       1.1   thorpej  * prop_ingest_context_type --
     92       1.1   thorpej  *	Return the type of last object visisted by an ingest context.
     93       1.1   thorpej  */
     94       1.1   thorpej prop_type_t
     95       1.1   thorpej prop_ingest_context_type(prop_ingest_context_t ctx)
     96       1.1   thorpej {
     97       1.1   thorpej 
     98       1.1   thorpej 	return (ctx->pic_type);
     99       1.1   thorpej }
    100       1.1   thorpej 
    101       1.1   thorpej /*
    102       1.1   thorpej  * prop_ingest_context_key --
    103       1.1   thorpej  *	Return the last key looked up by an ingest context.
    104       1.1   thorpej  */
    105       1.1   thorpej const char *
    106       1.1   thorpej prop_ingest_context_key(prop_ingest_context_t ctx)
    107       1.1   thorpej {
    108       1.1   thorpej 
    109       1.1   thorpej 	return (ctx->pic_key);
    110       1.1   thorpej }
    111       1.1   thorpej 
    112       1.1   thorpej /*
    113       1.1   thorpej  * prop_ingest_context_private --
    114       1.1   thorpej  *	Return the caller-private data associated with an ingest context.
    115       1.1   thorpej  */
    116       1.1   thorpej void *
    117       1.1   thorpej prop_ingest_context_private(prop_ingest_context_t ctx)
    118       1.1   thorpej {
    119       1.1   thorpej 
    120       1.1   thorpej 	return (ctx->pic_private);
    121       1.1   thorpej }
    122       1.1   thorpej 
    123       1.1   thorpej /*
    124       1.1   thorpej  * prop_dictionary_ingest --
    125       1.1   thorpej  *	Ingest a dictionary using handlers for each object to translate
    126       1.1   thorpej  *	into an arbitrary binary format.
    127       1.1   thorpej  */
    128  1.1.14.1  wrstuden bool
    129       1.1   thorpej prop_dictionary_ingest(prop_dictionary_t dict,
    130       1.1   thorpej 		       const prop_ingest_table_entry rules[],
    131       1.1   thorpej 		       prop_ingest_context_t ctx)
    132       1.1   thorpej {
    133       1.1   thorpej 	const prop_ingest_table_entry *pite;
    134       1.1   thorpej 	prop_object_t obj;
    135       1.1   thorpej 
    136       1.1   thorpej 	ctx->pic_error = PROP_INGEST_ERROR_NO_ERROR;
    137       1.1   thorpej 
    138       1.1   thorpej 	for (pite = rules; pite->pite_key != NULL; pite++) {
    139       1.1   thorpej 		ctx->pic_key = pite->pite_key;
    140       1.1   thorpej 		obj = prop_dictionary_get(dict, pite->pite_key);
    141       1.1   thorpej 		ctx->pic_type = prop_object_type(obj);
    142       1.1   thorpej 		if (obj == NULL) {
    143       1.1   thorpej 			if (pite->pite_flags & PROP_INGEST_FLAG_OPTIONAL) {
    144  1.1.14.1  wrstuden 				if ((*pite->pite_handler)(ctx, NULL) == false) {
    145       1.1   thorpej 					ctx->pic_error =
    146       1.1   thorpej 					    PROP_INGEST_ERROR_HANDLER_FAILED;
    147  1.1.14.1  wrstuden 					return (false);
    148       1.1   thorpej 				}
    149       1.1   thorpej 				continue;
    150       1.1   thorpej 			}
    151       1.1   thorpej 			ctx->pic_error = PROP_INGEST_ERROR_NO_KEY;
    152  1.1.14.1  wrstuden 			return (false);
    153       1.1   thorpej 		}
    154       1.1   thorpej 		if (ctx->pic_type != pite->pite_type &&
    155       1.1   thorpej 		    pite->pite_type != PROP_TYPE_UNKNOWN) {
    156       1.1   thorpej 			ctx->pic_error = PROP_INGEST_ERROR_WRONG_TYPE;
    157  1.1.14.1  wrstuden 			return (false);
    158       1.1   thorpej 		}
    159  1.1.14.1  wrstuden 		if ((*pite->pite_handler)(ctx, obj) == false) {
    160       1.1   thorpej 			ctx->pic_error = PROP_INGEST_ERROR_HANDLER_FAILED;
    161  1.1.14.1  wrstuden 			return (false);
    162       1.1   thorpej 		}
    163       1.1   thorpej 	}
    164       1.1   thorpej 
    165  1.1.14.1  wrstuden 	return (true);
    166       1.1   thorpej }
    167