1 |
#include <cstdlib> |
2 |
#include <cstdio> |
3 |
#include <cstring> |
4 |
#include <iostream> |
5 |
|
6 |
#include "MoleculeStamp.hpp" |
7 |
#include "simError.h" |
8 |
|
9 |
MoleculeStamp::MoleculeStamp(){ |
10 |
|
11 |
n_atoms = 0; |
12 |
n_bonds = 0; |
13 |
n_bends = 0; |
14 |
n_torsions = 0; |
15 |
|
16 |
unhandled = NULL; |
17 |
atoms = NULL; |
18 |
bonds = NULL; |
19 |
bends = NULL; |
20 |
torsions = NULL; |
21 |
|
22 |
have_name = 0; |
23 |
have_atoms = 0; |
24 |
have_bonds = 0; |
25 |
have_bends = 0; |
26 |
have_torsions = 0; |
27 |
|
28 |
} |
29 |
|
30 |
MoleculeStamp::~MoleculeStamp(){ |
31 |
int i; |
32 |
|
33 |
if( unhandled != NULL) delete unhandled; |
34 |
|
35 |
if( atoms != NULL ){ |
36 |
for( i=0; i<n_atoms; i++ ) delete atoms[i]; |
37 |
} |
38 |
|
39 |
if( bonds != NULL ){ |
40 |
for( i=0; i<n_bonds; i++ ) delete bonds[i]; |
41 |
} |
42 |
|
43 |
if( bends != NULL ){ |
44 |
for( i=0; i<n_bends; i++ ) delete bends[i]; |
45 |
} |
46 |
|
47 |
if( torsions != NULL ){ |
48 |
for( i=0; i<n_torsions; i++ ) delete torsions[i]; |
49 |
} |
50 |
|
51 |
} |
52 |
|
53 |
void MoleculeStamp::assignString( char* lhs, char* rhs ){ |
54 |
|
55 |
if( !strcmp( lhs, "name" ) ){ |
56 |
strcpy( name, rhs ); |
57 |
have_name = 1; |
58 |
} |
59 |
else{ |
60 |
if( unhandled == NULL ) unhandled = new LinkedAssign( lhs, rhs ); |
61 |
else unhandled->add( lhs, rhs ); |
62 |
have_extras = 1; |
63 |
} |
64 |
} |
65 |
|
66 |
void MoleculeStamp::assignDouble( char* lhs, double rhs ){ |
67 |
int i; |
68 |
|
69 |
if( !strcmp( lhs, "nAtoms" ) ){ |
70 |
n_atoms = (int)rhs; |
71 |
|
72 |
if( have_atoms ){ |
73 |
sprintf( painCave.errMsg, |
74 |
"MoleculeStamp error, n_atoms already declared" |
75 |
"for molecule: %s\n", |
76 |
name); |
77 |
painCave.isFatal = 1; |
78 |
simError(); |
79 |
} |
80 |
have_atoms = 1; |
81 |
atoms = new AtomStamp*[n_atoms]; |
82 |
for( i=0; i<n_atoms; i++ ) atoms[i] = NULL; |
83 |
} |
84 |
|
85 |
else if( !strcmp( lhs, "nBonds" ) ){ |
86 |
n_bonds = (int)rhs; |
87 |
|
88 |
if( have_bonds ){ |
89 |
sprintf( painCave.errMsg, |
90 |
"MoleculeStamp error, n_bonds already declared for" |
91 |
" molecule: %s\n", |
92 |
name); |
93 |
painCave.isFatal = 1; |
94 |
simError(); |
95 |
} |
96 |
have_bonds = 1; |
97 |
bonds = new BondStamp*[n_bonds]; |
98 |
for( i=0; i<n_bonds; i++ ) bonds[i] = NULL; |
99 |
} |
100 |
|
101 |
else if( !strcmp( lhs, "nBends" ) ){ |
102 |
n_bends = (int)rhs; |
103 |
|
104 |
if( have_bends ){ |
105 |
sprintf( painCave.errMsg, |
106 |
"MoleculeStamp error, n_bends already declared for" |
107 |
" molecule: %s\n", |
108 |
name); |
109 |
painCave.isFatal = 1; |
110 |
simError(); |
111 |
} |
112 |
have_bends = 1; |
113 |
bends = new BendStamp*[n_bends]; |
114 |
for( i=0; i<n_bends; i++ ) bends[i] = NULL; |
115 |
} |
116 |
|
117 |
else if( !strcmp( lhs, "nTorsions" ) ){ |
118 |
n_torsions = (int)rhs; |
119 |
|
120 |
if( have_torsions ){ |
121 |
sprintf( painCave.errMsg, |
122 |
"MoleculeStamp error, n_torsions already declared for" |
123 |
" molecule: %s\n", |
124 |
name ); |
125 |
painCave.isFatal = 1; |
126 |
simError(); |
127 |
} |
128 |
have_torsions = 1; |
129 |
torsions = new TorsionStamp*[n_torsions]; |
130 |
for( i=0; i<n_torsions; i++ ) torsions[i] = NULL; |
131 |
} |
132 |
else{ |
133 |
if( unhandled == NULL ) unhandled = new LinkedAssign( lhs, rhs ); |
134 |
else unhandled->add( lhs, rhs ); |
135 |
have_extras = 1; |
136 |
} |
137 |
} |
138 |
|
139 |
void MoleculeStamp::assignInt( char* lhs, int rhs ){ |
140 |
int i; |
141 |
|
142 |
if( !strcmp( lhs, "nAtoms" ) ){ |
143 |
n_atoms = rhs; |
144 |
|
145 |
if( have_atoms ){ |
146 |
sprintf( painCave.errMsg, |
147 |
"MoleculeStamp error, n_atoms already declared for" |
148 |
" molecule: %s\n", |
149 |
name); |
150 |
painCave.isFatal = 1; |
151 |
simError(); |
152 |
} |
153 |
have_atoms = 1; |
154 |
atoms = new AtomStamp*[n_atoms]; |
155 |
for( i=0; i<n_atoms; i++ ) atoms[i] = NULL; |
156 |
} |
157 |
|
158 |
else if( !strcmp( lhs, "nBonds" ) ){ |
159 |
n_bonds = rhs; |
160 |
|
161 |
if( have_bonds ){ |
162 |
sprintf( painCave.errMsg, |
163 |
"MoleculeStamp error, n_bonds already declared for" |
164 |
" molecule: %s\n", |
165 |
name); |
166 |
painCave.isFatal = 1; |
167 |
simError(); |
168 |
} |
169 |
have_bonds = 1; |
170 |
bonds = new BondStamp*[n_bonds]; |
171 |
for( i=0; i<n_bonds; i++ ) bonds[i] = NULL; |
172 |
} |
173 |
|
174 |
else if( !strcmp( lhs, "nBends" ) ){ |
175 |
n_bends = rhs; |
176 |
|
177 |
if( have_bends ){ |
178 |
sprintf( painCave.errMsg, |
179 |
"MoleculeStamp error, n_bends already declared for" |
180 |
" molecule: %s\n", |
181 |
name ); |
182 |
painCave.isFatal = 1; |
183 |
simError(); |
184 |
} |
185 |
have_bends = 1; |
186 |
bends = new BendStamp*[n_bends]; |
187 |
for( i=0; i<n_bends; i++ ) bends[i] = NULL; |
188 |
} |
189 |
|
190 |
else if( !strcmp( lhs, "nTorsions" ) ){ |
191 |
n_torsions = rhs; |
192 |
|
193 |
if( have_torsions ){ |
194 |
sprintf( painCave.errMsg, |
195 |
"MoleculeStamp error, n_torsions already declared for" |
196 |
" molecule: %s\n", |
197 |
name); |
198 |
painCave.isFatal = 1; |
199 |
simError(); |
200 |
} |
201 |
have_torsions = 1; |
202 |
torsions = new TorsionStamp*[n_torsions]; |
203 |
for( i=0; i<n_torsions; i++ ) torsions[i] = NULL; |
204 |
} |
205 |
else{ |
206 |
if( unhandled == NULL ) unhandled = new LinkedAssign( lhs, rhs ); |
207 |
else unhandled->add( lhs, rhs ); |
208 |
have_extras = 1; |
209 |
} |
210 |
} |
211 |
|
212 |
char* MoleculeStamp::addAtom( AtomStamp* the_atom, int atomIndex ){ |
213 |
|
214 |
char err[200]; |
215 |
|
216 |
if( have_atoms && atomIndex < n_atoms ) atoms[atomIndex] = the_atom; |
217 |
else{ |
218 |
if( have_atoms ){ |
219 |
sprintf( err, "MoleculeStamp error, %d out of nAtoms range", |
220 |
atomIndex ); |
221 |
return strdup( err ); |
222 |
} |
223 |
else return strdup("MoleculeStamp error, nAtoms not given before" |
224 |
"first atom declaration." ); |
225 |
} |
226 |
|
227 |
return NULL; |
228 |
} |
229 |
|
230 |
char* MoleculeStamp::addBond( BondStamp* the_bond, int bondIndex ){ |
231 |
|
232 |
char err[200]; |
233 |
|
234 |
if( have_bonds && bondIndex < n_bonds ) bonds[bondIndex] = the_bond; |
235 |
else{ |
236 |
if( have_bonds ){ |
237 |
sprintf( err, "MoleculeStamp error, %d out of nBonds range", |
238 |
bondIndex ); |
239 |
return strdup( err ); |
240 |
} |
241 |
else return strdup("MoleculeStamp error, nBonds not given before" |
242 |
"first bond declaration." ); |
243 |
} |
244 |
|
245 |
return NULL; |
246 |
} |
247 |
|
248 |
char* MoleculeStamp::addBend( BendStamp* the_bend, int bendIndex ){ |
249 |
|
250 |
char err[200]; |
251 |
|
252 |
if( have_bends && bendIndex < n_bends ) bends[bendIndex] = the_bend; |
253 |
else{ |
254 |
if( have_bends ){ |
255 |
sprintf( err, "MoleculeStamp error, %d out of nBends range", |
256 |
bendIndex ); |
257 |
return strdup( err ); |
258 |
} |
259 |
else return strdup("MoleculeStamp error, nBends not given before" |
260 |
"first bend declaration." ); |
261 |
} |
262 |
|
263 |
return NULL; |
264 |
} |
265 |
|
266 |
char* MoleculeStamp::addTorsion( TorsionStamp* the_torsion, int torsionIndex ){ |
267 |
|
268 |
char err[200]; |
269 |
|
270 |
if( have_torsions && torsionIndex < n_torsions ) |
271 |
torsions[torsionIndex] = the_torsion; |
272 |
else{ |
273 |
if( have_torsions ){ |
274 |
sprintf( err, "MoleculeStamp error, %d out of nTorsions range", |
275 |
torsionIndex ); |
276 |
return strdup( err ); |
277 |
} |
278 |
else return strdup("MoleculeStamp error, nTorsions not given before" |
279 |
"first torsion declaration." ); |
280 |
} |
281 |
|
282 |
return NULL; |
283 |
} |
284 |
|
285 |
char* MoleculeStamp::checkMe( void ){ |
286 |
|
287 |
int i; |
288 |
short int no_atom; |
289 |
char err[120]; |
290 |
|
291 |
if( !have_name || !have_atoms ){ |
292 |
if( !have_name ) return strdup( "MoleculeStamp error. Molecule's name" |
293 |
" was not given.\n" ); |
294 |
else return strdup( "MoleculeStamp error. Molecule contains no atoms." ); |
295 |
} |
296 |
|
297 |
no_atom = 0; |
298 |
for( i=0; i<n_atoms; i++ ){ |
299 |
if( atoms[i] == NULL ) no_atom = 1; |
300 |
} |
301 |
|
302 |
if( no_atom ){ |
303 |
sprintf( err, |
304 |
"MoleculeStamp error. Not all of the atoms were" |
305 |
" declared in molecule \"%s\".\n", name ); |
306 |
return strdup( err ); |
307 |
} |
308 |
|
309 |
return NULL; |
310 |
} |