1 |
/* |
2 |
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
* |
4 |
* The University of Notre Dame grants you ("Licensee") a |
5 |
* non-exclusive, royalty free, license to use, modify and |
6 |
* redistribute this software in source and binary code form, provided |
7 |
* that the following conditions are met: |
8 |
* |
9 |
* 1. Acknowledgement of the program authors must be made in any |
10 |
* publication of scientific results based in part on use of the |
11 |
* program. An acceptable form of acknowledgement is citation of |
12 |
* the article in which the program was described (Matthew |
13 |
* A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher |
14 |
* J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented |
15 |
* Parallel Simulation Engine for Molecular Dynamics," |
16 |
* J. Comput. Chem. 26, pp. 252-271 (2005)) |
17 |
* |
18 |
* 2. Redistributions of source code must retain the above copyright |
19 |
* notice, this list of conditions and the following disclaimer. |
20 |
* |
21 |
* 3. Redistributions in binary form must reproduce the above copyright |
22 |
* notice, this list of conditions and the following disclaimer in the |
23 |
* documentation and/or other materials provided with the |
24 |
* distribution. |
25 |
* |
26 |
* This software is provided "AS IS," without a warranty of any |
27 |
* kind. All express or implied conditions, representations and |
28 |
* warranties, including any implied warranty of merchantability, |
29 |
* fitness for a particular purpose or non-infringement, are hereby |
30 |
* excluded. The University of Notre Dame and its licensors shall not |
31 |
* be liable for any damages suffered by licensee as a result of |
32 |
* using, modifying or distributing the software or its |
33 |
* derivatives. In no event will the University of Notre Dame or its |
34 |
* licensors be liable for any lost revenue, profit or data, or for |
35 |
* direct, indirect, special, consequential, incidental or punitive |
36 |
* damages, however caused and regardless of the theory of liability, |
37 |
* arising out of the use of or inability to use software, even if the |
38 |
* University of Notre Dame has been advised of the possibility of |
39 |
* such damages. |
40 |
*/ |
41 |
|
42 |
#include <stdio.h> |
43 |
#include <stdlib.h> |
44 |
#include <string.h> |
45 |
|
46 |
#include "BASSyacc.h" |
47 |
#include "io/BASS_parse.h" |
48 |
#include "utils/simError.h" |
49 |
#ifdef IS_MPI |
50 |
#define __is_lex__ |
51 |
#include "io/mpiBASS.h" |
52 |
#endif |
53 |
|
54 |
#define HASH_SIZE 211 // the size of the hash table |
55 |
#define SHIFT 4 // the bit shift for the hash index |
56 |
|
57 |
|
58 |
//*** Global functions, variables, and structures ************ |
59 |
|
60 |
unsigned short is_initialized = 0; // tells whether to init the linked list |
61 |
|
62 |
// reserved word elements for the hash table |
63 |
struct res_element{ |
64 |
char word[100]; //the reserved word |
65 |
int token; //the token to return; |
66 |
struct res_element* next; |
67 |
}; |
68 |
|
69 |
struct res_element** reserved = NULL; //the table of reserved words |
70 |
|
71 |
void initialize_res_list(void); // function to initialize the list |
72 |
|
73 |
int hash ( char* text ); // generates the hash key |
74 |
|
75 |
void add_res_element( char* text, int the_token ); //adds elements to the table |
76 |
|
77 |
// the elements of the defined Hash table |
78 |
struct defined_element{ |
79 |
char defined[100]; |
80 |
char definition[DEFINED_BUFFER_SIZE]; |
81 |
struct defined_element* next; |
82 |
}; |
83 |
|
84 |
struct defined_element** defined_list = NULL; // the defined hash table |
85 |
|
86 |
|
87 |
|
88 |
//*** The Functions ******************************************* |
89 |
|
90 |
|
91 |
/* |
92 |
function to initialize the list of reserved words into memory. |
93 |
*/ |
94 |
|
95 |
void initialize_res_list(){ |
96 |
|
97 |
register unsigned short i; // loop counter |
98 |
|
99 |
reserved = ( struct res_element ** ) |
100 |
calloc( HASH_SIZE, sizeof( struct hash__element* ) ); |
101 |
|
102 |
for( i=0; i < HASH_SIZE; i++ ){ |
103 |
|
104 |
reserved[i] = NULL; |
105 |
} |
106 |
|
107 |
// add the reserved words |
108 |
|
109 |
add_res_element( "molecule", MOLECULE ); |
110 |
add_res_element( "atom", ATOM ); |
111 |
add_res_element( "bond", BOND ); |
112 |
add_res_element( "bend", BEND ); |
113 |
add_res_element( "torsion", TORSION ); |
114 |
add_res_element( "position", POSITION ); |
115 |
add_res_element( "members", MEMBERS ); |
116 |
add_res_element( "constraint", CONSTRAINT ); |
117 |
add_res_element( "component", COMPONENT ); |
118 |
add_res_element( "zConstraint", ZCONSTRAINT ); |
119 |
add_res_element( "orientation", ORIENTATION ); |
120 |
add_res_element( "rigidBody", RIGIDBODY ); |
121 |
add_res_element( "cutoffGroup", CUTOFFGROUP ); |
122 |
|
123 |
is_initialized = 1; // set the initialization boolean to true |
124 |
} |
125 |
|
126 |
|
127 |
/* |
128 |
checks for reserved words. |
129 |
If a reserved word is found, returns the token, |
130 |
else returns 0. |
131 |
*/ |
132 |
|
133 |
int res_word( char* text ){ |
134 |
|
135 |
int matched = 0; |
136 |
int key; // the hash key |
137 |
struct res_element* current_ptr; // points to the current hash element |
138 |
struct defined_element* def_ptr; // points to the current define element |
139 |
|
140 |
if( !is_initialized ){ // initialize the list if not already done |
141 |
|
142 |
initialize_res_list(); |
143 |
} |
144 |
|
145 |
// get the hash key |
146 |
|
147 |
key = hash( text ); |
148 |
current_ptr = reserved[key]; |
149 |
|
150 |
// walk through possible mutiple entries |
151 |
|
152 |
while( current_ptr != NULL ){ |
153 |
|
154 |
matched = !strcmp( text, current_ptr->word ); |
155 |
if( matched ) return current_ptr->token; // search successful |
156 |
|
157 |
current_ptr = current_ptr->next; |
158 |
} |
159 |
|
160 |
// if no keywords turn up, check the word against the list of defines |
161 |
|
162 |
if( defined_list != NULL ){ |
163 |
|
164 |
def_ptr = defined_list[key]; |
165 |
|
166 |
while( def_ptr != NULL ){ |
167 |
|
168 |
matched = !strcmp( text, def_ptr->defined ); |
169 |
if( matched ) return DEFINED; |
170 |
|
171 |
def_ptr = def_ptr->next; |
172 |
} |
173 |
} |
174 |
|
175 |
return 0; //search failed |
176 |
} |
177 |
|
178 |
/* |
179 |
* This is used to test whether a given word is defined |
180 |
* returns 1 if true, 0 if False. |
181 |
*/ |
182 |
|
183 |
int is_defined( char* text ){ |
184 |
|
185 |
int matched = 0; |
186 |
int key; |
187 |
struct defined_element* def_ptr; // points to the current define element |
188 |
|
189 |
key = hash( text ); |
190 |
|
191 |
if( defined_list != NULL ){ |
192 |
def_ptr = defined_list[key]; |
193 |
|
194 |
while( def_ptr != NULL ){ |
195 |
|
196 |
matched = !strcmp( text, def_ptr->defined ); |
197 |
if( matched ) return 1; // search succesful |
198 |
|
199 |
def_ptr = def_ptr->next; |
200 |
} |
201 |
} |
202 |
|
203 |
return 0; //search failed |
204 |
} |
205 |
|
206 |
/* |
207 |
* The next bit returns the text to substitute for any given define. |
208 |
*/ |
209 |
|
210 |
char* get_definition( char* defined ){ |
211 |
int key; |
212 |
int matched = 0; |
213 |
struct defined_element* def_ptr; |
214 |
|
215 |
if( defined_list != NULL ){ |
216 |
|
217 |
key = hash( defined ); |
218 |
def_ptr = defined_list[key]; |
219 |
|
220 |
while( def_ptr != NULL ){ |
221 |
|
222 |
matched = !strcmp( defined, def_ptr->defined ); |
223 |
if( matched ) return def_ptr->definition; |
224 |
|
225 |
def_ptr = def_ptr->next; |
226 |
} |
227 |
} |
228 |
|
229 |
// search failed, therefore there is an error |
230 |
|
231 |
sprintf( painCave.errMsg, "%s was not found in the defined list\n", |
232 |
defined ); |
233 |
painCave.isFatal = 1; |
234 |
simError(); |
235 |
return NULL; |
236 |
} |
237 |
|
238 |
/* |
239 |
* Add a define statement to the hash table |
240 |
*/ |
241 |
|
242 |
void insert_define( char* defined, char* definition ){ |
243 |
|
244 |
int i, key; |
245 |
struct defined_element* element; |
246 |
|
247 |
if( defined_list == NULL ){ |
248 |
|
249 |
defined_list = ( struct defined_element** ) |
250 |
calloc( HASH_SIZE, sizeof( struct defined_element* ) ); |
251 |
|
252 |
for( i=0; i<HASH_SIZE; i++ ){ |
253 |
|
254 |
defined_list[i] = NULL; |
255 |
} |
256 |
} |
257 |
|
258 |
key = hash( defined ); |
259 |
element = (struct defined_element* ) |
260 |
malloc( sizeof( struct defined_element ) ); |
261 |
|
262 |
// fill the element |
263 |
|
264 |
strcpy( element->defined, defined ); |
265 |
strcpy( element->definition, definition ); |
266 |
|
267 |
// add the element to the table |
268 |
|
269 |
element->next = defined_list[key]; |
270 |
defined_list[key] = element; |
271 |
} |
272 |
|
273 |
/* |
274 |
* adds an element to the hash table |
275 |
*/ |
276 |
|
277 |
void add_res_element( char* text, int the_token ){ |
278 |
|
279 |
int key; // the calculated hash key; |
280 |
struct res_element* element; // the element being added |
281 |
|
282 |
key = hash( text ); |
283 |
element = (struct res_element *) malloc( sizeof( struct res_element ) ); |
284 |
|
285 |
// fill the element |
286 |
|
287 |
strcpy( element->word, text ); |
288 |
element->token = the_token; |
289 |
|
290 |
// add the element to the table |
291 |
|
292 |
element->next = reserved[key]; |
293 |
reserved[key] = element; |
294 |
} |
295 |
|
296 |
/* |
297 |
generartes the hash key from the given word. |
298 |
*/ |
299 |
|
300 |
int hash ( char* text ){ |
301 |
|
302 |
register unsigned short int i = 0; // loop counter |
303 |
int key = 0; // the hash key |
304 |
|
305 |
while( text[i] != '\0' ){ |
306 |
|
307 |
key = ( ( key << SHIFT ) + text[i] ) % HASH_SIZE; |
308 |
|
309 |
i++; |
310 |
} |
311 |
|
312 |
if( key < 0 ){ |
313 |
|
314 |
// if the key is less than zero, we've had an overflow error |
315 |
|
316 |
sprintf( painCave.errMsg, |
317 |
"Meta-data parse error: There has been an overflow error in the hash key."); |
318 |
painCave.isFatal =1; |
319 |
simError(); |
320 |
} |
321 |
|
322 |
return key; |
323 |
} |
324 |
|
325 |
/* |
326 |
function to clean up the memory after we are finished with the lists |
327 |
*/ |
328 |
|
329 |
void kill_lists( ){ |
330 |
|
331 |
struct res_element* current_res_ptr; |
332 |
struct res_element* temp_res_ptr; |
333 |
|
334 |
struct defined_element* current_def_ptr; |
335 |
struct defined_element* temp_def_ptr; |
336 |
|
337 |
register unsigned short int i; |
338 |
|
339 |
if( reserved != NULL ){ |
340 |
|
341 |
for( i=0; i < HASH_SIZE; i++){ |
342 |
|
343 |
current_res_ptr = reserved[i]; |
344 |
|
345 |
while( current_res_ptr != NULL ){ |
346 |
|
347 |
temp_res_ptr = current_res_ptr->next; |
348 |
free( current_res_ptr ); |
349 |
current_res_ptr = temp_res_ptr; |
350 |
} |
351 |
} |
352 |
|
353 |
free( reserved ); |
354 |
} |
355 |
|
356 |
if( defined_list != NULL ){ |
357 |
|
358 |
for( i=0; i < HASH_SIZE; i++){ |
359 |
|
360 |
current_def_ptr = defined_list[i]; |
361 |
|
362 |
while( current_def_ptr != NULL ){ |
363 |
|
364 |
temp_def_ptr = current_def_ptr->next; |
365 |
free( current_def_ptr ); |
366 |
current_def_ptr = temp_def_ptr; |
367 |
} |
368 |
} |
369 |
|
370 |
free( defined_list ); |
371 |
} |
372 |
|
373 |
reserved = NULL; |
374 |
defined_list = NULL; |
375 |
|
376 |
is_initialized = 0; // initialization is now false |
377 |
} |