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