summaryrefslogtreecommitdiffstats
path: root/kalzium/src/moleculeparser.h
diff options
context:
space:
mode:
authortoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
committertoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
commitce599e4f9f94b4eb00c1b5edb85bce5431ab3df2 (patch)
treed3bb9f5d25a2dc09ca81adecf39621d871534297 /kalzium/src/moleculeparser.h
downloadtdeedu-ce599e4f9f94b4eb00c1b5edb85bce5431ab3df2.tar.gz
tdeedu-ce599e4f9f94b4eb00c1b5edb85bce5431ab3df2.zip
Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features.
BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdeedu@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'kalzium/src/moleculeparser.h')
-rw-r--r--kalzium/src/moleculeparser.h138
1 files changed, 138 insertions, 0 deletions
diff --git a/kalzium/src/moleculeparser.h b/kalzium/src/moleculeparser.h
new file mode 100644
index 00000000..c35c71be
--- /dev/null
+++ b/kalzium/src/moleculeparser.h
@@ -0,0 +1,138 @@
+/***************************************************************************
+ 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. *
+ * *
+ ***************************************************************************/
+
+#ifndef MOLECULEPARSER_H
+#define MOLECULEPARSER_H
+
+#include "element.h"
+#include "parser.h"
+
+#include <qmap.h>
+#include <qvaluelist.h>
+
+
+/**
+ * @class Element
+ */
+class ElementCount {
+ public:
+ ElementCount(Element *_element, int _count)
+ {
+ m_element = _element;
+ m_count = _count;
+ }
+ ElementCount(Element *_element)
+ {
+ m_element = _element;
+ m_count = 0;
+ }
+
+ ~ElementCount();
+
+ Element *element() const { return m_element; }
+ int count() const { return m_count; }
+ void add(int _count) { m_count += _count; }
+ void multiply(int _factor) { m_count *= _factor; }
+
+ Element *m_element;
+ int m_count;
+};
+
+
+/**
+ * @class ElementCountMap
+ */
+class ElementCountMap {
+ public:
+ ElementCountMap();
+ ~ElementCountMap();
+
+ void clear() { m_map.clear(); }
+
+ ElementCount *search(Element *_element);
+ void add(ElementCountMap &_map);
+ void add(Element *_element, int _count);
+ void multiply(int _factor);
+
+ typedef QValueList<ElementCount*>::Iterator Iterator;
+ Iterator begin() { return m_map.begin(); }
+ Iterator end() { return m_map.end(); }
+
+ private:
+ QValueList<ElementCount*> m_map;
+};
+
+
+
+/**
+ * Parse molecule formulas.
+ *
+ * Usage:
+ * @code
+ * MoleculeParser parser;
+ * QString chemical_formula = "C2H5OH";
+ * double weight;
+ *
+ * if (parser.weight(chemical_formula, &weight))
+ * cout << "Weight of " << chemical_formula << " = " << weight << ".\n";
+ * else
+ * cout << "Parse error\n";
+ * @endcode
+ *
+ * @author Inge Wallin
+ */
+class MoleculeParser : public Parser {
+
+public:
+ static const int ELEMENT_TOKEN = 300;
+
+ MoleculeParser();
+ MoleculeParser( const QString& _str);
+ ~MoleculeParser();
+
+ /**
+ * Try to parse the molecule @p molecule and get the weight of it.
+ * The calculated weight is stored in @p _result.
+ *
+ * @return whether the parsing was successful or not
+ */
+ bool weight(QString _moleculeString,
+ double *_resultMass,
+ ElementCountMap *_resultMap);
+
+ private:
+ // Helper functions
+ bool parseSubmolecule(double *_resultMass,
+ ElementCountMap *_resultMap);
+ bool parseTerm(double *_resultMass,
+ ElementCountMap *_resultMap);
+
+ Element *lookupElement( const QString& _name );
+
+ QMap<Element*, int> m_elementMap;
+
+ //if this booloean is "true" the parser found an error
+ bool m_error;
+
+protected:
+
+ /**
+ * Extends the standard tokenizer in Parser::getNextToken().
+ */
+ virtual int getNextToken();
+
+private:
+ Element *m_elementVal; // Valid if m_nextToken == ELEMENT_TOKEN
+};
+
+#endif