1*e89934bbSchristos /* $NetBSD: nvtable.c,v 1.2 2017/02/14 01:16:49 christos Exp $ */
241fbaed0Stron
341fbaed0Stron /*++
441fbaed0Stron /* NAME
541fbaed0Stron /* nvtable 3
641fbaed0Stron /* SUMMARY
741fbaed0Stron /* attribute list manager
841fbaed0Stron /* SYNOPSIS
941fbaed0Stron /* #include <nvtable.h>
1041fbaed0Stron /*
1141fbaed0Stron /* typedef struct {
1241fbaed0Stron /* .in +4
1341fbaed0Stron /* char *key;
1441fbaed0Stron /* char *value;
1541fbaed0Stron /* /* private fields... */
1641fbaed0Stron /* .in -4
1741fbaed0Stron /* } NVTABLE_INFO;
1841fbaed0Stron /*
1941fbaed0Stron /* NVTABLE *nvtable_create(size)
2041fbaed0Stron /* int size;
2141fbaed0Stron /*
2241fbaed0Stron /* NVTABLE_INFO *nvtable_update(table, key, value)
2341fbaed0Stron /* NVTABLE *table;
2441fbaed0Stron /* const char *key;
2541fbaed0Stron /* const char *value;
2641fbaed0Stron /*
2741fbaed0Stron /* char *nvtable_find(table, key)
2841fbaed0Stron /* NVTABLE *table;
2941fbaed0Stron /* const char *key;
3041fbaed0Stron /*
3141fbaed0Stron /* NVTABLE_INFO *nvtable_locate(table, key)
3241fbaed0Stron /* NVTABLE *table;
3341fbaed0Stron /* const char *key;
3441fbaed0Stron /*
3541fbaed0Stron /* void nvtable_delete(table, key)
3641fbaed0Stron /* NVTABLE *table;
3741fbaed0Stron /* const char *key;
3841fbaed0Stron /*
3941fbaed0Stron /* void nvtable_free(table)
4041fbaed0Stron /* NVTABLE *table;
4141fbaed0Stron /*
4241fbaed0Stron /* void nvtable_walk(table, action, ptr)
4341fbaed0Stron /* NVTABLE *table;
4441fbaed0Stron /* void (*action)(NVTABLE_INFO *, char *ptr);
4541fbaed0Stron /* char *ptr;
4641fbaed0Stron /*
4741fbaed0Stron /* NVTABLE_INFO **nvtable_list(table)
4841fbaed0Stron /* NVTABLE *table;
4941fbaed0Stron /* DESCRIPTION
5041fbaed0Stron /* This module maintains one or more attribute lists. It provides a
5141fbaed0Stron /* more convenient interface than hash tables, although it uses the
5241fbaed0Stron /* same underlying implementation. Each attribute list entry consists
5341fbaed0Stron /* of a unique string-valued lookup key and a string value.
5441fbaed0Stron /*
5541fbaed0Stron /* nvtable_create() creates a table of the specified size and returns a
5641fbaed0Stron /* pointer to the result.
5741fbaed0Stron /*
5841fbaed0Stron /* nvtable_update() stores or updates a (key, value) pair in the specified
5941fbaed0Stron /* table and returns a pointer to the resulting entry. The key and the
6041fbaed0Stron /* value are copied.
6141fbaed0Stron /*
6241fbaed0Stron /* nvtable_find() returns the value that was stored under the given key,
6341fbaed0Stron /* or a null pointer if it was not found. In order to distinguish
6441fbaed0Stron /* a null value from a non-existent value, use nvtable_locate().
6541fbaed0Stron /*
6641fbaed0Stron /* nvtable_locate() returns a pointer to the entry that was stored
6741fbaed0Stron /* for the given key, or a null pointer if it was not found.
6841fbaed0Stron /*
6941fbaed0Stron /* nvtable_delete() removes one entry that was stored under the given key.
7041fbaed0Stron /*
7141fbaed0Stron /* nvtable_free() destroys a hash table, including contents.
7241fbaed0Stron /*
7341fbaed0Stron /* nvtable_walk() invokes the action function for each table entry, with
7441fbaed0Stron /* a pointer to the entry as its argument. The ptr argument is passed
7541fbaed0Stron /* on to the action function.
7641fbaed0Stron /*
7741fbaed0Stron /* nvtable_list() returns a null-terminated list of pointers to
7841fbaed0Stron /* all elements in the named table. The list should be passed to
7941fbaed0Stron /* myfree().
8041fbaed0Stron /* RESTRICTIONS
8141fbaed0Stron /* A callback function should not modify the attribute list that is
8241fbaed0Stron /* specified to its caller.
8341fbaed0Stron /* DIAGNOSTICS
8441fbaed0Stron /* The following conditions are reported and cause the program to
8541fbaed0Stron /* terminate immediately: memory allocation failure; an attempt
8641fbaed0Stron /* to delete a non-existent entry.
8741fbaed0Stron /* SEE ALSO
8841fbaed0Stron /* mymalloc(3) memory management wrapper
8941fbaed0Stron /* htable(3) hash table manager
9041fbaed0Stron /* LICENSE
9141fbaed0Stron /* .ad
9241fbaed0Stron /* .fi
9341fbaed0Stron /* The Secure Mailer license must be distributed with this software.
9441fbaed0Stron /* AUTHOR(S)
9541fbaed0Stron /* Wietse Venema
9641fbaed0Stron /* IBM T.J. Watson Research
9741fbaed0Stron /* P.O. Box 704
9841fbaed0Stron /* Yorktown Heights, NY 10598, USA
9941fbaed0Stron /*--*/
10041fbaed0Stron
10141fbaed0Stron /* C library */
10241fbaed0Stron
10341fbaed0Stron #include <sys_defs.h>
10441fbaed0Stron
10541fbaed0Stron /* Utility library. */
10641fbaed0Stron
10741fbaed0Stron #include <mymalloc.h>
10841fbaed0Stron #include <htable.h>
10941fbaed0Stron #include <nvtable.h>
11041fbaed0Stron
11141fbaed0Stron /* nvtable_update - update or enter (key, value) pair */
11241fbaed0Stron
nvtable_update(NVTABLE * table,const char * key,const char * value)11341fbaed0Stron NVTABLE_INFO *nvtable_update(NVTABLE * table, const char *key, const char *value)
11441fbaed0Stron {
11541fbaed0Stron NVTABLE_INFO *ht;
11641fbaed0Stron
11741fbaed0Stron if ((ht = htable_locate(table, key)) != 0) {
11841fbaed0Stron myfree(ht->value);
11941fbaed0Stron } else {
120e262b48eSchristos ht = htable_enter(table, key, (void *) 0);
12141fbaed0Stron }
12241fbaed0Stron ht->value = mystrdup(value);
12341fbaed0Stron return (ht);
12441fbaed0Stron }
125