ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/mdtools/BASS_parse/BASS_parse.c
Revision: 117
Committed: Tue Sep 24 22:10:55 2002 UTC (22 years, 7 months ago) by mmeineke
Content type: text/plain
File size: 6914 byte(s)
Log Message:
fixed allot of warnings, and adde the molecule

File Contents

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