1 /* $NetBSD: dict_ht.c,v 1.1.1.1 2009/06/23 10:08:59 tron Exp $ */ 2 3 /*++ 4 /* NAME 5 /* dict_ht 3 6 /* SUMMARY 7 /* dictionary manager interface to hash tables 8 /* SYNOPSIS 9 /* #include <dict_ht.h> 10 /* 11 /* DICT *dict_ht_open(name, table, remove) 12 /* const char *name; 13 /* HTABLE *table; 14 /* void (*remove)(char *value) 15 /* DESCRIPTION 16 /* dict_ht_open() makes specified hash table accessible via the 17 /* generic dictionary operations documented in dict_open(3). 18 /* \fIremove\fR specifies an optional callback function 19 /* that is called by the hash table manager when the hash table is 20 /* removed from the dictionary manager's care. The hash table is not 21 /* destroyed when \fIremove\fR is a null pointer. 22 /* SEE ALSO 23 /* dict(3) generic dictionary manager 24 /* LICENSE 25 /* .ad 26 /* .fi 27 /* The Secure Mailer license must be distributed with this software. 28 /* AUTHOR(S) 29 /* Wietse Venema 30 /* IBM T.J. Watson Research 31 /* P.O. Box 704 32 /* Yorktown Heights, NY 10598, USA 33 /*--*/ 34 35 /* System library. */ 36 37 #include "sys_defs.h" 38 39 /* Utility library. */ 40 41 #include "mymalloc.h" 42 #include "htable.h" 43 #include "dict.h" 44 #include "dict_ht.h" 45 46 /* Application-specific. */ 47 48 typedef struct { 49 DICT dict; /* generic members */ 50 HTABLE *table; /* hash table */ 51 void (*remove) (char *); /* callback */ 52 } DICT_HT; 53 54 /* dict_ht_lookup - find hash-table entry */ 55 56 static const char *dict_ht_lookup(DICT *dict, const char *name) 57 { 58 DICT_HT *dict_ht = (DICT_HT *) dict; 59 60 dict_errno = 0; 61 62 return (htable_find(dict_ht->table, name)); 63 } 64 65 /* dict_ht_update - add or update hash-table entry */ 66 67 static void dict_ht_update(DICT *dict, const char *name, const char *value) 68 { 69 DICT_HT *dict_ht = (DICT_HT *) dict; 70 HTABLE_INFO *ht; 71 72 if ((ht = htable_locate(dict_ht->table, name)) != 0) { 73 myfree(ht->value); 74 } else { 75 ht = htable_enter(dict_ht->table, name, (char *) 0); 76 } 77 ht->value = mystrdup(value); 78 } 79 80 /* dict_ht_close - disassociate from hash table */ 81 82 static void dict_ht_close(DICT *dict) 83 { 84 DICT_HT *dict_ht = (DICT_HT *) dict; 85 86 if (dict_ht->remove) 87 htable_free(dict_ht->table, dict_ht->remove); 88 dict_free(dict); 89 } 90 91 /* dict_ht_open - create association with hash table */ 92 93 DICT *dict_ht_open(const char *name, HTABLE *table, void (*remove) (char *)) 94 { 95 DICT_HT *dict_ht; 96 97 dict_ht = (DICT_HT *) dict_alloc(DICT_TYPE_HT, name, sizeof(*dict_ht)); 98 dict_ht->dict.lookup = dict_ht_lookup; 99 dict_ht->dict.update = dict_ht_update; 100 dict_ht->dict.close = dict_ht_close; 101 dict_ht->table = table; 102 dict_ht->remove = remove; 103 return (&dict_ht->dict); 104 } 105