1 |
#include <cstring> |
2 |
#include <cstdio> |
3 |
#include <cstdlib> |
4 |
|
5 |
#include "SimSetup.hpp" |
6 |
#include "Globals.hpp" |
7 |
#include "BASS_interface.h" |
8 |
|
9 |
|
10 |
|
11 |
// Globals ************************************************ |
12 |
|
13 |
typedef enum { GLOBAL_BLK, MOLECULE_BLK, ATOM_BLK, BOND_BLK, BEND_BLK, |
14 |
TORSION_BLK, COMPONENT_BLK } block_type; |
15 |
|
16 |
block_type current_block = GLOBAL_BLK; |
17 |
#define MAX_NEST 20 // the max number of nested blocks |
18 |
block_type block_stack[MAX_NEST]; |
19 |
int block_stack_ptr = 0; |
20 |
|
21 |
void incr_block( block_type new_block ); |
22 |
void decr_block(); |
23 |
|
24 |
MakeStamps *the_stamps; |
25 |
Globals *the_globals; |
26 |
|
27 |
// Functions ********************************************** |
28 |
|
29 |
/* |
30 |
* This is the main event handler |
31 |
* |
32 |
* returns 1 if the event was handled. If the event isn't handled, the |
33 |
* event's err_msg is set, and 0 is returned |
34 |
*/ |
35 |
|
36 |
int event_handler( event* the_event ){ |
37 |
|
38 |
int handled = 0; |
39 |
|
40 |
switch( current_block ){ |
41 |
|
42 |
case GLOBAL_BLK: |
43 |
switch( the_event->event_type ){ |
44 |
|
45 |
case MOLECULE: |
46 |
incr_block( MOLECULE_BLK ); |
47 |
handled = the_stamps->newMolecule( the_event ); |
48 |
break; |
49 |
|
50 |
case COMPONENT: |
51 |
incr_block( COMPONENT_BLK ); |
52 |
handled = the_globals->newComponent( the_event ); |
53 |
break; |
54 |
|
55 |
case ASSIGNMENT: |
56 |
handled = the_globals->globalAssign( the_event ); |
57 |
break; |
58 |
|
59 |
case BLOCK_END: |
60 |
handled = the_globals->globalEnd( the_event ); |
61 |
break; |
62 |
|
63 |
default: |
64 |
the_event->err_msg = |
65 |
strdup("Not a valid global event\n" ); |
66 |
return 0; |
67 |
} |
68 |
break; |
69 |
|
70 |
case MOLECULE_BLK: |
71 |
|
72 |
switch( the_event->event_type ){ |
73 |
|
74 |
case ATOM: |
75 |
incr_block( ATOM_BLK ); |
76 |
handled = the_stamps->newAtom( the_event ); |
77 |
break; |
78 |
|
79 |
case BOND: |
80 |
incr_block( BOND_BLK ); |
81 |
handled = the_stamps->newBond( the_event ); |
82 |
break; |
83 |
|
84 |
case BEND: |
85 |
incr_block( BEND_BLK ); |
86 |
handled = the_stamps->newBend( the_event ); |
87 |
break; |
88 |
|
89 |
case TORSION: |
90 |
incr_block( TORSION_BLK ); |
91 |
handled = the_stamps->newTorsion( the_event ); |
92 |
break; |
93 |
|
94 |
case ASSIGNMENT: |
95 |
handled = the_stamps->moleculeAssign( the_event ); |
96 |
break; |
97 |
|
98 |
case BLOCK_END: |
99 |
decr_block(); |
100 |
handled = the_stamps->moleculeEnd( the_event ); |
101 |
break; |
102 |
|
103 |
default: |
104 |
the_event->err_msg = |
105 |
strdup( "Not a valid molecule event\n" ); |
106 |
return 0; |
107 |
} |
108 |
break; |
109 |
|
110 |
case ATOM_BLK: |
111 |
|
112 |
switch( the_event->event_type ){ |
113 |
|
114 |
case POSITION: |
115 |
handled = the_stamps->atomPosition( the_event ); |
116 |
break; |
117 |
|
118 |
case ORIENTATION: |
119 |
handled = the_stamps->atomOrientation( the_event ); |
120 |
break; |
121 |
|
122 |
case ASSIGNMENT: |
123 |
handled = the_stamps->atomAssign( the_event ); |
124 |
break; |
125 |
|
126 |
case BLOCK_END: |
127 |
decr_block(); |
128 |
handled = the_stamps->atomEnd( the_event ); |
129 |
break; |
130 |
|
131 |
default: |
132 |
the_event->err_msg = |
133 |
strdup( "Not a valid atom event\n" ); |
134 |
return 0; |
135 |
} |
136 |
break; |
137 |
|
138 |
case BOND_BLK: |
139 |
|
140 |
switch( the_event->event_type ){ |
141 |
|
142 |
case ASSIGNMENT: |
143 |
handled = the_stamps->bondAssign( the_event ); |
144 |
break; |
145 |
|
146 |
case MEMBER: |
147 |
handled = the_stamps->bondMember( the_event ); |
148 |
break; |
149 |
|
150 |
case CONSTRAINT: |
151 |
handled = the_stamps->bondConstraint( the_event ); |
152 |
break; |
153 |
|
154 |
case BLOCK_END: |
155 |
decr_block(); |
156 |
handled = the_stamps->bondEnd(the_event ); |
157 |
break; |
158 |
|
159 |
default: |
160 |
the_event->err_msg = |
161 |
strdup( "not a valid bond event\n" ); |
162 |
return 0; |
163 |
} |
164 |
break; |
165 |
|
166 |
case BEND_BLK: |
167 |
|
168 |
switch( the_event->event_type ){ |
169 |
|
170 |
case ASSIGNMENT: |
171 |
handled = the_stamps->bendAssign( the_event ); |
172 |
break; |
173 |
|
174 |
case MEMBER: |
175 |
handled = the_stamps->bendMember( the_event ); |
176 |
break; |
177 |
|
178 |
case CONSTRAINT: |
179 |
handled = the_stamps->bendConstraint( the_event ); |
180 |
break; |
181 |
|
182 |
case BLOCK_END: |
183 |
decr_block(); |
184 |
handled = the_stamps->bendEnd(the_event ); |
185 |
break; |
186 |
|
187 |
default: |
188 |
the_event->err_msg = |
189 |
strdup( "not a valid bend event\n" ); |
190 |
return 0; |
191 |
} |
192 |
break; |
193 |
|
194 |
case TORSION_BLK: |
195 |
|
196 |
switch( the_event->event_type ){ |
197 |
|
198 |
case ASSIGNMENT: |
199 |
handled = the_stamps->torsionAssign( the_event ); |
200 |
break; |
201 |
|
202 |
case MEMBER: |
203 |
handled = the_stamps->torsionMember( the_event ); |
204 |
break; |
205 |
|
206 |
case CONSTRAINT: |
207 |
handled = the_stamps->torsionConstraint( the_event ); |
208 |
break; |
209 |
|
210 |
case BLOCK_END: |
211 |
decr_block(); |
212 |
handled = the_stamps->torsionEnd(the_event ); |
213 |
break; |
214 |
|
215 |
default: |
216 |
the_event->err_msg = |
217 |
strdup( "not a valid torsion event\n" ); |
218 |
return 0; |
219 |
} |
220 |
break; |
221 |
|
222 |
case COMPONENT_BLK: |
223 |
|
224 |
switch( the_event->event_type ){ |
225 |
|
226 |
case ASSIGNMENT: |
227 |
handled = the_globals->componentAssign( the_event ); |
228 |
break; |
229 |
|
230 |
case START_INDEX: |
231 |
handled = the_globals->componentStartIndex( the_event ); |
232 |
break; |
233 |
|
234 |
case BLOCK_END: |
235 |
decr_block(); |
236 |
handled = the_globals->componentEnd(the_event ); |
237 |
break; |
238 |
|
239 |
default: |
240 |
the_event->err_msg = |
241 |
strdup( "not a valid component event\n" ); |
242 |
return 0; |
243 |
} |
244 |
break; |
245 |
|
246 |
default: |
247 |
the_event->err_msg = |
248 |
strdup( "event is not in a valid block type\n" ); |
249 |
return 0; |
250 |
} |
251 |
|
252 |
return handled; |
253 |
} |
254 |
|
255 |
void incr_block( block_type new_block ){ |
256 |
|
257 |
block_stack[block_stack_ptr] = current_block; |
258 |
block_stack_ptr++; |
259 |
|
260 |
if( block_stack_ptr >= MAX_NEST ){ |
261 |
fprintf( stderr, "Event blocks nested too deeply\n" ); |
262 |
exit(1); |
263 |
} |
264 |
|
265 |
else current_block = new_block; |
266 |
} |
267 |
|
268 |
|
269 |
void decr_block( void ){ |
270 |
|
271 |
block_stack_ptr--; |
272 |
|
273 |
if( block_stack_ptr < 0 ){ |
274 |
|
275 |
fprintf( stderr, "Too many event blocks closed\n" ); |
276 |
exit(1); |
277 |
} |
278 |
|
279 |
else current_block = block_stack[block_stack_ptr]; |
280 |
} |
281 |
|
282 |
void set_interface_stamps( MakeStamps* ms, Globals* g ){ |
283 |
|
284 |
the_stamps = ms; |
285 |
the_globals = g; |
286 |
} |