summaryrefslogtreecommitdiffstats
path: root/kalzium/src/moleculeparser.h
blob: 45b27436f6cb26339bb167d4b1e2c4ee9570ab8c (plain)
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
/***************************************************************************
    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 <tqmap.h>
#include <tqvaluelist.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 TQValueList<ElementCount*>::Iterator  Iterator;
	Iterator   begin() { return  m_map.begin(); }
	Iterator   end()   { return  m_map.end();   }

 private:
    TQValueList<ElementCount*>  m_map;
};



/**
 * Parse molecule formulas.
 *
 * Usage:
 * @code
 *   MoleculeParser  parser;
 *   TQString         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 TQString& _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(TQString         _moleculeString,
				 double          *_resultMass,
				 ElementCountMap *_resultMap);

 private:
    // Helper functions
    bool      parseSubmolecule(double          *_resultMass,
							   ElementCountMap *_resultMap);
    bool      parseTerm(double          *_resultMass, 
						ElementCountMap *_resultMap);

    Element  *lookupElement( const TQString& _name );

    TQMap<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