Home | History | Annotate | Line # | Download | only in util
      1 /*	$NetBSD: nvtable.c,v 1.2 2017/02/14 01:16:49 christos Exp $	*/
      2 
      3 /*++
      4 /* NAME
      5 /*	nvtable 3
      6 /* SUMMARY
      7 /*	attribute list manager
      8 /* SYNOPSIS
      9 /*	#include <nvtable.h>
     10 /*
     11 /*	typedef	struct {
     12 /* .in +4
     13 /*		char	*key;
     14 /*		char	*value;
     15 /*		/* private fields... */
     16 /* .in -4
     17 /*	} NVTABLE_INFO;
     18 /*
     19 /*	NVTABLE	*nvtable_create(size)
     20 /*	int	size;
     21 /*
     22 /*	NVTABLE_INFO *nvtable_update(table, key, value)
     23 /*	NVTABLE	*table;
     24 /*	const char *key;
     25 /*	const char *value;
     26 /*
     27 /*	char	*nvtable_find(table, key)
     28 /*	NVTABLE	*table;
     29 /*	const char *key;
     30 /*
     31 /*	NVTABLE_INFO *nvtable_locate(table, key)
     32 /*	NVTABLE	*table;
     33 /*	const char *key;
     34 /*
     35 /*	void	nvtable_delete(table, key)
     36 /*	NVTABLE	*table;
     37 /*	const char *key;
     38 /*
     39 /*	void	nvtable_free(table)
     40 /*	NVTABLE	*table;
     41 /*
     42 /*	void	nvtable_walk(table, action, ptr)
     43 /*	NVTABLE	*table;
     44 /*	void	(*action)(NVTABLE_INFO *, char *ptr);
     45 /*	char	*ptr;
     46 /*
     47 /*	NVTABLE_INFO **nvtable_list(table)
     48 /*	NVTABLE	*table;
     49 /* DESCRIPTION
     50 /*	This module maintains one or more attribute lists. It provides a
     51 /*	more convenient interface than hash tables, although it uses the
     52 /*	same underlying implementation. Each attribute list entry consists
     53 /*	of a unique string-valued lookup key and a string value.
     54 /*
     55 /*	nvtable_create() creates a table of the specified size and returns a
     56 /*	pointer to the result.
     57 /*
     58 /*	nvtable_update() stores or updates a (key, value) pair in the specified
     59 /*	table and returns a pointer to the resulting entry. The key and the
     60 /*	value are copied.
     61 /*
     62 /*	nvtable_find() returns the value that was stored under the given key,
     63 /*	or a null pointer if it was not found. In order to distinguish
     64 /*	a null value from a non-existent value, use nvtable_locate().
     65 /*
     66 /*	nvtable_locate() returns a pointer to the entry that was stored
     67 /*	for the given key, or a null pointer if it was not found.
     68 /*
     69 /*	nvtable_delete() removes one entry that was stored under the given key.
     70 /*
     71 /*	nvtable_free() destroys a hash table, including contents.
     72 /*
     73 /*	nvtable_walk() invokes the action function for each table entry, with
     74 /*	a pointer to the entry as its argument. The ptr argument is passed
     75 /*	on to the action function.
     76 /*
     77 /*	nvtable_list() returns a null-terminated list of pointers to
     78 /*	all elements in the named table. The list should be passed to
     79 /*	myfree().
     80 /* RESTRICTIONS
     81 /*	A callback function should not modify the attribute list that is
     82 /*	specified to its caller.
     83 /* DIAGNOSTICS
     84 /*	The following conditions are reported and cause the program to
     85 /*	terminate immediately: memory allocation failure; an attempt
     86 /*	to delete a non-existent entry.
     87 /* SEE ALSO
     88 /*	mymalloc(3) memory management wrapper
     89 /*	htable(3) hash table manager
     90 /* LICENSE
     91 /* .ad
     92 /* .fi
     93 /*	The Secure Mailer license must be distributed with this software.
     94 /* AUTHOR(S)
     95 /*	Wietse Venema
     96 /*	IBM T.J. Watson Research
     97 /*	P.O. Box 704
     98 /*	Yorktown Heights, NY 10598, USA
     99 /*--*/
    100 
    101 /* C library */
    102 
    103 #include <sys_defs.h>
    104 
    105 /* Utility library. */
    106 
    107 #include <mymalloc.h>
    108 #include <htable.h>
    109 #include <nvtable.h>
    110 
    111 /* nvtable_update - update or enter (key, value) pair */
    112 
    113 NVTABLE_INFO *nvtable_update(NVTABLE * table, const char *key, const char *value)
    114 {
    115     NVTABLE_INFO *ht;
    116 
    117     if ((ht = htable_locate(table, key)) != 0) {
    118 	myfree(ht->value);
    119     } else {
    120 	ht = htable_enter(table, key, (void *) 0);
    121     }
    122     ht->value = mystrdup(value);
    123     return (ht);
    124 }
    125