ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/mdtools/BASS_parse/BASS_parse.c
Revision: 160
Committed: Wed Oct 30 22:38:22 2002 UTC (21 years, 8 months ago) by mmeineke
Content type: text/plain
File size: 7112 byte(s)
Log Message:
*** empty log message ***

File Contents

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