1 /* $NetBSD: symtab.h,v 1.4 2014/12/10 04:38:01 christos Exp $ */ 2 3 /* 4 * Portions Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC") 5 * Portions Copyright (C) 2001 Internet Software Consortium. 6 * 7 * Permission to use, copy, modify, and/or distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL 12 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 13 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY 14 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 * 19 * Portions Copyright (C) 2001 Nominum, Inc. 20 * 21 * Permission to use, copy, modify, and/or distribute this software for any 22 * purpose with or without fee is hereby granted, provided that the above 23 * copyright notice and this permission notice appear in all copies. 24 * 25 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL 26 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 27 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY 28 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 29 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 30 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 31 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 32 */ 33 34 /* Id: symtab.h,v 1.10 2007/08/28 07:20:43 tbox Exp */ 35 36 #ifndef ISCCC_SYMTAB_H 37 #define ISCCC_SYMTAB_H 1 38 39 /***** 40 ***** Module Info 41 *****/ 42 43 /*! \file isccc/symtab.h 44 * \brief 45 * Provides a simple memory-based symbol table. 46 * 47 * Keys are C strings. A type may be specified when looking up, 48 * defining, or undefining. A type value of 0 means "match any type"; 49 * any other value will only match the given type. 50 * 51 * It's possible that a client will attempt to define a <key, type, 52 * value> tuple when a tuple with the given key and type already 53 * exists in the table. What to do in this case is specified by the 54 * client. Possible policies are: 55 * 56 *\li isccc_symexists_reject Disallow the define, returning #ISC_R_EXISTS 57 *\li isccc_symexists_replace Replace the old value with the new. The 58 * undefine action (if provided) will be called 59 * with the old <key, type, value> tuple. 60 *\li isccc_symexists_add Add the new tuple, leaving the old tuple in 61 * the table. Subsequent lookups will retrieve 62 * the most-recently-defined tuple. 63 * 64 * A lookup of a key using type 0 will return the most-recently 65 * defined symbol with that key. An undefine of a key using type 0 66 * will undefine the most-recently defined symbol with that key. 67 * Trying to define a key with type 0 is illegal. 68 * 69 * The symbol table library does not make a copy the key field, so the 70 * caller must ensure that any key it passes to isccc_symtab_define() 71 * will not change until it calls isccc_symtab_undefine() or 72 * isccc_symtab_destroy(). 73 * 74 * A user-specified action will be called (if provided) when a symbol 75 * is undefined. It can be used to free memory associated with keys 76 * and/or values. 77 */ 78 79 /*** 80 *** Imports. 81 ***/ 82 83 #include <isc/lang.h> 84 #include <isccc/types.h> 85 86 /*** 87 *** Symbol Tables. 88 ***/ 89 90 typedef union isccc_symvalue { 91 void * as_pointer; 92 int as_integer; 93 unsigned int as_uinteger; 94 } isccc_symvalue_t; 95 96 typedef void (*isccc_symtabundefaction_t)(char *key, unsigned int type, 97 isccc_symvalue_t value, void *userarg); 98 99 typedef isc_boolean_t (*isccc_symtabforeachaction_t)(char *key, 100 unsigned int type, 101 isccc_symvalue_t value, 102 void *userarg); 103 104 typedef enum { 105 isccc_symexists_reject = 0, 106 isccc_symexists_replace = 1, 107 isccc_symexists_add = 2 108 } isccc_symexists_t; 109 110 ISC_LANG_BEGINDECLS 111 112 isc_result_t 113 isccc_symtab_create(unsigned int size, 114 isccc_symtabundefaction_t undefine_action, void *undefine_arg, 115 isc_boolean_t case_sensitive, isccc_symtab_t **symtabp); 116 117 void 118 isccc_symtab_destroy(isccc_symtab_t **symtabp); 119 120 isc_result_t 121 isccc_symtab_lookup(isccc_symtab_t *symtab, const char *key, unsigned int type, 122 isccc_symvalue_t *value); 123 124 isc_result_t 125 isccc_symtab_define(isccc_symtab_t *symtab, char *key, unsigned int type, 126 isccc_symvalue_t value, isccc_symexists_t exists_policy); 127 128 isc_result_t 129 isccc_symtab_undefine(isccc_symtab_t *symtab, const char *key, unsigned int type); 130 131 void 132 isccc_symtab_foreach(isccc_symtab_t *symtab, isccc_symtabforeachaction_t action, 133 void *arg); 134 135 ISC_LANG_ENDDECLS 136 137 #endif /* ISCCC_SYMTAB_H */ 138