16511af8eaf998352e6cc07058710cff001746dd
[become] / src / sym.h
1 /* -*-c-*-
2 *
3 * $Id: sym.h,v 1.2 1997/08/04 10:24:25 mdw Exp $
4 *
5 * Symbol table management
6 *
7 * (c) 1996 Straylight
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of `become'
13 *
14 * `Become' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * `Become' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with `become'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29 /*----- Revision history --------------------------------------------------*
30 *
31 * $Log: sym.h,v $
32 * Revision 1.2 1997/08/04 10:24:25 mdw
33 * Sources placed under CVS control.
34 *
35 * Revision 1.1 1997/07/21 13:47:43 mdw
36 * Initial revision
37 *
38 */
39
40 #ifndef SYM_H
41 #define SYM_H
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 /*----- Required headers --------------------------------------------------*/
48
49 #include <stddef.h>
50
51 /*----- Type definitions --------------------------------------------------*/
52
53 /* --- Symbol table --- *
54 *
55 * A @sym_table@ contains the information needed to manage a symbol table.
56 * Users shouldn't fiddle with this information directly, but it needs to be
57 * here so that objects of the correct type can be created.
58 */
59
60 typedef struct sym_table {
61 unsigned long mask; /* Bit mask for hashing purposes */
62 size_t c; /* Down counter for growing table */
63 struct sym_base **a; /* Array of hash bins */
64 } sym_table;
65
66 /* --- A symbol table entry --- *
67 *
68 * I don't care what actually gets stored in symbol entries because I don't
69 * create them: that's the responsibility of my client. All I care about
70 * here is that whatever gets passed to me is a structure whose first member
71 * is a @sym_base@. The ANSI guarantees about structure layout are
72 * sufficient to allow me to manipulate such objects.
73 */
74
75 typedef struct sym_base {
76 struct sym_base *next; /* Next symbol in hash bin */
77 unsigned long hash; /* Hash value for symbol's name */
78 char *name; /* Name of this symbol */
79 size_t len; /* Length of the symbol's name */
80 } sym_base;
81
82 /* --- An iterator block --- */
83
84 typedef struct sym_iter {
85 sym_table *t; /* Symbol table being iterated */
86 sym_base *n; /* Address of next item to return */
87 size_t i; /* Index of next hash bin to use */
88 } sym_iter;
89
90 /*----- External functions ------------------------------------------------*/
91
92 /* --- @sym_createTable@ --- *
93 *
94 * Arguments: @sym_table *t@ = symbol table to initialise
95 *
96 * Returns: ---
97 *
98 * Use: Initialises the given symbol table.
99 */
100
101 extern void sym_createTable(sym_table */*t*/);
102
103 /* --- @sym_destroyTable@ --- *
104 *
105 * Arguments: @sym_table *t@ = pointer to symbol table in question
106 *
107 * Returns: ---
108 *
109 * Use: Destroys a symbol table, freeing all the memory it used to
110 * occupy.
111 */
112
113 extern void sym_destroyTable(sym_table */*t*/);
114
115 /* --- @sym_find@ --- *
116 *
117 * Arguments: @sym_table *t@ = pointer to symbol table in question
118 * @const char *n@ = pointer to symbol table to look up
119 * @long l@ = length of the name string or negative to measure
120 * @size_t sz@ = size of desired symbol object, or zero
121 * @unsigned *f@ = pointer to a flag, or null.
122 *
123 * Returns: The address of a @sym_base@ structure, or null if not found
124 * and @sz@ is zero.
125 *
126 * Use: Looks up a symbol in a given symbol table. The name is
127 * passed by the address of its first character. The length
128 * may be given, in which case the name may contain arbitrary
129 * binary data, or it may be given as a negative number, in
130 * which case the length of the name is calculated as
131 * @strlen(n)@.
132 *
133 * The return value is the address of a pointer to a @sym_base@
134 * block (which may have other things on the end, as above). If
135 * the symbol could be found, the return value points to the
136 * symbol block. If the symbol wasn't there, then if @sz@ is
137 * nonzero, a new symbol is created and its address is returned;
138 * otherwise a null pointer is returned.
139 *
140 * The value of @*f@ indicates whether a new symbol entry was
141 * created: a nonzero value indicates that an old value was
142 * found.
143 */
144
145 extern void *sym_find(sym_table */*t*/, const char */*n*/, long /*l*/,
146 size_t /*sz*/, unsigned */*f*/);
147
148 /* --- @sym_remove@ --- *
149 *
150 * Arguments: @sym_table *i@ = pointer to a symbol table object
151 * @void *b@ = pointer to symbol table entry
152 *
153 * Returns: ---
154 *
155 * Use: Removes the object from the symbol table. The space occupied
156 * by the object and its name is freed; anything else attached
157 * to the entry should already be gone by this point.
158 */
159
160 extern void sym_remove(sym_table */*t*/, void */*b*/);
161
162 /* --- @sym_createIter@ --- *
163 *
164 * Arguments: @sym_iter *i@ = pointer to an iterator object
165 * @sym_table *t@ = pointer to a symbol table object
166 *
167 * Returns: ---
168 *
169 * Use: Creates a new symbol table iterator which may be used to
170 * iterate through a symbol table.
171 */
172
173 extern void sym_createIter(sym_iter */*i*/, sym_table */*t*/);
174
175 /* --- @sym_next@ --- *
176 *
177 * Arguments: @sym_iter *i@ = pointer to iterator object
178 *
179 * Returns: Pointer to the next symbol found, or null when finished.
180 *
181 * Use: Returns the next symbol from the table. Symbols are not
182 * returned in any particular order.
183 */
184
185 extern void *sym_next(sym_iter */*i*/);
186
187 /*----- That's all, folks -------------------------------------------------*/
188
189 #ifdef __cplusplus
190 }
191 #endif
192
193 #endif