From 3ad191b99b6506be3a31b01ad2fd74eed510133e Mon Sep 17 00:00:00 2001 From: Kashyap Ramesh Puranik Date: Fri, 3 Jul 2009 10:48:57 +0000 Subject: [PATCH] Making changes to molecular parser, now there is no leakage, some KDE conventions implemented. Also you can expand symbols like EDTA by giving data like #EDTA#MeOH. Here EDTA will be expanded because its between # and #. and Et will be expanded because it consists of a long letter followed by short. svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=990793 --- libscience/moleculeparser.cpp | 50 ++++++++++++++++++++++++++--------- libscience/moleculeparser.h | 2 +- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/libscience/moleculeparser.cpp b/libscience/moleculeparser.cpp index e1ec0ca..9f62346 100644 --- a/libscience/moleculeparser.cpp +++ b/libscience/moleculeparser.cpp @@ -101,14 +101,14 @@ MoleculeParser::MoleculeParser( const QList& list) : Parser() { m_elementList = list; - m_aliasList = new (QSet); + m_aliasList = new QSet; } MoleculeParser::MoleculeParser(const QString& _str) : Parser(_str) { - m_aliasList = new (QSet); + m_aliasList = new QSet; } @@ -161,10 +161,10 @@ MoleculeParser::weight(const QString& _shortMoleculeString, return true; } -QSet* -MoleculeParser::getAliasList(void) +QSet +MoleculeParser::aliasList() { - return m_aliasList; + return *m_aliasList; } // ---------------------------------------------------------------- // helper methods for the public methods @@ -321,14 +321,13 @@ MoleculeParser::lookupElement( const QString& _name ) QString MoleculeParser::expandFormula( const QString& _shortString) { - QString _fullString = ""; // The expanded string that will be returned - QString::iterator i; // iterator - QString temp; // A temporary string that will contain a single element/group - QString expandedTerm; // expansion of a particular term. + QString _fullString; // The expanded string that will be returned + QString::const_iterator i; // iterator + QString temp; // A temporary string that will contain a single element/group + QString expandedTerm; // expansion of a particular term. // Go through all letters in the string. - for(i = (const QString::iterator) _shortString.begin(); - i != _shortString.end(); ) + for(i = _shortString.constBegin(); i != _shortString.constEnd(); ) { temp = ""; @@ -349,7 +348,7 @@ MoleculeParser::expandFormula( const QString& _shortString) } // If an expansion was made, return the expansion - else if ((expandedTerm = expandTerm(temp)) != "") { + else if (!((expandedTerm = expandTerm(temp)).isEmpty())) { kDebug() << "expanded" << temp << "to" << expandedTerm; _fullString += "("+expandedTerm+")"; } @@ -369,6 +368,33 @@ MoleculeParser::expandFormula( const QString& _shortString) _fullString += ')'; i++; } + + // If # is found, we have a short-form eg #EDTA# + else if (*i == '#') { + i ++; // go to the next character + // Get the term between # and # + while (*i != '#' && i != _shortString.constEnd() ) + { + temp += *i; + i ++; + } + // If the string ended, just add the part that comes after # + if ( i == _shortString.constEnd()) { + _fullString += temp; + break; + } + // else expand the term between # and # + else if (!temp.isEmpty()) + { + // if alias is not found, just add without expanding the term + if((expandedTerm = expandTerm(temp)).isEmpty()) + _fullString += temp; + // else add the expanded term + else + _fullString += expandedTerm; + } + i ++; + } // If number was found, return it else if ((*i).category() == QChar::Number_DecimalDigit) { _fullString += *i; diff --git a/libscience/moleculeparser.h b/libscience/moleculeparser.h index 6e8afbf..705154f 100644 --- a/libscience/moleculeparser.h +++ b/libscience/moleculeparser.h @@ -215,7 +215,7 @@ public: double *_resultMass, ElementCountMap *_resultMap); - QSet* getAliasList(void); + QSet aliasList(); private: // Helper functions bool parseSubmolecule(double *_resultMass, -- 2.47.3