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