From: Carsten Niehaus Date: Tue, 3 Jan 2006 12:36:35 +0000 (+0000) Subject: * Add null-check to the parser X-Git-Tag: v3.80.2~193 X-Git-Url: https://git.rmz.fi/?a=commitdiff_plain;h=02149297ea7a5ff18ac720a105e6d895a32fef78;p=libqmvoc.git * Add null-check to the parser * Remove no longer needed code and include * Indent getNextChar() svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=493826 --- diff --git a/libscience/moleculeparser.cpp b/libscience/moleculeparser.cpp index 398c601..db686d1 100644 --- a/libscience/moleculeparser.cpp +++ b/libscience/moleculeparser.cpp @@ -16,10 +16,6 @@ #include -/* PORTING - * #include "kalziumdataobject.h" - */ - #include "moleculeparser.h" @@ -42,16 +38,6 @@ ElementCountMap::~ElementCountMap() ElementCount * ElementCountMap::search(Element *_element) { - //not yet tested but the commented code should be the same as - //the foreach... so lets use Qt4 power -//X QList::ConstIterator it = m_map.constBegin(); -//X const QList::ConstIterator itEnd = m_map.constEnd(); -//X -//X for (; it != itEnd; ++it) { -//X if ((*it)->element() == _element) -//X return *it; -//X } - foreach( ElementCount* c, m_map ){ if ( c->element() == _element ) return c; @@ -64,16 +50,6 @@ ElementCountMap::search(Element *_element) void ElementCountMap::add(ElementCountMap &_map) { - //not yet tested but the commented code should be the same as - //the foreach... so lets use Qt4 power -//X QList::ConstIterator it = _map.m_map.constBegin(); -//X const QList::ConstIterator itEnd = _map.m_map.constEnd(); -//X -//X // Step throught _map and for each element, add it to the current one. -//X for (; it != itEnd; ++it) { -//X add((*it)->m_element, (*it)->m_count); -//X } - foreach( ElementCount* c, _map.m_map ){ add( c->m_element, c->m_count ); } @@ -141,6 +117,9 @@ MoleculeParser::weight(QString _moleculeString, double *_resultMass, ElementCountMap *_resultMap) { + if ( _moleculeString == QString() ) + return false; + // Clear the result variables and set m_error to false _resultMap->clear(); m_error = false; @@ -295,13 +274,8 @@ MoleculeParser::getNextToken() Element * MoleculeParser::lookupElement( const QString& _name ) { -//X QList elementList = KalziumDataObject::instance()->ElementList; - kdDebug() << "looking up " << _name << endl; -//X QList::ConstIterator it = elementList.constBegin(); -//X const QList::ConstIterator end = elementList.constEnd(); - foreach( Element* e, m_elementList ){ if ( e->dataAsVariant(ChemicalDataObject::symbol) == _name ) { kdDebug() << "Found element " << _name << endl; @@ -309,13 +283,6 @@ MoleculeParser::lookupElement( const QString& _name ) } } -//X for (; it != end; ++it) { -//X if ( (*it)->dataAsVariant(ChemicalDataObject::symbol) == _name ) { -//X kdDebug() << "Found element " << _name << endl; -//X return *it; -//X } -//X } - //if there is an error make m_error true. m_error = true; diff --git a/libscience/parser.cpp b/libscience/parser.cpp index 68acab5..42eb288 100644 --- a/libscience/parser.cpp +++ b/libscience/parser.cpp @@ -139,26 +139,29 @@ Parser::parseSimpleFloat(double *_result) int Parser::getNextChar() { - if (m_index == -1) - return -1; - - // If end of string, then reset the parser. - if (m_index == (int) m_str.length()) { - m_index = -1; - m_nextChar = -1; - } - else - m_nextChar = m_str.at(++m_index).toLatin1(); +// kdDebug() << "Parser::getNextChar(): char = " << m_nextChar << endl; +// kdDebug() << "m_str.size() " << m_str.size() << " with m_str: " << m_str << " and m_index: " << m_index << endl; + + m_index++; + + if (m_index == -1) + return -1; - // Take care of null-terminated strings. - if (m_nextChar == 0) { - m_index = -1; - m_nextChar = -1; - } + // If end of string, then reset the parser. + if (m_index == m_str.size()) { + m_index = -1; + m_nextChar = -1; + } + else + m_nextChar = m_str.at(m_index).toLatin1(); - //kdDebug() << "Parser::getNextChar(): char = " << m_nextChar << endl; + // Take care of null-terminated strings. + if (m_nextChar == 0) { + m_index = -1; + m_nextChar = -1; + } - return m_nextChar; + return m_nextChar; }