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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
/***************************************************************************
copyright : (C) 2005 by Inge Wallin
email : inge@lysator.liu.se
***************************************************************************/
/***************************************************************************
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <ctype.h>
#include <kdebug.h>
#include "kalziumdataobject.h"
#include "moleculeparser.h"
// ================================================================
// class ElementCountMap
ElementCountMap::ElementCountMap()
{
m_map.clear();
}
ElementCountMap::~ElementCountMap()
{
}
ElementCount *
ElementCountMap::search(Element *_element)
{
TQValueList<ElementCount *>::ConstIterator it = m_map.constBegin();
const TQValueList<ElementCount *>::ConstIterator itEnd = m_map.constEnd();
for (; it != itEnd; ++it) {
if ((*it)->element() == _element)
return *it;
}
return 0;
}
void
ElementCountMap::add(ElementCountMap &_map)
{
TQValueList<ElementCount *>::ConstIterator it = _map.m_map.constBegin();
const TQValueList<ElementCount *>::ConstIterator itEnd = _map.m_map.constEnd();
// Step throught _map and for each element, add it to the current one.
for (; it != itEnd; ++it) {
add((*it)->m_element, (*it)->m_count);
}
}
void
ElementCountMap::add(Element *_element, int _count)
{
ElementCount *elemCount;
elemCount = search(_element);
if (elemCount)
elemCount->m_count += _count;
else
m_map.append(new ElementCount(_element, _count));
}
void
ElementCountMap::multiply(int _factor)
{
Iterator it = begin();
Iterator itEnd = end();
for (; it != itEnd; ++it)
(*it)->multiply(_factor);
}
// ================================================================
// class MoleculeParser
MoleculeParser::MoleculeParser()
: Parser()
{
}
MoleculeParser::MoleculeParser(const TQString& _str)
: Parser(_str)
{
}
MoleculeParser::~MoleculeParser()
{
//Parser::~Parser();
}
// ----------------------------------------------------------------
// public methods
// Try to parse the molecule and get the weight of it.
//
// This method also acts as the main loop.
bool
MoleculeParser::weight(TQString _moleculeString,
double *_resultMass,
ElementCountMap *_resultMap)
{
// Clear the result variables and set m_error to false
_resultMap->clear();
m_error = false;
*_resultMass = 0.0;
// Initialize the parsing process, and parse te molecule.
start(_moleculeString);
parseSubmolecule(_resultMass, _resultMap);
if (nextToken() != -1)
return false;
if ( m_error )//there was an error in the input...
return false;
return true;
}
// ----------------------------------------------------------------
// helper methods for the public methods
// Parse a submolecule. This is a list of terms.
//
bool
MoleculeParser::parseSubmolecule(double *_resultMass,
ElementCountMap *_resultMap)
{
double subMass = 0.0;
ElementCountMap subMap;
*_resultMass = 0.0;
_resultMap->clear();
while (parseTerm(&subMass, &subMap)) {
//kdDebug() << "Parsed a term, weight = " << subresult << endl;
// Add the mass and composition of the submolecule to the total.
*_resultMass += subMass;
_resultMap->add(subMap);
}
return true;
}
// Parse a term within the molecule, i.e. a single atom or a
// submolecule within tqparenthesis followed by an optional number.
// Examples: Bk, Mn2, (COOH)2
//
// Return true if correct, otherwise return false.
// If correct, the mass of the term is returned in *_resultMass, and
// the flattened composition of the molecule in *_resultMap.
//
bool
MoleculeParser::parseTerm(double *_resultMass,
ElementCountMap *_resultMap)
{
*_resultMass = 0.0;
_resultMap->clear();
#if 0
kdDebug() << "parseTerm(): Next token = "
<< nextToken() << endl;
#endif
if (nextToken() == ELEMENT_TOKEN) {
//kdDebug() << "Parsed an element: " << m_elementVal->symbol() << endl;
*_resultMass = m_elementVal->mass();
_resultMap->add(m_elementVal, 1);
getNextToken();
}
else if (nextToken() == '(') {
// A submolecule.
getNextToken();
parseSubmolecule(_resultMass, _resultMap);
// Must end in a ")".
if (nextToken() == ')') {
//kdDebug() << "Parsed a submolecule. weight = " << *_result << endl;
getNextToken();
}
else
return false;
}
else
// Neither an element nor a list within ().
return false;
// Optional number.
if (nextToken() == INT_TOKEN) {
//kdDebug() << "Parsed a number: " << intVal() << endl;
*_resultMass *= intVal();
_resultMap->multiply(intVal());
getNextToken();
}
kdDebug() << "Weight of term = " << *_resultMass << endl;
return true;
}
// ----------------------------------------------------------------
// protected methods
// Extend Parser::getNextToken with elements.
int
MoleculeParser::getNextToken()
{
TQString elementName;
#if 0
kdDebug() << "getNextToken(): Next character = "
<< nextChar() << endl;
#endif
// Check if the token is an element name.
if ('A' <= nextChar() && nextChar() <= 'Z') {
elementName = char(nextChar());
getNextChar();
if ('a' <= nextChar() && nextChar() <= 'z') {
elementName.append(char(nextChar()));
getNextChar();
}
// Look up the element from the name..
m_elementVal = lookupElement(elementName);
if (m_elementVal)
{
m_nextToken = ELEMENT_TOKEN;
}
else
m_nextToken = -1;
}
else
return Parser::getNextToken();
return m_nextToken;
}
// ----------------------------------------------------------------
// private methods
Element *
MoleculeParser::lookupElement( const TQString& _name )
{
EList elementList = KalziumDataObject::instance()->ElementList;
//kdDebug() << "looking up " << _name << endl;
EList::ConstIterator it = elementList.constBegin();
const EList::ConstIterator end = elementList.constEnd();
for (; it != end; ++it) {
if ( (*it)->symbol() == _name ) {
kdDebug() << "Found element " << _name << endl;
return *it;
}
}
//if there is an error make m_error true.
m_error = true;
kdDebug() << k_funcinfo << "no such element, parsing error!: " << _name << endl;
return NULL;
}
|