The Gnome Chemistry Utils 0.13.3
|
00001 // -*- C++ -*- 00002 00003 /* 00004 * Gnome Chemistry Utils 00005 * formula.h 00006 * 00007 * Copyright (C) 2005-2010 Jean Bréfort <jean.brefort@normalesup.org> 00008 * 00009 * This program is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU General Public License as 00011 * published by the Free Software Foundation; either version 2 of the 00012 * License, or (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 00022 * USA 00023 */ 00024 00025 #ifndef GCU_FORMULA_H 00026 #define GCU_FORMULA_H 00027 00028 #include <string> 00029 #include <map> 00030 #include <list> 00031 #include <stdexcept> 00032 #include "isotope.h" 00033 #include "macros.h" 00034 #include "value.h" 00035 00037 namespace gcu 00038 { 00039 00052 typedef enum { 00053 GCU_FORMULA_PARSE_GUESS, 00054 GCU_FORMULA_PARSE_ATOM, 00055 GCU_FORMULA_PARSE_RESIDUE, 00056 GCU_FORMULA_PARSE_ASK, 00057 GCU_FORMULA_PARSE_NO_CASE=8 00058 } FormulaParseMode; 00059 00065 class parse_error: public std::exception 00066 { 00067 public: 00071 explicit 00072 parse_error (const std::string& __arg, int start, int length); 00073 00074 virtual 00075 ~parse_error () throw (); 00076 00080 virtual const char* 00081 what () const throw (); 00085 const char* 00086 what (int& start, int& length) const throw (); 00087 00091 void add_offset (int offset) {m_start += offset;} 00092 00093 private: 00094 std::string m_msg; 00095 int m_start, m_length; 00096 00097 }; 00098 00103 class FormulaElt 00104 { 00105 public: 00109 FormulaElt (); 00113 virtual ~FormulaElt (); 00117 virtual std::string Markup (); 00121 virtual std::string Text (); 00128 virtual void BuildRawFormula (std::map<int, int> &raw) = 0; 00132 virtual int GetValence () = 0; 00136 int stoich; 00140 unsigned start; 00144 unsigned end; 00145 }; 00146 00151 class FormulaAtom: public FormulaElt 00152 { 00153 public: 00157 FormulaAtom (int Z); 00161 virtual ~FormulaAtom (); 00165 std::string Markup (); 00169 std::string Text (); 00175 void BuildRawFormula (std::map<int, int> &raw); 00179 int GetValence (); 00182 int elt; 00183 }; 00184 00189 class FormulaBlock: public FormulaElt 00190 { 00191 public: 00194 FormulaBlock (); 00198 virtual ~FormulaBlock (); 00202 std::string Markup (); 00206 std::string Text (); 00213 void BuildRawFormula (std::map<int, int> &raw); 00217 int GetValence (); 00221 std::list<FormulaElt *> children; 00225 int parenthesis; 00226 }; 00227 00228 class Residue; 00229 00236 class FormulaResidue: public FormulaElt 00237 { 00238 public: 00244 FormulaResidue (Residue const *res, char const *symbol, int Z); 00248 virtual ~FormulaResidue (); 00252 std::string Markup (); 00256 std::string Text (); 00263 void BuildRawFormula (std::map<int, int> &raw); 00267 int GetValence (); 00271 Residue const *residue; 00275 std::string Symbol; 00279 GCU_RO_PROP (int, Z); 00280 }; 00281 00282 00288 class Formula 00289 { 00290 public: 00297 Formula (std::string entry, FormulaParseMode mode = GCU_FORMULA_PARSE_GUESS) throw (parse_error); 00298 00302 virtual ~Formula (); 00303 00307 char const *GetMarkup (); 00311 std::map<int,int> &GetRawFormula (); 00315 char const *GetRawMarkup (); 00322 void SetFormula (std::string entry) throw (parse_error); 00326 void Clear (); 00332 DimensionalValue GetMolecularWeight (bool &artificial); 00338 void CalculateIsotopicPattern (IsotopicPattern &pattern); 00339 00343 std::list<FormulaElt *> const &GetElements () const {return Details;} 00344 00345 private: 00346 bool BuildConnectivity (); 00347 void Parse (std::string &formula, std::list<FormulaElt *>&result) throw (parse_error); 00348 bool AnalString (char *sz, std::list<FormulaElt *> &result, bool &ambiguous, int offset); 00349 bool TryReplace (std::list<FormulaElt *> &result, std::list<FormulaElt *>::iterator it); 00350 00351 private: 00352 std::string Entry, Markup, RawMarkup; 00353 std::map<int,int> Raw; 00354 std::list<FormulaElt *> Details; 00355 DimensionalValue m_Weight; 00356 bool m_WeightCached; 00357 bool m_Artificial; 00358 bool m_ConnectivityCached; 00359 00371 GCU_PROP (FormulaParseMode, ParseMode); 00372 }; 00373 00374 } 00375 00376 #endif // GCU_FORMULA_H