ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libBASS/BASS_parse.c
Revision: 988
Committed: Tue Jan 27 19:37:48 2004 UTC (20 years, 5 months ago) by gezelter
Content type: text/plain
File size: 7197 byte(s)
Log Message:
More BASS changes to do new rigidBody scheme

File Contents

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