/* Public domain */ #ifndef _MAILPROCD_TABLE_H_ #define _MAILPROCD_TABLE_H_ #define TABLE_MAX_FIELDS 8 typedef struct tbl_entry { char *key; char *data; SLIST_ENTRY(tbl_entry) ents; SLIST_ENTRY(tbl_entry) bents; } TBL_Entry; typedef struct tbl_bucket { SLIST_HEAD(,tbl_entry) ents; unsigned int nents; } TBL_Bucket; typedef struct tbl { char *path; /* Path to file */ TBL_Bucket *buckets; /* Hash table */ int nbuckets; SLIST_HEAD(,tbl_entry) ents; /* Flat list */ } TBL; #define TBL_FOREACH(ent, table) \ SLIST_FOREACH((ent), &(table)->ents, ents) __BEGIN_DECLS void TBL_Init(TBL *, const char *, int); TBL *TBL_Load(const char *); int TBL_Save(TBL *, const char *, mode_t); void TBL_Destroy(TBL *); int TBL_Insert(TBL *, TBL_Entry *); int TBL_Delete(TBL *, const char *); static __inline__ Uint TBL_Hash(TBL *t, const char *key) { Uint h; u_char *p; for (h = 0, p = (u_char *)key; *p != '\0'; p++) { h = 31 * h + *p; } return (h % t->nbuckets); } /* Look up a named table entry. */ static __inline__ TBL_Entry * TBL_LookupHash(TBL *ta, const char *key, Uint h) { TBL_Entry *ent; SLIST_FOREACH(ent, &ta->buckets[h].ents, bents) { if (strcmp(key, ent->key) == 0) break; } return (ent); } static __inline__ char * TBL_Lookup(TBL *ta, const char *key) { TBL_Entry *tent; if ((tent = TBL_LookupHash(ta, key, TBL_Hash(ta,key))) == NULL) { return (NULL); } return (tent->data); } __END_DECLS #endif /* _MAILPROCD_TABLE_H_ */