Subversion Repositories tendra.SVN

Rev

Rev 5 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5 Rev 6
Line -... Line 1...
-
 
1
/*
-
 
2
 * Copyright (c) 2002-2005 The TenDRA Project <http://www.tendra.org/>.
-
 
3
 * All rights reserved.
-
 
4
 *
-
 
5
 * Redistribution and use in source and binary forms, with or without
-
 
6
 * modification, are permitted provided that the following conditions are met:
-
 
7
 *
-
 
8
 * 1. Redistributions of source code must retain the above copyright notice,
-
 
9
 *    this list of conditions and the following disclaimer.
-
 
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
-
 
11
 *    this list of conditions and the following disclaimer in the documentation
-
 
12
 *    and/or other materials provided with the distribution.
-
 
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
-
 
14
 *    may be used to endorse or promote products derived from this software
-
 
15
 *    without specific, prior written permission.
-
 
16
 *
-
 
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
-
 
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
-
 
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
-
 
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
-
 
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
-
 
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-
 
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-
 
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-
 
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-
 
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-
 
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
 
28
 *
-
 
29
 * $Id$
-
 
30
 */
1
/*
31
/*
2
    		 Crown Copyright (c) 1997
32
    		 Crown Copyright (c) 1997
3
    
33
 
4
    This TenDRA(r) Computer Program is subject to Copyright
34
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
35
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
36
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
37
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
38
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
39
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
40
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
41
    shall be deemed to be acceptance of the following conditions:-
12
    
42
 
13
        (1) Its Recipients shall ensure that this Notice is
43
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
44
        reproduced upon any copies or amended versions of it;
15
    
45
 
16
        (2) Any amended version of it shall be clearly marked to
46
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
47
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
48
        for the relevant amendment or amendments;
19
    
49
 
20
        (3) Its onward transfer from a recipient to another
50
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
51
        party shall be deemed to be that party's acceptance of
22
        these conditions;
52
        these conditions;
23
    
53
 
24
        (4) DERA gives no warranty or assurance as to its
54
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
55
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
56
        no liability whatsoever in relation to any use to which
27
        it may be put.
57
        it may be put.
28
*/
58
*/
Line 40... Line 70...
40
 
70
 
41
    These hash tables represent the various namespaces permitted in C
71
    These hash tables represent the various namespaces permitted in C
42
    plus the TDF token namespace.
72
    plus the TDF token namespace.
43
*/
73
*/
44
 
74
 
45
hash_table *exps ;
75
hash_table *exps;
46
hash_table *files ;
76
hash_table *files;
47
hash_table *keywords ;
77
hash_table *keywords;
48
hash_table *subsets ;
78
hash_table *subsets;
49
hash_table *tags ;
79
hash_table *tags;
50
hash_table *tag_fields ;
80
hash_table *tag_fields;
51
hash_table *tokens ;
81
hash_table *tokens;
52
hash_table *types ;
82
hash_table *types;
53
hash_table *type_fields ;
83
hash_table *type_fields;
54
 
84
 
55
 
85
 
56
/*
86
/*
57
    INITIALISATION ROUTINE
87
    INITIALISATION ROUTINE
58
 
88
 
59
    This routine initialises the standard hash tables and sets a few
89
    This routine initialises the standard hash tables and sets a few
60
    miscellaneous variables.
90
    miscellaneous variables.
61
*/
91
*/
62
 
92
 
63
void init_hash
93
void
64
    PROTO_Z ()
94
init_hash(void)
65
{
95
{
66
    buffer = alloc_nof ( char, buffsize + 100 ) ;
96
    buffer = alloc_nof(char, buffsize + 100);
67
    exps = make_hash_table ( "Expression" ) ;
97
    exps = make_hash_table("Expression");
68
    files = make_hash_table ( "Output file" ) ;
98
    files = make_hash_table("Output file");
69
    keywords = make_hash_table ( "Keyword" ) ;
99
    keywords = make_hash_table("Keyword");
70
    subsets = make_hash_table ( "Set" ) ;
100
    subsets = make_hash_table("Set");
71
    tags = make_hash_table ( "Tag" ) ;
101
    tags = make_hash_table("Tag");
72
    tag_fields = make_hash_table ( "Field selector struct/union" ) ;
102
    tag_fields = make_hash_table("Field selector struct/union");
73
    tokens = make_hash_table ( "Token" ) ;
103
    tokens = make_hash_table("Token");
74
    types = make_hash_table ( "Type" ) ;
104
    types = make_hash_table("Type");
75
    type_fields = make_hash_table ( "Field selector" ) ;
105
    type_fields = make_hash_table("Field selector");
76
    return ;
106
    return;
77
}
107
}
78
 
108
 
79
 
109
 
80
/*
110
/*
81
    HASHING ROUTINE
111
    HASHING ROUTINE
82
 
112
 
83
    This routine finds the hash value of the string nm.  This value must
113
    This routine finds the hash value of the string nm.  This value must
84
    lie in the range [ 0, hash_size ).
114
    lie in the range [ 0, hash_size ).
85
*/
115
*/
86
 
116
 
87
static int hash
117
static int
88
    PROTO_N ( ( nm ) )
-
 
89
    PROTO_T ( char *nm )
118
hash(char *nm)
90
{
119
{
91
    char *s ;
120
    char *s;
92
    int n = 0 ;
121
    int n = 0;
93
    for ( s = nm ; *s ; s++ ) n += *s ;
122
    for (s = nm; *s; s++)n += *s;
94
    return ( n % hash_size ) ;
123
    return(n % hash_size);
95
}
124
}
96
 
125
 
97
 
126
 
98
/*
127
/*
99
    CREATE A NEW HASH TABLE
128
    CREATE A NEW HASH TABLE
100
 
129
 
101
    This routine creates a new hash table called nm.
130
    This routine creates a new hash table called nm.
102
*/
131
*/
103
 
132
 
104
hash_table *make_hash_table
133
hash_table *
105
    PROTO_N ( ( nm ) )
-
 
106
    PROTO_T ( char *nm )
134
make_hash_table(char *nm)
107
{
135
{
108
    int i ;
136
    int i;
109
    hash_table *t = alloc_nof ( hash_table, 1 ) ;
137
    hash_table *t = alloc_nof(hash_table, 1);
110
    t->name = nm ;
138
    t->name = nm;
111
    for ( i = 0 ; i < hash_size ; i++ ) t->array [i] = null ;
139
    for (i = 0; i < hash_size; i++)t->array [i] = null;
112
    return ( t ) ;
140
    return(t);
113
}
141
}
114
 
142
 
115
 
143
 
116
/*
144
/*
117
    LOOK UP A NAME IN A HASH TABLE
145
    LOOK UP A NAME IN A HASH TABLE
Line 119... Line 147...
119
    This routine looks up the object nm (version v) in the hth column
147
    This routine looks up the object nm (version v) in the hth column
120
    of hash table t.  It returns the object if it is found, or null
148
    of hash table t.  It returns the object if it is found, or null
121
    otherwise.
149
    otherwise.
122
*/
150
*/
123
 
151
 
124
static object *lookup_hash
152
static object *
125
    PROTO_N ( ( t, nm, v, h ) )
-
 
126
    PROTO_T ( hash_table *t X char *nm X int v X int h )
153
lookup_hash(hash_table *t, char *nm, int v, int h)
127
{
154
{
128
    hash_elem *e = t->array [h] ;
155
    hash_elem *e = t->array [h];
129
    while ( e ) {
156
    while (e) {
130
	if ( streq ( nm, e->obj->name ) ) {
157
	if (streq(nm, e->obj->name)) {
131
	    if ( v == e->vers || v == any_version ) return ( e->obj ) ;
158
	    if (v == e->vers || v == any_version) return(e->obj);
132
	}
159
	}
133
	e = e->next ;
160
	e = e->next;
134
    }
161
    }
135
    return ( null ) ;
162
    return(null);
136
}
163
}
137
 
164
 
138
 
165
 
139
/*
166
/*
140
    ADD AN OBJECT TO A HASH TABLE
167
    ADD AN OBJECT TO A HASH TABLE
Line 142... Line 169...
142
    This routine adds the object p (version v) to the hash table t,
169
    This routine adds the object p (version v) to the hash table t,
143
    reporting an error and returning the old value if it is already
170
    reporting an error and returning the old value if it is already
144
    defined.  Otherwise it returns p.
171
    defined.  Otherwise it returns p.
145
*/
172
*/
146
 
173
 
147
object *add_hash
174
object *
148
    PROTO_N ( ( t, p, v ) )
-
 
149
    PROTO_T ( hash_table *t X object *p X int v )
175
add_hash(hash_table *t, object *p, int v)
150
{
176
{
151
    hash_elem *e ;
177
    hash_elem *e;
152
    char *nm = p->name ;
178
    char *nm = p->name;
153
    int h = hash ( nm ) ;
179
    int h = hash(nm);
154
    object *q = lookup_hash ( t, nm, v, h ) ;
180
    object *q = lookup_hash(t, nm, v, h);
155
    if ( q != null ) {
181
    if (q != null) {
156
	char *fn = q->filename ;
182
	char *fn = q->filename;
157
	if ( fn ) {
183
	if (fn) {
158
	    char *err = "%s '%s' already defined (%s, line %d)" ;
184
	    char *err = "%s '%s' already defined (%s, line %d)";
159
	    error ( ERR_SERIOUS, err, t->name, nm, fn, q->line_no ) ;
185
	    error(ERR_SERIOUS, err, t->name, nm, fn, q->line_no);
160
	} else {
186
	} else {
161
	    error ( ERR_SERIOUS, "%s '%s' already defined", t->name, nm ) ;
187
	    error(ERR_SERIOUS, "%s '%s' already defined", t->name, nm);
162
	}
188
	}
163
	return ( q ) ;
189
	return(q);
164
    }
190
    }
165
    alloc_variable ( e, hash_elem, 1000 ) ;
191
    alloc_variable(e, hash_elem, 1000);
166
    e->obj = p ;
192
    e->obj = p;
167
    e->vers = v ;
193
    e->vers = v;
168
    e->next = t->array [h] ;
194
    e->next = t->array [h];
169
    t->array [h] = e ;
195
    t->array [h] = e;
170
    return ( p ) ;
196
    return(p);
171
}
197
}
172
 
198
 
173
 
199
 
174
/*
200
/*
175
    SEARCH A HASH TABLE FOR AN OBJECT
201
    SEARCH A HASH TABLE FOR AN OBJECT
Line 177... Line 203...
177
    This routine searches the hash table t for the object named nm
203
    This routine searches the hash table t for the object named nm
178
    (version v), returning it if it is found.  If it is not found then
204
    (version v), returning it if it is found.  If it is not found then
179
    null is returned.
205
    null is returned.
180
*/
206
*/
181
 
207
 
182
object *search_hash
208
object *
183
    PROTO_N ( ( t, nm, v ) )
-
 
184
    PROTO_T ( hash_table *t X char *nm X int v )
209
search_hash(hash_table *t, char *nm, int v)
185
{
210
{
186
    int h = hash ( nm ) ;
211
    int h = hash(nm);
187
    return ( lookup_hash ( t, nm, v, h ) ) ;
212
    return(lookup_hash(t, nm, v, h));
188
}
213
}
189
 
214
 
190
 
215
 
191
/*
216
/*
192
    SORT THE ELEMENTS OF A HASH LIST
217
    SORT THE ELEMENTS OF A HASH LIST
193
 
218
 
194
    This routine sorts the elements of the hash table t into a single
219
    This routine sorts the elements of the hash table t into a single
195
    alphabetical list.  The table cannot be used subsequently.
220
    alphabetical list.  The table cannot be used subsequently.
196
*/
221
*/
197
 
222
 
198
hash_elem *sort_hash
223
hash_elem *
199
    PROTO_N ( ( t ) )
-
 
200
    PROTO_T ( hash_table *t )
224
sort_hash(hash_table *t)
201
{
225
{
202
    int i ;
226
    int i;
203
    hash_elem *r = null ;
227
    hash_elem *r = null;
204
    for ( i = 0 ; i < hash_size ; i++ ) {
228
    for (i = 0; i < hash_size; i++) {
205
	hash_elem *p = t->array [i]  ;
229
	hash_elem *p = t->array [i] ;
206
	while ( p ) {
230
	while (p) {
207
	    hash_elem *pn = p->next ;
231
	    hash_elem *pn = p->next;
208
	    hash_elem *q = r, *s = null ;
232
	    hash_elem *q = r, *s = null;
209
	    while ( q ) {
233
	    while (q) {
210
		if ( strcmp ( p->obj->name, q->obj->name ) <= 0 ) break ;
234
		if (strcmp(p->obj->name, q->obj->name) <= 0)break;
211
		s = q ;
235
		s = q;
212
		q = q->next ;
236
		q = q->next;
213
	    }
237
	    }
214
	    if ( s == null ) {
238
	    if (s == null) {
215
		p->next = r ;
239
		p->next = r;
216
		r = p ;
240
		r = p;
217
	    } else {
241
	    } else {
218
		p->next = s->next ;
242
		p->next = s->next;
219
		s->next = p ;
243
		s->next = p;
220
	    }
244
	    }
221
	    p = pn ;
245
	    p = pn;
222
	}
246
	}
223
    }
247
    }
224
    return ( r ) ;
248
    return(r);
225
}
249
}