1 |
#include <cstdlib> |
2 |
#include <cstdio> |
3 |
|
4 |
#include "MakeStamps.hpp" |
5 |
#include "MoleculeStamp.hpp" |
6 |
|
7 |
LinkedMolStamp::~LinkedMolStamp(){ |
8 |
if( mol_stamp != NULL ) delete mol_stamp; |
9 |
if( next != NULL ) delete next; |
10 |
} |
11 |
|
12 |
MoleculeStamp* LinkedMolStamp::match( char* id ){ |
13 |
|
14 |
if( mol_stamp != NULL ){ |
15 |
|
16 |
if(!strcmp( mol_stamp->getID(), id )) return mol_stamp; |
17 |
|
18 |
if( next != NULL ) return next->match( id ); |
19 |
} |
20 |
|
21 |
return NULL; |
22 |
} |
23 |
|
24 |
MakeStamps::MakeStamps(){ |
25 |
|
26 |
int i; |
27 |
|
28 |
my_mols = new LinkedMolStamp*[hash_size]; |
29 |
for( i=0; i<hash_size; i++ ){ |
30 |
my_mols[i] = NULL; |
31 |
} |
32 |
|
33 |
} |
34 |
|
35 |
MakeStamps::~MakeStamps(){ |
36 |
|
37 |
int i; |
38 |
|
39 |
for( i=0; i<hash_size; i++ ){ |
40 |
if( my_mols[i] != NULL ) delete my_mols[i]; |
41 |
} |
42 |
delete[] my_mols; |
43 |
} |
44 |
|
45 |
int MakeStamps::hash( char* text ){ |
46 |
|
47 |
register unsigned short int i = 0; // loop counter |
48 |
int key = 0; // the hash key |
49 |
|
50 |
while( text[i] != '\0' ){ |
51 |
|
52 |
key = ( ( key << hash_shift ) + text[i] ) % hash_size; |
53 |
|
54 |
i++; |
55 |
} |
56 |
|
57 |
if( key < 0 ){ |
58 |
|
59 |
// if the key is less than zero, we've had an overflow error |
60 |
|
61 |
fprintf( stderr, |
62 |
"There has been an overflow error in the hash key."); |
63 |
exit(0); |
64 |
} |
65 |
|
66 |
return key; |
67 |
} |
68 |
|
69 |
MoleculeStamp* MakeStamps::getMolecule( char* the_id ){ |
70 |
int key; |
71 |
|
72 |
key = hash( the_id ); |
73 |
if( my_mols[key] != NULL ){ |
74 |
return my_mols[key]->match( the_id ); |
75 |
} |
76 |
|
77 |
return NULL; |
78 |
} |
79 |
|
80 |
void MakeStamps::addMolStamp( MoleculeStamp* the_stamp ){ |
81 |
|
82 |
int key; |
83 |
LinkedMolStamp* linked_mol; |
84 |
|
85 |
key = hash( the_stamp->getID() ); |
86 |
|
87 |
linked_mol = new LinkedMolStamp; |
88 |
linked_mol->setStamp( the_stamp ); |
89 |
linked_mol->setNext( my_mols[key] ); |
90 |
|
91 |
my_mols[key] = linked_mol; |
92 |
} |
93 |
|
94 |
|
95 |
int MakeStamps::newMolecule( event* the_event ){ |
96 |
|
97 |
current_mol = new MoleculeStamp; |
98 |
return 1; |
99 |
} |
100 |
|
101 |
int MakeStamps::moleculeAssign( event* the_event ){ |
102 |
|
103 |
switch( the_event->evt.asmt.asmt_type ){ |
104 |
|
105 |
case STRING: |
106 |
current_mol->assignString( the_event->evt.asmt.lhs, |
107 |
the_event->evt.asmt.rhs.sval ); |
108 |
return 1; |
109 |
break; |
110 |
|
111 |
case DOUBLE: |
112 |
current_mol->assignDouble( the_event->evt.asmt.lhs, |
113 |
the_event->evt.asmt.rhs.dval ); |
114 |
return 1; |
115 |
break; |
116 |
|
117 |
case INT: |
118 |
current_mol->assignInt( the_event->evt.asmt.lhs, |
119 |
the_event->evt.asmt.rhs.ival ); |
120 |
return 1; |
121 |
break; |
122 |
|
123 |
default: |
124 |
the_event->err_msg = strdup( "MakeStamp error. Invalid molecule" |
125 |
" assignment type" ); |
126 |
return 0; |
127 |
break; |
128 |
} |
129 |
return 0; |
130 |
} |
131 |
|
132 |
int MakeStamps::moleculeEnd( event* the_event ){ |
133 |
|
134 |
the_event->err_msg = current_mol->checkMe(); |
135 |
// if err_msg is set, then something is wrong |
136 |
if( the_event->err_msg != NULL ) return 0; |
137 |
|
138 |
addMolStamp( current_mol ); |
139 |
return 1; |
140 |
} |
141 |
|
142 |
int MakeStamps::newAtom( event* the_event ){ |
143 |
|
144 |
current_atom = new AtomStamp; |
145 |
|
146 |
the_event->err_msg = current_mol->addAtom( current_atom, |
147 |
the_event->evt.blk_index ); |
148 |
if( the_event->err_msg != NULL ) return 0; |
149 |
|
150 |
return 1; |
151 |
} |
152 |
|
153 |
int MakeStamps::atomPosition( event* the_event ){ |
154 |
|
155 |
current_atom->setPosition( the_event->evt.pos.x, |
156 |
the_event->evt.pos.y, |
157 |
the_event->evt.pos.z ); |
158 |
return 1; |
159 |
} |
160 |
|
161 |
|
162 |
int MakeStamps::atomOrientation( event* the_event ){ |
163 |
|
164 |
current_atom->setOrientation( the_event->evt.ornt.x, |
165 |
the_event->evt.ornt.y, |
166 |
the_event->evt.ornt.z ); |
167 |
return 1; |
168 |
} |
169 |
|
170 |
int MakeStamps::atomAssign( event* the_event ){ |
171 |
|
172 |
switch( the_event->evt.asmt.asmt_type ){ |
173 |
|
174 |
case STRING: |
175 |
current_atom->assignString( the_event->evt.asmt.lhs, |
176 |
the_event->evt.asmt.rhs.sval ); |
177 |
return 1; |
178 |
break; |
179 |
|
180 |
case DOUBLE: |
181 |
current_atom->assignDouble( the_event->evt.asmt.lhs, |
182 |
the_event->evt.asmt.rhs.dval ); |
183 |
return 1; |
184 |
break; |
185 |
|
186 |
case INT: |
187 |
current_atom->assignInt( the_event->evt.asmt.lhs, |
188 |
the_event->evt.asmt.rhs.ival ); |
189 |
return 1; |
190 |
break; |
191 |
|
192 |
default: |
193 |
the_event->err_msg = strdup( "MakeStamp error. Invalid atom" |
194 |
" assignment type" ); |
195 |
return 0; |
196 |
break; |
197 |
} |
198 |
return 0; |
199 |
} |
200 |
|
201 |
int MakeStamps::atomEnd( event* the_event ){ |
202 |
|
203 |
the_event->err_msg = current_atom->checkMe(); |
204 |
if( the_event->err_msg != NULL ) return 0; |
205 |
|
206 |
return 1; |
207 |
} |
208 |
|
209 |
int MakeStamps::newBond( event* the_event ){ |
210 |
|
211 |
current_bond = new BondStamp; |
212 |
|
213 |
the_event->err_msg = current_mol->addBond( current_bond, |
214 |
the_event->evt.blk_index ); |
215 |
if( the_event->err_msg != NULL ) return 0; |
216 |
|
217 |
return 1; |
218 |
} |
219 |
|
220 |
int MakeStamps::bondAssign( event* the_event ){ |
221 |
|
222 |
switch( the_event->evt.asmt.asmt_type ){ |
223 |
|
224 |
case STRING: |
225 |
current_bond->assignString( the_event->evt.asmt.lhs, |
226 |
the_event->evt.asmt.rhs.sval ); |
227 |
return 1; |
228 |
break; |
229 |
|
230 |
case DOUBLE: |
231 |
current_bond->assignDouble( the_event->evt.asmt.lhs, |
232 |
the_event->evt.asmt.rhs.dval ); |
233 |
return 1; |
234 |
break; |
235 |
|
236 |
case INT: |
237 |
current_bond->assignInt( the_event->evt.asmt.lhs, |
238 |
the_event->evt.asmt.rhs.ival ); |
239 |
return 1; |
240 |
break; |
241 |
|
242 |
default: |
243 |
the_event->err_msg = strdup( "MakeStamp error. Invalid bond" |
244 |
" assignment type" ); |
245 |
return 0; |
246 |
break; |
247 |
} |
248 |
return 0; |
249 |
} |
250 |
|
251 |
int MakeStamps::bondMember( event* the_event ){ |
252 |
|
253 |
current_bond->members( the_event->evt.mbr.a, |
254 |
the_event->evt.mbr.b ); |
255 |
return 1; |
256 |
} |
257 |
|
258 |
int MakeStamps::bondConstraint( event* the_event ){ |
259 |
|
260 |
current_bond->constrain( the_event->evt.cnstr ); |
261 |
return 1; |
262 |
} |
263 |
|
264 |
int MakeStamps::bondEnd( event* the_event ){ |
265 |
|
266 |
the_event->err_msg = current_bond->checkMe(); |
267 |
if( the_event->err_msg != NULL ) return 0; |
268 |
|
269 |
return 1; |
270 |
} |
271 |
|
272 |
int MakeStamps::newBend( event* the_event ){ |
273 |
|
274 |
current_bend = new BendStamp; |
275 |
|
276 |
the_event->err_msg = current_mol->addBend( current_bend, |
277 |
the_event->evt.blk_index ); |
278 |
if( the_event->err_msg != NULL ) return 0; |
279 |
|
280 |
return 1; |
281 |
} |
282 |
|
283 |
int MakeStamps::bendAssign( event* the_event ){ |
284 |
|
285 |
switch( the_event->evt.asmt.asmt_type ){ |
286 |
|
287 |
case STRING: |
288 |
current_bend->assignString( the_event->evt.asmt.lhs, |
289 |
the_event->evt.asmt.rhs.sval ); |
290 |
return 1; |
291 |
break; |
292 |
|
293 |
case DOUBLE: |
294 |
current_bend->assignDouble( the_event->evt.asmt.lhs, |
295 |
the_event->evt.asmt.rhs.dval ); |
296 |
return 1; |
297 |
break; |
298 |
|
299 |
case INT: |
300 |
current_bend->assignInt( the_event->evt.asmt.lhs, |
301 |
the_event->evt.asmt.rhs.ival ); |
302 |
return 1; |
303 |
break; |
304 |
|
305 |
default: |
306 |
the_event->err_msg = strdup( "MakeStamp error. Invalid bend" |
307 |
" assignment type" ); |
308 |
return 0; |
309 |
break; |
310 |
} |
311 |
return 0; |
312 |
} |
313 |
|
314 |
int MakeStamps::bendMember( event* the_event ){ |
315 |
|
316 |
current_bend->members( the_event->evt.mbr.a, |
317 |
the_event->evt.mbr.b, |
318 |
the_event->evt.mbr.c ); |
319 |
return 1; |
320 |
} |
321 |
|
322 |
int MakeStamps::bendConstraint( event* the_event ){ |
323 |
|
324 |
current_bend->constrain( the_event->evt.cnstr ); |
325 |
return 1; |
326 |
} |
327 |
|
328 |
int MakeStamps::bendEnd( event* the_event ){ |
329 |
|
330 |
the_event->err_msg = current_bend->checkMe(); |
331 |
if( the_event->err_msg != NULL ) return 0; |
332 |
|
333 |
return 1; |
334 |
} |
335 |
|
336 |
int MakeStamps::newTorsion( event* the_event ){ |
337 |
|
338 |
current_torsion = new TorsionStamp; |
339 |
|
340 |
the_event->err_msg = current_mol->addTorsion( current_torsion, |
341 |
the_event->evt.blk_index ); |
342 |
if( the_event->err_msg != NULL ) return 0; |
343 |
|
344 |
return 1; |
345 |
} |
346 |
|
347 |
int MakeStamps::torsionAssign( event* the_event ){ |
348 |
|
349 |
switch( the_event->evt.asmt.asmt_type ){ |
350 |
|
351 |
case STRING: |
352 |
current_torsion->assignString( the_event->evt.asmt.lhs, |
353 |
the_event->evt.asmt.rhs.sval ); |
354 |
return 1; |
355 |
break; |
356 |
|
357 |
case DOUBLE: |
358 |
current_torsion->assignDouble( the_event->evt.asmt.lhs, |
359 |
the_event->evt.asmt.rhs.dval ); |
360 |
return 1; |
361 |
break; |
362 |
|
363 |
case INT: |
364 |
current_torsion->assignInt( the_event->evt.asmt.lhs, |
365 |
the_event->evt.asmt.rhs.ival ); |
366 |
return 1; |
367 |
break; |
368 |
|
369 |
default: |
370 |
the_event->err_msg = strdup( "MakeStamp error. Invalid torsion" |
371 |
" assignment type" ); |
372 |
return 0; |
373 |
break; |
374 |
} |
375 |
return 0; |
376 |
} |
377 |
|
378 |
int MakeStamps::torsionMember( event* the_event ){ |
379 |
|
380 |
current_torsion->members( the_event->evt.mbr.a, |
381 |
the_event->evt.mbr.b, |
382 |
the_event->evt.mbr.c, |
383 |
the_event->evt.mbr.d ); |
384 |
return 1; |
385 |
} |
386 |
|
387 |
int MakeStamps::torsionConstraint( event* the_event ){ |
388 |
|
389 |
current_torsion->constrain( the_event->evt.cnstr ); |
390 |
return 1; |
391 |
} |
392 |
|
393 |
int MakeStamps::torsionEnd( event* the_event ){ |
394 |
|
395 |
the_event->err_msg = current_torsion->checkMe(); |
396 |
if( the_event->err_msg != NULL ) return 0; |
397 |
|
398 |
return 1; |
399 |
} |