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