ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/mdtools/BASS_parse/BASS_parse.c
Revision: 138
Committed: Wed Oct 16 21:07:02 2002 UTC (21 years, 8 months ago) by chuckv
Content type: text/plain
File size: 7090 byte(s)
Log Message:

changed mpiInterface.c and .h -> mpiBASS.c and .h for clarity of purpose

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 chuckv 133 #ifdef IS_MPI
8     #define __is_lex__
9 chuckv 138 #include "../headers/mpiBASS.h"
10 chuckv 133 #endif
11    
12 mmeineke 10 #define HASH_SIZE 211 // the size of the hash table
13     #define SHIFT 4 // the bit shift for the hash index calculation
14    
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     add_res_element( "startIndex", START_INDEX );
78     add_res_element( "orientation", ORIENTATION );
79    
80     is_initialized = 1; // set the initialization boolean to true
81     }
82    
83    
84     /*
85     checks for reserved words.
86     If a reserved word is found, returns the token,
87     else returns 0.
88     */
89    
90     int res_word( char* text ){
91    
92     int matched = 0;
93     int key; // the hash key
94     struct res_element* current_ptr; // points to the current hash element
95     struct defined_element* def_ptr; // points to the current define element
96    
97     if( !is_initialized ){ // initialize the list if not already done
98    
99     initialize_res_list();
100     }
101    
102     // get the hash key
103    
104     key = hash( text );
105     current_ptr = reserved[key];
106    
107     // walk through possible mutiple entries
108    
109     while( current_ptr != NULL ){
110    
111     matched = !strcmp( text, current_ptr->word );
112     if( matched ) return current_ptr->token; // search successful
113    
114     current_ptr = current_ptr->next;
115     }
116    
117     // if no keywords turn up, check the word against the list of defines
118    
119     if( defined_list != NULL ){
120     def_ptr = defined_list[key];
121    
122     while( def_ptr != NULL ){
123    
124     matched = !strcmp( text, def_ptr->defined );
125     if( matched ) return DEFINED;
126    
127     def_ptr = def_ptr->next;
128     }
129     }
130    
131     return 0; //search failed
132     }
133    
134     /*
135     * This is used to test whether a given word is defined
136     * returns 1 if true, 0 if False.
137     */
138    
139     int is_defined( char* text ){
140    
141     int matched = 0;
142     int key;
143     struct defined_element* def_ptr; // points to the current define element
144    
145     key = hash( text );
146    
147     if( defined_list != NULL ){
148     def_ptr = defined_list[key];
149    
150     while( def_ptr != NULL ){
151    
152     matched = !strcmp( text, def_ptr->defined );
153     if( matched ) return 1; // search succesful
154    
155     def_ptr = def_ptr->next;
156     }
157     }
158    
159     return 0; //search failed
160     }
161    
162     /*
163     * The next bit returns the text to substitute for any given define.
164     */
165    
166     char* get_definition( char* defined ){
167     int key;
168     int matched = 0;
169     struct defined_element* def_ptr;
170    
171     if( defined_list != NULL ){
172    
173     key = hash( defined );
174     def_ptr = defined_list[key];
175    
176     while( def_ptr != NULL ){
177    
178     matched = !strcmp( defined, def_ptr->defined );
179     if( matched ) return def_ptr->definition;
180    
181     def_ptr = def_ptr->next;
182     }
183     }
184    
185     // search failed, therefore there is an error
186    
187     fprintf( stderr, "%s was not found in the defined list\n", defined );
188 chuckv 133 #ifdef IS_MPI
189     mpiInterfaceExit();
190     #endif
191     exit(0);
192     return NULL;
193 mmeineke 10 }
194    
195     /*
196     * Add a define statement to the hash table
197     */
198    
199     void insert_define( char* defined, char* definition ){
200    
201     int i, key;
202     struct defined_element* element;
203    
204     if( defined_list == NULL ){
205    
206     defined_list = ( struct defined_element** )
207     calloc( HASH_SIZE, sizeof( struct defined_element* ) );
208    
209     for( i=0; i<HASH_SIZE; i++ ){
210    
211     defined_list[i] = NULL;
212     }
213     }
214    
215     key = hash( defined );
216     element = (struct defined_element* )
217     malloc( sizeof( struct defined_element ) );
218    
219     // fill the element
220    
221     strcpy( element->defined, defined );
222     strcpy( element->definition, definition );
223    
224     // add the element to the table
225    
226     element->next = defined_list[key];
227     defined_list[key] = element;
228     }
229    
230     /*
231     * adds an element to the hash table
232     */
233    
234     void add_res_element( char* text, int the_token ){
235    
236     int key; // the calculated hash key;
237     struct res_element* element; // the element being added
238    
239     key = hash( text );
240     element = (struct res_element *) malloc( sizeof( struct res_element ) );
241    
242     // fill the element
243    
244     strcpy( element->word, text );
245     element->token = the_token;
246    
247     // add the element to the table
248    
249     element->next = reserved[key];
250     reserved[key] = element;
251     }
252    
253     /*
254     generartes the hash key from the given word.
255     */
256    
257     int hash ( char* text ){
258    
259     register unsigned short int i = 0; // loop counter
260     int key = 0; // the hash key
261    
262     while( text[i] != '\0' ){
263    
264     key = ( ( key << SHIFT ) + text[i] ) % HASH_SIZE;
265    
266     i++;
267     }
268    
269     if( key < 0 ){
270    
271     // if the key is less than zero, we've had an overflow error
272    
273     fprintf( stderr,
274     "There has been an overflow error in the hash key.");
275 chuckv 133 #ifdef IS_MPI
276     mpiInterfaceExit();
277     #endif
278 mmeineke 10 exit(0);
279     }
280    
281     return key;
282     }
283    
284     /*
285     function to clean up the memory after we are finished with the lists
286     */
287    
288     void kill_lists( ){
289    
290     struct res_element* current_res_ptr;
291     struct res_element* temp_res_ptr;
292    
293     struct defined_element* current_def_ptr;
294     struct defined_element* temp_def_ptr;
295    
296     register unsigned short int i;
297    
298     if( reserved != NULL ){
299    
300     for( i=0; i < HASH_SIZE; i++){
301    
302     current_res_ptr = reserved[i];
303    
304     while( current_res_ptr != NULL ){
305    
306     temp_res_ptr = current_res_ptr->next;
307     free( current_res_ptr );
308     current_res_ptr = temp_res_ptr;
309     }
310     }
311    
312     free( reserved );
313     }
314    
315     if( defined_list != NULL ){
316    
317     for( i=0; i < HASH_SIZE; i++){
318    
319     current_def_ptr = defined_list[i];
320    
321     while( current_def_ptr != NULL ){
322    
323     temp_def_ptr = current_def_ptr->next;
324     free( current_def_ptr );
325     current_def_ptr = temp_def_ptr;
326     }
327     }
328    
329     free( defined_list );
330     }
331    
332     reserved = NULL;
333     defined_list = NULL;
334    
335     is_initialized = 0; // initialization is now false
336     }