-
Notifications
You must be signed in to change notification settings - Fork 5
/
AtomT.cpp
226 lines (179 loc) · 6.36 KB
/
AtomT.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* This file is part of esynth.
*
* esynth is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* esynth is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with esynth. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
#include <sstream>
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include "AtomT.h"
AtomT::AtomT(const AtomT* const that)
{
this->theAtomT.atomType = that->theAtomT.atomType;
this->theAtomT.specificNum = that->theAtomT.specificNum;
this->theAtomT.specialT = that->theAtomT.specialT;
}
AtomT::AtomT(AtomEnumT type, int val, SpecialEnumT spec)
{
this->theAtomT.atomType = type;
this->theAtomT.specificNum = val;
this->theAtomT.specialT = spec;
}
AtomEnumT AtomT::convertToAtomEnum(const std::string& s)
{
if (s == "C") return ATOM_T_CARBON;
else if (s == "Cl") return ATOM_T_CHLORINE;
else if (s == "H") return ATOM_T_HYDROGEN;
else if (s == "N") return ATOM_T_NITROGEN;
else if (s == "O") return ATOM_T_OXYGEN;
else if (s == "P") return ATOM_T_PHOSPHORUS;
else if (s == "S") return ATOM_T_SULFUR;
else if (s == "F") return ATOM_T_FLUORINE;
else if (s == "Br") return ATOM_T_BROMINE;
else if (s == "B") return ATOM_T_BORON;
else if (s == "I") return ATOM_T_IODINE;
// In case of emergency
std::cerr << "Element |" << s << "| not recognized." << std::endl;
throw "Atom type not recognized...attention required.";
return ATOM_T_UNKNOWN;
}
SpecialEnumT AtomT::convertToSpecialEnum(const std::string& s)
{
//
// Force the contents to be lower case
//
std::string loweredS = s;
std::transform(loweredS.begin(), loweredS.end(), loweredS.begin(), ::tolower);
if (loweredS == "am") return SPECIAL_T_AM;
else if (loweredS == "ar") return SPECIAL_T_AROMATIC;
else if (loweredS == "pl") return SPECIAL_T_PL;
else if (loweredS == "co") return SPECIAL_T_CO;
else if (loweredS == "o") return SPECIAL_T_O;
else if (loweredS == "cat") return SPECIAL_T_CAT;
// In case of emergency
if (loweredS.length() > 0)
{
std::cerr << "Special Enumeration |" << s << "| not recognized." << std::endl;
throw "Atom type not recognized...attention required.";
}
return SPECIAL_T_NONE;
}
//
// Split the string into the constituent elements: <ELEMENT>.(<Specification> | <VAL>)
//
// returns <VAL>
//
int parseString(const std::string& s, std::string& prefix, std::string& suffix)
{
suffix = "";
// Look for the period
int pos = s.find(".");
// If no period, then we have just an element
if (pos == std::string::npos)
{
prefix = s;
return -1;
}
prefix = s.substr(0, pos);
suffix = s.substr(pos+1);
// Check if the last position is a numeric value
std::string lastChar = s.substr(s.size() - 1);
int val = -1;
if (isdigit(lastChar[0]))
{
val = atoi(lastChar.c_str());
// Cut down the suffix string by one character.
suffix = suffix.substr(0, suffix.size() - 1);
}
return val;
}
//
// An AtomT is of the form: <Atom-Type>.<int-type> OR
// <Atom-Type>.<string-type>
AtomT::AtomT(const std::string& in)
{
std::string prefix = "";
std::string suffix = "";
this->theAtomT.specificNum = parseString(in, prefix, suffix);
this->theAtomT.atomType = convertToAtomEnum(prefix);
this->theAtomT.specialT = convertToSpecialEnum(suffix);
}
// **************************************************************************************
bool AtomT::operator==(const AtomT& that) const
{
if (this->theAtomT.atomType != that.theAtomT.atomType) return false;
if (this->theAtomT.specificNum != that.theAtomT.specificNum) return false;
if (this->theAtomT.specialT != that.theAtomT.specialT) return false;
return true;
}
// **************************************************************************************
std::string AtomT::toString() const
{
std::ostringstream oss;
switch(theAtomT.atomType)
{
case ATOM_T_CARBON: oss << "C"; break;
case ATOM_T_CHLORINE: return "Cl";
case ATOM_T_HYDROGEN: oss << "H"; break;
case ATOM_T_NITROGEN: oss << "N"; break;
case ATOM_T_OXYGEN: oss << "O"; break;
case ATOM_T_PHOSPHORUS: oss << "P"; break;
case ATOM_T_SULFUR: oss << "S"; break;
case ATOM_T_FLUORINE: oss << "F"; break;
case ATOM_T_BROMINE: oss << "Br"; break;
case ATOM_T_BORON: oss << "B"; break;
case ATOM_T_IODINE: oss << "I"; break;
}
oss << ".";
switch(theAtomT.specialT)
{
case SPECIAL_T_AM: oss << "am"; break;
case SPECIAL_T_AROMATIC: oss << "ar"; break;
case SPECIAL_T_PL: oss << "pl"; break;
case SPECIAL_T_CO: oss << "co"; break;
case SPECIAL_T_O: oss << "O"; break;
case SPECIAL_T_CAT: oss << "cat"; break;
}
if (theAtomT.specificNum != -1) oss << theAtomT.specificNum;
return oss.str();
}
// **************************************************************************************
std::string AtomT::getAtomType() const
{
switch(theAtomT.atomType)
{
case ATOM_T_CARBON: return "C";
case ATOM_T_CHLORINE: return "Cl";
case ATOM_T_HYDROGEN: return "H";
case ATOM_T_NITROGEN: return "N";
case ATOM_T_OXYGEN: return "O";
case ATOM_T_PHOSPHORUS: return "P";
case ATOM_T_SULFUR: return "S";
case ATOM_T_FLUORINE: return "F";
case ATOM_T_BROMINE: return "Br";
case ATOM_T_BORON: return "B";
case ATOM_T_IODINE: return "I";
}
std::cerr << theAtomT.atomType << std::endl;
return "Error";
}
// **************************************************************************************
std::ostream& operator<< (std::ostream& os, const AtomT& atomType)
{
os << atomType.toString();
return os;
}