ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/mdtools/BASS_parse/BASS_parse.c
Revision: 11
Committed: Tue Jul 9 18:40:59 2002 UTC (21 years, 11 months ago) by mmeineke
Content type: text/plain
File size: 6963 byte(s)
Log Message:
This commit was generated by cvs2svn to compensate for changes in r10, which
included commits to RCS files with non-trunk default branches.

File Contents

# User Rev Content
1 mmeineke 10 #include <stdio.h>
2     #include <stdlib.h>
3     #include <string.h>
4    
5     #include "BASS_parse.h"
6    
7     #define HASH_SIZE 211 // the size of the hash table
8     #define SHIFT 4 // the bit shift for the hash index calculation
9    
10    
11    
12     //*** Global functions, variables, and structures ************
13    
14     unsigned short is_initialized = 0; // tells whether to init the linked list
15    
16     // reserved word elements for the hash table
17     struct res_element{
18     char word[100]; //the reserved word
19     int token; //the token to return;
20     struct res_element* next;
21     };
22    
23     struct res_element** reserved = NULL; //the table of reserved words
24    
25     void initialize_res_list(void); // function to initialize the list
26    
27     int hash ( char* text ); // generates the hash key
28    
29     void add_res_element( char* text, int the_token ); //adds elements to the table
30    
31     // the elements of the defined Hash table
32     struct defined_element{
33     char defined[100];
34     char definition[DEFINED_BUFFER_SIZE];
35     struct defined_element* next;
36     };
37    
38     struct defined_element** defined_list = NULL; // the defined hash table
39    
40    
41    
42     //*** The Functions *******************************************
43    
44    
45     /*
46     function to initialize the list of reserved words into memory.
47     */
48    
49     void initialize_res_list(){
50    
51     register unsigned short i; // loop counter
52    
53     reserved = ( struct res_element ** )
54     calloc( HASH_SIZE, sizeof( struct hash__element* ) );
55    
56     for( i=0; i < HASH_SIZE; i++ ){
57    
58     reserved[i] = NULL;
59     }
60    
61     // add the reserved words
62    
63     add_res_element( "molecule", MOLECULE );
64     add_res_element( "atom", ATOM );
65     add_res_element( "bond", BOND );
66     add_res_element( "bend", BEND );
67     add_res_element( "torsion", TORSION );
68     add_res_element( "position", POSITION );
69     add_res_element( "members", MEMBERS );
70     add_res_element( "constraint", CONSTRAINT );
71     add_res_element( "component", COMPONENT );
72     add_res_element( "startIndex", START_INDEX );
73     add_res_element( "orientation", ORIENTATION );
74    
75     is_initialized = 1; // set the initialization boolean to true
76     }
77    
78    
79     /*
80     checks for reserved words.
81     If a reserved word is found, returns the token,
82     else returns 0.
83     */
84    
85     int res_word( char* text ){
86    
87     register unsigned short int i; // loop counter
88     int matched = 0;
89     int key; // the hash key
90     struct res_element* current_ptr; // points to the current hash element
91     struct defined_element* def_ptr; // points to the current define element
92    
93     if( !is_initialized ){ // initialize the list if not already done
94    
95     initialize_res_list();
96     }
97    
98     // get the hash key
99    
100     key = hash( text );
101     current_ptr = reserved[key];
102    
103     // walk through possible mutiple entries
104    
105     while( current_ptr != NULL ){
106    
107     matched = !strcmp( text, current_ptr->word );
108     if( matched ) return current_ptr->token; // search successful
109    
110     current_ptr = current_ptr->next;
111     }
112    
113     // if no keywords turn up, check the word against the list of defines
114    
115     if( defined_list != NULL ){
116     def_ptr = defined_list[key];
117    
118     while( def_ptr != NULL ){
119    
120     matched = !strcmp( text, def_ptr->defined );
121     if( matched ) return DEFINED;
122    
123     def_ptr = def_ptr->next;
124     }
125     }
126    
127     return 0; //search failed
128     }
129    
130     /*
131     * This is used to test whether a given word is defined
132     * returns 1 if true, 0 if False.
133     */
134    
135     int is_defined( char* text ){
136    
137     int matched = 0;
138     int key;
139     struct defined_element* def_ptr; // points to the current define element
140    
141     key = hash( text );
142    
143     if( defined_list != NULL ){
144     def_ptr = defined_list[key];
145    
146     while( def_ptr != NULL ){
147    
148     matched = !strcmp( text, def_ptr->defined );
149     if( matched ) return 1; // search succesful
150    
151     def_ptr = def_ptr->next;
152     }
153     }
154    
155     return 0; //search failed
156     }
157    
158     /*
159     * The next bit returns the text to substitute for any given define.
160     */
161    
162     char* get_definition( char* defined ){
163     int key;
164     int matched = 0;
165     struct defined_element* def_ptr;
166    
167     if( defined_list != NULL ){
168    
169     key = hash( defined );
170     def_ptr = defined_list[key];
171    
172     while( def_ptr != NULL ){
173    
174     matched = !strcmp( defined, def_ptr->defined );
175     if( matched ) return def_ptr->definition;
176    
177     def_ptr = def_ptr->next;
178     }
179     }
180    
181     // search failed, therefore there is an error
182    
183     fprintf( stderr, "%s was not found in the defined list\n", defined );
184     exit(1);
185     }
186    
187     /*
188     * Add a define statement to the hash table
189     */
190    
191     void insert_define( char* defined, char* definition ){
192    
193     int i, key;
194     struct defined_element* element;
195    
196     if( defined_list == NULL ){
197    
198     defined_list = ( struct defined_element** )
199     calloc( HASH_SIZE, sizeof( struct defined_element* ) );
200    
201     for( i=0; i<HASH_SIZE; i++ ){
202    
203     defined_list[i] = NULL;
204     }
205     }
206    
207     key = hash( defined );
208     element = (struct defined_element* )
209     malloc( sizeof( struct defined_element ) );
210    
211     // fill the element
212    
213     strcpy( element->defined, defined );
214     strcpy( element->definition, definition );
215    
216     // add the element to the table
217    
218     element->next = defined_list[key];
219     defined_list[key] = element;
220     }
221    
222     /*
223     * adds an element to the hash table
224     */
225    
226     void add_res_element( char* text, int the_token ){
227    
228     int key; // the calculated hash key;
229     struct res_element* element; // the element being added
230    
231     key = hash( text );
232     element = (struct res_element *) malloc( sizeof( struct res_element ) );
233    
234     // fill the element
235    
236     strcpy( element->word, text );
237     element->token = the_token;
238    
239     // add the element to the table
240    
241     element->next = reserved[key];
242     reserved[key] = element;
243     }
244    
245     /*
246     generartes the hash key from the given word.
247     */
248    
249     int hash ( char* text ){
250    
251     register unsigned short int i = 0; // loop counter
252     int key = 0; // the hash key
253    
254     while( text[i] != '\0' ){
255    
256     key = ( ( key << SHIFT ) + text[i] ) % HASH_SIZE;
257    
258     i++;
259     }
260    
261     if( key < 0 ){
262    
263     // if the key is less than zero, we've had an overflow error
264    
265     fprintf( stderr,
266     "There has been an overflow error in the hash key.");
267     exit(0);
268     }
269    
270     return key;
271     }
272    
273     /*
274     function to clean up the memory after we are finished with the lists
275     */
276    
277     void kill_lists( ){
278    
279     struct res_element* current_res_ptr;
280     struct res_element* temp_res_ptr;
281    
282     struct defined_element* current_def_ptr;
283     struct defined_element* temp_def_ptr;
284    
285     register unsigned short int i;
286    
287     if( reserved != NULL ){
288    
289     for( i=0; i < HASH_SIZE; i++){
290    
291     current_res_ptr = reserved[i];
292    
293     while( current_res_ptr != NULL ){
294    
295     temp_res_ptr = current_res_ptr->next;
296     free( current_res_ptr );
297     current_res_ptr = temp_res_ptr;
298     }
299     }
300    
301     free( reserved );
302     }
303    
304     if( defined_list != NULL ){
305    
306     for( i=0; i < HASH_SIZE; i++){
307    
308     current_def_ptr = defined_list[i];
309    
310     while( current_def_ptr != NULL ){
311    
312     temp_def_ptr = current_def_ptr->next;
313     free( current_def_ptr );
314     current_def_ptr = temp_def_ptr;
315     }
316     }
317    
318     free( defined_list );
319     }
320    
321     reserved = NULL;
322     defined_list = NULL;
323    
324     is_initialized = 0; // initialization is now false
325     }