1 /*	$NetBSD: dict_alloc.c,v 1.1.1.1 2009/06/23 10:08:58 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	dict_alloc 3
6 /* SUMMARY
7 /*	dictionary memory manager
8 /* SYNOPSIS
9 /*	#include <dict.h>
10 /*
11 /*	DICT	*dict_alloc(dict_type, dict_name, size)
12 /*	const char *dict_type;
13 /*	const char *dict_name;
14 /*	ssize_t	size;
15 /*
16 /*	void	dict_free(dict)
17 /*	DICT	*ptr;
18 /* DESCRIPTION
19 /*	dict_alloc() allocates memory for a dictionary structure of
20 /*	\fIsize\fR bytes, initializes all generic dictionary
21 /*	properties to default settings,
22 /*	and installs default methods that do not support any operation.
23 /*	The caller is supposed to override the default methods with
24 /*	ones that it supports.
25 /*	The purpose of the default methods is to trap an attempt to
26 /*	invoke an unsupported method.
27 /*
28 /*	dict_free() releases memory and cleans up after dict_alloc().
29 /*	It is up to the caller to dispose of any memory that was allocated
30 /*	by the caller.
31 /*
32 /*	Arguments:
33 /* .IP dict_type
34 /*	The official name for this type of dictionary, as used by
35 /*	dict_open(3) etc. This is stored under the \fBtype\fR
36 /*	member.
37 /* .IP dict_name
38 /*	Dictionary name. This is stored as the \fBname\fR member.
39 /* .IP size
40 /*	The size in bytes of the dictionary subclass structure instance.
41 /* SEE ALSO
42 /*	dict(3)
43 /* DIAGNOSTICS
44 /*	Fatal errors: the process invokes a default method.
45 /* LICENSE
46 /* .ad
47 /* .fi
48 /*	The Secure Mailer license must be distributed with this software.
49 /* AUTHOR(S)
50 /*	Wietse Venema
51 /*	IBM T.J. Watson Research
52 /*	P.O. Box 704
53 /*	Yorktown Heights, NY 10598, USA
54 /*--*/
55 
56 /* System libraries. */
57 
58 #include "sys_defs.h"
59 
60 /* Utility library. */
61 
62 #include "msg.h"
63 #include "mymalloc.h"
64 #include "dict.h"
65 
66 /* dict_default_lookup - trap unimplemented operation */
67 
68 static const char *dict_default_lookup(DICT *dict, const char *unused_key)
69 {
70     msg_fatal("%s table %s: lookup operation is not supported",
71 	      dict->type, dict->name);
72 }
73 
74 /* dict_default_update - trap unimplemented operation */
75 
76 static void dict_default_update(DICT *dict, const char *unused_key,
77 				        const char *unused_value)
78 {
79     msg_fatal("%s table %s: update operation is not supported",
80 	      dict->type, dict->name);
81 }
82 
83 /* dict_default_delete - trap unimplemented operation */
84 
85 static int dict_default_delete(DICT *dict, const char *unused_key)
86 {
87     msg_fatal("%s table %s: delete operation is not supported",
88 	      dict->type, dict->name);
89 }
90 
91 /* dict_default_sequence - trap unimplemented operation */
92 
93 static int dict_default_sequence(DICT *dict, int unused_function,
94 		         const char **unused_key, const char **unused_value)
95 {
96     msg_fatal("%s table %s: sequence operation is not supported",
97 	      dict->type, dict->name);
98 }
99 
100 /* dict_default_close - trap unimplemented operation */
101 
102 static void dict_default_close(DICT *dict)
103 {
104     msg_fatal("%s table %s: close operation is not supported",
105 	      dict->type, dict->name);
106 }
107 
108 /* dict_alloc - allocate dictionary object, initialize super-class */
109 
110 DICT   *dict_alloc(const char *dict_type, const char *dict_name, ssize_t size)
111 {
112     DICT   *dict = (DICT *) mymalloc(size);
113 
114     dict->type = mystrdup(dict_type);
115     dict->name = mystrdup(dict_name);
116     dict->flags = DICT_FLAG_FIXED;
117     dict->lookup = dict_default_lookup;
118     dict->update = dict_default_update;
119     dict->delete = dict_default_delete;
120     dict->sequence = dict_default_sequence;
121     dict->close = dict_default_close;
122     dict->lock_fd = -1;
123     dict->stat_fd = -1;
124     dict->mtime = 0;
125     dict->fold_buf = 0;
126     return dict;
127 }
128 
129 /* dict_free - super-class destructor */
130 
131 void    dict_free(DICT *dict)
132 {
133     myfree(dict->type);
134     myfree(dict->name);
135     myfree((char *) dict);
136 }
137